Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. This management command compute stats based on VTE progress data of a given task id
  4. """
  5. from __future__ import unicode_literals
  6.  
  7. import json
  8. import logging
  9. from django.core.management.base import BaseCommand
  10.  
  11. from core import pretty_dict
  12. from vte.progress import VteProgress
  13.  
  14. logger = logging.getLogger(__name__)
  15.  
  16.  
  17. def compute_estimated_work_time(task_id):
  18. """
  19. Compute the work time estimation of a given task id based on stored vte progress timestamp
  20. :param task_id:
  21. :return: stats
  22. """
  23. logger.info('compute_work_time_estimation task:%s' % task_id)
  24. count = 0
  25. previous_vp = None
  26. nb_sessions = 0
  27. used_play_time = 0
  28. first_ts = 0
  29. last_ts = 0
  30. total_dt = 0
  31. dt_list = []
  32. play_time_list = []
  33. limited_dt = 0
  34. for p in VteProgress.query(hash_key=task_id):
  35. assert (int(p.ts) > int(last_ts))
  36. count += 1
  37. vp = json.loads(p.progress)
  38. if previous_vp is not None:
  39. dt = (int(p.ts) - int(last_ts)) // 1000000
  40. is_new_session = vp['task_session'] != previous_vp['task_session']
  41. play_time = vp['video_position'] != previous_vp['video_position']
  42. if is_new_session:
  43. nb_sessions += 1
  44. else:
  45. total_dt += dt
  46. dt_list.append(dt)
  47. play_time_list.append(play_time)
  48. else:
  49. first_ts = p.ts
  50. last_ts = p.ts
  51. previous_vp = vp
  52. logger.info('compute_work_time_estimation task:%s, %d progress loaded' % (task_id, count))
  53.  
  54. # Compute stats to estimate the work time
  55. max_time = (int(last_ts) - int(first_ts)) // 1000000
  56. work_time = max_allowed_dt = avg_dt = stddev_dt = None
  57. if count:
  58. work_time = 0
  59. if len(dt_list):
  60. avg_dt = int(0.5 + total_dt / len(dt_list))
  61. stddev_dt = int(0.5 + math.sqrt(sum(((dt - avg_dt) * (dt - avg_dt)) for dt in dt_list) / len(dt_list)))
  62. max_allowed_dt = avg_dt + 2 * stddev_dt
  63. for dt in dt_list:
  64. if dt > max_allowed_dt:
  65. limited_dt += 1
  66. work_time += max_allowed_dt
  67. else:
  68. work_time += dt
  69. # Adding sessions, assuming one session si in the avg
  70. work_time += (nb_sessions + 1) * (avg_dt if avg_dt else 300)
  71. work_time = int(0.5 + work_time)
  72.  
  73. logger.info('compute_work_time_estimation task:%s, %d progress loaded' % (task_id, count))
  74. stats = OrderedDict([
  75. ('progress_count', count),
  76. ('nb_sessions', nb_sessions),
  77. ('max_time', max_time),
  78. ('total_dt', total_dt),
  79. ('avg_dt', avg_dt),
  80. ('stddev_dt', stddev_dt),
  81. ('max_allowed_dt', max_allowed_dt),
  82. ('used_play_time', used_play_time),
  83. ('limited_dt', limited_dt),
  84. ('estimated_work_time', work_time),
  85. ])
  86. logger.info('compute_work_time_estimation task:%s, stats:\n%s' % (task_id, stats))
  87. logger.info('compute_work_time_estimation task:%s, estimated_work_time: %s (%s), total_dt: %s (%s)'
  88. ', max_time: %s (%s)' % (task_id, work_time, timedelta(seconds=work_time) if work_time else 'n/a',
  89. total_dt, timedelta(seconds=total_dt) if total_dt else 'n/a',
  90. max_time, timedelta(seconds=max_time)))
  91. return stats
  92.  
  93.  
  94. class Command(BaseCommand):
  95. help = 'Compute VTE Progress Stats'
  96.  
  97. def add_arguments(self, parser):
  98. parser.add_argument('task_id', help='Task Id')
  99.  
  100. def handle(self, *args, **options):
  101. self.stdout.write(Command.help)
  102.  
  103. task_id = options['task_id']
  104. stats = compute_estimated_work_time(task_id)
  105. self.stdout.write(pretty_dict(stats))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement