updated scheduler
authorJordan Augé <jordan.auge@lip6.fr>
Tue, 22 Jul 2014 06:43:33 +0000 (08:43 +0200)
committerJordan Augé <jordan.auge@lip6.fr>
Tue, 22 Jul 2014 06:43:33 +0000 (08:43 +0200)
plugins/scheduler2/static/js/scheduler2.js
plugins/scheduler2/templates/scheduler.html

index 0b294b4..293324e 100755 (executable)
@@ -478,10 +478,6 @@ var SCHEDULER_COLWIDTH = 50;
                  * be updated when resources arrive.  Should be the pgcd in fact XXX */\r
                 this._granularity = DEFAULT_GRANULARITY;\r
                 scope.granularity = this._granularity;\r
-                this._all_slots = this._generate_all_slots();\r
-\r
-                // A list of {id, time} dictionaries representing the slots for the given day\r
-                scope.slots = this._all_slots;\r
                 this.scope_resources_by_key = {};\r
 \r
                 this.do_resize();\r
@@ -495,7 +491,16 @@ var SCHEDULER_COLWIDTH = 50;
             do_resize: function()\r
             {\r
                 var scope = this._get_scope();\r
-                var num_hidden_cells, new_max;\r
+                var num_hidden_cells, new_max, lcm;\r
+\r
+                // do_resize has to be called when the window is resized, or one parameter changes\r
+                // e.g. when new resources have been received\r
+                //\r
+                this.resource_granularities = [3600, 1800]; //, 2400]; /* s */\r
+\r
+                /* Compute the slot length to accommodate all resources. This\r
+                 * is the GCD of all resource granularities. */\r
+                this._slot_length = this._gcdn(this.resource_granularities);\r
 \r
                 $('#' + schedulerTblId + ' thead tr th:eq(0)').css("width", SCHEDULER_FIRST_COLWIDTH);\r
                 //self get width might need fix depending on the template \r
@@ -503,13 +508,23 @@ var SCHEDULER_COLWIDTH = 50;
 \r
                 /* Number of visible cells...*/\r
                 this._num_visible_cells = parseInt((tblwidth - SCHEDULER_FIRST_COLWIDTH) / SCHEDULER_COLWIDTH);\r
+\r
                 /* ...should be a multiple of the lcm of all encountered granularities. */\r
-                // XXX Should be updated everytime a new resource is added\r
-                this._lcm_colspan = this._lcm(this._granularity, RESOURCE_DEFAULT_GRANULARITY) / this._granularity;\r
-                this._num_visible_cells = this._num_visible_cells - this._num_visible_cells % this._lcm_colspan;\r
+                lcm = this._lcmn(this.resource_granularities) / this._slot_length;\r
+                this._num_visible_cells = this._num_visible_cells - this._num_visible_cells % lcm;\r
+\r
+                // A list of {id, time} dictionaries representing the slots for the given day\r
+                this._all_slots = this._generate_all_slots();\r
+\r
                 /* scope also needs this value */\r
+                scope.slots = this._all_slots;\r
+                scope.slot_length = this._slot_length;\r
                 scope.num_visible_cells = this._num_visible_cells;\r
-                scope.lcm_colspan = this._lcm_colspan;\r
+                scope.lcm_colspan = this._lcmn(this.resource_granularities); // XXX WHY ?\r
+\r
+                /* Redraw... */\r
+                this._scope_clear_leases();\r
+                this._set_all_lease_slots();\r
 \r
                 // Slider max value\r
                 if ($('#tblSlider').data('slider') != undefined) {\r
@@ -517,8 +532,8 @@ var SCHEDULER_COLWIDTH = 50;
 \r
                     $('#tblSlider').slider('setAttribute', 'max', num_hidden_cells);\r
                     $('#tblSlider').slider('setValue', scope.from, true);\r
-                    this._get_scope().$apply();\r
                 }\r
+                this._get_scope().$apply();\r
 \r
 \r
             },\r
@@ -580,7 +595,7 @@ var SCHEDULER_COLWIDTH = 50;
                 // Setup leases with a default free status...\r
                 $.each(this.scope_resources_by_key, function(resource_key, resource) {\r
                     resource.leases = [];\r
-                    var colspan_lease = resource.granularity / self._granularity; //eg. 3600 / 1800 => 2 cells\r
+                    var colspan_lease = resource.granularity / self._slot_length; //eg. 3600 / 1800 => 2 cells\r
                     time = SchedulerDateSelected.getTime();\r
                     for (i=0; i < self._all_slots.length / colspan_lease; i++) { // divide by granularity\r
                         resource.leases.push({\r
@@ -715,7 +730,7 @@ var SCHEDULER_COLWIDTH = 50;
                     return true; // ~ continue\r
 \r
                 id_end   = Math.ceil((lease.end_time   - day_timestamp) / resource.granularity);\r
-                colspan_lease = resource.granularity / this._granularity; //eg. 3600 / 1800 => 2 cells\r
+                colspan_lease = resource.granularity / this._slot_length; //eg. 3600 / 1800 => 2 cells\r
                 if (id_end >= this._all_slots.length / colspan_lease) {\r
                     /* Limit the display to the current day */\r
                     id_end = this._all_slots.length / colspan_lease\r
@@ -819,11 +834,11 @@ var SCHEDULER_COLWIDTH = 50;
                 \r
                 $('#tblSlider').slider({\r
                     min: 0,\r
-                    max: num_hidden_cells, // / self._lcm_colspan,\r
+                    max: num_hidden_cells,\r
                     value: init_cell,\r
                 }).on('slide', function(ev) {\r
                     var scope = self._get_scope();\r
-                    scope.from = ev.value * self._lcm_colspan;\r
+                    scope.from = ev.value;\r
                     scope.$apply();\r
                 });\r
                 scope.from = init_cell;\r
@@ -843,6 +858,12 @@ var SCHEDULER_COLWIDTH = 50;
             return (y==0) ? x : this._gcd(y, x % y);\r
         },\r
 \r
+        _gcdn : function(array)\r
+        {\r
+            var self = this;\r
+            return array.reduce(function(prev, cur, idx, arr) { return self._gcd(prev, cur); });\r
+        },\r
+\r
         /**\r
          * Least common multiple\r
          */\r
@@ -850,6 +871,12 @@ var SCHEDULER_COLWIDTH = 50;
         {\r
             return x * y / this._gcd(x, y);\r
         },\r
+\r
+        _lcmn : function(array)\r
+        {\r
+            var self = this;\r
+            return array.reduce(function(prev, cur, idx, arr) { return self._lcm(prev, cur); });\r
+        },\r
     \r
         _pad_str : function(i)\r
         {\r
@@ -876,7 +903,7 @@ var SCHEDULER_COLWIDTH = 50;
                 /// ...and add the slot to the list of results\r
                 slots.push({ id: i, time: tmpTime });\r
                 // Increment the date with the granularity\r
-                d = new Date(d.getTime() + this._granularity * 1000);\r
+                d = new Date(d.getTime() + this._slot_length * 1000);\r
                 i++;\r
             }\r
             return slots;\r
index 231a1d2..e07637a 100755 (executable)
                                                        style="word-wrap: break-word; word-break: break-all; ">\r
                                                        {[{ resource.hostname }]}\r
                                                </th>\r
-                        <td ng-repeat="lease in resource.leases | offset: from / lcm_colspan  | limitTo: num_visible_cells / lcm_colspan"\r
+                        <td ng-repeat="lease in resource.leases | offset: from / (resource.granularity / slot_length)  | limitTo: num_visible_cells / (resource.granularity / slot_length)"\r
                                                        data-slotid="{[{ lease.id }]}" \r
                                                        data-groupid="{[{ lease.groupid }]}" \r
                                                        ng-class="{{ 'lease.status' }}"\r
                                                        ng-class="{{ 'lease.success' }}"\r
-                                                       colspan="{[{resource.granularity / granularity}]}"\r
+                                                       colspan="{[{resource.granularity / slot_length}]}"\r
                                                        ng-click="select(from+$index, lease, $parent.resource)">\r
                                                </td>\r
                     </tr>\r