11# Create your tasks here
22import time
33import uuid
4- import json
54import logging
6- import requests
75from datetime import timedelta
86from typing import List , Dict
97from django .core import signals
10- from django .conf import settings
118from django .utils .timezone import now
129from celery import shared_task
13- from requests_toolbelt import user_agent
10+
11+ from api import manager
1412from api .models .resource import Resource
15- from api import __version__ as drycc_version
1613logger = logging .getLogger (__name__ )
1714
1815
@@ -41,30 +38,11 @@ def retrieve_resource(resource):
4138
4239@shared_task
4340def measure_config (config : List [Dict [str , str ]]):
44- """
45- [
46- {
47- "app_id": "test",
48- "owner_id": "test",
49- "container_type": web,
50- "cpu": "1",
51- "memory": "2G",
52- "timestamp": 1609231998.9103732
53- }
54- ]
55- """
5641 task_id = uuid .uuid4 ().hex
5742 signals .request_started .send (sender = task_id )
5843 try :
59- requests .post (
60- url = "%s/measurements/config/" % settings .WORKFLOW_MANAGER_URL ,
61- headers = {
62- 'Content-Type' : 'application/json' ,
63- 'Authorization' : 'token %s' % settings .WORKFLOW_MANAGER_TOKEN ,
64- 'User-Agent' : user_agent ('Drycc Controller ' , drycc_version )
65- },
66- data = json .dumps (config )
67- )
44+ measurement = manager .Measurement ()
45+ measurement .post_config (config )
6846 except Exception as e :
6947 logger .exception ("write influxdb point fail: {}" .format (e ))
7048 finally :
@@ -73,29 +51,11 @@ def measure_config(config: List[Dict[str, str]]):
7351
7452@shared_task
7553def measure_volumes (volumes : List [Dict [str , str ]]):
76- """
77- [
78- {
79- "name": "disk",
80- "app_id": "test",
81- "owner_id": "test",
82- "size": "100G",
83- "timestamp": "1609231998.9103732"
84- }
85- ]
86- """
8754 task_id = uuid .uuid4 ().hex
8855 signals .request_started .send (sender = task_id )
8956 try :
90- requests .post (
91- url = "%s/measurements/volumes/" % settings .WORKFLOW_MANAGER_URL ,
92- headers = {
93- 'Content-Type' : 'application/json' ,
94- 'Authorization' : 'token %s' % settings .WORKFLOW_MANAGER_TOKEN ,
95- 'User-Agent' : user_agent ('Drycc Controller ' , drycc_version )
96- },
97- data = json .dumps (volumes )
98- )
57+ measurement = manager .Measurement ()
58+ measurement .post_volumes (volumes )
9959 except Exception as e :
10060 logger .exception ("write influxdb point fail: {}" .format (e ))
10161 finally :
@@ -104,30 +64,11 @@ def measure_volumes(volumes: List[Dict[str, str]]):
10464
10565@shared_task
10666def measure_networks (networks : List [Dict [str , str ]]):
107- """
108- [
109- {
110- "app_id": "test",
111- "owner_id": "test",
112- "pod_name": "django2test-web-xxxxxx",
113- "rx_bytes": "10000",
114- "tx_bytes": "200000",
115- "timestamp": "1609231998.9103732"
116- }
117- ]
118- """
11967 task_id = uuid .uuid4 ().hex
12068 signals .request_started .send (sender = task_id )
12169 try :
122- requests .post (
123- url = "%s/measurements/networks/" % settings .WORKFLOW_MANAGER_URL ,
124- headers = {
125- 'Content-Type' : 'application/json' ,
126- 'Authorization' : 'token %s' % settings .WORKFLOW_MANAGER_TOKEN ,
127- 'User-Agent' : user_agent ('Drycc Controller ' , drycc_version )
128- },
129- data = json .dumps (networks )
130- )
70+ measurement = manager .Measurement ()
71+ measurement .post_networks (networks )
13172 except Exception as e :
13273 logger .exception ("write influxdb point fail: {}" .format (e ))
13374 finally :
@@ -136,29 +77,11 @@ def measure_networks(networks: List[Dict[str, str]]):
13677
13778@shared_task
13879def measure_instances (instances : List [Dict [str , str ]]):
139- """
140- [
141- {
142- "app_id": "test",
143- "owner_id": "test",
144- "container_type": "web",
145- "container_count": 1,
146- "timestamp": "1609231998.9103732"
147- }
148- ]
149- """
15080 task_id = uuid .uuid4 ().hex
15181 signals .request_started .send (sender = task_id )
15282 try :
153- requests .post (
154- url = "%s/measurements/instances/" % settings .WORKFLOW_MANAGER_URL ,
155- headers = {
156- 'Content-Type' : 'application/json' ,
157- 'Authorization' : 'token %s' % settings .WORKFLOW_MANAGER_TOKEN ,
158- 'User-Agent' : user_agent ('Drycc Controller ' , drycc_version )
159- },
160- data = json .dumps (instances )
161- )
83+ measurement = manager .Measurement ()
84+ measurement .post_instances (instances )
16285 except Exception as e :
16386 logger .exception ("write influxdb point fail: {}" .format (e ))
16487 finally :
@@ -167,29 +90,11 @@ def measure_instances(instances: List[Dict[str, str]]):
16790
16891@shared_task
16992def measure_resources (resources : List [Dict [str , str ]]):
170- """
171- [
172- {
173- "name": "test1",
174- "app_id": "redis",
175- "owener_id": "test",
176- "plan": "redis:small",
177- "timestamp": "1609231998.9103732"
178- }
179- ]
180- """
18193 task_id = uuid .uuid4 ().hex
18294 signals .request_started .send (sender = task_id )
18395 try :
184- requests .post (
185- url = "%s/measurements/resources/" % settings .WORKFLOW_MANAGER_URL ,
186- headers = {
187- 'Content-Type' : 'application/json' ,
188- 'Authorization' : 'token %s' % settings .WORKFLOW_MANAGER_TOKEN ,
189- 'User-Agent' : user_agent ('Drycc Controller ' , drycc_version )
190- },
191- data = json .dumps (resources )
192- )
96+ measurement = manager .Measurement ()
97+ measurement .post_resources (resources )
19398 except Exception as e :
19499 logger .exception ("write influxdb point fail: {}" .format (e ))
195100 finally :
0 commit comments