views.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from django.db.models import F
  5. from apps.app.models import App
  6. from apps.host.models import Host
  7. from apps.schedule.models import Task
  8. from apps.monitor.models import Detection
  9. from apps.alarm.models import Alarm
  10. from apps.deploy.models import Deploy, DeployRequest
  11. from apps.account.utils import get_host_perms
  12. from libs.utils import json_response, human_date, parse_time
  13. from libs.parser import JsonParser, Argument
  14. from datetime import datetime, timedelta
  15. import json
  16. def get_statistic(request):
  17. if request.user.is_supper:
  18. app = App.objects.count()
  19. host = Host.objects.count()
  20. else:
  21. deploy_perms, host_perms = request.user.deploy_perms, get_host_perms(request.user)
  22. app = App.objects.filter(id__in=deploy_perms['apps']).count()
  23. host = len(host_perms)
  24. data = {
  25. 'app': app,
  26. 'host': host,
  27. 'task': Task.objects.count(),
  28. 'detection': Detection.objects.count()
  29. }
  30. return json_response(data)
  31. def get_alarm(request):
  32. form, error = JsonParser(
  33. Argument('type', required=False),
  34. Argument('name', required=False)
  35. ).parse(request.GET, True)
  36. if error is None:
  37. now = datetime.now()
  38. data = {human_date(now - timedelta(days=x + 1)): 0 for x in range(14)}
  39. for alarm in Alarm.objects.filter(status='1', created_at__gt=human_date(now - timedelta(days=14)), **form):
  40. date = alarm.created_at[:10]
  41. if date in data:
  42. data[date] += 1
  43. data = [{'date': k, 'value': v} for k, v in data.items()]
  44. return json_response(data)
  45. return json_response(error=error)
  46. def get_request(request):
  47. form, error = JsonParser(
  48. Argument('duration', type=list, help='参数错误')
  49. ).parse(request.body)
  50. if error is None:
  51. s_date = form.duration[0]
  52. e_date = (parse_time(form.duration[1]) + timedelta(days=1)).strftime('%Y-%m-%d')
  53. data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()}
  54. for req in DeployRequest.objects.filter(created_at__gt=s_date, created_at__lt=e_date):
  55. data[req.deploy.app_id]['count'] += 1
  56. data = sorted(data.values(), key=lambda x: x['count'], reverse=True)[:10]
  57. return json_response(data)
  58. return json_response(error=error)
  59. def get_deploy(request):
  60. host = Host.objects.filter(deleted_at__isnull=True).count()
  61. data = {x.id: {'name': x.name, 'count': 0} for x in App.objects.all()}
  62. for dep in Deploy.objects.all():
  63. data[dep.app_id]['count'] += len(json.loads(dep.host_ids))
  64. data = filter(lambda x: x['count'], data.values())
  65. return json_response({'host': host, 'res': list(data)})