From a8cce62b86263ecbc7ee1f6c05fe3da641dd9bfe Mon Sep 17 00:00:00 2001 From: Alain Klotz Date: Thu, 31 Aug 2023 18:02:28 +0200 Subject: [PATCH] Json bdd and scheduler --- src/core/pyros_django/scheduling/A_Scheduler.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/core/pyros_django/scheduling/models.py | 28 ++++++++++++++-------------- 2 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/core/pyros_django/scheduling/A_Scheduler.py b/src/core/pyros_django/scheduling/A_Scheduler.py index fff434c..d85e872 100755 --- a/src/core/pyros_django/scheduling/A_Scheduler.py +++ b/src/core/pyros_django/scheduling/A_Scheduler.py @@ -39,6 +39,7 @@ for short_path in short_paths: from src.core.pyros_django.majordome.agent.Agent import Agent, build_agent, log, parse_args from seq_submit.models import Sequence from user_mgmt.models import Period, ScientificProgram, SP_Period +from scheduling.models import PredictiveSchedule, EffectiveSchedule # = Specials import glob import shutil @@ -85,7 +86,7 @@ class A_Scheduler(Agent): _TEST_COMMANDS_LIST = [ # Format : ("self cmd_name cmd_args", timeout, "expected_result", expected_status), (True, "self do_create_seq_1 6", 200, '', Agent.CMD_STATUS.CMD_EXECUTED), - (True, "self do_stop asap", 500, "STOPPING", Agent.CMD_STATUS.CMD_EXECUTED), + #(True, "self do_stop asap", 500, "STOPPING", Agent.CMD_STATUS.CMD_EXECUTED), ] """ @@ -232,6 +233,35 @@ class A_Scheduler(Agent): schedule_order = np.zeros(86400, dtype=int) -1 schedule_jd = np.zeros(86400, dtype=float) schedule_scientific_programm_id = np.zeros(86400, dtype=int) -1 + # --- + # --- Get the numpy matrix in database (via Json) + # --- Get the effective schedule matrix + print("STEP 2000") + time.sleep(5) + input_matrix = EffectiveSchedule.objects.last().conv_numpy() + # input_matrix = EffectiveSchedule.objects.get(id=10).scheduler_matrix + print(f"STEP 2500 {type(input_matrix)=}") + print(f"STEP 2600 {input_matrix=}") + + print(f"STEP 3000 {input_matrix.shape=}") + time.sleep(10) + # --- Get the effective schedule arrays + schedule_eff_jd, schedule_eff_binary, schedule_eff_sequence_id, schedule_eff_scientific_programm_id, schedule_eff_order, schedule_eff_visibility = input_matrix + print(f"{schedule_eff_jd=}") + # --- Get the index in the night + night, index = self._fn.date2night("now", 86400) + print(f"{night=} {index=}") + schedule_sequence_id[0:index] = schedule_eff_sequence_id[0:index] + schedule_binary[0:index] = schedule_eff_binary[0:index] + schedule_visibility[0:index] = schedule_eff_visibility[0:index] + schedule_order[0:index] = schedule_eff_order[0:index] + schedule_jd[0:index] = schedule_eff_jd[0:index] + schedule_scientific_programm_id[0:index] = schedule_eff_scientific_programm_id[0:index] + try: + pass + except: + pass + print(f"{schedule_jd=}") # =================================================================== # --- Loop over the sequences of the night to extract useful infos @@ -288,7 +318,10 @@ class A_Scheduler(Agent): else: sequence_info['error'] = f"File {ephfile} not exists" sequence_infos.append(sequence_info) - schedule_jd = eph_info['jd'] + try: + schedule_jd = eph_info['jd'] + except: + pass # =================================================================== # --- Get informations of priority and quota from scientific programs @@ -456,8 +489,21 @@ class A_Scheduler(Agent): self.dprint("\n" + "="*70 + "\n=== Save the schedule\n" + "="*70 + "\n") self.dprint("Order ID_seq K_start ID_sp Priority Duration Status\n") self.dprint(f"{seqs=}") + # --- Prepare the output matrix + ouput_matrix = np.array([schedule_jd, schedule_binary, schedule_sequence_id, schedule_scientific_programm_id, schedule_order, schedule_visibility]) + # --- Save the numpy matrix in ASCII fpathname = os.path.join(rootdir, subdir, "scheduler_schedule.txt") - np.savetxt(fpathname, np.array([schedule_jd, schedule_binary, schedule_sequence_id, schedule_scientific_programm_id, schedule_order, schedule_visibility]).T) + np.savetxt(fpathname, ouput_matrix.T) + # --- Save the numpy matrix in database (via Json) + v = PredictiveSchedule() + v.scheduler_matrix = ouput_matrix + v.save() + # --- Save the numpy matrix in database (via Json) + v = EffectiveSchedule() + v.scheduler_matrix = ouput_matrix + v.save() + + print(f"{schedule_jd=}") # --- Update the running state self._routine_running = self.RUNNING_NOTHING print(f"_compute_schedule_1 finished in {time.time() - t0:.2f} seconds") diff --git a/src/core/pyros_django/scheduling/models.py b/src/core/pyros_django/scheduling/models.py index 7c8ddd3..10b3350 100644 --- a/src/core/pyros_django/scheduling/models.py +++ b/src/core/pyros_django/scheduling/models.py @@ -7,6 +7,7 @@ import numpy as np import json from json import JSONEncoder import numpy +import ast class NumpyArrayEncoder(JSONEncoder): def default(self, obj): @@ -15,40 +16,39 @@ class NumpyArrayEncoder(JSONEncoder): return JSONEncoder.default(self, obj) + class EffectiveSchedule(models.Model): scheduler_matrix = JSONField(blank=True, null=True) - + + def save(self, *args, **kwargs): # Transform numpy matrix to JSON - scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder) + scheduler_matrix_as_json = json.dumps({"array":self.scheduler_matrix}, cls=NumpyArrayEncoder) self.scheduler_matrix = scheduler_matrix_as_json super(EffectiveSchedule, self).save(*args, **kwargs) - def get(self, *args, **kwargs): + def conv_numpy(self): # Transform JSON to numpy matrix matrix_as_json = json.loads(self.scheduler_matrix) - - self.scheduler_matrix = numpy.asarray(matrix_as_json["array"]) - return super().get(**kwargs) - + + return numpy.array(matrix_as_json) + class PredictiveSchedule(models.Model): scheduler_matrix = JSONField(blank=True, null=True) - + def save(self, *args, **kwargs): # Transform numpy matrix to JSON scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder) self.scheduler_matrix = scheduler_matrix_as_json super(PredictiveSchedule, self).save(*args, **kwargs) - def get(self, *args, **kwargs): + def conv_numpy(self, *args, **kwargs): # Transform JSON to numpy matrix matrix_as_json = json.loads(self.scheduler_matrix) - - self.scheduler_matrix = numpy.asarray(matrix_as_json["array"]) - return super().get(**kwargs) - - + + return numpy.array(matrix_as_json) + class SchedulerHistory(EffectiveSchedule): night_datetime = models.DateTimeField(blank=True, null=True) \ No newline at end of file -- libgit2 0.21.2