|
7 | 7 | import json |
8 | 8 | import ssl |
9 | 9 | import time |
| 10 | +import zlib |
10 | 11 | import random |
11 | 12 | import aiohttp |
12 | 13 | import requests |
13 | 14 | import warnings |
14 | 15 |
|
15 | 16 | from urllib.parse import urljoin |
16 | | -from asgiref.sync import async_to_sync |
17 | 17 | from django.db import transaction |
18 | 18 | from django.db.models import Q |
19 | 19 | from django.core.cache import cache |
@@ -1226,42 +1226,6 @@ def _get_app(self): |
1226 | 1226 | self.check_object_permissions(self.request, app) |
1227 | 1227 | return app |
1228 | 1228 |
|
1229 | | - def _get_usage(self, func): |
1230 | | - def wrap(app_id, ptype, every, start, stop, step): |
1231 | | - result = {} |
1232 | | - for item in async_to_sync(func)(app_id, ptype, every, start, stop, step): |
1233 | | - result[item['metric']['pod']] = item['values'] |
1234 | | - return result |
1235 | | - return wrap |
1236 | | - |
1237 | | - @method_decorator(cache_page(settings.DRYCC_METRICS_EXPIRY)) |
1238 | | - @method_decorator(vary_on_headers("Authorization")) |
1239 | | - def status(self, request, **kwargs): |
1240 | | - warnings.warn( |
1241 | | - 'this interface will be removed in the next version.', PendingDeprecationWarning) |
1242 | | - app_id = self._get_app().id |
1243 | | - data = serializers.MetricSerializer(data=self.request.query_params) |
1244 | | - if not data.is_valid(): |
1245 | | - return Response(data.errors, status=status.HTTP_422_UNPROCESSABLE_ENTITY) |
1246 | | - every, start, stop, step = data.validated_data["every"], data.validated_data[ |
1247 | | - 'start'], data.validated_data['stop'], data.validated_data['step'] |
1248 | | - params = { |
1249 | | - "app_id": app_id, "ptype": kwargs['ptype'], "every": every, |
1250 | | - "start": start, "stop": stop, "step": step, |
1251 | | - } |
1252 | | - return Response({ |
1253 | | - "id": app_id, |
1254 | | - "ptype": kwargs['ptype'], |
1255 | | - "usage": { |
1256 | | - "cpus": self._get_usage(monitor.query_cpu_usage)(**params), |
1257 | | - "memory": self._get_usage(monitor.query_memory_usage)(**params), |
1258 | | - "networks": { |
1259 | | - "receive": self._get_usage(monitor.query_network_receive_usage)(**params), |
1260 | | - "transmit": self._get_usage(monitor.query_network_transmit_usage)(**params), |
1261 | | - } |
1262 | | - } |
1263 | | - }) |
1264 | | - |
1265 | 1229 | @method_decorator(cache_page(settings.DRYCC_METRICS_EXPIRY)) |
1266 | 1230 | @method_decorator(vary_on_headers("Authorization")) |
1267 | 1231 | def metric(self, request, **kwargs): |
@@ -1302,16 +1266,17 @@ async def sample(self, name, labels_str, value): |
1302 | 1266 | app_id = labels.get("namespace", None) |
1303 | 1267 | if not app_id: |
1304 | 1268 | return None |
1305 | | - owner_id, timeout = self.cache.get(app_id, self.default_cache_value) |
1306 | | - if (owner_id < 0 and timeout < 0) or time.time() > timeout: |
| 1269 | + account_id, timeout = self.cache.get(app_id, self.default_cache_value) |
| 1270 | + if (account_id < 0 and timeout < 0) or time.time() > timeout: |
1307 | 1271 | if app := await models.app.App.objects.filter(id=app_id).afirst(): |
1308 | | - owner_id = app.owner_id |
| 1272 | + account_id = app.owner_id |
1309 | 1273 | else: |
1310 | | - owner_id = -1 |
1311 | | - self.cache[app_id] = (owner_id, time.time() + random.randint(600, 1200)) |
1312 | | - if owner_id < 0: |
| 1274 | + account_id = -1 |
| 1275 | + self.cache[app_id] = (account_id, time.time() + random.randint(600, 1200)) |
| 1276 | + if account_id < 0: |
1313 | 1277 | return None |
1314 | | - labels.update({'vm_project_id': app_id, 'vm_account_id': owner_id}) |
| 1278 | + project_id = zlib.crc32(app_id.encode("utf-8")) |
| 1279 | + labels.update({'vm_account_id': account_id, 'vm_project_id': project_id}) |
1315 | 1280 | return "%s{%s} %s\n" % (name, ",".join([f'{k}="{v}"' for k, v in labels.items()]), value) |
1316 | 1281 |
|
1317 | 1282 | async def get(self, request, node, metrics=None): |
|
0 commit comments