mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-21 17:21:09 +01:00
updated home page
This commit is contained in:
4
app.py
4
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/<string:action>')
|
||||
api.add_resource(StatsApi,
|
||||
'/api/stats',
|
||||
'/api/stats/<string:action>')
|
||||
|
||||
# start flask app
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -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()
|
||||
|
||||
70
routes/stats.py
Normal file
70
routes/stats.py
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user