Skip to content

Commit 3ab9575

Browse files
committed
chore(app): add project_id for vm
1 parent 35f45da commit 3ab9575

2 files changed

Lines changed: 12 additions & 47 deletions

File tree

rootfs/api/serializers/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ class AuthSerializer(serializers.Serializer):
148148
class UserSerializer(serializers.ModelSerializer):
149149
class Meta:
150150
model = User
151-
fields = ['email', 'username', 'password', 'first_name', 'last_name', 'is_superuser',
152-
'is_staff', 'groups', 'user_permissions', 'last_login', 'date_joined',
153-
'is_active']
151+
fields = ['id', 'email', 'username', 'password', 'first_name', 'last_name',
152+
'is_superuser', 'is_staff', 'groups', 'user_permissions', 'last_login',
153+
'date_joined', 'is_active']
154154
read_only_fields = ['id', 'is_superuser', 'is_staff', 'groups',
155155
'user_permissions', 'last_login', 'date_joined', 'is_active']
156156
extra_kwargs = {'password': {'write_only': True}}

rootfs/api/views.py

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import json
88
import ssl
99
import time
10+
import zlib
1011
import random
1112
import aiohttp
1213
import requests
1314
import warnings
1415

1516
from urllib.parse import urljoin
16-
from asgiref.sync import async_to_sync
1717
from django.db import transaction
1818
from django.db.models import Q
1919
from django.core.cache import cache
@@ -1226,42 +1226,6 @@ def _get_app(self):
12261226
self.check_object_permissions(self.request, app)
12271227
return app
12281228

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-
12651229
@method_decorator(cache_page(settings.DRYCC_METRICS_EXPIRY))
12661230
@method_decorator(vary_on_headers("Authorization"))
12671231
def metric(self, request, **kwargs):
@@ -1302,16 +1266,17 @@ async def sample(self, name, labels_str, value):
13021266
app_id = labels.get("namespace", None)
13031267
if not app_id:
13041268
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:
13071271
if app := await models.app.App.objects.filter(id=app_id).afirst():
1308-
owner_id = app.owner_id
1272+
account_id = app.owner_id
13091273
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:
13131277
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})
13151280
return "%s{%s} %s\n" % (name, ",".join([f'{k}="{v}"' for k, v in labels.items()]), value)
13161281

13171282
async def get(self, request, node, metrics=None):

0 commit comments

Comments
 (0)