Skip to content
Snippets Groups Projects
models.py 4.03 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(max_length=30, blank=True, null=True)
    repository = models.CharField(max_length=100, blank=True, null=True)
    commit_id = models.CharField(max_length=30, blank=True, null=True)
    path = models.CharField(max_length=100, blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed

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


class Task(models.Model):
    taskID = models.CharField(db_index=True, max_length=30, blank=True, null=True)
    task_type = models.CharField(max_length=20, default="task")

    # note: the apparent naming reversal is intentional. Predecessors are somebody elses successors.
Nico Vermaas's avatar
Nico Vermaas committed
    # todo: change to Integer... but then the specification service has to be changed to using a dict
    # to properly translate a python 'None' string to a Null value.
Nico Vermaas's avatar
Nico Vermaas committed
    new_predecessor_id = models.CharField(max_length=12, blank=True, null=True)
    predecessor = models.ForeignKey('self', related_name='task_successors', on_delete=models.SET_NULL, null=True,blank=True)
Nico Vermaas's avatar
Nico Vermaas committed
    workflow = models.ForeignKey(Workflow, related_name='tasks', on_delete=models.SET_NULL, null=True, blank=True)
    project = models.CharField(max_length=100, blank=True, null=True, default="unknown")
    sas_id = models.CharField(max_length=30, blank=True, null=True)
    priority = models.IntegerField(default=0)
    purge_policy = models.CharField(max_length=5, default="no", blank=True, null=True)
Nico Vermaas's avatar
Nico Vermaas committed
    new_workflow_id = models.CharField(max_length=12, blank=True, null=True)
    new_workflow_uri = models.CharField(max_length=100, blank=True, null=True)
    stage_request_id = models.IntegerField(default=0)

    inputs = models.JSONField(null=True, blank=True)
    outputs = models.JSONField(null=True, blank=True)

    skip = models.BooleanField(default=False)
Nico Vermaas's avatar
Nico Vermaas committed
    creationTime = models.DateTimeField(default=datetime.utcnow, blank=True)

Nico Vermaas's avatar
Nico Vermaas committed
    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

    def __str__(self):
        return str(self.id) + ' - ' + str(self.taskID) + ' - ' + 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):
    task = models.ForeignKey(Task, related_name='tasks', on_delete=models.CASCADE, null=False)
    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)
    step_name = models.CharField(max_length=30, blank=True, null=True)
    start_time = models.DateTimeField(blank=True, null=True)
    end_time = models.DateTimeField(blank=True, null=True)
    status = models.CharField(max_length=50,default="defined", blank=True, null=True)

    def __str__(self):
        return str(self.id)+' - ('+str(self.task__taskID)+')'

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

    @property
    def property_taskID(self):
        return self.task.taskID
Nico Vermaas's avatar
Nico Vermaas committed

    @property
    def property_task_type(self):
        return self.task.task_type
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)+')'