Skip to content
Snippets Groups Projects
models.py 4.77 KiB
Newer Older
Nico Vermaas's avatar
Nico Vermaas committed
from django.db import models
from django.urls import reverse
from django.utils.timezone import datetime
from django.db.models import Sum

# constants
datetime_format_string = '%Y-%m-%dT%H:%M:%SZ'

class Workflow(models.Model):
    workflow_uri = models.CharField(unique=True, max_length=30, blank=True, null=True)
    repository = models.CharField(max_length=100, blank=True, null=True)
    commit_id = models.CharField(max_length=15, blank=True, null=True)
    path = models.CharField(max_length=100, blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    oi_size_fraction = models.FloatField(blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed

    def __str__(self):
Nico Vermaas's avatar
Nico Vermaas committed
        return str(self.id)
Nico Vermaas's avatar
Nico Vermaas committed


class Task(models.Model):
    task_type = models.CharField(max_length=20, default="task")
    filter = models.CharField(max_length=30, blank=True, null=True)
    new_status = models.CharField(max_length=50, default="defining", null=True)
    status = models.CharField(db_index=True, default="unknown", max_length=50,blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    resume = models.BooleanField(verbose_name="Resume", default=True)
Nico Vermaas's avatar
Nico Vermaas committed
    creationTime = models.DateTimeField(verbose_name="CreationTime",default=datetime.utcnow, blank=True)
    priority = models.IntegerField(default=100, null=True)
    purge_policy = models.CharField(max_length=5, default="no", blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    stage_request_id = models.IntegerField(null=True)
    # LOFAR properties
    project = models.CharField(max_length=100, blank=True, null=True, default="unknown")
Nico Vermaas's avatar
Nico Vermaas committed
    sas_id = models.CharField(verbose_name="SAS_ID",max_length=15, blank=True, null=True)
    inputs = models.JSONField(null=True, blank=True)
    outputs = models.JSONField(null=True, blank=True)
Nico Vermaas's avatar
Nico Vermaas committed
    metrics = models.JSONField(null=True, blank=True)
Nico Vermaas's avatar
Nico Vermaas committed
    size_to_process = models.PositiveBigIntegerField(default=0, null=True, blank=True)
    size_processed = models.PositiveBigIntegerField(default=0, null=True, blank=True)
    total_processing_time = models.IntegerField(default=0, null=True, blank=True)
    # relationships
    workflow = models.ForeignKey(Workflow, related_name='tasks', on_delete=models.SET_NULL, null=True, blank=True)
    predecessor = models.ForeignKey('self', related_name='successors', on_delete=models.SET_NULL, null=True, blank=True)
Nico Vermaas's avatar
Nico Vermaas committed

    def __str__(self):
        return str(self.id) + ' - ' + str(self.sas_id)
Nico Vermaas's avatar
Nico Vermaas committed

    # this translates a view-name (from urls.py) back to a url, to avoid hardcoded url's in the html templates
    # bad : <td><a href="/atdb/taaks/{{ task.id }}/" target="_blank">{{ task.taskID }} </a> </td>
    # good: <td><a href="{{ task.get_absolute_url }}" target="_blank">{{ task.taskID }} </a> </td>
    def get_absolute_url(self):
        return reverse('task-detail-view-api', kwargs={'pk': self.pk})

Nico Vermaas's avatar
Nico Vermaas committed

class LogEntry(models.Model):
    cpu_cycles = models.IntegerField(null=True,blank=True)
    wall_clock_time = models.IntegerField(null=True,blank=True)
    url_to_log_file = models.CharField(max_length=100, blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    service = models.CharField(max_length=30, blank=True, null=True)
    step_name = models.CharField(max_length=30, blank=True, null=True)
    timestamp = models.DateTimeField(blank=True, null=True)
    status = models.CharField(max_length=50,default="defined", blank=True, null=True)
    description = models.CharField(max_length=100, blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    size_processed = models.PositiveBigIntegerField(default=0, null=True, blank=True)
    # relationships
    task = models.ForeignKey(Task, related_name='log_entries', on_delete=models.CASCADE, null=False)

    def __str__(self):
        return str(self.id)+ ' - '+ str(self.task)+' - '+self.status
Nico Vermaas's avatar
Nico Vermaas committed
class Status(models.Model):
    name = models.CharField(max_length=50, default="unknown")
    timestamp = models.DateTimeField(default=datetime.utcnow, blank=True)
    task = models.ForeignKey(Task, related_name='status_history', on_delete=models.CASCADE, null=False)
Nico Vermaas's avatar
Nico Vermaas committed

    # the representation of the value in the REST API
    def __str__(self):
        formatedDate = self.timestamp.strftime(datetime_format_string)
        return str(self.name)+' ('+str(formatedDate)+')'

Nico Vermaas's avatar
Nico Vermaas committed

class Configuration(models.Model):
    filter = models.CharField(max_length=30, blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    key = models.CharField(max_length=50)
    value = models.CharField(max_length=255)

    # the representation of the value in the REST API
    def __str__(self):
Nico Vermaas's avatar
Nico Vermaas committed
        return str(self.key)


class Job(models.Model):
    type = models.CharField(db_index=True, max_length=20, default=None,null=True, blank=True)
    task_id = models.IntegerField(null=True, blank=True)
    job_id = models.IntegerField(null=True, blank=True)
    metadata = models.JSONField(null=True, blank=True)

    # the representation of the value in the REST API
    def __str__(self):
        return 'task_id:'+str(self.task_id)+', job_id:'+str(self.job_id)