https://rt-wiki.bestpractical.com/index.php?title=SetTimeWorkedAutomatically&feed=atom&action=historySetTimeWorkedAutomatically - Revision history2024-03-28T20:00:24ZRevision history for this page on the wikiMediaWiki 1.37.2https://rt-wiki.bestpractical.com/index.php?title=SetTimeWorkedAutomatically&diff=3552&oldid=prevAdmin: 40 revisions imported2016-04-06T20:37:43Z<p>40 revisions imported</p>
<p><b>New page</b></p><div>Hi,<br />
<br />
Searching on past threads, I couldn't find how to set the "[[TimeWorked]]" automatically when a ticket was resolved, taking into account the "Started" and the "Resolved" dates. This prompted me to developed the following Scrip, which uses Date::Calc to find the date difference and update the [[TimeWorked]] field. Does RT already provide such an option?<br />
<br />
Thanks,<br />
<br />
Miguel<br />
<br />
:That's not what the field is meant for. It's hours spent working, not days until the ticket was closed. In general the former will be a small fraction of the latter. --Jerrad<br />
<br />
:I built upon the original scrip to add the option to control closed hours and weekends. I also added comments and color-coding to the perl text. --Max<br />
<br />
:- To change the amount of closed hours, edit the number value of the "$closed_hours" variable. It is currently set to 15.( ''E.G. 9am -> 6pm = 9 hours. 24 - 9 = 15 hours'') <br />
:- To change the amount of weekend days to count, edit the number value of the "$weekend_closed_days" variable. It is currently set to 2 (''E.G. Saturday + Sunday = 2 days'').<br />
<br />
* Condition: On Resolve<br />
* Action: User Defined<br />
* Template: Global template: Blank<br />
* Stage: '''[[TransactionCreate|Transactio]][[TransactionCreate|nBatch]]'''<br />
* Custom action preparation code: '''return 1;'''<br />
* Custom action cleanup code:<br />
<br />
----<br />
<p><font color="blue">'''use'''</font> Date::Calc qw(Delta_DHMS);</p><br />
<br />
<p><font color="green"># Declare relevant variables</font><br />
<br><font color="blue">'''my'''</font> <font color="orange">$time_elapsed</font> = <font color="gray">&quot;0&quot;</font>;<br />
<br><font color="blue">'''my'''</font> <font color="orange">$not_started</font> = <font color="blue">'''undef'''</font>;<br />
<br><font color="blue">'''my'''</font> <font color="orange">$not_resolved</font> = <font color="blue">'''undef'''</font>;<br />
<br><font color="blue">'''my'''</font> (<font color="purple">@diff</font>, <font color="purple">@date_created</font>, <font color="purple">@date_started</font>, <font color="purple">@date_resolved</font>, <font color="purple">@date_last_updated</font>);</p><br />
<br />
<p><font color="green"># Get ticket date values and hold them in variables</font><br />
<br><font color="blue">'''my'''</font> <font color="orange">$date_created_string</font> = <font color="orange">$self</font>->TicketObj->Created;<br />
<br><font color="blue">'''my'''</font> <font color="orange">$date_started_string</font> = <font color="orange">$self</font>->TicketObj->Started;<br />
<br><font color="blue">'''my'''</font> <font color="orange">$date_resolved_string</font> = <font color="orange">$self</font>->TicketObj->Resolved;<br />
<br><font color="blue">'''my'''</font> <font color="orange">$date_last_updated_string</font> = <font color="orange">$self</font>->TicketObj->LastUpdated;</p><br />
<br />
<p><font color="green"># If the ticket created date string is in a (YYYY-MM-DD HH:MM:SS) format</font><br />
<br><font color="blue">'''if'''</font> (<font color="orange">$date_created_string</font> =~ <font color="gray">/(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array out of the individual date &amp; time values</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@date_created</font> = (<font color="orange">$1</font>, <font color="orange">$2</font>, <font color="orange">$3</font>, <font color="orange">$4</font>, <font color="orange">$5</font>, <font color="orange">$6</font>); <br />
<br>}</p><br />
<br />
<p><font color="green"># If the ticket started date string is in a (YYYY-MM-DD HH:MM:SS) format</font> <br />
<br><font color="blue">'''if'''</font> (<font color="orange">$date_started_string</font> =~ <font color="gray">/(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># If ticket starts date is unset</font> <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">'''if'''</font>(<font color="orange">$1</font> == <font color="red">1970</font>){ <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Set not_started variable to true</font> <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="orange">$not_started</font> = <font color="gray">&quot;1&quot;</font>; <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Otherwise</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">'''else'''</font> { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># The ticket started date has not been set</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@date_started</font> = (<font color="orange">$1</font>, <font color="orange">$2</font>, <font color="orange">$3</font>, <font color="orange">$4</font>, <font color="orange">$5</font>, <font color="orange">$6</font>); <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br />
<br>}</p><br />
<br />
<p><font color="green"># If the ticket resolved date string is in a (YYYY-MM-DD HH:MM:SS) format</font><br />
<br><font color="blue">'''if'''</font> (<font color="orange">$date_resolved_string</font> =~ <font color="gray">/(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array out of the individual date &amp; time values</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># If ticket resolved date is unset</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">'''if'''</font> (<font color="orange">$1</font> == <font color="red">1970</font>){ <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Set not_resolved variable to true</font> <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="orange">$not_resolved</font> = <font color="gray">&quot;1&quot;</font>; <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Otherwise</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="blue">'''else'''</font> { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># The ticket resolved date has not been set</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@date_resolved</font> = (<font color="orange">$1</font>, <font color="orange">$2</font>, <font color="orange">$3</font>, <font color="orange">$4</font>, <font color="orange">$5</font>, <font color="orange">$6</font>); <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;} <br />
<br>}</p><br />
<br />
<p><font color="green"># If the ticket last updated date string is in a (YYYY-MM-DD HH:MM:SS) format</font><br />
<br><font color="blue">'''if'''</font> (<font color="orange">$date_last_updated_string</font> =~ <font color="gray">/(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array out of the individual date &amp; time values</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@date_last_updated</font> = (<font color="orange">$1</font>, <font color="orange">$2</font>, <font color="orange">$3</font>, <font color="orange">$4</font>, <font color="orange">$5</font>, <font color="orange">$6</font>); <br />
<br>}</p><br />
<br />
<p><font color="green"># If the ticket started and resolved dates have not been set</font><br />
<br><font color="blue">'''if'''</font> (<font color="orange">$not_started</font> &amp;&amp; <font color="orange">$not_resolved</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array of delta values using the ticket created &amp; last updated dates</font> <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@diff</font> = Delta_DHMS(<font color="purple">@date_created</font>, <font color="purple">@date_last_updated</font>); <br />
<br>} <br />
<br><font color="green"># If the ticket started date has not been set</font> <br />
<br><font color="blue">'''elsif'''</font> (<font color="orange">$not_started</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array of delta values using the ticket created &amp; resolved dates</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@diff</font> = Delta_DHMS(<font color="purple">@date_created</font>, <font color="purple">@date_resolved</font>); <br />
<br>} <br />
<br><font color="green"># If the ticket resolved date has not been set</font><br />
<br><font color="blue">'''elsif'''</font> (<font color="orange">$not_resolved</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array of delta values using the ticket started &amp; last updated dates</font><br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@diff</font> = Delta_DHMS(<font color="purple">@date_started</font>, <font color="purple">@date_last_updated</font>); <br />
<br>} <br />
<br><font color="blue">'''else'''</font> { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Create an array of delta values using the ticket started &amp; resolved dates</font> <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="purple">@diff</font> = Delta_DHMS(<font color="purple">@date_started</font>, <font color="purple">@date_resolved</font>); <br />
<br>}</p><br />
<br />
<p><font color="green"># Only count open business hours</font> <br />
<br><font color="blue">'''my'''</font> <font color="orange">$closed_hours</font> = <font color="red">12</font>; <br />
<br><font color="blue">'''my'''</font> <font color="orange">$days</font> = <font color="orange">$diff</font>[<font color="red">0</font>];</p><br />
<br />
<p><font color="green"># Option to remove weekends</font> <br />
<br><font color="blue">'''my'''</font> <font color="orange">$weekend_closed_days</font> = <font color="red">2</font>; <br />
<br><font color="blue">'''my'''</font> <font color="orange">$week_count</font> = <font color="red">0</font>; <br />
<br><font color="blue">'''for'''</font> (<font color="blue">'''my'''</font> <font color="orange">$i</font> = <font color="red">0</font>; <font color="orange">$i</font> <= <font color="orange">$days</font>; <font color="orange">$i</font> += <font color="red">7</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="orange">$week_count</font> += <font color="red">1</font>; <br />
<br>} <br />
<br><font color="orange">$days</font> = <font color="orange">$days</font> - (<font color="orange">$week_count</font> * <font color="orange">$weekend_closed_days</font>);</p><br />
<br />
<p><font color="green"># Convert the results into minutes</font><br />
<br><font color="blue">'''my'''</font> <font color="orange">$day_to_minutes</font> = <font color="orange">$days</font> * <font color="red">1440</font>; <br />
<br><font color="blue">'''my'''</font> <font color="orange">$hour_to_minutes</font> = <font color="orange">$diff</font>[<font color="red">1</font>] * <font color="red">60</font>; <br />
<br><font color="blue">'''my'''</font> <font color="orange">$closed_hours_to_minutes</font> = (<font color="red">60</font> * <font color="orange">$closed_hours</font>) * <font color="orange">$days</font>;</p><br />
<br />
<p><font color="green"># Calculate the elapsed time</font><br />
<br><font color="blue">'''my'''</font> <font color="orange">$time_elapsed_sum</font> = <font color="orange">$day_to_minutes</font> + <font color="orange">$hour_to_minutes</font> + <font color="orange">$diff</font>[<font color="red">2</font>] - <font color="orange">$closed_hours_to_minutes</font>;</p><br />
<br />
<p><font color="green"># If the sum of the elapsed times is greater then zero</font> <br />
<br><font color="blue">'''if'''</font> (<font color="orange">$time_elapsed_sum</font> > <font color="red">0</font>) { <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="green"># Set the elapsed time </font> <br />
<br>&nbsp;&nbsp;&nbsp;&nbsp;<font color="orange">$time_elapsed</font> = qq($time_elapsed_sum\.$diff[<font color="red">3</font>]); <br />
<br>}</p><br />
<br />
<p><font color="green"># Log successful scrip execution</font><br />
<br><font color="orange">$RT::Logger</font>->info(<font color="gray">&quot;\n\nSetting TimeWorked on ticket to: </font> $time_elapsed<font color="gray">&quot;</font>);</p><br />
<br />
<p><font color="green"># Set the ticket worked time to the elapsed time</font><br />
<br><font color="orange">$self</font>->TicketObj->SetTimeWorked( <font color="orange">$time_elapsed</font> );</p><br>----</div>Admin