-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmeasure_networks.py
More file actions
68 lines (63 loc) · 2.5 KB
/
measure_networks.py
File metadata and controls
68 lines (63 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import uuid
import time
import logging
from asgiref.sync import async_to_sync
from django.utils import timezone
from django.core.management.base import BaseCommand
from django.conf import settings
from api import monitor
from api.models.app import App
from api.tasks import send_measurements
logger = logging.getLogger(__name__)
class Command(BaseCommand):
"""Management command for push data to manager"""
def _measure_networks(self, app_map, timestamp):
stop = timestamp - (timestamp % 3600)
start = stop - 3600
networks = []
for item in async_to_sync(monitor.query_network_receive_flow)(app_map.keys(), start, stop): # noqa
metric = item["metric"]
_, value = item["value"]
networks.append({
"app_id": str(app_map[metric['namespace']].uuid),
"owner": app_map[metric['namespace']].owner_id,
"type": "network",
"unit": "bytes",
"name": "rx",
"usage": value,
"kwargs": {
"pod": metric['pod'],
},
"timestamp": start
})
for item in async_to_sync(monitor.query_network_transmit_flow)(app_map.keys(), start, stop): # noqa
metric = item["metric"]
_, value = item["value"]
networks.append({
"app_id": str(app_map[metric['namespace']].uuid),
"owner": app_map[metric['namespace']].owner_id,
"type": "network",
"unit": "bytes",
"name": "tx",
"usage": value,
"kwargs": {
"pod": metric['pod'],
},
"timestamp": start
})
send_measurements.delay(networks)
def handle(self, *args, **options):
if settings.WORKFLOW_MANAGER_URL:
timestamp = int(time.time())
task_id = uuid.uuid4().hex
logger.info(f"pushing {task_id} limits to workflow_manager when {timezone.now()}")
app_map = {}
for app in App.objects.all():
app_map[app.id] = app
if len(app_map) % 1000 == 0:
self._measure_networks(app_map, timestamp)
app_map = {}
if len(app_map) > 0:
self._measure_networks(app_map, timestamp)
logger.info(f"pushed {task_id} limits to workflow_manager when {timezone.now()}")
self.stdout.write("done")