Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- This management command compute stats based on VTE progress data of a given task id
- """
- from __future__ import unicode_literals
- import json
- import logging
- from django.core.management.base import BaseCommand
- from core import pretty_dict
- from vte.progress import VteProgress
- logger = logging.getLogger(__name__)
- def compute_estimated_work_time(task_id):
- """
- Compute the work time estimation of a given task id based on stored vte progress timestamp
- :param task_id:
- :return: stats
- """
- logger.info('compute_work_time_estimation task:%s' % task_id)
- count = 0
- previous_vp = None
- nb_sessions = 0
- used_play_time = 0
- first_ts = 0
- last_ts = 0
- total_dt = 0
- dt_list = []
- play_time_list = []
- limited_dt = 0
- for p in VteProgress.query(hash_key=task_id):
- assert (int(p.ts) > int(last_ts))
- count += 1
- vp = json.loads(p.progress)
- if previous_vp is not None:
- dt = (int(p.ts) - int(last_ts)) // 1000000
- is_new_session = vp['task_session'] != previous_vp['task_session']
- play_time = vp['video_position'] != previous_vp['video_position']
- if is_new_session:
- nb_sessions += 1
- else:
- total_dt += dt
- dt_list.append(dt)
- play_time_list.append(play_time)
- else:
- first_ts = p.ts
- last_ts = p.ts
- previous_vp = vp
- logger.info('compute_work_time_estimation task:%s, %d progress loaded' % (task_id, count))
- # Compute stats to estimate the work time
- max_time = (int(last_ts) - int(first_ts)) // 1000000
- work_time = max_allowed_dt = avg_dt = stddev_dt = None
- if count:
- work_time = 0
- if len(dt_list):
- avg_dt = int(0.5 + total_dt / len(dt_list))
- stddev_dt = int(0.5 + math.sqrt(sum(((dt - avg_dt) * (dt - avg_dt)) for dt in dt_list) / len(dt_list)))
- max_allowed_dt = avg_dt + 2 * stddev_dt
- for dt in dt_list:
- if dt > max_allowed_dt:
- limited_dt += 1
- work_time += max_allowed_dt
- else:
- work_time += dt
- # Adding sessions, assuming one session si in the avg
- work_time += (nb_sessions + 1) * (avg_dt if avg_dt else 300)
- work_time = int(0.5 + work_time)
- logger.info('compute_work_time_estimation task:%s, %d progress loaded' % (task_id, count))
- stats = OrderedDict([
- ('progress_count', count),
- ('nb_sessions', nb_sessions),
- ('max_time', max_time),
- ('total_dt', total_dt),
- ('avg_dt', avg_dt),
- ('stddev_dt', stddev_dt),
- ('max_allowed_dt', max_allowed_dt),
- ('used_play_time', used_play_time),
- ('limited_dt', limited_dt),
- ('estimated_work_time', work_time),
- ])
- logger.info('compute_work_time_estimation task:%s, stats:\n%s' % (task_id, stats))
- logger.info('compute_work_time_estimation task:%s, estimated_work_time: %s (%s), total_dt: %s (%s)'
- ', max_time: %s (%s)' % (task_id, work_time, timedelta(seconds=work_time) if work_time else 'n/a',
- total_dt, timedelta(seconds=total_dt) if total_dt else 'n/a',
- max_time, timedelta(seconds=max_time)))
- return stats
- class Command(BaseCommand):
- help = 'Compute VTE Progress Stats'
- def add_arguments(self, parser):
- parser.add_argument('task_id', help='Task Id')
- def handle(self, *args, **options):
- self.stdout.write(Command.help)
- task_id = options['task_id']
- stats = compute_estimated_work_time(task_id)
- self.stdout.write(pretty_dict(stats))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement