Skip to content
Snippets Groups Projects
scheduling.py 7.34 KiB
Newer Older
"""
This file contains the database models
"""

from django.db.models import ForeignKey, CharField, DateTimeField, BooleanField, IntegerField, BigIntegerField, \
    ManyToManyField, CASCADE, SET_NULL, PROTECT
from django.contrib.postgres.fields import ArrayField, JSONField
from .specification import AbstractChoice, BasicCommon, Template, NamedCommon # , WorkRequestBlueprint
from rest_framework.serializers import HyperlinkedRelatedField
    role = ForeignKey('RoleChoice', null=False, on_delete=PROTECT)
    datatype = ForeignKey('DatatypeChoice', null=False, on_delete=PROTECT)
    dataformats = ManyToManyField('DataformatChoice', blank=True)
    output_of = ForeignKey('SubtaskTemplate', related_name='inputs', blank=True, on_delete=PROTECT)
    input_of = ForeignKey('SubtaskTemplate', related_name='outputs', blank=True, on_delete=PROTECT)
class SubtaskStateChoice(AbstractChoice):
    """Defines the model and predefined list of possible SubtaskStatusChoice's for Subtask.
    The items in the Choices class below are automagically populated into the database via a data migration."""
        SCHEDULING = "scheduling"
        SCHEDULED = "scheduled"
        QUEUEING = "queueing"
        QUEUED = "queued"
        STARTING = "starting"
        STARTED = "started"
        FINISHING = "finishing"
        FINISHED = "finished"
        CANCELLING = "cancelling"
        CANCELLED = "cancelled"
        ERROR = "error"


class SubtaskTypeChoice(AbstractChoice):
    """Defines the model and predefined list of possible SubtaskTypeChoice's for Subtask.
    The items in the Choices class below are automagically populated into the database via a data migration."""
    class Choices(Enum):
        OBSERVATION = "observation"
        PIPELINE = "pipeline"
        COPY = "copy"
        INSPECTION = "inspection"
        DELETION = "deletion"


class StationTypeChoice(AbstractChoice):
        """Defines the model and predefined list of possible StationTypeChoice's for AntennaSet.
        The items in the Choices class below are automagically populated into the database via a data migration."""

        class Choices(Enum):
            CORE = "core"
            REMOTE = "remote"
            INTERNATIONAL = "international"


class Algorithm(AbstractChoice):
    """Defines the model and predefined list of possible Algorithm's for DataproductHash.
    The items in the Choices class below are automagically populated into the database via a data migration."""

    class Choices(Enum):
        MD5 = 'md5'
        AES256 = 'aes256'


class ScheduleMethod(AbstractChoice):
    """Defines the model and predefined list of possible Algorithm's for DataproductHash.
    The items in the Choices class below are automagically populated into the database via a data migration."""

    class Choices(Enum):
        MANUAL = 'manual'
        BATCH = 'batch'
        DYNAMIC = 'dynamic'

    queue = BooleanField(default=False)
    realtime = BooleanField(default=False)

class DefaultSubtaskTemplate(BasicCommon):
    name = CharField(max_length=30, unique=True)
    template = ForeignKey('SubtaskTemplate', on_delete=PROTECT)
class DataproductSpecificationsTemplate(Template):
class DefaultDataproductSpecificationsTemplate(BasicCommon):
    name = CharField(max_length=30, unique=True)
    template = ForeignKey('DataproductSpecificationsTemplate', on_delete=PROTECT)
class SubtaskInputSelectionTemplate(Template):
    pass

# todo: so we need to specify a default?

# todo: do we need to specify a default?

class Subtask(BasicCommon):
    type = ForeignKey('SubtaskTypeChoice', null=False, on_delete=PROTECT)
    start_time = DateTimeField()
    stop_time = DateTimeField()
    state = ForeignKey('SubtaskStateChoice', null=False, on_delete=PROTECT, related_name='task_states')
    specifications_doc = JSONField()
    work_request_blueprint = ForeignKey('WorkRequestBlueprint', null=True, on_delete=SET_NULL)
    specifications_template = ForeignKey('SubtaskTemplate', null=False, on_delete=PROTECT)
    do_cancel = DateTimeField()
    priority = IntegerField()
    schedule_method = ForeignKey('ScheduleMethod', null=False, on_delete=PROTECT)
    cluster = ForeignKey('Cluster', null=False, on_delete=PROTECT)
    scheduler_input_doc = JSONField()
    # resource_claim = ForeignKey("ResourceClaim", null=False, on_delete=PROTECT) # todo <-- how is this external reference supposed to work?
class SubtaskInput(BasicCommon):
    subtask = ForeignKey('Subtask', null=False, on_delete=CASCADE)
    task_relation_blueprint = ForeignKey('TaskRelationBlueprint', null=True, on_delete=SET_NULL)
    connector = ForeignKey('SubtaskConnector', null=True, on_delete=SET_NULL)
    producer = ForeignKey('SubtaskOutput', on_delete=PROTECT)
    dataproducts = ManyToManyField('Dataproduct')
    selection_doc = JSONField()
    selection_template = ForeignKey('SubtaskInputSelectionTemplate', on_delete=PROTECT)


class SubtaskOutput(BasicCommon):
    subtask = ForeignKey('Subtask', null=False, on_delete=CASCADE)
    connector = ForeignKey('SubtaskConnector', null=True, on_delete=SET_NULL)


class Dataproduct(BasicCommon):
    filename = CharField(max_length=128)
    directory = CharField(max_length=1024)
    dataformat = ForeignKey('DataformatChoice', null=False, on_delete=PROTECT)
    deleted_since = DateTimeField(null=True)
    pinned_since = DateTimeField(null=True)
    specifications_template = ForeignKey('DataproductSpecificationsTemplate', null=False, on_delete=CASCADE)
    producer = ForeignKey('SubtaskOutput', on_delete=PROTECT)
    do_cancel = DateTimeField() # BooleanField() <- after consulting JDM, we opt for datetimefield for consistency
    expected_size = BigIntegerField()
    size = BigIntegerField()
    feedback_doc = JSONField()
    feedback_template = ForeignKey('DataproductFeedbackTemplate', on_delete=PROTECT)
    station_type = ForeignKey('StationTypeChoice', null=False, on_delete=PROTECT)
    rcus = ArrayField(IntegerField(), size=128, blank=False)
    inputs = ArrayField(CharField(max_length=30), size=128, blank=True)


class DataproductTransform(BasicCommon):
    input = ForeignKey('Dataproduct', related_name='inputs', on_delete=PROTECT)
    output = ForeignKey('Dataproduct',  related_name='outputs', on_delete=PROTECT)
    identity = BooleanField()


class Filesystem(NamedCommon):
    capacity = BigIntegerField()
    cluster = ForeignKey('Cluster', on_delete=PROTECT)


class Cluster(NamedCommon):
    location = CharField(max_length=128)


class DataproductArchiveInfo(BasicCommon):
    dataproduct = ForeignKey('Dataproduct', on_delete=PROTECT)
    storage_ticket = CharField(max_length=128)
    public_since = DateTimeField()
    corrupted_since = DateTimeField()


class DataproductHash(BasicCommon):
    dataproduct = ForeignKey('Dataproduct', on_delete=PROTECT)
    algorithm = ForeignKey('Algorithm', null=False, on_delete=PROTECT)
    hash = CharField(max_length=128)


class TaskRelationBlueprint(BasicCommon):
    # dummy, this will eventually come from the renaming of the WorkRelationBlueprint in LEI-86...
    pass