Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report running total of new users for months queried. #27

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading