def count_active_users(frame, end_date, days_back): """ Tally active users according to externally-defined heuristics, specifically client-reported subsession_start_date. :frame DataFrame(): conforming to main_summary schema :end_date DT.date(): the last day on which to count activity. :days_back int: how far back to go; 0 for daily and 28 for monthly. Note that return values are not stable over time as activity trickles in with long submission latencies. See https://bugzilla.mozilla.org/show_bug.cgi?id=1240849 """ params = U.generate_filter_parameters(end_date, days_back) filtered = filter_date_range( frame, frame.subsession_start_date, params["min_activity_iso"], params["max_activity_iso"], frame.submission_date_s3, params["min_submission_string"], params["max_submission_string"], ) return count_distinct_clientids(filtered)
def test_generate_filter_parameters(): """ Check the two meaningful cases: DAU (0 days) and MAU(28 days). """ expected0 = { 'min_activity_iso': '2017-01-31', 'max_activity_iso': '2017-02-01', 'min_submission_string': '20170131', 'max_submission_string': '20170210' } actual0 = utils.generate_filter_parameters(DT.date(2017, 1, 31), 0) assert expected0 == actual0, str(actual0) expected28 = { 'min_activity_iso': '2017-01-03', 'max_activity_iso': '2017-02-01', 'min_submission_string': '20170103', 'max_submission_string': '20170210' } actual28 = utils.generate_filter_parameters(DT.date(2017, 1, 31), 28) assert expected28 == actual28
def extract_month(first_day, frame): """ Pull a month's worth of activity out of frame according to the heuristics implemented in moztelemetry.standards. :first_day DT.date(Y, m, 1) :frame DataFrame homologous with main_summary """ month = first_day.month day_pointer = first_day while day_pointer.month == month: day_pointer += DT.timedelta(1) last_day = day_pointer - DT.timedelta(1) days_back = (last_day - first_day).days params = generate_filter_parameters(last_day, days_back) filtered = filter_date_range(frame, frame.subsession_start_date, params['min_activity_iso'], params['max_activity_iso'], frame.submission_date_s3, params['min_submission_string'], params['max_submission_string']) return filtered