From 2ce9945f67d9e7eb0821ce03d08467bf09f76038 Mon Sep 17 00:00:00 2001
From: Quentin Codelupi <quentin.codelupi@cern.ch>
Date: Tue, 30 Jul 2019 15:47:47 +0200
Subject: [PATCH] [add] check data before start pulling

---
 .idea/inspectionProfiles/Project_Default.xml |  2 +-
 routine_pull.py                              | 69 +++++++++++++++-----
 2 files changed, 54 insertions(+), 17 deletions(-)

diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
index ab875d7..393f99d 100644
--- a/.idea/inspectionProfiles/Project_Default.xml
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -3,7 +3,7 @@
     <option name="myName" value="Project Default" />
     <inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
       <Languages>
-        <language minSize="146" name="Python" />
+        <language minSize="213" name="Python" />
       </Languages>
     </inspection_tool>
     <inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
diff --git a/routine_pull.py b/routine_pull.py
index 6cbfcf7..528c104 100644
--- a/routine_pull.py
+++ b/routine_pull.py
@@ -1,4 +1,5 @@
 import mysql.connector
+import time
 
 import property_pull
 import pytimber as pytimber
@@ -22,19 +23,19 @@ def start_pull(_fill_infos, _devices=None):
     if _devices is None:
         _devices = ['BLMED.06L7.B1B30_TCPA.A6L7.B1']
 
-    print(fill_infos)
+    print(_fill_infos)
     if _fill_infos["endTime"] is None:
         return -1
 
     for iDevice in _devices:
 
-        property_pull.property_pull(fill_infos['fillNumber'], 'ALL', iDevice,
-                                    fill_infos['startTime'].strftime("%Y-%m-%d %H:%M:%S.000"),
-                                    fill_infos['endTime'].strftime("%Y-%m-%d %H:%M:%S.000"))
+        property_pull.property_pull(_fill_infos['fillNumber'], 'ALL', iDevice,
+                                    _fill_infos['startTime'].strftime("%Y-%m-%d %H:%M:%S.000"),
+                                    _fill_infos['endTime'].strftime("%Y-%m-%d %H:%M:%S.000"))
 
-        for iMode in fill_infos["beamModes"]:
+        for iMode in _fill_infos["beamModes"]:
             print(iMode['mode'])
-            property_pull.property_pull(fill_infos['fillNumber'], iMode['mode'], iDevice,
+            property_pull.property_pull(_fill_infos['fillNumber'], iMode['mode'], iDevice,
                                         iMode['startTime'].strftime("%Y-%m-%d %H:%M:%S.000"),
                                         iMode['endTime'].strftime("%Y-%m-%d %H:%M:%S.000"))
 
@@ -47,16 +48,29 @@ def start_pull(_fill_infos, _devices=None):
             '''
 
 
-if __name__ == '__main__':
+def routine_pull(_start_fill=None, _end_fill=None):
     ldb = pytimber.LoggingDB()
-
-    # res = ldb.get('HX:FILLN', datetime.datetime.now())
-    # fillNumber = int(res['HX:FILLN'][1][0])
-    fill_number = 7493
-
-    fill_infos = ldb.getLHCFillData(fill_number, False)
-
     gcursor = mydb.cursor()
+    _start_fill = None
+    _end_fill = None
+
+    if _start_fill is None and _end_fill is None:
+        res = ldb.get('HX:FILLN', dt.datetime.now())
+        new_fill_number = int(res['HX:FILLN'][1][0])
+        print(new_fill_number)
+
+        gcursor.execute('select max(name) from fill limit 1;')
+        last_fill_pulled = gcursor.fetchone()[0]
+        print(last_fill_pulled)
+
+        if new_fill_number == last_fill_pulled:
+            print('already pulled last fill')
+            return -1
+    else:
+        new_fill_number = _end_fill
+        last_fll_pulled = _start_fill - 1
+
+    # Select only the first 6 devices Cause: not all device works properly with NXCALS yet
     gcursor.execute('select name from device limit 6')
     devices_request = gcursor.fetchall()
     devices = []
@@ -66,6 +80,29 @@ if __name__ == '__main__':
     gcursor.close()
     mydb.close()
 
-    print(devices)
-
+    fill_number = 7472
+    fill_infos = ldb.getLHCFillData(fill_number, False)
     start_pull(fill_infos, devices)
+
+    '''
+    for iFill in range(last_fill_pulled + 1, new_fill_number):
+        fill_infos = ldb.getLHCFillData(iFill, False)
+
+        # check if the fill is finished
+        if fill_infos['endTime'] is None:
+            break
+
+        # check if data is too new
+        midnight_stamp = dt.datetime.now().replace(second=0, minute=0, hour=0) - dt.timedelta(days=1)
+        if time.mktime(midnight_stamp.timetuple()) < fill_infos['endTime']:
+            continue
+
+        start_pull(fill_infos, devices)
+    '''
+
+
+if __name__ == '__main__':
+    routine_pull()
+
+
+
-- 
GitLab