From 109cebdfae7782dd0780ec38a1a757bffc0ebfd3 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Tue, 16 Aug 2016 08:06:25 +0000
Subject: [PATCH] Task #9607: get cluster info from spec in radb

---
 .../ResourceAssignmentDatabase/radb.py        | 63 ++++++++++++++++++-
 .../static/app/controllers/datacontroller.js  | 36 -----------
 .../static/app/controllers/gridcontroller.js  |  2 +-
 3 files changed, 61 insertions(+), 40 deletions(-)

diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py
index e013b2f0377..f974a13f872 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py
+++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py
@@ -192,6 +192,8 @@ class RADatabase:
 
         tasks = list(self._executeQuery(query, qargs, fetch=_FETCH_ALL))
 
+        self._addClusterToTasks(tasks)
+
         return tasks
 
 
@@ -221,8 +223,48 @@ class RADatabase:
             if task['successor_ids'] is None:
                 task['successor_ids'] = []
 
+        self._addClusterToTasks([task])
         return task
 
+    def _addClusterToTasks(self, tasks):
+        #parse the parset content of the specification
+        #and retreive the storageClusterName for each task if possible
+        try:
+            spec_ids = [t['specification_id'] for t in tasks if t != None]
+
+            specs = self.getSpecifications(specification_ids=spec_ids)
+            spec_dict = {s['id']:s for s in specs}
+
+            keys = ['Output_Correlated',
+                    'Output_IncoherentStokes',
+                    'Output_CoherentStokes',
+                    'Output_InstrumentModel',
+                    'Output_SkyImage',
+                    'Output_Pulsar']
+
+            for task in tasks:
+                try:
+                    spec = spec_dict.get(task['specification_id'])
+                    if spec:
+                        parset = [line.strip() for line in spec['content'].split('\n')]
+                        parset = [line for line in parset if line.startswith('Observation.DataProducts')]
+                        parset = [line.split('=') for line in parset]
+                        parset = {items[0]:items[1] for items in parset}
+
+                        enabled_keys = [key for key in keys if parset.get('Observation.DataProducts.%s.enabled' % key, '').lower() in ['true', 't', 'yes', 'y', '1']]
+                        clusters = [parset.get('Observation.DataProducts.%s.storageClusterName' % key, 'CEP2') for key in enabled_keys]
+                        clusters = [c if c else 'CEP2' for c in clusters]
+
+                        #pick median cluster
+                        clusters.sort()
+                        task['cluster'] = clusters[len(clusters)/2]
+                    else:
+                        task['cluster'] = 'unknown'
+                except Exception as e:
+                    task['cluster'] = 'unknown'
+        except Exception as e:
+            pass
+
     def _convertTaskTypeAndStatusToIds(self, task_status, task_type):
         '''converts task_status and task_type to id's in case one and/or the other are strings'''
         if task_status and isinstance(task_status, basestring):
@@ -382,10 +424,25 @@ class RADatabase:
             self.commit()
         return ids
 
-    def getSpecifications(self):
-        query = '''SELECT * from resource_allocation.specification;'''
+    def getSpecifications(self, specification_ids = None):
+        query = '''SELECT * from resource_allocation.specification'''
 
-        return list(self._executeQuery(query, fetch=_FETCH_ALL))
+        conditions = []
+        qargs = []
+
+        if specification_ids is not None:
+            if isinstance(specification_ids, int): # just a single id
+                conditions.append('id = %s')
+                qargs.append(specification_ids)
+            else: #assume a list/enumerable of id's
+                if len(specification_ids):
+                    conditions.append('id in %s')
+                    qargs.append(tuple(specification_ids))
+
+        if conditions:
+            query += ' WHERE ' + ' AND '.join(conditions)
+
+        return list(self._executeQuery(query, qargs, fetch=_FETCH_ALL))
 
     def getSpecification(self, specification_id):
         query = '''SELECT * from resource_allocation.specification spec
diff --git a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js
index b77735c1f72..a7f4e8870ca 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js
+++ b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js
@@ -15,7 +15,6 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
     self.resourceGroupsDict = {};
     self.resourceGroupMemberships = {};
     self.resourceClaimDict = {};
-    self.task2resourceClaimDict = {};
     self.resourceUsagesDict = {};
     self.tasktypesDict = {};
 
@@ -247,21 +246,6 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
         self.resourceClaims = visibleClaims;
         self.resourceClaimDict = self.toIdBasedDict(self.resourceClaims);
 
-        self.task2resourceClaimDict = {};
-
-        for(var claim_id in self.resourceClaimDict) {
-            var claim = self.resourceClaimDict[claim_id];
-
-            if(!self.task2resourceClaimDict.hasOwnProperty(claim.task_id)) {
-                self.task2resourceClaimDict[claim.task_id] = {};
-            }
-
-            if(!self.task2resourceClaimDict[claim.task_id].hasOwnProperty(claim_id)) {
-                self.task2resourceClaimDict[claim.task_id][claim_id] = claim;
-            }
-        }
-
-
         self.computeMinMaxResourceClaimTimes();
 
         var newLoadedHours = {};
@@ -538,14 +522,6 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
                     self.resourceClaims.push(claim);
                     self.resourceClaimDict[claim_id] = claim;
                 }
-
-                if(!self.task2resourceClaimDict.hasOwnProperty(claim.task_id)) {
-                    self.task2resourceClaimDict[claim.task_id] = {};
-                }
-
-                if(!self.task2resourceClaimDict[claim.task_id].hasOwnProperty(claim_id)) {
-                    self.task2resourceClaimDict[claim.task_id][claim_id] = claim;
-                }
             }
 
             self.computeMinMaxResourceClaimTimes();
@@ -799,14 +775,6 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
                                     changedClaim.endtime = new Date(changedClaim.endtime);
                                     self.resourceClaims.push(changedClaim);
                                     self.resourceClaimDict[changedClaim.id] = changedClaim;
-
-                                    if(!self.task2resourceClaimDict.hasOwnProperty(changedClaim.task_id)) {
-                                        self.task2resourceClaimDict[changedClaim.task_id] = {};
-                                    }
-
-                                    if(!self.task2resourceClaimDict[changedClaim.task_id].hasOwnProperty(changedClaim.id)) {
-                                        self.task2resourceClaimDict[changedClaim.task_id][changedClaim.id] = claim;
-                                    }
                                 }
                             } else if(change.changeType == 'delete') {
                                 delete self.resourceClaimDict[changedClaim.id]
@@ -816,10 +784,6 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
                                         break;
                                     }
                                 }
-
-                                if(self.task2resourceClaimDict.hasOwnProperty(changedClaim.task_id)) {
-                                    delete self.task2resourceClaimDict[changedClaim.task_id][changedClaim.id];
-                                }
                             }
 
                             self.claimChangeCntr++;
diff --git a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/gridcontroller.js b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/gridcontroller.js
index f59a1eb8935..97d6da7fd8c 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/gridcontroller.js
+++ b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/gridcontroller.js
@@ -250,7 +250,7 @@ $scope.columns = [
                         mom_object_group_id: task.mom_object_group_id,
                         mom_object_group_name: task.mom_object_group_name,
                         mom_object_group_mom2object_id: task.mom_object_group_mom2object_id,
-                        cluster: $scope.dataService.task2resourceClaimDict.hasOwnProperty(task.id) ? 'CEP4' : 'CEP2'
+                        cluster: task.cluster
                     };
                     gridTasks.push(gridTask);
                 }
-- 
GitLab