From fafd18bd370776369f506f62bc86ffa09deb82ac Mon Sep 17 00:00:00 2001 From: pyros_astroguita Date: Fri, 20 Oct 2023 08:49:05 +0200 Subject: [PATCH] add quota related methods --- src/core/pyros_django/dashboard/templates/dashboard/observation_index.html | 18 ++++++++++++++++++ src/core/pyros_django/scp_mgmt/A_SCP_Manager.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++---------------------- src/core/pyros_django/scp_mgmt/models.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------- src/core/pyros_django/scp_mgmt/views.py | 3 ++- 4 files changed, 126 insertions(+), 57 deletions(-) diff --git a/src/core/pyros_django/dashboard/templates/dashboard/observation_index.html b/src/core/pyros_django/dashboard/templates/dashboard/observation_index.html index 91385ea..0eb50cb 100644 --- a/src/core/pyros_django/dashboard/templates/dashboard/observation_index.html +++ b/src/core/pyros_django/dashboard/templates/dashboard/observation_index.html @@ -102,6 +102,24 @@ +
  • + {% comment %} + Old version to be remastered : + +
    + +

    Proposal

    + {% load static %} html5 +
    + {% endcomment %} + +
  • {% endif %} {# TBD #} {% if USER_LEVEL|ifinlist:"Admin,Operator,Unit-PI,Management board member,Observer,TAC" %} diff --git a/src/core/pyros_django/scp_mgmt/A_SCP_Manager.py b/src/core/pyros_django/scp_mgmt/A_SCP_Manager.py index c31f45a..b8e33a3 100644 --- a/src/core/pyros_django/scp_mgmt/A_SCP_Manager.py +++ b/src/core/pyros_django/scp_mgmt/A_SCP_Manager.py @@ -19,6 +19,7 @@ for short_path in short_paths: from majordome.agent.Agent import Agent, build_agent from user_mgmt.models import PyrosUser, Institute, SP_Period, Period, SP_Period, SP_Period_Guest, SP_PeriodWorkflow import vendor.guitastro.src.guitastro as guitastro +from scp_mgmt.models import Quota # Django imports from django.shortcuts import reverse @@ -47,6 +48,9 @@ class A_SCP_Manager(Agent): # Format : “cmd_name” : (timeout, exec_mode, tooltip) "do_generate_ephem_moon_and_sun_for_period": (3, Agent.EXEC_MODE.SEQUENTIAL, 'generate ephem of moon & sun for a period'), + "do_set_quota_for_SP": (60, Agent.EXEC_MODE.SEQUENTIAL, 'set quota for scientific programs for id period'), + "do_set_quota_for_institutes": (60, Agent.EXEC_MODE.SEQUENTIAL, 'set quota for institutes for id period'), + "do_run_quota_workflow": (60, Agent.EXEC_MODE.SEQUENTIAL, 'set quota for a period'), } # new init with obsconfig @@ -229,8 +233,8 @@ class A_SCP_Manager(Agent): self.send_mail_to_observers_for_notification(next_sp_to_be_notified) SP_PeriodWorkflow.objects.create(period=self.period,action=SP_PeriodWorkflow.NOTIFICATION) self.update_sun_moon_ephems() - self.set_quota_for_institutes(self.period.id) - self.set_quota_for_sp(self.period.id) + self.do_set_quota_for_institutes(self.period.id) + self.do_set_quota_for_SP(self.period.id) def routine_process_body(self): print("routine automatic period workflow") @@ -245,27 +249,49 @@ class A_SCP_Manager(Agent): for n in range(int((end_date - start_date).days)): yield start_date + timedelta(n) - def set_quota_for_institutes(self, id_period): - for institute in Institute.objects.all(): - quota_f = institute.quota_f - # the lowest id of quota table for this period should be the first night of the period - period_quota = Period.objects.get(id=id_period).quota - institute_quota = period_quota.convert_to_quota(quota_f) - new_quota = Quota() - new_quota.set_attributes_and_save(institute_quota) - institute.quota = new_quota - institute.save() + def do_run_quota_workflow(self, id_period:int): + try: + self.update_sun_moon_ephems() + except Exception as e: + print(e) + self.do_set_quota_for_institutes(id_period) + self.do_set_quota_for_SP(id_period) + + def do_set_quota_for_institutes(self, id_period:int): + try: + for institute in Institute.objects.all(): + quota_f = institute.quota_f + # the lowest id of quota table for this period should be the first night of the period + period_quota = Period.objects.get(id=id_period).quota + institute_quota = period_quota.convert_to_quota(quota_f) + institute_quota["night_id"] = 0 + institute_quota["id_period"] = id_period + new_quota = Quota() + new_quota.set_attributes_and_save(institute_quota) + institute.quota = new_quota + institute.save() + except Exception as e: + print(e) + raise e - def set_quota_for_SP(self, id_period): - period = Period.objects.get(id=id_period) - for sp_period in SP_Period.objects.filter(period=period): - sp = sp_period.scientific_program - institute = sp.institute - institute_quota = institute.quota - new_quota = Quota() - quota_attributes = institute_quota.convert_to_quota(sp.quota_f) - new_quota.set_attributes_and_save(quota_attributes) - + def do_set_quota_for_SP(self, id_period:int): + try: + period = Period.objects.get(id=id_period) + for sp_period in SP_Period.objects.filter(period=period): + sp = sp_period.scientific_program + institute = sp.institute + institute_quota = institute.quota + new_quota = Quota() + quota_attributes = institute_quota.convert_to_quota(sp.quota_f) + quota_attributes["night_id"] = 1 + quota_attributes["id_period"] = id_period + new_quota.set_attributes_and_save(quota_attributes) + sp.quota = new_quota + sp.save() + + except Exception as e: + print(e) + raise e if __name__ == "__main__": diff --git a/src/core/pyros_django/scp_mgmt/models.py b/src/core/pyros_django/scp_mgmt/models.py index 5ad1bb9..6973886 100644 --- a/src/core/pyros_django/scp_mgmt/models.py +++ b/src/core/pyros_django/scp_mgmt/models.py @@ -4,90 +4,114 @@ from django.db import models class Quota(models.Model): id_period = models.BigIntegerField(blank=True, null=True) night_id = models.CharField(max_length=8, null=True, blank=True, db_index=True) + d_total = models.BigIntegerField(default=0, blank=True, null=True) d_totalq = models.BigIntegerField(default=0, blank=True, null=True) d_totalx = models.BigIntegerField(default=0, blank=True, null=True) + d_previous = models.BigIntegerField(default=0, blank=True, null=True) d_previousq = models.BigIntegerField(default=0, blank=True, null=True) d_previousx = models.BigIntegerField(default=0, blank=True, null=True) + d_current = models.BigIntegerField(default=0, blank=True, null=True) d_currentq = models.BigIntegerField(default=0, blank=True, null=True) d_currentx = models.BigIntegerField(default=0, blank=True, null=True) + d_passed = models.BigIntegerField(default=0, blank=True, null=True) d_passedq = models.BigIntegerField(default=0, blank=True, null=True) d_passedx = models.BigIntegerField(default=0, blank=True, null=True) + d_schedule = models.BigIntegerField(default=0, blank=True, null=True) d_scheduleq = models.BigIntegerField(default=0, blank=True, null=True) d_schedulex = models.BigIntegerField(default=0, blank=True, null=True) + d_next = models.BigIntegerField(default=0, blank=True, null=True) d_nextq = models.BigIntegerField(default=0, blank=True, null=True) d_nextx = models.BigIntegerField(default=0, blank=True, null=True) - @property - def d_total(self): - return self.d_totalq + self.d_totalx + # @property + # def d_total(self): + # return self.d_totalq + self.d_totalx - @property - def d_previous(self): - return self.d_previousq + self.d_previousx + # @property + # def d_previous(self): + # return self.d_previousq + self.d_previousx - @property - def d_current(self): - return self.d_currentq + self.d_currentx + # @property + # def d_current(self): + # return self.d_currentq + self.d_currentx - @property - def d_passed(self): - return self.d_passedq + self.d_passedx + # @property + # def d_passed(self): + # return self.d_passedq + self.d_passedx - @property - def d_schedule(self): - return self.d_scheduleq + self.d_schedulex + # @property + # def d_schedule(self): + # return self.d_scheduleq + self.d_schedulex - @property - def d_next(self): - return self.d_nextq + self.d_nextx + # @property + # def d_next(self): + # return self.d_nextq + self.d_nextx def set_attributes_and_save(self, quota_attributes:dict): - if quota_attributes.get("id_period") != None: self.id_period = quota_attributes["id_period"] if quota_attributes.get("night_id") != None: self.night_id = quota_attributes["night_id"] + if quota_attributes.get("d_totalq") != None: self.d_totalq = quota_attributes["d_totalq"] if quota_attributes.get("d_totalx") != None: self.d_totalx = quota_attributes["d_totalx"] + if quota_attributes.get("d_previousq") != None: self.d_previousq = quota_attributes["d_previousq"] if quota_attributes.get("d_previousx") != None: self.d_previousx = quota_attributes["d_previousx"] + if quota_attributes.get("d_currentq") != None: self.d_currentq = quota_attributes["d_currentq"] if quota_attributes.get("d_currentx") != None: self.d_currentx = quota_attributes["d_currentx"] + + if quota_attributes.get("d_passedq") != None: + self.d_passedq = quota_attributes["d_passedq"] + if quota_attributes.get("d_passedx") != None: + self.d_passedx = quota_attributes["d_passedx"] + + if quota_attributes.get("d_scheduleq") != None: self.d_scheduleq = quota_attributes["d_scheduleq"] if quota_attributes.get("d_schedulex") != None: - self.d_schedule = quota_attributes["d_schedulex"] + self.d_schedulex = quota_attributes["d_schedulex"] + + if quota_attributes.get("d_nextx") != None: + self.d_nextq = quota_attributes["d_nextx"] if quota_attributes.get("d_nextq") != None: self.d_nextq = quota_attributes["d_nextq"] - if quota_attributes.get("d_nextx") != None: - self.d_nextx = quota_attributes["d_nextx"] + + + if quota_attributes.get("d_total") != None: + self.d_total = quota_attributes["d_total"] + if quota_attributes.get("d_previous") != None: + self.d_previous = quota_attributes["d_previous"] + if quota_attributes.get("d_current") != None: + self.d_current = quota_attributes["d_current"] + if quota_attributes.get("d_passed") != None: + self.d_passed = quota_attributes["d_passed"] + if quota_attributes.get("d_schedule") != None: + self.d_schedule = quota_attributes["d_schedule"] + if quota_attributes.get("d_next") != None: + self.d_next = quota_attributes["d_next"] self.save() def convert_to_quota(self, quota_f): quota_institute = {} - quota_institute["d_totalq"] = self.d_totalq * quota_f - quota_institute["d_totalx"] = self.d_totalx * quota_f - quota_institute["d_previousq"] = self.d_previousq * quota_f - quota_institute["d_previousx"] = self.d_previousx * quota_f - quota_institute["d_currentq"] = self.d_currentq * quota_f - quota_institute["d_currentx"] = self.d_currentx * quota_f - quota_institute["d_passedq"] = self.d_passedq * quota_f - quota_institute["d_passedx"] = self.d_passedx * quota_f - quota_institute["d_scheduleq"] = self.d_scheduleq * quota_f - quota_institute["d_schedulex"] = self.d_schedulex * quota_f - quota_institute["d_nextq"] = self.d_nextq * quota_f - quota_institute["d_nextx"] = self.d_nextx * quota_f + quota_institute["d_total"] = self.d_total * quota_f + quota_institute["d_previous"] = self.d_previous * quota_f + quota_institute["d_current"] = self.d_current * quota_f + quota_institute["d_passed"] = self.d_passed * quota_f + quota_institute["d_schedule"] = self.d_schedule * quota_f + quota_institute["d_next"] = self.d_next * quota_f return quota_institute diff --git a/src/core/pyros_django/scp_mgmt/views.py b/src/core/pyros_django/scp_mgmt/views.py index 828ed5d..67667a8 100644 --- a/src/core/pyros_django/scp_mgmt/views.py +++ b/src/core/pyros_django/scp_mgmt/views.py @@ -815,7 +815,8 @@ def quota_sp(request): config = OBSConfig(os.environ["PATH_TO_OBSCONF_FILE"],os.environ["unit_name"]) current_night = config.fn.date2night("now") current_period = Period.objects.exploitation_period() - quota_current_night = Quota.objects.get(id_period=current_period.id, night_id=current_night) + # lowest id is period line + quota_current_night = Quota.objects.filter(id_period=current_period.id, night_id=current_night).order_by("id").first() return render(request, 'scp_mgmt/quota_sp.html', locals()) -- libgit2 0.21.2