11import uuid
22import time
33import logging
4+ from asgiref .sync import async_to_sync
45from django .utils import timezone
56from django .core .management .base import BaseCommand
67from django .conf import settings
@@ -18,21 +19,34 @@ def _measure_networks(self, app_map, timestamp):
1819 stop = timestamp - (timestamp % 3600 )
1920 start = stop - 3600
2021 networks = []
21- for namespace , pod_name , rx_bytes , tx_bytes in monitor .query_network_flow (
22- app_map .keys (), start , stop ):
23- owner_id = app_map [namespace ].owner_id
24- base_measure = {
25- "app_id" : str (app_map [namespace ].uuid ),
26- "owner" : owner_id ,
22+ for metric , (_ , value ) in async_to_sync (monitor .query_network_receive_flow (
23+ app_map .keys (), start , stop )):
24+ networks .append ({
25+ "app_id" : str (app_map [metric ['namespace' ]].uuid ),
26+ "owner" : app_map [metric ['namespace' ]].owner_id ,
2727 "type" : "network" ,
2828 "unit" : "bytes" ,
29+ "name" : "rx" ,
30+ "usage" : value ,
2931 "kwargs" : {
30- "pod" : pod_name ,
32+ "pod" : metric [ 'pod' ] ,
3133 },
3234 "timestamp" : start
33- }
34- networks .append (base_measure | {"name" : "rx" , "usage" : rx_bytes })
35- networks .append (base_measure | {"name" : "tx" , "usage" : tx_bytes })
35+ })
36+ for metric , (_ , value ) in async_to_sync (monitor .query_network_transmit_flow (
37+ app_map .keys (), start , stop )):
38+ networks .append ({
39+ "app_id" : str (app_map [metric ['namespace' ]].uuid ),
40+ "owner" : app_map [metric ['namespace' ]].owner_id ,
41+ "type" : "network" ,
42+ "unit" : "bytes" ,
43+ "name" : "tx" ,
44+ "usage" : value ,
45+ "kwargs" : {
46+ "pod" : metric ['pod' ],
47+ },
48+ "timestamp" : start
49+ })
3650 send_measurements .delay (networks )
3751
3852 def handle (self , * args , ** options ):
0 commit comments