@@ -33,38 +33,37 @@ def post(self, resource):
3333 if not is_valid :
3434 return dict (status = 'fail' , message = message ), 400
3535
36+ QUERY = f'''{{container!="POD", image!="", namespace="{ namespace } ", pod=~"{ app_alias } .*"}}'''
37+
3638 try :
3739 if resource == 'cpu' :
3840 prom_data = prometheus .query_rang (
3941 start = start ,
4042 end = end ,
4143 step = step ,
42- metric = 'sum(rate(container_cpu_usage_seconds_total{container!="POD", image!="", namespace="' +
43- namespace + '", pod=~"' + app_alias + '.*"}[5m]))' )
44+ metric = f'''sum(rate(container_cpu_usage_seconds_total{ QUERY } [5m]))''' )
4445 elif resource == 'memory' :
4546 prom_data = prometheus .query_rang (
4647 start = start ,
4748 end = end ,
4849 step = step ,
49- 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]))'' ' )
5051 elif resource == 'network' :
5152 prom_data = prometheus .query_rang (
5253 start = start ,
5354 end = end ,
5455 step = step ,
55- metric = 'sum(rate(container_network_receive_bytes_total{namespace="' +
56- namespace + '", pod=~"' + app_alias + '.*"}[5m]))'
57- )
56+ metric = f'''sum(rate(container_network_receive_bytes_total{ QUERY } [5m]))''' )
57+
5858 elif resource == 'gpu' :
5959 prom_data = prometheus .query_rang (
6060 start = start ,
6161 end = end ,
6262 step = step ,
63- metric = 'sum(rate(container_gpu_usage_seconds_total{namespace="' +
64- namespace + '"}[5m]))'
63+ metric = f'''sum(rate(container_gpu_usage_seconds_total{ QUERY } [5m]))'''
6564 )
6665 else :
67- 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
6867 except Exception as error :
6968 return dict (status = 'fail' , message = str (error )), 500
7069
0 commit comments