X-Git-Url: http://mmka.chem.univ.gda.pl/gitweb/?a=blobdiff_plain;f=qcg%2Fmodels.py;h=316f56e4caa886b6aaf238d3d3dd77c951b51699;hb=9e9b697db82a9dbd7b3fa9f6d8aba80a1d606732;hp=8c2be2db61fe9c21985f03f3118b59e4d22d5e0b;hpb=063e6b5a3e9c75e43c93bbcf06424181dd4ad894;p=qcg-portal.git diff --git a/qcg/models.py b/qcg/models.py index 8c2be2d..316f56e 100644 --- a/qcg/models.py +++ b/qcg/models.py @@ -4,12 +4,12 @@ from django.core.urlresolvers import reverse from django.db import models from django.utils.timezone import now from pyqcg.service import Job as QcgJob, Task as QcgTask -from pyqcg.utils import JobStatus, TaskStatus, TaskType +from pyqcg.utils import JobStatus, TaskStatus, TaskType, AllocationType, EPRUtils, DescriptionType from qcg.utils import username_from_dn, get_attributes -__all__ = ['User', 'Job', 'Task', 'Allocation', 'NodeInfo'] +__all__ = ['User', 'Job', 'Task', 'Allocation', 'NodeInfo', 'JobTemplate'] class User(AbstractUser): @@ -20,6 +20,10 @@ class User(AbstractUser): return Task.objects.filter(job__owner=self) +DESC_TYPE_CHOICES = list(enumerate(field for field in dir(DescriptionType) if not field.startswith('__'))) +DESC_TYPE_CHOICES_REVERSED = {v: k for k, v in DESC_TYPE_CHOICES} + + class Job(models.Model): STATUS_CHOICES = list(enumerate(field for field in dir(JobStatus) if not field.startswith('__'))) STATUS_CHOICES_REVERSED = {v: k for k, v in STATUS_CHOICES} @@ -31,6 +35,8 @@ class Job(models.Model): status = models.IntegerField(u"Status", choices=STATUS_CHOICES) note = models.TextField(u"Notatka", blank=True, default='') description = models.TextField(u"Opis", blank=True, default='') + description_type = models.IntegerField(u"Typ opisu", choices=DESC_TYPE_CHOICES, blank=True, null=True) + qcg_description = models.TextField(u"Opis QCG", blank=True, default='') project = models.TextField(u"Projekt", blank=True, default='') submission_time = models.DateTimeField(u"Data wysłania") finish_time = models.DateTimeField(u"Data zakończenia", blank=True, null=True) @@ -55,35 +61,33 @@ class Job(models.Model): @property def qcg_job(self): if self._job is None: - self._job = QcgJob(self.epr) + self._job = QcgJob(EPRUtils.deserialize_epr(self.epr)) return self._job @staticmethod def qcg_map(qcg_job, user=None): - attrs = get_attributes(qcg_job, ('job_id', 'note', 'description', 'submission_time', 'finish_time', - 'project', 'purged')) + attrs = get_attributes(qcg_job, ('job_id', 'note', 'description', 'qcg_description', 'submission_time', + 'finish_time', 'project', 'purged')) - attrs['epr'] = str(qcg_job.epr) + attrs['epr'] = EPRUtils.serialize_epr(qcg_job.epr) attrs['status'] = Job.STATUS_CHOICES_REVERSED[qcg_job.status] + attrs['description_type'] = DESC_TYPE_CHOICES_REVERSED[qcg_job.description_type] attrs['proxy_lifetime'] = now() + qcg_job.proxy_lifetime username = username_from_dn(qcg_job.user_dn) if user is not None: if user.username != username: - raise ValueError('') + raise ValueError('Username does not match ({} vs. {})!'.format(repr(user.username), repr(username))) attrs['owner'] = user else: attrs['owner'] = User.objects.get(username=username) return attrs - @classmethod - def from_qcg(cls, qcg_job): - job = cls(**cls.qcg_map(qcg_job)) - job._job = qcg_job - - return job + @property + def terminated(self): + return self.get_status_display() in [JobStatus.FINISHED, JobStatus.FAILED, JobStatus.CANCELED] class Task(models.Model): @@ -103,6 +107,7 @@ class Task(models.Model): type = models.IntegerField(u"Typ", choices=TYPE_CHOICES) note = models.TextField(u"Notatka", blank=True, default='') description = models.TextField(u"Opis", blank=True, default='') + description_type = models.IntegerField(u"Typ opisu", choices=DESC_TYPE_CHOICES, blank=True, null=True) submission_time = models.DateTimeField(u"Data wysłania") start_time = models.DateTimeField(u"Data rozpoczęcia", blank=True, null=True) finish_time = models.DateTimeField(u"Data zakończenia", blank=True, null=True) @@ -128,35 +133,31 @@ class Task(models.Model): @property def qcg_task(self): - if self._qcg_task is None: - self._qcg_task = QcgTask(self.epr) + if self._task is None: + self._task = QcgTask(EPRUtils.deserialize_epr(self.epr)) - return self._qcg_task + return self._task @staticmethod - def qcg_map(qcg_task, jobs=None): + def qcg_map(qcg_task, job=None): attrs = get_attributes(qcg_task, ('task_id', 'status_description', 'note', 'description', 'submission_time', 'start_time', 'finish_time', 'reserved_time_slot', 'purged')) - attrs['epr'] = str(qcg_task.epr) + attrs['epr'] = EPRUtils.serialize_epr(qcg_task.epr) attrs['status'] = Task.STATUS_CHOICES_REVERSED[qcg_task.status] attrs['type'] = Task.TYPE_CHOICES_REVERSED[qcg_task.type] + attrs['description_type'] = DESC_TYPE_CHOICES_REVERSED[qcg_task.description_type] attrs['proxy_lifetime'] = now() + qcg_task.proxy_lifetime - if jobs is not None and qcg_task.job_id in jobs: - attrs['job'] = jobs[qcg_task.job_id] + if job is not None: + if qcg_task.job_id != job.job_id: + raise ValueError('Job id does not match ({} vs. {})!'.format(repr(qcg_task.job_id), repr(job.job_id))) + attrs['job'] = job else: attrs['job'] = Job.objects.get(job_id=qcg_task.job_id) return attrs - @classmethod - def from_qcg(cls, qcg_task): - task = cls(**cls.qcg_map(qcg_task)) - task._task = qcg_task - - return task - @property def reserved_time_slot(self): if self.reserved_time_start or self.reserved_time_finish: @@ -169,13 +170,21 @@ class Task(models.Model): @property def short_host_names(self): - return {alloc.host_name.split('.')[0] for alloc in self.allocations.all()} + return {alloc.host_name.split('.', 1)[0] for alloc in self.allocations.all()} + + @property + def terminated(self): + return self.get_status_display() in [TaskStatus.FINISHED, TaskStatus.FAILED, TaskStatus.CANCELED] class Allocation(models.Model): + STATUS_CHOICES = list(enumerate(field for field in dir(AllocationType) if not field.startswith('__'))) + STATUS_CHOICES_REVERSED = {v: k for k, v in STATUS_CHOICES} + task = models.ForeignKey(Task, verbose_name='Zadanie', related_name='allocations') host_name = models.CharField(u"Host", max_length=100) + status = models.IntegerField(u"Status", choices=STATUS_CHOICES) status_description = models.TextField(u"Opis statusu", blank=True, default='') processes_count = models.PositiveIntegerField(u"Liczba procesów") slots_count = models.PositiveIntegerField(u"Liczba rdzeni") @@ -190,6 +199,7 @@ class Allocation(models.Model): efficiency = models.IntegerField(u"Efektywność", blank=True, null=True) # ?? comment = models.TextField(u"Komentarz", blank=True, default='') memory_usage = models.PositiveIntegerField(u"Użycie pamięci", blank=True, null=True) + working_directory = models.CharField(u"Katalog roboczy", max_length=1024, blank=True, default='') class Meta: verbose_name = u"Alokacja" @@ -201,10 +211,15 @@ class Allocation(models.Model): @staticmethod def qcg_map(qcg_allocation): - return get_attributes(qcg_allocation, ('host_name', 'status_description', 'processes_count', 'slots_count', - 'processes_group_id', 'submission_time', 'estimated_start_time', - 'finish_time', 'local_submission_time', 'local_start_time', - 'local_finish_time', 'purged', 'efficiency', 'comment', 'memory_usage')) + attrs = get_attributes(qcg_allocation, ('host_name', 'status_description', 'processes_count', 'slots_count', + 'processes_group_id', 'submission_time', 'estimated_start_time', + 'finish_time', 'local_submission_time', 'local_start_time', + 'local_finish_time', 'purged', 'efficiency', 'comment', 'memory_usage', + 'working_directory')) + + attrs['status'] = Allocation.STATUS_CHOICES_REVERSED[qcg_allocation.status] + + return attrs class NodeInfo(models.Model): @@ -227,6 +242,23 @@ class NodeInfo(models.Model): @staticmethod def qcg_map(qcg_node): - return {'name': qcg_node.name, - # FIXME - 'count': qcg_node.slots_count.intValue() if qcg_node.slots_count is not None else None} + return {'name': qcg_node.name, 'count': qcg_node.slots_count} + + +class JobTemplate(models.Model): + name = models.CharField(u"Nazwa", max_length=100) + description = models.TextField(u"Opis") + + owner = models.ForeignKey(User, verbose_name=u"Właściciel", related_name='templates') + created = models.DateTimeField(u"Utworzono", auto_now_add=True) + updated = models.DateTimeField(u"Uaktualniono", auto_now=True) + + class Meta: + verbose_name = u"Szablon zadania" + verbose_name_plural = u"Szablony zadania" + + def __unicode__(self): + return u"{} ({})".format(self.name, self.owner) + + def get_absolute_url(self): + return reverse('template_submit', kwargs={'template_id': self.id})