55from django .core .management .base import BaseCommand
66from django .conf import settings
77from api import influxdb
8- from api .models .config import Config
8+ from api .models .app import App
99from api .tasks import send_measurements
1010
1111logger = logging .getLogger (__name__ )
1414class Command (BaseCommand ):
1515 """Management command for push data to manager"""
1616
17- def _measure_networks (self , config_map , timestamp ):
17+ def _measure_networks (self , app_map , timestamp ):
1818 stop = timestamp - (timestamp % 3600 )
1919 start = stop - 3600
2020 networks = []
21- for record in influxdb .query_network_flow (config_map .keys (), start , stop ):
21+ for record in influxdb .query_network_flow (app_map .keys (), start , stop ):
2222 app_id = record ["namespace" ]
23- owner_id = config_map [app_id ].owner_id
23+ owner_id = app_map [app_id ].owner_id
2424 networks .append ({
2525 "app_id" : app_id ,
26- "owner_id " : owner_id ,
26+ "owner " : owner_id ,
2727 "name" : record ["pod_name" ],
2828 "type" : "network" ,
2929 "unit" : "bytes" ,
@@ -37,13 +37,13 @@ def handle(self, *args, **options):
3737 timestamp = int (time .time ())
3838 task_id = uuid .uuid4 ().hex
3939 logger .info (f"pushing { task_id } limits to workflow_manager when { timezone .now ()} " )
40- config_map = {}
41- for config in Config .objects .all ():
42- config_map [ config . app_id ] = config
43- if len (config_map ) % 1000 == 0 :
44- send_measurements . delay ( self ._measure_networks (config_map , timestamp ) )
45- config_map = {}
46- if len (config_map ) > 0 :
47- send_measurements . delay ( self ._measure_networks (config_map , timestamp ) )
40+ app_map = {}
41+ for app in App .objects .all ():
42+ app_map [ app . id ] = app
43+ if len (app_map ) % 1000 == 0 :
44+ self ._measure_networks (app_map , timestamp )
45+ app_map = {}
46+ if len (app_map ) > 0 :
47+ self ._measure_networks (app_map , timestamp )
4848 logger .info (f"pushed { task_id } limits to workflow_manager when { timezone .now ()} " )
4949 self .stdout .write ("done" )
0 commit comments