From c2ed9947642076a6f5f0aa6d42edee151b6aac3e Mon Sep 17 00:00:00 2001 From: vapao Date: Tue, 14 Jan 2020 00:27:34 +0800 Subject: [PATCH] U api update --- spug_api/apps/home/__init__.py | 0 spug_api/apps/home/urls.py | 10 +++++++ spug_api/apps/home/views.py | 49 ++++++++++++++++++++++++++++++++++ spug_api/spug/urls.py | 1 + 4 files changed, 60 insertions(+) create mode 100644 spug_api/apps/home/__init__.py create mode 100644 spug_api/apps/home/urls.py create mode 100644 spug_api/apps/home/views.py diff --git a/spug_api/apps/home/__init__.py b/spug_api/apps/home/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spug_api/apps/home/urls.py b/spug_api/apps/home/urls.py new file mode 100644 index 0000000..0637e8a --- /dev/null +++ b/spug_api/apps/home/urls.py @@ -0,0 +1,10 @@ +from django.urls import path + +from .views import * + +urlpatterns = [ + path('statistic/', get_statistic), + path('alarm/', get_alarm), + path('deploy/', get_deploy), + path('request/', get_request), +] diff --git a/spug_api/apps/home/views.py b/spug_api/apps/home/views.py new file mode 100644 index 0000000..14438dd --- /dev/null +++ b/spug_api/apps/home/views.py @@ -0,0 +1,49 @@ +from django.db.models import F +from apps.app.models import App +from apps.host.models import Host +from apps.schedule.models import Task +from apps.monitor.models import Detection +from apps.alarm.models import Alarm +from apps.deploy.models import Deploy, DeployRequest +from libs.utils import json_response, human_date +from datetime import datetime, timedelta +import json + + +def get_statistic(request): + data = { + 'app': App.objects.count(), + 'host': Host.objects.count(), + 'task': Task.objects.count(), + 'detection': Detection.objects.count() + } + return json_response(data) + + +def get_alarm(request): + now = datetime.now() + data = {human_date(now - timedelta(days=x + 1)): 0 for x in range(14)} + for alarm in Alarm.objects.filter(status='1', created_at__gt=human_date(now - timedelta(days=14))): + date = alarm.created_at[:10] + if date in data: + data[date] += 1 + data = [{'date': k, 'value': v} for k, v in data.items()] + return json_response(data) + + +def get_request(request): + data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()} + for req in DeployRequest.objects.filter(created_at__gt=human_date()): + data[req.deploy.app_id]['count'] += 1 + data = sorted(data.values(), key=lambda x: x['count'], reverse=True)[:5] + return json_response(data) + + +def get_deploy(request): + host = Host.objects.count() + data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()} + for dep in Deploy.objects.all(): + data[dep.app_id]['count'] += len(json.loads(dep.host_ids)) + data = filter(lambda x: x['count'], data.values()) + return json_response({'host': host, 'res': list(data)}) + diff --git a/spug_api/spug/urls.py b/spug_api/spug/urls.py index 24d3792..cae2353 100644 --- a/spug_api/spug/urls.py +++ b/spug_api/spug/urls.py @@ -26,5 +26,6 @@ urlpatterns = [ path('config/', include('apps.config.urls')), path('app/', include('apps.app.urls')), path('deploy/', include('apps.deploy.urls')), + path('home/', include('apps.home.urls')), path('apis/', include('apps.apis.urls')), ]