From 28f9ec5ffa9228434885483b9c76b9390ecd3c67 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Fri, 17 Mar 2017 11:41:44 +0100 Subject: [PATCH] Support the before query for user count and blender syc users --- cloud/stats/__init__.py | 10 +++++++--- cloud/stats/routes.py | 12 +++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/cloud/stats/__init__.py b/cloud/stats/__init__.py index 3c18ab0..a914f66 100644 --- a/cloud/stats/__init__.py +++ b/cloud/stats/__init__.py @@ -41,12 +41,12 @@ def count_nodes(query=None) -> int: return count -def count_users() -> int: +def count_users(query=None) -> int: u = current_app.db()['users'] - return u.count() + return u.count(query) -def count_blender_sync() -> int: +def count_blender_sync(query=None) -> int: pipeline = [ # 0 Find all startups.blend that are not deleted { @@ -79,6 +79,10 @@ def count_blender_sync() -> int: {'$count': 'tot'} ] c = current_app.db()['nodes'] + # If we provide a query, we extend the first $match step in the aggregation pipeline with + # with the extra parameters (for example _created) + if query: + pipeline[0]['$match'].update(query) # Return either a list with one item or an empty list r = list(c.aggregate(pipeline=pipeline)) count = 0 if not r else r[0]['tot'] diff --git a/cloud/stats/routes.py b/cloud/stats/routes.py index 588e63a..c0bc682 100644 --- a/cloud/stats/routes.py +++ b/cloud/stats/routes.py @@ -15,16 +15,18 @@ def get_stats(before: datetime.datetime): query_comments = {'node_type': 'comment'} query_assets = {'node_type': 'asset'} + date_query = {} + if before: - d = {'_created': {'$lt': before}} - query_comments.update(d) - query_assets.update(d) + date_query = {'_created': {'$lt': before}} + query_comments.update(date_query) + query_assets.update(date_query) stats = { 'comments': count_nodes(query_comments), 'assets': count_nodes(query_assets), - 'users_total': count_users(), - 'users_blender_sync': count_blender_sync(), + 'users_total': count_users(date_query), + 'users_blender_sync': count_blender_sync(date_query), } return stats