diff --git a/app.py b/app.py index bfda920c..44446f79 100644 --- a/app.py +++ b/app.py @@ -7,6 +7,7 @@ from routes.deploys import DeployApi from routes.files import FileApi from routes.nodes import NodeApi from routes.spiders import SpiderApi +from routes.stats import StatsApi from routes.tasks import TaskApi # flask app instance @@ -40,6 +41,9 @@ api.add_resource(TaskApi, api.add_resource(FileApi, '/api/files', '/api/files/') +api.add_resource(StatsApi, + '/api/stats', + '/api/stats/') # start flask app if __name__ == '__main__': diff --git a/db/manager.py b/db/manager.py index 4d15b6e0..3c3ca875 100644 --- a/db/manager.py +++ b/db/manager.py @@ -73,5 +73,9 @@ class DbManager(object): return item.get('version') return None + def aggregate(self, col_name: str, pipelines, **kwargs): + col = self.db[col_name] + return col.aggregate(pipelines, **kwargs) + db_manager = DbManager() diff --git a/routes/stats.py b/routes/stats.py new file mode 100644 index 00000000..e425737b --- /dev/null +++ b/routes/stats.py @@ -0,0 +1,70 @@ +import os + +from flask_restful import reqparse, Resource + +from db.manager import db_manager +from utils import jsonify + + +class StatsApi(Resource): + def get(self, action=None): + # action + if action is not None: + if not hasattr(self, action): + return { + 'status': 'ok', + 'code': 400, + 'error': 'action "%s" invalid' % action + }, 400 + return getattr(self, action)() + + else: + return {} + + def get_home_stats(self): + # overview stats + task_count = db_manager.count('tasks', {}) + spider_count = db_manager.count('spiders', {}) + node_count = db_manager.count('nodes', {}) + deploy_count = db_manager.count('deploys', {}) + + # daily stats + cur = db_manager.aggregate('tasks', [ + { + '$project': { + 'date': { + '$dateToString': { + 'format': '%Y-%m-%d', + 'date': '$create_ts' + } + } + } + }, + { + '$group': { + '_id': '$date', + 'count': { + '$sum': 1 + } + } + }, + { + '$sort': { + '_id': 1 + } + } + ]) + daily_tasks = [] + for item in cur: + daily_tasks.append(item) + + return { + 'status': 'ok', + 'overview_stats': { + 'task_count': task_count, + 'spider_count': spider_count, + 'node_count': node_count, + 'deploy_count': deploy_count, + }, + 'daily_tasks': daily_tasks + }