Skip to content

Commit

Permalink
Report running total of new users for months queried.
Browse files Browse the repository at this point in the history
  • Loading branch information
grosscol committed Oct 29, 2024
1 parent 54ffd73 commit f139bce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
23 changes: 19 additions & 4 deletions bravo_api/blueprints/status/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,23 @@ def total_user_count(collection: pymongo.collection.Collection) -> int:
return len([item for item in cursor])


def new_user_count(collection: pymongo.collection.Collection) -> dict:
def add_running_user_count(user_counts: list) -> list:
"""
Given list of monthly user counts in ascending date order,
return same list with running sum of users added to each month.
"""
acc = 0
for month in user_counts:
acc = acc + month['new_users']
month['run_total'] = acc

return user_counts


def new_user_count(collection: pymongo.collection.Collection) -> list:
"""
Given users collection, query for new users that agreed to terms per month.
Return array of dicts one per month. E.g.
Return array of dicts one per month in date descending order. E.g.
[{'month': 10, 'new_users': 20, 'year': 2023},
{'month': 9, 'new_users': 30, 'year': 2023},
Expand All @@ -109,11 +122,13 @@ def new_user_count(collection: pymongo.collection.Collection) -> dict:
"year": "$_id.year",
"month": "$_id.month",
"new_users": 1}},
{"$sort": {"year": -1, "month": -1}}
{"$sort": {"year": 1, "month": 1}}
]

user_counts = collection.aggregate(user_counts_pline)
return [item for item in user_counts]
count_list = add_running_user_count([item for item in user_counts])

return count_list[::-1]


def active_user_count(collection: pymongo.collection.Collection) -> dict:
Expand Down
7 changes: 3 additions & 4 deletions tests/status/test_usage_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
{'max_user_per_day': 1, 'month': 9, 'year': 2023},
{'max_user_per_day': 1, 'month': 8, 'year': 2023}]

NEW_USERS_EXPECTED = [{'month': 10, 'new_users': 1, 'year': 2023},
{'month': 9, 'new_users': 2, 'year': 2023},
{'month': 8, 'new_users': 1, 'year': 2023}]

NEW_USERS_EXPECTED = [{'month': 10, 'new_users': 1, 'run_total': 4, 'year': 2023},
{'month': 9, 'new_users': 2, 'run_total': 3, 'year': 2023},
{'month': 8, 'new_users': 1, 'run_total': 1, 'year': 2023}]

TOTAL_USERS_EXPECTED = 4

Expand Down

0 comments on commit f139bce

Please sign in to comment.