From a717969926bc22c7fddf33379ab9d34f0f76c9a3 Mon Sep 17 00:00:00 2001 From: jorgepastorr Date: Sat, 3 Feb 2024 22:43:27 +0100 Subject: [PATCH] id_launch in monitordata --- orchestra/contrib/orchestration/manager.py | 3 +++ orchestra/contrib/resources/aggregations.py | 9 +++++---- orchestra/contrib/resources/backends.py | 3 ++- orchestra/contrib/resources/models.py | 1 + 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/orchestra/contrib/orchestration/manager.py b/orchestra/contrib/orchestration/manager.py index 62572d04..ebe442de 100644 --- a/orchestra/contrib/orchestration/manager.py +++ b/orchestra/contrib/orchestration/manager.py @@ -2,6 +2,7 @@ import logging import threading import traceback from collections import OrderedDict +import random from django.core.mail import mail_admins @@ -111,6 +112,7 @@ def execute(scripts, serialize=False, run_async=None): executions = [] threads_to_join = [] logs = [] + id_launch = random.randint(0, 2147483646) for key, value in scripts.items(): route, __, async_action = key backend, operations = value @@ -126,6 +128,7 @@ def execute(scripts, serialize=False, run_async=None): with db.clone(model=BackendLog) as handle: log = backend.create_log(*args, using=handle.target) log._state.db = handle.origin + log.id_launch = id_launch kwargs['log'] = log task = keep_log(backend.execute, log, operations) logger.debug('%s is going to be executed on %s.' % (backend, route.host)) diff --git a/orchestra/contrib/resources/aggregations.py b/orchestra/contrib/resources/aggregations.py index 0806b410..8dd0ac0a 100644 --- a/orchestra/contrib/resources/aggregations.py +++ b/orchestra/contrib/resources/aggregations.py @@ -33,10 +33,11 @@ class Last(Aggregation): def filter(self, dataset, date=None): - # dataset = dataset.order_by('object_id', '-id').distinct('monitor') - now = timezone.now() - epoch = now - datetime.timedelta(minutes=2) - dataset = dataset.filter( created_at__range=(epoch, now )) + lastdataset = dataset.order_by('-id').first() + dataset = dataset.filter( launch_id=lastdataset.launch_id) + # now = timezone.now() + # epoch = now - datetime.timedelta(minutes=2) + # dataset = dataset.filter( created_at__range=(epoch, now )) if date is not None: dataset = dataset.filter(created_at__lte=date) diff --git a/orchestra/contrib/resources/backends.py b/orchestra/contrib/resources/backends.py index a997df5f..29ecf465 100644 --- a/orchestra/contrib/resources/backends.py +++ b/orchestra/contrib/resources/backends.py @@ -72,6 +72,7 @@ class ServiceMonitor(ServiceBackend): from .models import MonitorData name = self.get_name() app_label, model_name = self.model.split('.') + id_launch = log.id_launch ct = ContentType.objects.get_by_natural_key(app_label, model_name.lower()) for line in log.stdout.splitlines(): line = line.strip() @@ -83,7 +84,7 @@ class ServiceMonitor(ServiceBackend): content_object = ct.get_object_for_this_type(pk=object_id) MonitorData.objects.create( monitor=name, object_id=object_id, content_type=ct, value=value, state=state, - created_at=self.current_date, content_object_repr=str(content_object), + created_at=self.current_date, content_object_repr=str(content_object), launch_id=id_launch, ) def execute(self, *args, **kwargs): diff --git a/orchestra/contrib/resources/models.py b/orchestra/contrib/resources/models.py index f4491ab6..12e4ac51 100644 --- a/orchestra/contrib/resources/models.py +++ b/orchestra/contrib/resources/models.py @@ -280,6 +280,7 @@ class MonitorData(models.Model): content_object = GenericForeignKey() objects = MonitorDataQuerySet.as_manager() + launch_id = models.PositiveIntegerField(_("launch id"), blank=True, null=True) class Meta: get_latest_by = 'id'