d34826d5324cc8906a8904f08b548979801bad1a
[plewww.git] / planetlab / slices / leases.js
1 /* need to put some place else in CSS ? */
2
3
4 /* decorations / headers */
5
6 var txt_nodename = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#008"};
7 var txt_timeslot = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#008"};
8 var attr_rules={'fill':"#888", 'stroke-dasharray':'- ', 'stroke-width':0.5};
9 var x_nodename = 200;
10 var x_sep=10;
11 var y_header = 12
12 var y_sep = 20
13
14 /* lease dimensions and colors */
15 var anim_delay=500;
16 var x_grain = 24;
17 var y_node = 15;
18 var radius= 6;
19 /* lease was originally free and is still free */
20 var attr_lease_free_free={'fill':"#def", 'stroke-width':0.5, 'stroke-dasharray':''};
21 /* lease was originally free and is now set for our usage */
22 var attr_lease_free_mine={'fill':"green", 'stroke-width':1, 'stroke-dasharray':'-..'};
23 /* was mine and is still mine */
24 var attr_lease_mine_mine={'fill':"#beb", 'stroke-width':0.5, 'stroke-dasharray':''};
25 /* was mine and is about to be released */
26 var attr_lease_mine_free={'fill':"white", 'stroke-width':1, 'stroke-dasharray':'-..'};
27 // refrained from using gradient color, was not animated properly
28 // var color_lease_mine_free="0-#fff-#def:50-#fff";
29 var attr_lease_other={'fill':"#f88"};
30
31 /* other slices name */
32 var txt_slice = {"font": '"Trebuchet MS", Verdana, Arial, Helvetica, sans-serif', stroke: "none", fill: "#444",
33                  "font-size": 15 };
34
35 ////////////////////////////////////////////////////////////
36 // the scheduler object
37 function Scheduler (slicename, axisx, axisy, data) {
38
39     this.slicename=slicename;
40     this.axisx=axisx;
41     this.axisy=axisy;
42     this.data=data;
43
44     // utilities to keep track of all the leases
45     this.leases=[];
46     this.append_lease = function (lease) { 
47         this.leases.push(lease);
48     }
49
50     // how many time slots 
51     this.nb_grains = function () { return axisx.length;}
52
53     this.init = function (id) {
54         this.total_width = x_nodename + this.nb_grains()*x_grain; 
55         this.total_height = 2*y_header + this.axisy.length*(y_node+y_sep); 
56         paper = Raphael (id, this.total_width, this.total_height);
57
58         // create the time slots legend
59         var top=0;
60         var left=x_nodename;
61
62         var col=0;
63         for (var i=0, len=axisx.length; i < len; ++i) {
64             // pick the printable part
65             timeslot=axisx[i][1];
66             var y = top+y_header;
67             if (col%2 == 0) y += y_header;
68             col +=1;
69             var timelabel=paper.text(left,y,timeslot).attr(txt_timeslot)
70                 .attr({"font-size":y_header, "text-anchor":"middle"});
71             var path_spec="M"+left+" "+(y+y_header/2)+"L"+left+" "+this.total_height;
72             var rule=paper.path(path_spec).attr(attr_rules);
73             left+=x_grain;
74         }
75
76         // move to the lines below: 
77         top += 2*y_header+y_sep;
78     
79         var data_index=0;
80         for (var i=0, len=axisy.length; i<len; ++i) {
81             node=axisy[i];
82             left=0;
83             var nodelabel = paper.text(x_nodename-x_sep,top+y_node/2,node).attr(txt_nodename)
84                 .attr ({"font-size":y_node, "text-anchor":"end","baseline":"bottom"});
85             
86             left += x_nodename;
87             var grain=0;
88             while (grain < this.nb_grains()) {
89                 slicename=data[data_index][0];
90                 duration=data[data_index][1];
91                 var lease=paper.rect (left,top,x_grain*duration,y_node,radius);
92                 lease.nodename=node;
93                 if (slicename == "") {
94                     lease.initial="free";
95                     lease_methods.init_free(lease);
96                 } else if (slicename == this.slicename) {
97                     lease.initial="mine";
98                     lease_methods.init_mine(lease);
99                 } else {
100                     lease_initial="other";
101                     lease_methods.init_other(lease,slicename);
102                 }
103                 lease.from_time = axisx[grain%this.nb_grains()][0];
104                 grain += duration;
105                 lease.until_time = axisx[grain%this.nb_grains()][0];
106                 // record scheduler in lease
107                 lease.scheduler=this;
108                 // and vice versa
109                 this.append_lease(lease);
110                 // move on with the loop
111                 left += x_grain*duration;
112                 data_index +=1;
113             }
114             top += y_node + y_sep;
115         };
116     }
117
118     this.submit = function () {
119         for (var i=0, len=this.leases.length; i<len; ++i) {
120             var lease=this.leases[i];
121             if (lease.current != lease.initial) {
122                 var from_time=lease.from_time;
123                 var until_time=lease.until_time;
124                 /* scan the leases just after this one and merge if appropriate */
125                 var j=i+1;
126                 while (j<len && lease_methods.compare (lease, until_time, this.leases[j])) {
127                     window.console.log('merged index='+j);
128                     until_time=this.leases[j].until_time;
129                     ++j; ++i;
130                 }
131                 var method=(lease.current=='free') ? 'DeleteLeases' : 'AddLeases';
132                 window.console.log(method + "(" + 
133                                    "[" + lease.nodename + "]," + 
134                                    this.slicename + "," +
135                                    from_time + "," +
136                                    until_time + ')');
137             }
138         }
139     }
140
141     this.clear = function () {
142         for (var i=0, len=this.leases.length; i<len; ++i) {
143             var lease=this.leases[i];
144             if (lease.current != lease.initial) {
145                 if (lease.initial == 'free') lease_methods.init_free(lease,lease_methods.click_mine);
146                 else                         lease_methods.init_mine(lease,lease_methods.click_free);
147             }
148         }
149     }
150
151     /* initialize mode buttons */
152     this.init_mode = function (default_mode, node_button, timeslot_button) {
153         this.node_button=node_button;
154         this.timeslot_button=timeslot_button;
155         var scheduler=this;
156         /* xxx set callbacks on buttons */
157         node_button.onclick = function () { scheduler.set_mode('node'); }
158         timeslot_button.onclick = function () { scheduler.set_mode('timeslot'); }
159         this.set_mode(default_mode);
160     }
161
162     /* expecting mode to be either 'node' or 'timeslot' */
163     this.set_mode = function (mode) {
164         this.mode=mode;
165         var active_button = (mode=='node') ? this.node_button : this.timeslot_button;
166         active_button.checked='checked';
167     }
168         
169
170 } // end Scheduler
171
172 //////////////////////////////////////// couldn't find how to inhererit from the raphael objects...
173 var lease_methods = {
174     
175     /* in the process of merging leases before posting to the API */
176     compare: function (lease, until_time, next_lease) {
177         return (next_lease['nodename'] == lease['nodename'] &&
178                 next_lease['from_time'] == until_time &&
179                 next_lease['initial'] == lease['initial'] &&
180                 next_lease['current'] == lease['current']);
181     },
182
183     init_free: function (lease, unclick) {
184         lease.current="free";
185         // set color
186         lease.animate((lease.initial=="free") ? attr_lease_free_free : attr_lease_mine_free,anim_delay);
187         // keep track of the current status
188         // record action
189         lease.click (lease_methods.click_free);
190         if (unclick) lease.unclick(unclick);
191     },
192                      
193     // find out all the currently free leases that overlap this one
194     click_free: function (event) {
195         var scheduler = this.scheduler;
196         if (scheduler.mode=='node') {
197             lease_methods.init_mine(this,lease_methods.click_free);
198         } else {
199             for (var i=0, len=scheduler.leases.length; i<len; ++i) {
200                 scan=scheduler.leases[i];
201                 // overlap ?
202                 if (scan.from_time<=this.from_time && scan.until_time>=this.until_time) 
203                     if (scan.current == "free") lease_methods.init_mine(scan,lease_methods.click_free);
204             }
205         }
206     },
207
208     init_mine: function (lease, unclick) {
209         lease.current="mine";
210         lease.animate((lease.initial=="mine") ? attr_lease_mine_mine : attr_lease_free_mine,anim_delay);
211         lease.click (lease_methods.click_mine);
212         if (unclick) lease.unclick(unclick);
213     },
214
215     click_mine: function (event) {
216         var scheduler = this.scheduler;
217         // this lease was originally free but is now marked for booking
218         // we free just this lease
219         if (scheduler.mode=='node') {
220             lease_methods.init_free(this, lease_methods.click_mine);
221         } else {
222             for (var i=0, len=scheduler.leases.length; i<len; ++i) {
223                 scan=scheduler.leases[i];
224                 // overlap ?
225                 if (scan.from_time<=this.from_time && scan.until_time>=this.until_time) {
226                     if (scan.current == "mine") lease_methods.init_free(scan,lease_methods.click_mine);
227                 }
228                 // the other ones just remain as they are
229             }
230         }
231     },
232
233
234     init_other: function (lease, slicename) {
235         lease.animate (attr_lease_other,anim_delay);
236         /* a text obj to display the name of the slice that owns that lease */
237         var slicelabel = paper.text (lease.attr("x")+lease.attr("width")/2,
238                                      lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_slice);
239         /* hide it right away */
240         slicelabel.hide();
241         /* record it */
242         lease.label=slicelabel;
243         lease.hover ( function (e) { this.label.toFront(); this.label.show(); },
244                       function (e) { this.label.hide(); } ); 
245     },
246 }
247
248 function init_scheduler () {
249     // Grab the data
250     var data = [], axisx = [], axisy = [];
251     var table = $$("table#leases_data")[0];
252     // no reservable nodes - no data
253     if ( ! table) return;
254     // the nodenames
255     table.getElementsBySelector("tbody>tr>th").each(function (cell) {
256         axisy.push(getInnerText(cell));
257     });
258     // the timeslot labels
259     table.getElementsBySelector("thead>tr>th").each(function (cell) {
260         /* [0]: timestamp -- [1]: displayable*/
261         axisx.push(getInnerText(cell).split("&"));
262     });
263     // leases - expect colspan to describe length in grains
264     table.getElementsBySelector("tbody>tr>td").each(function (cell) {
265         data.push(new Array (getInnerText(cell),cell.colSpan));
266     });
267     // slicename : the upper-left cell
268     var scheduler = new Scheduler (getInnerText(table.getElementsBySelector("thead>tr>td")[0]), axisx, axisy, data);
269     table.hide();
270     // leases_area is a <div> created by slice.php as a placeholder
271     scheduler.init ("leases_area");
272
273     var submit=$$("button#leases_submit")[0];
274     submit.onclick = function () { scheduler.submit(); }
275     var clear=$$("button#leases_clear")[0];
276     clear.onclick = function () { scheduler.clear(); }
277
278     var node_button=$$("input#leases_mode_node")[0];
279     var timeslot_button=$$("input#leases_mode_timeslot")[0];
280     scheduler.init_mode ('timeslot',node_button,timeslot_button);
281
282 }
283
284 Event.observe(window, 'load', init_scheduler);