def test_store_mau_metrics(monkeypatch, sm_test_data): """ Basic minimal test """ mock_today = datetime(year=sm_test_data['year_for'], month=sm_test_data['month_for'], day=1) freezer = freeze_time(mock_today) freezer.start() site = sm_test_data['site'] data = store_mau_metrics(site=site) freezer.stop() site_mau = data['smo'].mau assert site_mau == len(sm_test_data['student_modules']) assert data['smo'].year == mock_today.year assert data['smo'].month == mock_today.month assert len(data['cmos']) == len(sm_test_data['course_overviews']) for cmo in data['cmos']: assert cmo.year == mock_today.year assert cmo.month == mock_today.month expected_course_sm = get_student_modules_for_course_in_site( site=site, course_id=cmo.course_id) expected_mau = get_mau_from_student_modules( student_modules=expected_course_sm, year=mock_today.year, month=mock_today.month) # TODO: Fix, rudimentary check, improve assert expected_mau
def test_get_mau_from_sm_for_site(sm_test_data): sm = get_student_modules_for_site(sm_test_data['site']) users = get_mau_from_student_modules(student_modules=sm, year=2019, month=10) sm_check = sm.values_list('student__id', flat=True).distinct() assert set(users) == set(sm_check)
def get_all_mau_for_site_course(site, courselike, month_for): """ Extract a queryset of distinct MAU user ids for the site and course """ sm_recs = get_student_modules_for_course_in_site(site, as_course_key(courselike)) mau_ids = get_mau_from_student_modules(student_modules=sm_recs, year=month_for.year, month=month_for.month) return mau_ids
def backfill_monthly_metrics_for_site(site, overwrite): """Quick hack function to backfill all historical site metrics for the site We are a bit verbose with the output to help testing and validation since this function was quickly put together """ site_sm = get_student_modules_for_site(site) if not site_sm: return None first_created = site_sm.order_by('created').first().created # We do this because there _might_ be a bug in `dateutil.rrule`. It was # skipping over February when we used the `created` field directly for the # start_month variable start_month = datetime(year=first_created.year, month=first_created.month, day=1).replace(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): mau = get_mau_from_student_modules(student_modules=site_sm, year=dt.year, month=dt.month) month_sm = site_sm.filter(created__year=dt.year, created__month=dt.month) month_learners = month_sm.values_list('student__id', flat=True).distinct() obj, created = SiteMonthlyMetrics.add_month( site=site, year=dt.year, month=dt.month, active_user_count=month_learners.count(), overwrite=overwrite) backfill_rec = dict(obj=obj, created=created, dt=dt, mau=mau) backfilled.append(backfill_rec) return backfilled