2018-04-02 07:54:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import os
|
2023-09-22 07:06:58 +00:00
|
|
|
import uuid
|
2022-02-17 12:13:31 +00:00
|
|
|
|
2018-04-02 07:54:49 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
2023-09-22 07:06:58 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2018-04-02 07:54:49 +00:00
|
|
|
|
2022-10-24 12:14:18 +00:00
|
|
|
from ops.celery import app
|
|
|
|
|
2018-04-02 07:54:49 +00:00
|
|
|
|
|
|
|
class CeleryTask(models.Model):
|
2022-10-24 12:14:18 +00:00
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
2022-11-16 07:04:46 +00:00
|
|
|
name = models.CharField(max_length=1024, verbose_name=_('Name'))
|
2023-01-16 11:02:09 +00:00
|
|
|
date_last_publish = models.DateTimeField(null=True, verbose_name=_("Date last publish"))
|
2022-10-24 12:14:18 +00:00
|
|
|
|
2024-01-18 07:28:13 +00:00
|
|
|
__summary = None
|
|
|
|
__state = None
|
|
|
|
|
2022-10-24 12:14:18 +00:00
|
|
|
@property
|
2022-10-27 11:23:15 +00:00
|
|
|
def meta(self):
|
2022-10-24 12:14:18 +00:00
|
|
|
task = app.tasks.get(self.name, None)
|
2022-10-27 11:23:15 +00:00
|
|
|
return {
|
2022-11-16 07:04:46 +00:00
|
|
|
"comment": getattr(task, 'verbose_name', None),
|
2022-10-27 11:23:15 +00:00
|
|
|
"queue": getattr(task, 'queue', 'default')
|
|
|
|
}
|
2022-11-01 03:52:51 +00:00
|
|
|
|
2022-11-16 07:04:46 +00:00
|
|
|
@property
|
|
|
|
def summary(self):
|
2024-01-18 07:28:13 +00:00
|
|
|
if self.__summary is not None:
|
|
|
|
return self.__summary
|
2022-11-16 07:04:46 +00:00
|
|
|
executions = CeleryTaskExecution.objects.filter(name=self.name)
|
|
|
|
total = executions.count()
|
|
|
|
success = executions.filter(state='SUCCESS').count()
|
|
|
|
return {'total': total, 'success': success}
|
|
|
|
|
2024-01-18 07:28:13 +00:00
|
|
|
@summary.setter
|
|
|
|
def summary(self, value):
|
|
|
|
self.__summary = value
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def compute_state_color(states: list, default_count=5):
|
|
|
|
color = 'green'
|
|
|
|
states = states[:default_count]
|
|
|
|
if not states:
|
|
|
|
return color
|
|
|
|
if states[0] == 'FAILURE':
|
|
|
|
color = 'red'
|
|
|
|
elif 'FAILURE' in states:
|
|
|
|
color = 'yellow'
|
|
|
|
return color
|
|
|
|
|
2022-10-27 11:23:15 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2024-01-18 07:28:13 +00:00
|
|
|
if self.__state is not None:
|
|
|
|
return self.__state
|
|
|
|
last_five_executions = CeleryTaskExecution.objects.filter(
|
|
|
|
name=self.name
|
|
|
|
).order_by('-date_published').values('state')[:5]
|
|
|
|
states = [i['state'] for i in last_five_executions]
|
|
|
|
color = self.compute_state_color(states)
|
|
|
|
return color
|
2022-10-27 11:23:15 +00:00
|
|
|
|
2024-01-18 07:28:13 +00:00
|
|
|
@state.setter
|
|
|
|
def state(self, value):
|
|
|
|
self.__state = value
|
2022-10-24 12:14:18 +00:00
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
class Meta:
|
2022-12-23 07:49:32 +00:00
|
|
|
verbose_name = _("Celery Task")
|
2022-11-15 08:29:40 +00:00
|
|
|
ordering = ('name',)
|
2022-12-27 06:48:00 +00:00
|
|
|
permissions = [
|
|
|
|
('view_taskmonitor', _('Can view task monitor'))
|
|
|
|
]
|
2022-11-15 08:29:40 +00:00
|
|
|
|
2022-10-24 12:14:18 +00:00
|
|
|
|
|
|
|
class CeleryTaskExecution(models.Model):
|
2018-04-02 07:54:49 +00:00
|
|
|
LOG_DIR = os.path.join(settings.PROJECT_DIR, 'data', 'celery')
|
|
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
2023-01-16 11:02:09 +00:00
|
|
|
name = models.CharField(max_length=1024, verbose_name=_('Name'))
|
2022-09-29 12:44:45 +00:00
|
|
|
args = models.JSONField(verbose_name=_("Args"))
|
|
|
|
kwargs = models.JSONField(verbose_name=_("Kwargs"))
|
|
|
|
state = models.CharField(max_length=16, verbose_name=_("State"))
|
|
|
|
is_finished = models.BooleanField(default=False, verbose_name=_("Finished"))
|
2024-03-18 10:53:22 +00:00
|
|
|
creator = models.ForeignKey('users.User', on_delete=models.SET_NULL, default=None, null=True,
|
|
|
|
verbose_name=_('Creator'), db_constraint=False)
|
2022-11-16 07:04:46 +00:00
|
|
|
date_published = models.DateTimeField(auto_now_add=True, verbose_name=_('Date published'))
|
|
|
|
date_start = models.DateTimeField(null=True, verbose_name=_('Date start'))
|
|
|
|
date_finished = models.DateTimeField(null=True, verbose_name=_('Date finished'))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def time_cost(self):
|
|
|
|
if self.date_finished and self.date_start:
|
|
|
|
return (self.date_finished - self.date_start).total_seconds()
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def timedelta(self):
|
|
|
|
if self.date_start and self.date_finished:
|
|
|
|
return self.date_finished - self.date_start
|
|
|
|
return None
|
2018-04-02 07:54:49 +00:00
|
|
|
|
2022-12-02 09:18:11 +00:00
|
|
|
@property
|
|
|
|
def is_success(self):
|
|
|
|
return self.state == 'SUCCESS'
|
|
|
|
|
2018-04-02 07:54:49 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "{}: {}".format(self.name, self.id)
|
2022-12-23 07:49:32 +00:00
|
|
|
|
|
|
|
class Meta:
|
2023-09-22 07:06:58 +00:00
|
|
|
ordering = ['-date_start']
|
2022-12-23 07:49:32 +00:00
|
|
|
verbose_name = _("Celery Task Execution")
|