def handle(self, *args, **options): date = datetime.date.today() if options['yesterday']: date = date - datetime.timedelta(days=1) WorkMetrics.update_all_work_metrics() DailyWorkMetrics.update_daily_work_metrics(date)
def update_yesterdays_metrics(): """ Task to update daily metrics for yesterday. This will replace any existing task. """ WorkMetrics.update_all_work_metrics() yesterday = date.today() - timedelta(days=1) DailyWorkMetrics.update_daily_work_metrics(yesterday)
def test_update(self): work = Work.objects.get(pk=1) metrics = WorkMetrics.create_or_update(work) metrics.n_languages = 99 metrics.save() metrics = WorkMetrics.create_or_update(work) metrics.refresh_from_db() self.assertEqual(metrics.n_languages, 1)
def test_metrics(self): for work in Work.objects.all(): WorkMetrics.create_or_update(work) date = datetime.date(2019, 2, 1) DailyWorkMetrics.create_or_update(date) dailies = DailyWorkMetrics.objects.filter(date=date).all() self.assertEqual(len(dailies), 2)
def update_yesterdays_metrics(): """ Task to update daily metrics for yesterday. This will replace any existing task. """ try: WorkMetrics.update_all_work_metrics() yesterday = date.today() - timedelta(days=1) DailyWorkMetrics.update_daily_work_metrics(yesterday) except Exception as e: log.error(f"Error updating work metrics: {e}", exc_info=e) raise e
def test_metrics(self): for work in Work.objects.all(): WorkMetrics.create_or_update(work) date = datetime.date(2019, 2, 1) DailyWorkMetrics.create_or_update(date) dailies = DailyWorkMetrics.objects.filter(date=date).all() # DailyWorkMetrics should have been created for two places: # - `za` (8 Works, pk 1 to 8 and 10 of work fixtures) # - `za-cpt` (1 Work, pk 9 of work fixtures) self.assertEqual(len(dailies), 2)
def get_context_data(self, **kwargs): context = super(WorkOverviewView, self).get_context_data(**kwargs) context['active_tasks'] = Task.objects\ .filter(work=self.work)\ .exclude(state='done')\ .exclude(state='cancelled')\ .order_by('-created_at') context['work_timeline'] = self.get_work_timeline() # ensure work metrics are up to date WorkMetrics.create_or_update(self.work) return context
def get_context_data(self, **kwargs): context = super(WorkOverviewView, self).get_context_data(**kwargs) work_tasks = Task.objects.filter(work=self.work) context['active_tasks'] = work_tasks.exclude(state=Task.DONE)\ .exclude(state=Task.CANCELLED)\ .order_by('-created_at') context['work_timeline'] = self.get_work_timeline(self.work) context['contributors'] = self.get_top_contributors() # ensure work metrics are up to date WorkMetrics.create_or_update(self.work) return context
def test_basics(self): work = Work.objects.get(pk=1) metrics = WorkMetrics.create_or_update(work) self.assertEqual(metrics.n_languages, 1) self.assertEqual(metrics.n_points_in_time, 1) self.assertEqual(metrics.n_expressions, 1) self.assertEqual(metrics.n_expected_expressions, 1)
def test_metrics_for_all_fixtures(self): for work in Work.objects.all(): WorkMetrics.create_or_update(work)
def update_work_metrics(self): self.stdout.write(self.style.SUCCESS('Updating individual work metrics.')) for work in Work.objects.all(): WorkMetrics.create_or_update(work) self.stdout.write(self.style.SUCCESS('Done'))