execution_time.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import rospy
5 import mongodb_store_msgs.srv as dc_srv
6 import mongodb_store.util as dc_util
7 from mongodb_store.message_store import MessageStoreProxy
8 from strands_executive_msgs.msg import Task, TaskEvent
9 from datetime import datetime, timedelta
10 from task_executor import task_routine, task_query
11 import argparse
12 
13 
14 if __name__ == '__main__':
15 
16  rospy.init_node("execution_time")
17 
18  msg_store = MessageStoreProxy(collection='task_events')
19 
20  parser = argparse.ArgumentParser(description='Prints a summary of tasks executed within the queried time window.')
21  parser.add_argument('start', metavar='S', type=task_query.mkdatetime, nargs='?', default=datetime.utcfromtimestamp(0),
22  help='start datetime of query, defaults to no start. Formatted "d/m/y H:M" e.g. "06/07/14 06:38"')
23 
24  parser.add_argument('end', metavar='E', type=task_query.mkdatetime, nargs='?', default=datetime.utcnow(),
25  help='end datetime of query, defaults to now. Formatted "d/m/y H:M" e.g. "06/07/14 06:38"')
26 
27 
28  args = parser.parse_args()
29 
30 
31 
32  try:
33 
34  start = args.start
35  end = args.end
36 
37  results = task_query.query_tasks(msg_store,
38  start_date=start,
39  end_date=end,
40  event=[TaskEvent.TASK_STARTED, TaskEvent.TASK_FINISHED, TaskEvent.NAVIGATION_SUCCEEDED]
41  )
42 
43 
44  duration = timedelta()
45  charge_wait_duration = timedelta()
46 
47  started_task_event = TaskEvent()
48  count = 0
49  dubious = []
50  charge_wait_count = 0
51  unstarted_count = 0
52  start_count = 0
53  day_durations = {}
54 
55 
56 
57  for i in range(0, len(results)):
58 
59  if i > 0:
60  previous = results[i-1][0]
61 
62  task_event = results[i][0]
63 
64  if i < len(results) - 1:
65  next = results[i+1][0]
66 
67  start = False
68  end = False
69 
70  if task_event.event == TaskEvent.TASK_STARTED or task_event.task.task_id != previous.task.task_id:
71  start = True
72  elif (task_event.event == TaskEvent.TASK_FINISHED or task_event.task.task_id != next.task.task_id or (task_event.task.action == '' and task_event.event == TaskEvent.NAVIGATION_SUCCEEDED)) and started_task_event.task.task_id != 0:
73  end = True
74 
75  if start:
76  started_task_event = task_event
77  start_count += 1
78  elif end:
79 
80  # if we're closing the previous event
81  if task_event.task.task_id == started_task_event.task.task_id:
82 
83  end_time = datetime.utcfromtimestamp(task_event.time.to_sec())
84  task_duration = end_time - datetime.utcfromtimestamp(started_task_event.time.to_sec())
85 
86  if task_event.task.action == 'wait_action' and task_event.task.start_node_id == 'ChargingPoint':
87  charge_wait_duration += task_duration
88  charge_wait_count += 1
89  else:
90 
91  if task_duration > timedelta(hours=3):
92  dubious.append((started_task_event, task_event))
93  # task_query.print_event(started_task_event)
94  # task_query.print_event(task_event)
95  # print '%s\n' % task_duration
96  else:
97 
98  task_date = end_time.date()
99 
100  if task_date in day_durations:
101  day_durations[task_date].append(task_duration)
102  else:
103  day_durations[task_date] = [task_duration]
104 
105  duration += task_duration
106  count += 1
107 
108  # make sure we don't look for another end to this task
109  started_task_event = TaskEvent()
110 
111  else:
112  # print 'Finished an unstarted task: %s' % task_event
113  unstarted_count += 1
114  # task_query.print_event(started_task_event)
115  # task_query.print_event(task_event)
116  # print '\n'
117 
118  start = results[0][0].time
119  end = results[-1][0].time
120 
121 
122  print 'Starts: %s' % start_count
123  print 'Ends: %s (%s + %s + %s)' % (count + charge_wait_count + len(dubious), count, charge_wait_count, len(dubious))
124 
125  print 'Unstarted: %s' % unstarted_count
126  print 'Dubious: %s' % len(dubious)
127 
128  print 'Tasks Completed: %s' % count
129  print 'Task Duration: %s' % duration
130 
131  print 'Charge Waits Finished: %s' % charge_wait_count
132  print 'Charge Waits Duration: %s' % charge_wait_duration
133 
134  print 'Tasks Start: %s' % datetime.utcfromtimestamp(start.to_sec())
135  print 'Tasks End: %s' % datetime.utcfromtimestamp(end.to_sec())
136 
137  print 'Total activity span: %s ' % timedelta(seconds=(end - start).to_sec())
138 
139  days = day_durations.keys()
140  days.sort()
141 
142  print 'Tasks and duration each day'
143  total_possible = timedelta()
144  for day in days:
145  total_possible += timedelta(hours=9)
146  durations = day_durations[day]
147  total = timedelta()
148  for d in durations:
149  total += d
150  print '%s:\t%s\t%s' % (day, len(durations), total)
151 
152  print 'Autonomy percentage: %.2f' % ((duration.total_seconds()/total_possible.total_seconds()) * 100)
153 
154 
155 
156  except rospy.ServiceException, e:
157  print "Service call failed: %s"%e
158 
159 
160 


task_executor
Author(s): Nick Hawes
autogenerated on Tue Mar 17 2015 20:08:13