1111class AppUsageView (Resource ):
1212 @jwt_required
1313 def post (self , resource ):
14- if resource not in ['cpu' , 'memory' , 'network' ]:
15- return dict (status = 'fail' , message = 'Invalid resource name, pass cpu, memory, network' ), 400
14+ if resource not in ['cpu' , 'memory' , 'network' , 'gpu' ]:
15+ return dict (status = 'fail' , message = 'Invalid resource name, pass cpu, memory, network, gpu ' ), 400
1616
1717 app = get_app_data (request )
18-
19- if app .status_code != 200 :
20- return dict (status = 'fail' , message = app .message ), app .status_code
18+ try :
19+ if app .status_code != 200 :
20+ return dict (status = 'fail' , message = app .message ), app .status_code
21+ except :
22+ return app
2123
2224 start = app .start
2325 end = app .end
@@ -31,30 +33,37 @@ def post(self, resource):
3133 if not is_valid :
3234 return dict (status = 'fail' , message = message ), 400
3335
36+ QUERY = f'''{{container!="POD", image!="", namespace="{ namespace } ", pod=~"{ app_alias } .*"}}'''
37+
3438 try :
3539 if resource == 'cpu' :
3640 prom_data = prometheus .query_rang (
3741 start = start ,
3842 end = end ,
3943 step = step ,
40- metric = 'sum(rate(container_cpu_usage_seconds_total{container!="POD", image!="", namespace="' +
41- namespace + '", pod=~"' + app_alias + '.*"}[5m]))' )
44+ metric = f'''sum(rate(container_cpu_usage_seconds_total{ QUERY } [5m]))''' )
4245 elif resource == 'memory' :
4346 prom_data = prometheus .query_rang (
4447 start = start ,
4548 end = end ,
4649 step = step ,
47- metric = ' sum(rate(container_memory_usage_bytes{container_name!="POD", image!="",pod=~"' + app_alias + '.*", namespace="' + namespace + '" }[5m]))' )
50+ metric = f''' sum(rate(container_memory_usage_bytes{ QUERY } [5m]))'' ' )
4851 elif resource == 'network' :
4952 prom_data = prometheus .query_rang (
5053 start = start ,
5154 end = end ,
5255 step = step ,
53- metric = 'sum(rate(container_network_receive_bytes_total{namespace="' +
54- namespace + '", pod=~"' + app_alias + '.*"}[5m]))'
56+ metric = f'''sum(rate(container_network_receive_bytes_total{ QUERY } [5m]))''' )
57+
58+ elif resource == 'gpu' :
59+ prom_data = prometheus .query_rang (
60+ start = start ,
61+ end = end ,
62+ step = step ,
63+ metric = f'''sum(rate(container_gpu_usage_seconds_total{ QUERY } [5m]))'''
5564 )
5665 else :
57- return dict (status = 'fail' , message = 'Invalid resource name, pass cpu, memory, network' ), 400
66+ return dict (status = 'fail' , message = 'Invalid resource name, pass cpu, memory, network, gpu ' ), 400
5867 except Exception as error :
5968 return dict (status = 'fail' , message = str (error )), 500
6069
0 commit comments