|
1 | | -import os |
2 | | -import datetime |
3 | 1 | import json |
4 | | -from types import SimpleNamespace |
5 | | - |
6 | | - |
7 | | -import datetime |
8 | 2 | from prometheus_http_client import Prometheus |
9 | 3 | from flask_restful import Resource, request |
10 | | -from flask import current_app, render_template |
| 4 | +from app.helpers.utils import get_project_data |
11 | 5 |
|
12 | 6 | from app.helpers.authenticate import ( |
13 | 7 | jwt_required |
14 | 8 | ) |
15 | 9 |
|
16 | | -# Todo: figure out a way to connect to projects and get projects |
17 | | - |
18 | 10 |
|
19 | 11 | class ProjectMemoryUsageView(Resource): |
20 | | - |
21 | 12 | @jwt_required |
22 | 13 | def post(self, project_id): |
23 | | - return dict(status='success', data=dict()), 200 |
| 14 | + project = get_project_data(project_id, request) |
| 15 | + |
| 16 | + if project.status_code != 200: |
| 17 | + return dict(status='fail', message=project.message), project.status_code |
| 18 | + |
| 19 | + start = project.start |
| 20 | + end = project.end |
| 21 | + step = project.step |
| 22 | + namespace = project.namespace |
| 23 | + prometheus = Prometheus() |
| 24 | + |
| 25 | + try: |
| 26 | + prom_memory_data = prometheus.query_rang( |
| 27 | + start=start, |
| 28 | + end=end, |
| 29 | + step=step, |
| 30 | + metric='sum(rate(container_memory_usage_bytes{container_name!="POD", image!="", namespace="'+namespace+'"}[5m]))') |
| 31 | + except Exception as error: |
| 32 | + return dict(status='fail', message=str(error)), 500 |
| 33 | + |
| 34 | + new_data = json.loads(prom_memory_data) |
| 35 | + final_data_list = [] |
| 36 | + |
| 37 | + try: |
| 38 | + for value in new_data["data"]["result"][0]["values"]: |
| 39 | + mem_case = {'timestamp': float( |
| 40 | + value[0]), 'value': float(value[1])} |
| 41 | + final_data_list.append(mem_case) |
| 42 | + except: |
| 43 | + return dict(status='fail', message='No values found'), 404 |
| 44 | + |
| 45 | + return dict(status='success', data=dict(values=final_data_list)), 200 |
24 | 46 |
|
25 | 47 |
|
26 | 48 | class ProjectCPUView(Resource): |
27 | | - # @jwt_required |
| 49 | + @jwt_required |
28 | 50 | def post(self, project_id): |
29 | | - return dict(status='success', data=dict()), 200 |
| 51 | + project = get_project_data(project_id, request) |
| 52 | + |
| 53 | + if project.status_code != 200: |
| 54 | + return dict(status='fail', message=project.message), project.status_code |
| 55 | + |
| 56 | + start = project.start |
| 57 | + end = project.end |
| 58 | + step = project.step |
| 59 | + namespace = project.namespace |
| 60 | + prometheus = Prometheus() |
| 61 | + |
| 62 | + try: |
| 63 | + prom_cpu_data = prometheus.query_rang( |
| 64 | + start=start, |
| 65 | + end=end, |
| 66 | + step=step, |
| 67 | + metric='sum(rate(container_cpu_usage_seconds_total{container!="POD", image!="",namespace="' + |
| 68 | + namespace+'"}[5m]))' |
| 69 | + ) |
| 70 | + except Exception as error: |
| 71 | + return dict(status='fail', message=str(error)), 500 |
| 72 | + |
| 73 | + new_data = json.loads(prom_cpu_data) |
| 74 | + final_data_list = [] |
| 75 | + |
| 76 | + try: |
| 77 | + for value in new_data["data"]["result"][0]["values"]: |
| 78 | + mem_case = {'timestamp': float( |
| 79 | + value[0]), 'value': float(value[1])} |
| 80 | + final_data_list.append(mem_case) |
| 81 | + except: |
| 82 | + return dict(status='fail', message='No values found'), 404 |
| 83 | + |
| 84 | + return dict(status='success', data=dict(values=final_data_list)), 200 |
30 | 85 |
|
31 | 86 |
|
32 | 87 | class ProjectNetworkRequestView(Resource): |
33 | | - # @jwt_required |
| 88 | + @ jwt_required |
34 | 89 | def post(self, project_id): |
35 | | - return dict(status='success', data=dict()), 200 |
| 90 | + project = get_project_data(project_id, request) |
36 | 91 |
|
| 92 | + if project.status_code != 200: |
| 93 | + return dict(status='fail', message=project.message), project.status_code |
37 | 94 |
|
38 | | -class ProjectStorageUsageView(Resource): |
39 | | - # @jwt_required |
40 | | - def post(self, project_id): |
41 | | - return dict(status='success', data=dict()), 200 |
| 95 | + start = project.start |
| 96 | + end = project.end |
| 97 | + step = project.step |
| 98 | + namespace = project.namespace |
| 99 | + prometheus = Prometheus() |
| 100 | + |
| 101 | + try: |
| 102 | + prom_ntw_data = prometheus.query_rang( |
| 103 | + start=start, |
| 104 | + end=end, |
| 105 | + step=step, |
| 106 | + metric='sum(rate(container_network_receive_bytes_total{namespace="' + |
| 107 | + namespace+'"}[5m]))' |
| 108 | + ) |
| 109 | + except Exception as error: |
| 110 | + return dict(status='fail', message=str(error)), 500 |
| 111 | + |
| 112 | + new_data = json.loads(prom_ntw_data) |
| 113 | + final_data_list = [] |
| 114 | + |
| 115 | + try: |
| 116 | + for value in new_data["data"]["result"][0]["values"]: |
| 117 | + mem_case = {'timestamp': float( |
| 118 | + value[0]), 'value': float(value[1])} |
| 119 | + final_data_list.append(mem_case) |
| 120 | + except: |
| 121 | + return dict(status='fail', message='No values found'), 404 |
| 122 | + |
| 123 | + return dict(status='success', data=dict(values=final_data_list)), 200 |
0 commit comments