Skip to content

Commit

Permalink
Merge pull request #27 from statgen/runtot
Browse files Browse the repository at this point in the history
Report running total of new users for months queried.
  • Loading branch information
grosscol authored Oct 30, 2024
2 parents 54ffd73 + c9da9fb commit 3a58690
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
29 changes: 22 additions & 7 deletions bravo_api/blueprints/status/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,27 @@ 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},
{'month': 8, 'new_users': 40, 'year': 2023}]
[{'month': 10, 'new_users': 20, 'run_total': 90, 'year': 2023},
{'month': 9, 'new_users': 30, 'run_total': 70, 'year': 2023},
{'month': 8, 'new_users': 40, 'run_total': 30, 'year': 2023}]
"""
user_counts_pline = [
{"$match": {"agreed_to_terms": {"$eq": True}}},
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 3a58690

Please sign in to comment.