From a7bbdafa9a59d2d697bca7e5b851b0b39eb9b245 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Tue, 12 Apr 2016 08:58:12 +0000
Subject: [PATCH] Task #8887: also compute and display
 unaccounter-for-resource-usage

---
 .../ResourceAssignmentDatabase/radb.py        | 28 +++++++++++++--
 .../chartresourceusagecontroller.js           | 35 +++++++++++++------
 .../static/app/controllers/datacontroller.js  |  8 +++--
 3 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py
index c93539ae4f7..a4515276793 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py
+++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radb.py
@@ -1304,7 +1304,7 @@ class RADatabase:
 
         # sort events per resource by event timestamp ascending
         # and integrate event delta's into usage
-        all_usages = []
+        all_usages = {}
         for resource_id, status_events in eventsDict.items():
             usages = {}
             for status, events in status_events.items():
@@ -1328,9 +1328,31 @@ class RADatabase:
                         prev_usage = usage
 
             resource_usages = { 'resource_id': resource_id, 'usages': usages }
-            all_usages.append(resource_usages)
+            all_usages[resource_id] = resource_usages
+
+        resource_ids = all_usages.keys()
+        resources = self.getResources(resource_ids=resource_ids, include_availability=True)
+
+        for resource in resources:
+            resource_id = resource['id']
+            resource_usages = all_usages[resource_id]
+            # copy resource capacities
+            resource_usages['total_capacity'] = resource['total_capacity']
+            resource_usages['available_capacity'] = resource['available_capacity']
+            resource_usages['used_capacity'] = resource['used_capacity']
+            # and compute unaccounted-for usage,
+            # which is the actual used_capacity minus the currently allocated total claim size
+            utcnow = datetime.utcnow()
+            allocated_usages = resource_usages['usages'].get('allocated', [])
+            past_allocated_usages = sorted([au for au in allocated_usages if au['timestamp'] <= utcnow])
+            if past_allocated_usages:
+                currently_allocated_usage = past_allocated_usages[-1]
+                resource_usages['misc_used_capacity'] = resource['used_capacity'] - currently_allocated_usage['value']
+            else:
+                resource_usages['misc_used_capacity'] = 0
 
-        return all_usages
+        all_usages_list = all_usages.values()
+        return all_usages_list
 
 
 if __name__ == '__main__':
diff --git a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/chartresourceusagecontroller.js b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/chartresourceusagecontroller.js
index e98475b3b64..c028453a3a5 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/chartresourceusagecontroller.js
+++ b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/chartresourceusagecontroller.js
@@ -71,7 +71,7 @@ chartResourceUsageControllerMod.controller('ChartResourceUsageController', ['$sc
         var numResources = $scope.dataService.resources.length;
         var resource = $scope.dataService.selected_resource;
 
-        if(!resource || numResources == 0) {
+        if(!resource || numResources == 0 || !resourceUsagesDict[resource.id]) {
             $scope.chartSeries.splice(0, $scope.chartSeries.length);
             $scope.chartConfig.title.text = "No resource selected";
             return;
@@ -80,7 +80,7 @@ chartResourceUsageControllerMod.controller('ChartResourceUsageController', ['$sc
         //set title to resource name
         $scope.chartConfig.title.text = resource.name;
 
-        var status_usages = resourceUsagesDict[resource.id];
+        var status_usages = resourceUsagesDict[resource.id].usages;
 
         //first scan of all statuses and timestamps in usages for this resource
         var statuses = [];
@@ -140,12 +140,13 @@ chartResourceUsageControllerMod.controller('ChartResourceUsageController', ['$sc
                     t_idx += 1;
                 }
 
-                var series = $scope.chartSeries.find(function(series) {return series.name == status});
-
-                if(!series) {
-                    series = {name: status, type: 'area', step: true, lineWidth:0, marker:{enabled:false} };
-                    $scope.chartSeries.push(series);
+                //make sure the series are in the right order for proper stacking
+                var seriesIdx = $scope.chartSeries.findIndex(function(series) {return series.name == status});
+                if(seriesIdx > -1) {
+                    $scope.chartSeries.splice(seriesIdx, 1);
                 }
+                series = {name: status, type: 'area', step: true, lineWidth:0, marker:{enabled:false}, animation:false };
+                $scope.chartSeries.push(series);
 
                 series.data = usage_data;
 
@@ -156,6 +157,21 @@ chartResourceUsageControllerMod.controller('ChartResourceUsageController', ['$sc
                 }
             }
 
+            //plot area for resource misc_used capacity
+            //make sure it is the last of the 'area' series so it is at the bottom of the stacked area charts
+            var misc_used_cap_series_idx = $scope.chartSeries.findIndex(function(series) {return series.name == 'misc used capacity'});
+            if(misc_used_cap_series_idx > -1) {
+                $scope.chartSeries.splice(misc_used_cap_series_idx, 1);
+            }
+
+            var misc_used_capacity = resourceUsagesDict[resource.id].misc_used_capacity;
+            if(misc_used_capacity > 0) {
+                misc_used_cap_series = {name: 'misc used capacity', type: 'area', color: '#aaaaff', lineWidth:1, marker:{enabled:false}, dashStyle:'Dash', animation:false };
+                $scope.chartSeries.push(misc_used_cap_series);
+                misc_used_cap_series.data = timestamps.map(function(t) { return [t.getTime(), misc_used_capacity]; });
+                expectedSeriesNames.push('misc used capacity');
+            }
+
             //plot horizontal line for resource total capacity
             var tot_cap_series = $scope.chartSeries.find(function(series) {return series.name == 'total capacity'});
             if(!tot_cap_series) {
@@ -172,9 +188,8 @@ chartResourceUsageControllerMod.controller('ChartResourceUsageController', ['$sc
                 used_cap_series = {name: 'used capacity', type: 'line', color: '#ff9966', lineWidth:3, marker:{enabled:false}, dashStyle:'Dash'};
                 $scope.chartSeries.push(used_cap_series);
             }
-            var used_capacity = resource.total_capacity - resource.available_capacity;
-            used_cap_series.data = [[timestamps[0].getTime(), used_capacity],
-                                    [timestamps[timestamps.length-1].getTime(), used_capacity]]
+            used_cap_series.data = [[timestamps[0].getTime(), resource.used_capacity],
+                                    [timestamps[timestamps.length-1].getTime(), resource.used_capacity]]
             expectedSeriesNames.push('used capacity');
         }
 
diff --git a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js
index d5b7f4e0498..3f7124d94f7 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js
+++ b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/static/app/controllers/datacontroller.js
@@ -100,7 +100,7 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
                         usage.timestamp = new Date(usage.timestamp);
                     }
                 }
-                self.resourceUsagesDict[result.resourceusages[i].resource_id] = resource_usages;
+                self.resourceUsagesDict[result.resourceusages[i].resource_id] = result.resourceusages[i];
             }
 
             defer.resolve();
@@ -273,7 +273,11 @@ angular.module('raeApp').factory("dataService", ['$http', '$q', function($http,
                         if(existingObj.hasOwnProperty(prop) &&
                            changedObj.hasOwnProperty(prop) &&
                            existingObj[prop] != changedObj[prop]) {
-                            existingObj[prop] = changedObj[prop];
+                            if(existingObj[prop] instanceof Date) {
+                                existingObj[prop] = new Date(changedObj[prop]);
+                            } else {
+                                existingObj[prop] = changedObj[prop];
+                            }
                         }
                     }
                 };
-- 
GitLab