def backfill_monthly_metrics_for_site(site, overwrite=False, use_raw_sql=False): """Backfill specified months' historical site metrics for the specified site """ site_sm = get_student_modules_for_site(site) if not site_sm: return None first_created = site_sm.order_by('created').first().created start_month = datetime(year=first_created.year, month=first_created.month, day=1, tzinfo=utc) last_month = datetime.utcnow().replace(tzinfo=utc) - relativedelta( months=1) backfilled = [] for dt in rrule(freq=MONTHLY, dtstart=start_month, until=last_month): obj, created = fill_month(site=site, month_for=dt, student_modules=site_sm, overwrite=overwrite, use_raw=use_raw_sql) backfilled.append(dict(obj=obj, created=created, dt=dt)) return backfilled
def test_fill_month_with_sm_wo_overwrite(monkeypatch, smm_test_data): """ """ assert SiteMonthlyMetrics.objects.count() == 0 mock_today = smm_test_data['mock_today'] freezer = freeze_time(mock_today) freezer.start() site = smm_test_data['site'] def mock_get_student_modules_for_site(site): assert site == site return StudentModule.objects.all() monkeypatch.setattr('figures.pipeline.site_monthly_metrics.get_student_modules_for_site', mock_get_student_modules_for_site) obj, created = fill_month(site=site, month_for=smm_test_data['month_before'], student_modules=StudentModule.objects.all()) freezer.stop() assert obj and created assert obj.active_user_count == len(smm_test_data['month_before_sm']) assert obj.site == site assert obj.month_for == smm_test_data['month_before'].date()