Skip to content

Commit

Permalink
Merge pull request teamhephy#36 from jianxiaoguo/dev
Browse files Browse the repository at this point in the history
chore(influxdb): modify influxdb code
  • Loading branch information
duanhongyi authored Jan 27, 2021
2 parents 902905f + f4f22a2 commit 53fd658
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ language: go
services:
- docker
script:
- make test
- make docker-build-test upload-coverage
- DEV_REGISTRY="docker.io" make test
- DEV_REGISTRY="docker.io" make docker-build-test upload-coverage
notifications:
slack:
rooms:
Expand Down
2 changes: 1 addition & 1 deletion charts/controller/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ env:
name: database-creds
key: password
- name: DRYCC_DATABASE_URL
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSPORT)@$(DRYCC_DATABASE_SERVICE_HOST):$(DRYCC_DATABASE_SERVICE_PORT)/$(DRYCC_DATABASE_USER)"
value: "postgres://$(DRYCC_DATABASE_USER):$(DRYCC_DATABASE_PASSWORD)@$(DRYCC_DATABASE_SERVICE_HOST):$(DRYCC_DATABASE_SERVICE_PORT)/$(DRYCC_DATABASE_USER)"
{{- end }}
- name: WORKFLOW_NAMESPACE
valueFrom:
Expand Down
20 changes: 14 additions & 6 deletions rootfs/api/influxdb.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import threading
import logging
from typing import Iterator
from contextlib import closing
from django.conf import settings
from influxdb_client import InfluxDBClient
from influxdb_client.client.flux_table import FluxRecord
from influxdb_client.rest import ApiException

local = threading.local()
logger = logging.getLogger(__name__)


def _get_influxdb_client() -> InfluxDBClient:
if not hasattr(local, "influxdb_client"):
local.influxdb_client = InfluxDBClient(
url=settings.INFLUXDB_URL,
token=settings.INFLUXDB_TOKEN,
org=settings.INFLUXDB_ORG
url=settings.DRYCC_INFLUXDB_URL,
token=settings.DRYCC_INFLUXDB_TOKEN,
org=settings.DRYCC_INFLUXDB_ORG
)
return local.influxdb_client


def _query_stream(flux_script: str) -> Iterator[FluxRecord]:
with closing(_get_influxdb_client()) as client:
with closing(client.query_api()) as query_api:
with closing(query_api.query_stream(flux_script)) as records:
yield from records
try:
query_api = client.query_api()
records = query_api.query_stream(flux_script)
except ApiException as e:
logger.exception(e)
yield from []
else:
yield from records


def query_container_count(
Expand Down
10 changes: 10 additions & 0 deletions rootfs/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,13 @@ def update(self, instance, validated_data):
instance.attach_update()
instance.save()
return instance


class MetricSerializer(serializers.Serializer):
import time
now = int(time.time())
default_start = (now - now % 3600) - 3600
default_stop = now - now % 3600
start = serializers.IntegerField(min_value=946656000, max_value=4102416000, required=False, default=default_start) # noqa
stop = serializers.IntegerField(min_value=946656000, max_value=4102416000, required=False, default=default_stop) # noqa
every = serializers.CharField(max_length=50, required=False, default='5m') # noqa
2 changes: 2 additions & 0 deletions rootfs/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,6 @@
views.UserView.as_view({'patch': 'enable'})),
url(r'^users/(?P<username>[\w.@+-]+)/disable/?$',
views.UserView.as_view({'patch': 'disable'})),
url(r'^apps/(?P<id>{})/metrics/(?P<container_type>[a-z0-9]+(\-[a-z0-9]+)*)?$'.format(settings.APP_URL_REGEX), # noqa
views.MetricView.as_view({'get': 'status'})),
]
27 changes: 18 additions & 9 deletions rootfs/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ class MetricView(BaseDryccViewSet):
"""Getting monitoring indicators from influxdb"""

def _get_app(self):
app = get_object_or_404(models.App, id=self.kwargs['app_id'])
app = get_object_or_404(models.App, id=self.kwargs['id'])
self.check_object_permissions(self.request, app)
return app

Expand Down Expand Up @@ -976,13 +976,22 @@ def status(self, request, **kwargs):
],
}
"""
app_id, container_type = self._get_app().pk, kwargs['container_type']
start, stop, every = kwargs['start'], kwargs['stop'], kwargs["every"]
return {
app_id, container_type = self._get_app().id, kwargs['container_type']

data = serializers.MetricSerializer(data=self.request.query_params)
if not data.is_valid():
return Response(data.errors, status=422)
start, stop, every = data.validated_data['start'], data.validated_data[
'stop'], data.validated_data["every"]
return Response({
"app_id": app_id,
"container_type": container_type,
"container_count": self._get_container_count(app_id, container_type, start, stop),
"cpu_usage_list": self._get_cpus(app_id, container_type, start, stop, every),
"memory": self._get_memory(app_id, container_type, start, stop, every),
"networks": self._get_networks(app_id, container_type, start, stop, every)
}
"container_count": self._get_container_count(
app_id, container_type, start, stop),
"cpu_usage_list": self._get_cpus(
app_id, container_type, start, stop, every),
"memory": self._get_memory(
app_id, container_type, start, stop, every),
"networks": self._get_networks(
app_id, container_type, start, stop, every)
})

0 comments on commit 53fd658

Please sign in to comment.