id_launch in monitordata

This commit is contained in:
Jorge Pastor 2024-02-03 22:43:27 +01:00
parent dcd525c79e
commit a717969926
4 changed files with 11 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import logging
import threading import threading
import traceback import traceback
from collections import OrderedDict from collections import OrderedDict
import random
from django.core.mail import mail_admins from django.core.mail import mail_admins
@ -111,6 +112,7 @@ def execute(scripts, serialize=False, run_async=None):
executions = [] executions = []
threads_to_join = [] threads_to_join = []
logs = [] logs = []
id_launch = random.randint(0, 2147483646)
for key, value in scripts.items(): for key, value in scripts.items():
route, __, async_action = key route, __, async_action = key
backend, operations = value backend, operations = value
@ -126,6 +128,7 @@ def execute(scripts, serialize=False, run_async=None):
with db.clone(model=BackendLog) as handle: with db.clone(model=BackendLog) as handle:
log = backend.create_log(*args, using=handle.target) log = backend.create_log(*args, using=handle.target)
log._state.db = handle.origin log._state.db = handle.origin
log.id_launch = id_launch
kwargs['log'] = log kwargs['log'] = log
task = keep_log(backend.execute, log, operations) task = keep_log(backend.execute, log, operations)
logger.debug('%s is going to be executed on %s.' % (backend, route.host)) logger.debug('%s is going to be executed on %s.' % (backend, route.host))

View File

@ -33,10 +33,11 @@ class Last(Aggregation):
def filter(self, dataset, date=None): def filter(self, dataset, date=None):
# dataset = dataset.order_by('object_id', '-id').distinct('monitor') lastdataset = dataset.order_by('-id').first()
now = timezone.now() dataset = dataset.filter( launch_id=lastdataset.launch_id)
epoch = now - datetime.timedelta(minutes=2) # now = timezone.now()
dataset = dataset.filter( created_at__range=(epoch, now )) # epoch = now - datetime.timedelta(minutes=2)
# dataset = dataset.filter( created_at__range=(epoch, now ))
if date is not None: if date is not None:
dataset = dataset.filter(created_at__lte=date) dataset = dataset.filter(created_at__lte=date)

View File

@ -72,6 +72,7 @@ class ServiceMonitor(ServiceBackend):
from .models import MonitorData from .models import MonitorData
name = self.get_name() name = self.get_name()
app_label, model_name = self.model.split('.') app_label, model_name = self.model.split('.')
id_launch = log.id_launch
ct = ContentType.objects.get_by_natural_key(app_label, model_name.lower()) ct = ContentType.objects.get_by_natural_key(app_label, model_name.lower())
for line in log.stdout.splitlines(): for line in log.stdout.splitlines():
line = line.strip() line = line.strip()
@ -83,7 +84,7 @@ class ServiceMonitor(ServiceBackend):
content_object = ct.get_object_for_this_type(pk=object_id) content_object = ct.get_object_for_this_type(pk=object_id)
MonitorData.objects.create( MonitorData.objects.create(
monitor=name, object_id=object_id, content_type=ct, value=value, state=state, 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): def execute(self, *args, **kwargs):

View File

@ -280,6 +280,7 @@ class MonitorData(models.Model):
content_object = GenericForeignKey() content_object = GenericForeignKey()
objects = MonitorDataQuerySet.as_manager() objects = MonitorDataQuerySet.as_manager()
launch_id = models.PositiveIntegerField(_("launch id"), blank=True, null=True)
class Meta: class Meta:
get_latest_by = 'id' get_latest_by = 'id'