a322f566e9654042a1028ec6c35fcfcbdf490c47
[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 method=(lease.current=='free') ? 'DeleteLeases' : 'AddLeases';
123                 window.console.log(method + "(" + 
124                                    "[" + lease.nodename + "]," + 
125                                    this.slicename + "," +
126                                    lease.from_time + "," +
127                                    lease.until_time + ')');
128             }
129         }
130     }
131
132     this.clear = function () {
133         for (var i=0, len=this.leases.length; i<len; ++i) {
134             var lease=this.leases[i];
135             if (lease.current != lease.initial) {
136                 if (lease.initial == 'free') lease_methods.init_free(lease,lease_methods.click_mine);
137                 else                         lease_methods.init_mine(lease,lease_methods.click_free);
138             }
139         }
140     }
141
142     /* initialize mode buttons */
143     this.init_mode = function (default_mode, node_button, timeslot_button) {
144         this.node_button=node_button;
145         this.timeslot_button=timeslot_button;
146         var scheduler=this;
147         /* xxx set callbacks on buttons */
148         node_button.onclick = function () { scheduler.set_mode('node'); }
149         timeslot_button.onclick = function () { scheduler.set_mode('timeslot'); }
150         this.set_mode(default_mode);
151     }
152
153     /* expecting mode to be either 'node' or 'timeslot' */
154     this.set_mode = function (mode) {
155         this.mode=mode;
156         var active_button = (mode=='node') ? this.node_button : this.timeslot_button;
157         active_button.checked='checked';
158     }
159         
160
161 } // Scheduler
162
163 //////////////////////////////////////// couldn't find how to inhererit from the raphael objects...
164 var lease_methods = {
165     
166     init_free: function (lease, unclick) {
167         lease.current="free";
168         // set color
169         lease.animate((lease.initial=="free") ? attr_lease_free_free : attr_lease_mine_free,anim_delay);
170         // keep track of the current status
171         // record action
172         lease.click (lease_methods.click_free);
173         if (unclick) lease.unclick(unclick);
174     },
175                      
176     // find out all the currently free leases that overlap this one
177     click_free: function (event) {
178         var scheduler = this.scheduler;
179         if (scheduler.mode=='node') {
180             lease_methods.init_mine(this,lease_methods.click_free);
181         } else {
182             for (var i=0, len=scheduler.leases.length; i<len; ++i) {
183                 scan=scheduler.leases[i];
184                 // overlap ?
185                 if (scan.from_time<=this.from_time && scan.until_time>=this.until_time) 
186                     if (scan.current == "free") lease_methods.init_mine(scan,lease_methods.click_free);
187             }
188         }
189     },
190
191     init_mine: function (lease, unclick) {
192         lease.current="mine";
193         lease.animate((lease.initial=="mine") ? attr_lease_mine_mine : attr_lease_free_mine,anim_delay);
194         lease.click (lease_methods.click_mine);
195         if (unclick) lease.unclick(unclick);
196     },
197
198     click_mine: function (event) {
199         var scheduler = this.scheduler;
200         // this lease was originally free but is now marked for booking
201         // we free just this lease
202         if (scheduler.mode=='node') {
203             lease_methods.init_free(this, lease_methods.click_mine);
204         } else {
205             for (var i=0, len=scheduler.leases.length; i<len; ++i) {
206                 scan=scheduler.leases[i];
207                 // overlap ?
208                 if (scan.from_time<=this.from_time && scan.until_time>=this.until_time) {
209                     if (scan.current == "mine") lease_methods.init_free(scan,lease_methods.click_mine);
210                 }
211                 // the other ones just remain as they are
212             }
213         }
214     },
215
216
217     init_other: function (lease, slicename) {
218         lease.animate (attr_lease_other,anim_delay);
219         /* a text obj to display the name of the slice that owns that lease */
220         var slicelabel = paper.text (lease.attr("x")+lease.attr("width")/2,
221                                      lease.attr("y")+lease.attr("height")/2,slicename).attr(txt_slice);
222         /* hide it right away */
223         slicelabel.hide();
224         /* record it */
225         lease.label=slicelabel;
226         lease.hover ( function (e) { this.label.toFront(); this.label.show(); },
227                       function (e) { this.label.hide(); } ); 
228     },
229 }
230
231 function init_scheduler () {
232     // Grab the data
233     var data = [], axisx = [], axisy = [];
234     var table = $$("table#leases_data")[0];
235     // no reservable nodes - no data
236     if ( ! table) return;
237     // the nodenames
238     table.getElementsBySelector("tbody>tr>th").each(function (cell) {
239         axisy.push(getInnerText(cell));
240     });
241     // the timeslot labels
242     table.getElementsBySelector("thead>tr>th").each(function (cell) {
243         /* [0]: timestamp -- [1]: displayable*/
244         axisx.push(getInnerText(cell).split("&"));
245     });
246     // leases - expect colspan to describe length in grains
247     table.getElementsBySelector("tbody>tr>td").each(function (cell) {
248         data.push(new Array (getInnerText(cell),cell.colSpan));
249     });
250     // slicename : the upper-left cell
251     var scheduler = new Scheduler (getInnerText(table.getElementsBySelector("thead>tr>td")[0]), axisx, axisy, data);
252     table.hide();
253     // leases_area is a <div> created by slice.php as a placeholder
254     scheduler.init ("leases_area");
255
256     var submit=$$("button#leases_submit")[0];
257     submit.onclick = function () { scheduler.submit(); }
258     var clear=$$("button#leases_clear")[0];
259     clear.onclick = function () { scheduler.clear(); }
260
261     var node_button=$$("input#leases_mode_node")[0];
262     var timeslot_button=$$("input#leases_mode_timeslot")[0];
263     scheduler.init_mode ('timeslot',node_button,timeslot_button);
264
265 }
266
267 Event.observe(window, 'load', init_scheduler);