From 56bac4c9a04156bcf03a5e76d92ad228a8c4a7d8 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Mon, 8 Jan 2024 10:22:38 +0000
Subject: [PATCH] Use request time instead of file mtime for cal_report_at

---
 webservice/job_monitor.py | 12 +++++++-----
 webservice/webservice.py  |  6 +++---
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/webservice/job_monitor.py b/webservice/job_monitor.py
index 1e9ff45e4..d30ed8230 100644
--- a/webservice/job_monitor.py
+++ b/webservice/job_monitor.py
@@ -234,7 +234,8 @@ class JobsMonitor:
         ).fetchall()]
         success = set(statuses) == {'COMPLETED'}
         r = self.job_db.execute(
-            "SELECT det_type, karabo_id, command, req_id, proposal, run, action, mymdc_id "
+            "SELECT det_type, karabo_id, command, "
+            "req_id, proposal, run, action, mymdc_id, timestamp "
             "FROM executions JOIN requests USING (req_id)"
             "WHERE exec_id = ?",
             (exec_id,)
@@ -262,7 +263,7 @@ class JobsMonitor:
                             exc_info=True)
 
             self.record_correction_report(
-                r['mymdc_id'], r['command'], r['karabo_id'], success
+                r['mymdc_id'], r['command'], r['karabo_id'], success, r['timestamp']
             )
 
         return r['req_id']
@@ -337,7 +338,9 @@ class JobsMonitor:
             log.error("Failed to update MDC dark run id %s", dark_run_id)
             log.error(Errors.MDC_RESPONSE.format(response))
 
-    def record_correction_report(self, mymdc_run_id, command, karabo_id, success):
+    def record_correction_report(
+            self, mymdc_run_id, command, karabo_id, success, request_time: str
+    ):
         """Add report to MyMdC when a correction execution has finished"""
         args = shlex.split(command)
         try:
@@ -350,7 +353,6 @@ class JobsMonitor:
             log.error("Jobs finished, but report file %s missing", report_path)
             return
 
-        ts = datetime.fromtimestamp(os.stat(report_path).st_mtime, tz=timezone.utc)
         desc = f"{karabo_id} detector corrections"
         if not success:
             desc += " (errors occurred)"
@@ -361,7 +363,7 @@ class JobsMonitor:
         response = self.mdc.create_report_api({
             "name": os.path.basename(report_path),
             "cal_report_path": os.path.dirname(report_path).rstrip('/') + '/',
-            "cal_report_at": ts.isoformat(),
+            "cal_report_at": request_time,
             "run_id": mymdc_run_id,
             "description": desc,
         })
diff --git a/webservice/webservice.py b/webservice/webservice.py
index 0652255f0..6e5994648 100644
--- a/webservice/webservice.py
+++ b/webservice/webservice.py
@@ -956,7 +956,7 @@ class ActionsServer:
         This will trigger a correction process to be launched for that run in
         the given cycle and proposal.
         """
-        request_time = datetime.now()
+        request_time = datetime.now(tz=timezone.utc)
         try:
             runnr = runnr.strip('r')
 
@@ -1093,7 +1093,7 @@ class ActionsServer:
         return queued_msg.encode()
 
     async def handle_recorrect(self, rid, instrument, cycle, proposal, runnr):
-        request_time = datetime.now()
+        request_time = datetime.now(tz=timezone.utc)
 
         try:
             if self.check_unfinished_correction(proposal, int(runnr)):
@@ -1216,7 +1216,7 @@ class ActionsServer:
         :param runnr: is the run number in integer form, i.e. without leading
                      "r"
         """
-        request_time = datetime.now()
+        request_time = datetime.now(tz=timezone.utc)
         try:
             pdus, karabo_das, wait_runs = ast.literal_eval(','.join(extra))
 
-- 
GitLab