Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigigraph / javascript / source / class / vigigraph / Application.js @ 849b1c45

History | View | Annotate | Download (38.3 KB)

1
/* ************************************************************************
2

3
   Vigigraph User Interface
4
   Copyright: 2007-2009 CS-SI <http://www.c-s.fr>
5

6
   License: GPLv2+
7

8
   Authors: Arnaud MAZIN <arnaud.mazin@c-s.fr>
9
            Aurelien BOMPARD <aurelien.bompard@c-s.fr>
10
            Thomas BURGUIERE <thomas.burguiere@c-s.fr>
11
            Francis LAHEUGUERE <francis.laheuguere@c-s.fr>
12

13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License as published by
15
   the Free Software Foundation; either version 2 of the License, or
16
   (at your option) any later version.
17
  
18
   This program is distributed in the hope that it will be useful,
19
   but WITHOUT ANY WARRANTY; without even the implied warranty of
20
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
   GNU General Public License for more details.
22
  
23
   You should have received a copy of the GNU General Public License
24
   along with this program; if not, write to the Free Software
25
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26

27
************************************************************************ */
28

    
29
/* ************************************************************************
30

31
//#require(qx.event.handler.DragAndDropHandler)
32
//#resource(vigigraph.image:image)
33

34
// List all static resources that should be copied into the build version,
35
// if the resource filter option is enabled (default: disabled)
36
//#embed(qx.icontheme/32/status/dialog-information.png)
37
//#embed(vigigraph.image/test.png)
38

39
************************************************************************ */
40

    
41
var urls = {
42
    "maingroups": "/rpc/maingroups",
43
    "hostgroups": "/rpc/hostgroups",
44
    "hosts": "/rpc/hosts",
45
    // NOTE Graph Groups (frontend) -> ServiceGroup (backend)
46
    "graphgroups": "/rpc/servicegroups",
47
    "graphs": "/rpc/graphs",
48
    "selectHostAndService": "/rpc/selectHostAndService",
49
    "searchHostAndService": "/rpc/searchHostAndService",
50
    "supPage": "/rpc/supPage",
51
    "getImage": "/rpc/getImage",
52
    "getStartTime": "/rpc/getStartTime",
53
    "graphsList": "/rpc/graphsList",
54
    "tempoDelayRefresh": "/rpc/tempoDelayRefresh",
55
    "getIndicators": "/rpc/getIndicators",
56
    "exportCSV": "/rpc/exportCSV"
57
};
58

    
59
/**
60
 * Main Application (based on the qooxdoo skeleton)
61
 */
62
qx.Class.define("vigigraph.Application",
63
{
64
  extend : qx.application.Gui,
65

    
66

    
67
  /*
68
  *****************************************************************************
69
     MEMBERS
70
  *****************************************************************************
71
  */
72

    
73
  members :
74
  {
75
    /**
76
     * Main function.
77
     *
78
     * @type member
79
     */
80
    main : function()
81
    {
82
      this.base(arguments);
83

    
84
      // Define alias for vigigraph resource path
85
      qx.io.Alias.getInstance().add("vigigraph", qx.core.Setting.get("vigigraph.resourceUri"));
86

    
87
      // Change the window title
88
      document.title = this.tr("Vigilo Graphic");
89

    
90
      // Host picker
91
      var w1 = new qx.ui.window.Window(this.tr("Host Picker"), "icon/16/devices/network-wired.png");
92
      w1.setSpace(20, 250, 48, 102);
93
      w1.setShowClose(false);
94
      w1.setShowMinimize(false);
95
      w1.setShowMaximize(false);
96
      w1.setResizable(false, false, false, false);
97

    
98
      var gl = new qx.ui.layout.GridLayout;
99
      gl.setDimension("auto", "auto");
100
      gl.setColumnCount(4);
101
      gl.setRowCount(6);                        //Number of rows in the main window
102
      gl.setVerticalSpacing(4);
103
      gl.setHorizontalSpacing(2);
104

    
105
      gl.setColumnWidth(0, 100);
106
      gl.setColumnWidth(1, 180);
107
      gl.setColumnWidth(2, 18);
108
      gl.setColumnWidth(3, 18);
109

    
110
      //gl.setColumnHorizontalAlignment(0, "right");
111
      gl.setColumnVerticalAlignment(0, "middle");
112

    
113
      gl.setRowHeight(0, 20);
114
      gl.setRowHeight(1, 20);
115
      gl.setRowHeight(2, 20);
116
      gl.setRowHeight(3, 20);
117
      gl.setRowHeight(4, 20);
118
      gl.setRowHeight(5, 20);
119

    
120
      gl.add(new qx.ui.basic.Label(this.tr("Main Group")), 0, 0);
121
      gl.add(new qx.ui.basic.Label(this.tr("Host Group")), 0, 1);
122
      gl.add(new qx.ui.basic.Label(this.tr("Host Name")), 0, 2);
123
      gl.add(new qx.ui.basic.Label(this.tr("Graph Group")), 0, 3);
124
      gl.add(new qx.ui.basic.Label(this.tr("Graph Name")), 0, 4);
125

    
126
      var combo1 = new qx.ui.form.ComboBox;
127
      var combo2 = new qx.ui.form.ComboBox;
128
      var combo3 = new qx.ui.form.ComboBox;
129
      var combo4 = new qx.ui.form.ComboBox;
130
      var combo5 = new qx.ui.form.ComboBox;
131

    
132
      var r1=new qx.ui.form.Button("","icon/16/actions/view-refresh.png");
133
      r1.setToolTip(new qx.ui.popup.ToolTip(this.tr("Refresh")));
134
      var r2=new qx.ui.form.Button("","icon/16/actions/view-refresh.png");
135
      r2.setToolTip(new qx.ui.popup.ToolTip(this.tr("Refresh")));
136
      var r3=new qx.ui.form.Button("","icon/16/actions/view-refresh.png");
137
      r3.setToolTip(new qx.ui.popup.ToolTip(this.tr("Refresh")));
138
      var r4=new qx.ui.form.Button("","icon/16/actions/view-refresh.png");
139
      r4.setToolTip(new qx.ui.popup.ToolTip(this.tr("Refresh")));
140
      var r5=new qx.ui.form.Button("","icon/16/actions/view-refresh.png");
141
      r5.setToolTip(new qx.ui.popup.ToolTip(this.tr("Refresh")));
142

    
143
      var b1=new qx.ui.form.Button("","icon/16/actions/zoom.png");
144
      b1.setToolTip(new qx.ui.popup.ToolTip(this.tr("Search")));
145
      var b3=new qx.ui.form.Button("","icon/16/actions/go-right.png");
146
      b3.setToolTip(new qx.ui.popup.ToolTip(this.tr("Show Nagios page")));
147
      b3.setEnabled(false);
148
      var b5=new qx.ui.form.Button("","icon/16/actions/go-right.png");
149
      b5.setToolTip(new qx.ui.popup.ToolTip(this.tr("Show graph")));
150
      b5.setEnabled(false);
151

    
152
      var bp=new qx.ui.form.Button("","icon/16/actions/document-print.png");
153
      bp.setToolTip(new qx.ui.popup.ToolTip(this.tr("Print Graph(s)")));
154
      bp.setEnabled(true);
155

    
156
      gl.add(r1,2,0);
157
      gl.add(r2,2,1);
158
      gl.add(r3,2,2);
159
      gl.add(r4,2,3);
160
      gl.add(r5,2,4);
161
      
162
      gl.add(b1,3,0);
163
      gl.add(b3,3,2);
164
      gl.add(b5,3,4);
165

    
166
      gl.add(bp,3,5);
167

    
168
      _updateServerGroupList();
169

    
170
      var w2 = undefined;
171

    
172
      function getIndexWindow (cdi, w)
173
      {
174
        // var cdi = qx.ui.core.ClientDocument.getInstance();
175
        var index_l = -1;
176
        if (cdi)
177
        {
178
          var nb = cdi.getChildren().length;
179
          for (i = 0; i < nb; i++)
180
          {
181
            var c_l = cdi.getChildren()[i];
182
            if (c_l)
183
            {
184
              if (c_l.name == "qx.ui.window.Window")
185
              {
186
                caption_l = c_l.getCaption();
187
                if (c_l == w)
188
                {
189
                  index_l = i;
190
                }
191
              }
192
            }
193
          }
194
        }
195
        return index_l;
196
      }
197

    
198
      function tempoFire(Delay)
199
      {
200
        var a = setTimeout(tempoFall, Delay);
201
      }
202

    
203
      function tempoFall()
204
      {
205
        if ( w2 != undefined)
206
        {
207
          w2.close();
208
        }
209
      }
210

    
211
      // Buttons
212
      bp.addEventListener("execute",function(e) {
213
        var nb = document.images.length;
214

    
215
        // liste src
216
        var index_p = -1;
217
        var src_l = "";
218
        var src_tab = new Array;
219
        var j = 0;
220
        for (i = 0; i < nb; i++)
221
        {
222
          src_l = document.images[i].src;
223

    
224
          // graphe rrd ?
225
          pos = src_l.indexOf("/rrdgraph.py");
226
          if (pos != -1)
227
          {
228
            src_tab[j] = encodeURIComponent(src_l);
229
            j += 1; 
230
          }
231
        }
232

    
233
        // impression Ă  partir nouvelle fenĂȘtre
234
        var lh = src_tab.length;
235
        if (lh > 0)
236
        {
237
          var url = urls.graphsList;
238
          url += "?";
239
          for (i = 0; i < lh; i++)
240
          {
241
            if (i > 0)
242
            {
243
              url += "&";
244
            }
245
            url += i;
246
            url += "=";
247
            url += src_tab[i];
248
          }
249

    
250
          w2 = window.open(url);
251
          w2.onload = function(){
252
            w2.print();
253
            wDelay = 1000;
254
            tempoFire(wDelay);
255
          }
256
        }
257
      });
258

    
259
      b3.addEventListener("execute",function(e) {
260
        var win = new qx.client.NativeWindow(urls.supPage+"?host="+combo3.getSelected().getLabel());
261
        win.setDimension(800,600);
262
        win.setDependent(false);
263
        win.open();
264
      });
265

    
266
      b5.addEventListener("execute",function(e) { 
267
        var host=combo3.getSelected().getLabel();
268
        var graph=combo5.getSelected().getLabel();
269
        this.openGraph(host, graph, null, null, true);
270
      }, this);
271

    
272
      b1.addEventListener("execute",function(e) { 
273
        var w_search = new qx.ui.window.Window(this.tr("Search"), "icon/16/actions/zoom.png");
274
        w_search.addToDocument();
275
        w_search.set({
276
                      showMinimize : false,
277
                      showMaximize : false,
278
                      resizable    : true
279
                     });
280
        var w_search_v = new qx.ui.layout.VerticalBoxLayout();
281
        w_search_v.setWidth("100%");
282
        w_search_v.setHeight("100%");
283
        w_search_v.setHorizontalChildrenAlign("center");
284
        w_search.add(w_search_v);
285
        var w_search_h = new qx.ui.layout.HorizontalBoxLayout();
286
        w_search_h.auto()
287
        w_search_h.setVerticalChildrenAlign("middle");
288
        w_search_h.setPadding(2, 5, 5, 5);
289
        w_search_v.add(w_search_h);
290
        w_search_h.add(new qx.ui.basic.Label(this.tr("Host:")));
291
        var search_host = new qx.ui.form.TextField();
292
        w_search_h.add(search_host);
293
        search_host.setMarginRight(5);
294
        search_host.setLiveUpdate(true);
295
        search_host.tabIndex = 1;
296
        search_host.setToolTip(new qx.ui.popup.ToolTip(this.tr("Search for a host (\"*\" for all, 100 max)")));
297
        w_search_h.add(new qx.ui.basic.Label(this.tr("Graph:")));
298
        var search_service = new qx.ui.form.TextField();
299
        w_search_h.add(search_service);
300
        search_service.setMarginRight(5);
301
        search_service.setLiveUpdate(true);
302
        search_service.tabIndex = 2;
303
        search_service.setToolTip(new qx.ui.popup.ToolTip(this.tr("Search for a graph (\"*\" for all, 100 max)")));
304
        var search_button=new qx.ui.form.Button("","icon/16/actions/zoom.png");
305
        search_button.setWidth(20);
306
        search_button.setHeight(20);
307
        w_search_h.add(search_button);
308
        var search_results_model = new qx.ui.table.model.Simple();
309
        search_results_model.setColumns([ this.tr("Host"), this.tr("Graph") ]);
310
        var custom = {
311
          tableColumnModel :
312
            function(obj)
313
            {
314
              return new qx.ui.table.columnmodel.Resize(obj);
315
            }
316
        };
317
        var search_results = new qx.ui.table.Table(search_results_model, custom);
318
        search_results.hide();
319
        with (search_results) {
320
          set({ left:0, top:0, right:0, bottom: 10, width:"95%", height:"90%", border:"inset-thin" });
321
          //setMetaColumnCounts([1, -1]);
322
          getSelectionModel().setSelectionMode(qx.ui.table.selection.Model.SINGLE_SELECTION);
323
          setColumnVisibilityButtonVisible(false);
324
          setStatusBarVisible(false);
325
          var tcm = search_results.getTableColumnModel();
326
          var resizeBehavior = tcm.getBehavior();
327
          // This uses the set() method to set all attriutes at once; uses flex
328
          resizeBehavior.set(0, { width:"1*", minWidth:40, maxWidth:180  });
329
          resizeBehavior.set(1, { width:"1*", minWidth:40, maxWidth:180  });
330

    
331
          //// We could also set them individually:
332
          ////resizeBehavior.setWidth(1, "50%");
333
          ////resizeBehavior.setMinWidth(1, 100);
334
          ////resizeBehavior.setMaxWidth(1, 320);
335
        };
336
        w_search_v.add(search_results);
337
        //search_results.setLocation(10, 40);
338
        function _searchResultsUpdater(host,service)
339
        {
340
          var url = urls.searchHostAndService;
341
          if (host && service) { url = url+"?host="+host+"&service="+service };
342
          else if (host) { url = url+"?host="+host };
343
          else if (service) { url = url+"?service="+service };
344

    
345
          search_results_model.setData([]);
346
          var g=new qx.io.remote.Request(url,"GET","application/json");
347
          g.addEventListener("completed", function(e) {
348
            var rowData = [];
349
            r=e.getContent().items;
350
            for(var i=0,l=r.length ; i<l ; i++) // does not work in IE with "for (i in r)"
351
            {
352
              rowData.push(r[i]);
353
            }
354
            search_results_model.setData(rowData);
355
            search_results.show();
356
            //qx.log.Logger.ROOT_LOGGER.debug(rowData);
357
          });
358
          g.send();
359
        }
360
        function _chooseInCombos(host, host_main_group, host_sec_group, service, service_group) {
361
          function _selectItem(o, item) {
362
            var c_list = o.getList();
363
            var c_item = c_list.findStringExact(item);
364

    
365
            if (c_item) {
366
              o.setSelected(c_item);
367
            }
368
          }
369
          combo1.addEventListener("changeEnabled", function(e) { 
370
            if (e.getValue() == true) {
371
              e.getTarget().removeEventListener("changeEnabled", arguments.callee);
372
              _selectItem(e.getTarget(), host_main_group);
373
            }
374
          });
375
          combo2.addEventListener("changeEnabled", function(e) { 
376
            if (e.getValue() == true) {
377
              e.getTarget().removeEventListener("changeEnabled", arguments.callee);
378
              _selectItem(e.getTarget(), host_sec_group);
379
            }
380
          });
381
          combo3.addEventListener("changeEnabled", function(e) { 
382
            if (e.getValue() == true) {
383
              e.getTarget().removeEventListener("changeEnabled", arguments.callee);
384
              _selectItem(e.getTarget(), host); 
385
            }
386
          });
387
          if ((service) && (service_group)) {
388
            combo4.addEventListener("changeEnabled", function(e) {
389
              if (e.getValue() == true) {
390
                e.getTarget().removeEventListener("changeEnabled", arguments.callee);
391
                _selectItem(e.getTarget(), service_group); 
392
              }
393
            });
394
            combo5.addEventListener("changeEnabled", function(e) {
395
              if (e.getValue() == true) {
396
                e.getTarget().removeEventListener("changeEnabled", arguments.callee);
397
                _selectItem(e.getTarget(), service); 
398
              }
399
            });
400
          }
401
          _updateServerGroupList();
402
        }
403
        function _selectHostAndService(host, service, mainObj)
404
        {
405
          var url = urls.selectHostAndService;
406
          if (host && service) { url = url+"?host="+host+"&service="+service };
407
          else if (host) { url = url+"?host="+host };
408
          else if (service) { url = url+"?service="+service };
409

    
410
          var g=new qx.io.remote.Request(url,"GET","application/json");
411
          g.addEventListener("completed", function(e) {
412
            r=e.getContent().items;
413
            var host_main_group = r[0];
414
            var host_sec_group = r[1];
415

    
416
            if (r.length < 3) { 
417
              _chooseInCombos(host, host_main_group, host_sec_group, null, null);
418
            }
419
            else {
420
              var srv_group = r[2];
421
              _chooseInCombos(host, host_main_group, host_sec_group, service, srv_group);
422
              mainObj.openGraph(host, service, null, null, true);
423
            }
424
            //qx.log.Logger.ROOT_LOGGER.debug(rowData);
425
          });
426
          g.send();
427
        }
428
        // On selection, update the Host Picker and close the search window
429
        search_results.addEventListener("cellClick", function(e) {
430
          var row = e.getRow();
431
          var col = e.getColumn();
432
          var value_host = search_results_model.getValue(0, row);
433
          var value_service = search_results_model.getValue(1, row);
434
          if (!value_service) {
435
            _selectHostAndService(value_host, null, this);
436
          } else {
437
            _selectHostAndService(value_host, value_service, this);
438
          }
439
        }, this);
440
        // Give keyboard focus to the search field
441
        search_host.addEventListener("appear", function(e)
442
        {
443
          this.getInputElement().focus();
444
        }, search_host);
445
        // Submit events
446
        search_host.addEventListener("keydown", function(e) {
447
          if (e.getKeyIdentifier() == "Enter") {
448
            _searchResultsUpdater(search_host.getValue(), search_service.getValue());
449
          }
450
        });
451
        search_host.addEventListener("changeValue", function(e) {
452
          _searchResultsUpdater(search_host.getValue(), search_service.getValue());document.body.outerHTML
453
        });
454
        search_service.addEventListener("keydown", function(e) {
455
          if (e.getKeyIdentifier() == "Enter") {
456
            _searchResultsUpdater(search_host.getValue(), search_service.getValue());
457
          }
458
        });
459
        search_service.addEventListener("changeValue", function(e) {
460
          _searchResultsUpdater(search_host.getValue(), search_service.getValue());
461
        });
462
        // Submit button
463
        search_button.addEventListener("execute",function(e) {
464
          _searchResultsUpdater(search_host.getValue(), search_service.getValue());
465
        });
466
        w_search.open();
467
        w_search.setTop(w1.getTop());document.body.outerHTML
468
        w_search.setLeft(qx.html.Location.getPageBoxRight(w1.getElement()));
469
        // centerToBrowser does not work on window objects (breaks the buttons in the title bar)
470
        //w_search.centerToBrowser()
471
      }, this);
472
      // end of b1
473
      //////////////////////////////////////
474

    
475

    
476
      gl.add(combo1, 1, 0);
477
      gl.add(combo2, 1, 1);
478
      gl.add(combo3, 1, 2);
479
      gl.add(combo4, 1, 3);
480
      gl.add(combo5, 1, 4);
481
      w1.add(gl);
482
      w1.open();
483
      w1.addToDocument();
484

    
485
      function _genericListUpdater(url,combobox)
486
      {
487
        var g=new qx.io.remote.Request(url,"GET","application/json");
488
        g.addEventListener("completed", function(e) {
489
          qx.log.Logger.ROOT_LOGGER.debug("AJAX call completed: "+url);
490
          combobox.setEnabled(false);
491
          combobox.getList().removeAll();
492
          r=e.getContent().items;
493
          for(var i = 0 ; i < r.length ; i++) // does not work in IE with "for (i in r)"
494
          {
495
            //var label = r[i][0];
496
            //var id = r[i][1];
497
            combobox.add(new qx.ui.form.ListItem(r[i][0], null, r[i][1]));
498
          }
499
          combobox.setSelected(null);
500
          combobox.setEnabled(true);
501
        });
502
        g.send();
503
      }
504

    
505
      function _updateServerGroupList()
506
      {
507
        _genericListUpdater(urls.maingroups,combo1);
508
        combo2.getList().removeAll();
509
        combo2.setSelected(null);
510
        combo3.getList().removeAll();
511
        combo3.setSelected(null);
512
        combo4.getList().removeAll();
513
        combo4.setSelected(null);
514
        combo5.getList().removeAll();
515
        combo5.setSelected(null);
516
        b3.setEnabled(false);
517
        b5.setEnabled(false);
518
        r2.setEnabled(false);
519
        r3.setEnabled(false);
520
        r4.setEnabled(false);
521
        r5.setEnabled(false);
522
      }
523
      function _updateHostGroupList(maingroupid)
524
      {
525
        _genericListUpdater(urls.hostgroups+"?maingroupid="+maingroupid,combo2);
526
        combo3.getList().removeAll();
527
        combo3.setSelected(null);
528
        combo4.getList().removeAll();
529
        combo4.setSelected(null);
530
        combo5.getList().removeAll();
531
        combo5.setSelected(null);
532
        b3.setEnabled(false);
533
        b5.setEnabled(false);
534
        r2.setEnabled(true);
535
        r3.setEnabled(false);
536
        r4.setEnabled(false);
537
        r5.setEnabled(false);
538
      }
539
      function _updateHostList(othergroupid)
540
      {
541
        _genericListUpdater(urls.hosts+"?othergroupid="+othergroupid,combo3);
542
        combo4.getList().removeAll();
543
        combo4.setSelected(null);
544
        combo5.getList().removeAll();
545
        combo5.setSelected(null);
546
        b3.setEnabled(false);
547
        b5.setEnabled(false);
548
        r3.setEnabled(true);
549
        r4.setEnabled(false);
550
        r5.setEnabled(false);
551
      }
552
      function _updateGraphGroupList(idhost)
553
      {
554
        _genericListUpdater(urls.graphgroups+"?idhost="+idhost,combo4);
555
        combo5.getList().removeAll();
556
        combo5.setSelected(null);
557
        r4.setEnabled(true);
558
        r5.setEnabled(false);
559
        b5.setEnabled(false);
560
      }
561
      function _updateGraphList(idservice)
562
      {
563
        _genericListUpdater(urls.graphs+"?idservice="+idservice,combo5);
564
        r5.setEnabled(true);
565
        b5.setEnabled(false);
566
      }
567
      combo1.addEventListener("changeSelected", function(e) { if(e.getValue()) _updateHostGroupList(e.getValue().getValue()); });
568
      combo2.addEventListener("changeSelected", function(e) { if(e.getValue()) _updateHostList(e.getValue().getValue()); });
569
      combo3.addEventListener("changeSelected", function(e) { if(e.getValue()) { b3.setEnabled(true);_updateGraphGroupList(e.getValue().getValue());} });
570
      combo4.addEventListener("changeSelected", function(e) { if(e.getValue()) { _updateGraphList(e.getValue().getValue());} });
571
      //combo4.addEventListener("changeSelected", function(e) { if(e.getValue()) { _updateGraphList(combo3.getSelected().getLabel(),e.getValue().getLabel());} });
572
      combo5.addEventListener("changeSelected", function(e) { if(e.getValue()) b5.setEnabled(true); });
573

    
574
      r1.addEventListener("execute",function(e) { _updateServerGroupList();});
575
      r2.addEventListener("execute",function(e) { _updateHostGroupList(combo1.getSelected().getLabel());});
576
      r3.addEventListener("execute",function(e) { _updateHostList(combo2.getSelected().getLabel());});
577
      r4.addEventListener("execute",function(e) { _updateGraphGroupList(combo3.getSelected().getLabel());});
578
      r5.addEventListener("execute",function(e) { _updateGraphList(combo3.getSelected().getLabel(),combo4.getSelected().getLabel());});
579
      // Restore previously opened windows
580
      var state = qx.client.History.getInstance().getState();
581
      qx.log.Logger.ROOT_LOGGER.debug("state: "+state);
582
      if (state != "") {
583
        var windows;
584
        if (state.match(/\+/)) { // More than one windows opened
585
          windows = state.split(/\+/);
586
        } else {
587
          windows = new Array(state);
588
        }
589
        for (var i=0, l=windows.length; i<l; i++) {
590
          var item = windows[i];
591
          if (!item.match(/;/)) { qx.log.Logger.ROOT_LOGGER.debug("skipping graph: "+item); continue; }
592
          var item_a = item.split(/;/);
593
          var host, graph, left, top;
594
          host = item_a[0];
595
          graph = decodeURIComponent(item_a[1]);
596
          wleft = item_a[2];
597
          wtop = item_a[3];
598
          qx.log.Logger.ROOT_LOGGER.debug("restoring graph: "+host+";"+graph+";"+wleft+";"+wtop);
599
          this.openGraph(host, graph, wleft, wtop, false);
600
        }
601
      }
602
    },
603

    
604

    
605
    /**
606
     * Graph-opening function
607
     *
608
     * @type member
609
     */
610

    
611
    openGraph: function(host, graph, wleft, wtop, add_to_history)
612
    {
613
      //  position - sauvegarde valeurs courantes
614
      var wleft_c = wleft;
615
      var wtop_c = wtop;
616

    
617
      var url;
618
      var start;
619
      var duration;
620
      var fakeIncr=0;
621
      // Create UI elements
622
      var w=new qx.ui.window.Window(this.tr("\"%1\" Graph for host %2", graph, host), "icon/16/apps/accessories-time-tracking.png");
623
      var l=new qx.ui.layout.FlowLayout;
624
      l.set({"paddingTop": 26 });
625
      var l2=new qx.ui.layout.HorizontalBoxLayout;
626
      var bt_refresh=new qx.ui.form.Button("","icon/16/actions/view-refresh.png");
627
      bt_refresh.setToolTip(new qx.ui.popup.ToolTip(this.tr("Reload graph")));
628
      bt_refresh.setBorder("outset");
629
      // Timeframe menu
630
      var time_menu = new qx.ui.menu.Menu();
631
      var timeframes = [ 
632
        [12, this.tr("Last 12 hours")],
633
        [24, this.tr("Last 24 hours")],
634
        [48, this.tr("Last 48 hours")],
635
        [7*24, this.tr("Last 7 days")],
636
        [14*24, this.tr("Last 14 days")],
637
        [3*31*24, this.tr("Last 3 months")],
638
        [183*24, this.tr("Last 6 months")],
639
        [365*24, this.tr("Last year")]
640
      ];
641
      for (var i = 0, item; item = timeframes[i]; i++) {
642
          var menu_bt = new qx.ui.menu.Button(item[1]);
643
          menu_bt.title = item[0]
644
          menu_bt.addEventListener("execute",function(e) { setTail(this.title); });
645
          time_menu.add(menu_bt);
646
      }
647
      time_menu.addToDocument();
648
      var time_menu_bt = new qx.ui.form.Button(this.tr("Timeframe"),"icon/16/actions/history.png");
649
      time_menu_bt.setToolTip(new qx.ui.popup.ToolTip(this.tr("Timeframe menu")));
650
      // Browse buttons
651
      var bt_first=new qx.ui.form.Button("","icon/16/actions/start.png");
652
      bt_first.setToolTip(new qx.ui.popup.ToolTip(this.tr("Graph start")));
653
      var bt_prev=new qx.ui.form.Button("","icon/16/actions/go-previous.png");
654
      bt_prev.setToolTip(new qx.ui.popup.ToolTip(this.tr("Last section")));
655
      var bt_next=new qx.ui.form.Button("","icon/16/actions/go-next.png");
656
      bt_next.setToolTip(new qx.ui.popup.ToolTip(this.tr("Next section")));
657
      var bt_last=new qx.ui.form.Button("","icon/16/actions/dialog-finish.png");
658
      bt_last.setToolTip(new qx.ui.popup.ToolTip(this.tr("Graph end")));
659
      var bt_zoomin=new qx.ui.form.Button("","icon/16/actions/zoom-in.png");
660
      bt_zoomin.setToolTip(new qx.ui.popup.ToolTip(this.tr("Zoom in")));
661
      var bt_zoomout=new qx.ui.form.Button("","icon/16/actions/zoom-out.png");
662
      bt_zoomout.setToolTip(new qx.ui.popup.ToolTip(this.tr("Zoom out")));
663
      var bt_print=new qx.ui.form.Button("","icon/16/actions/document-print.png");
664
      bt_print.setToolTip(new qx.ui.popup.ToolTip(this.tr("Print Graph")));
665
      var h1=new qx.ui.layout.HorizontalBoxLayout;
666

    
667
      // Export Menu
668
      var indicator_menu = new qx.ui.menu.Menu();
669
      indicator_menu.addToDocument();
670
      var indicator_menu_bt = new qx.ui.form.Button(this.tr("Export CSV"),"icon/16/actions/document-export.png");
671
      indicator_menu_bt.setToolTip(new qx.ui.popup.ToolTip(this.tr("Export CSV")));
672

    
673
      w.setDimension("auto", "auto");
674
      w.setShowMinimize(false);
675
      w.setShowMaximize(false);
676
      w.setResizable(false, false, false, false);
677

    
678
      function setUrl(start,duration)
679
      {
680
        url= urls.getImage+"?host="+encodeURIComponent(host)+"&start="+start+"&duration="+duration+"&graph="+encodeURIComponent(graph);
681
        qx.log.Logger.ROOT_LOGGER.debug(url);
682
      }
683
      function loadImage(myUrl,o)
684
      {
685
        o.removeAll();
686
        var i=new qx.io.remote.Request(url,"GET","text/plain");
687
        i.addEventListener("completed", function(e) { 
688
          img = e.getContent();
689
          if (img)
690
          {
691
            o.add(new qx.ui.basic.Image(e.getContent()));
692
          }
693
        });
694
        i.send();
695
      }
696
      function getTime() // we use a function because the window can be opened a long time without reloading
697
      {
698
        var now_obj = new Date();
699
        return parseInt(now_obj.getTime() / 1000);
700
      }
701
      function updateGraphOnStartTime()
702
      {
703
        var url= urls.getStartTime+"?host="+encodeURIComponent(host);
704
        var g=new qx.io.remote.Request(url,"GET","text/plain");
705
        g.addEventListener("completed", function(e) { 
706
          start = parseInt(e.getContent());
707
          setStep(start);
708
          bt_first.setEnabled(false); 
709
          bt_prev.setEnabled(false);
710
        });
711
        g.send();
712
      }
713
      function getIndexWindow (cdi, w)
714
      {
715
        // var cdi = qx.ui.core.ClientDocument.getInstance();
716
        var index_l = -1;
717
        if (cdi)
718
        {
719
          var nb = cdi.getChildren().length;
720
          for (i = 0; i < nb; i++)
721
          {
722
            var c_l = cdi.getChildren()[i];
723
            if (c_l)
724
            {
725
              if (c_l.name == "qx.ui.window.Window")
726
              {
727
                caption_l = c_l.getCaption();
728
                if (c_l == w)
729
                {
730
                  index_l = i;
731
                }
732
              }
733
            }
734
          }
735
        }
736
        return index_l;
737
      }
738

    
739
      // timer pour rafraichissement
740
      var delay = 5*60*1000;
741
      var t = undefined;
742
      function tempoFireRefresh()
743
      {
744
        t = setTimeout(tempoFallRefresh, delay);
745
      }
746
      function tempoFallRefresh()
747
      {
748
        var now = getTime();
749
        start = now - 24 * 3600;
750
        duration = 24 * 3600;
751
        setUrl(start, duration);
752
        loadImage(url,l);
753

    
754
        bt_first.setEnabled(true);
755
        bt_prev.setEnabled(true);
756
        bt_next.setEnabled(true);
757
        bt_last.setEnabled(true);
758
        bt_zoomin.setEnabled(true);
759
        bt_zoomout.setEnabled(true);
760

    
761
        // rafraichissement periodique
762
        tempoFireRefresh();
763
      }
764
      function tempoClearRefresh()
765
      {
766
        if (t != undefined)
767
        {
768
          clearTimeout(t);
769
        }
770
      }
771
      function tempoDelayRefresh()
772
      {
773
        var url= urls.tempoDelayRefresh;
774
        var r = new qx.io.remote.Request(url,"GET","text/plain");
775
        r.addEventListener("completed", function(e) { 
776
          delay = e.getContent();
777
        });
778
        r.send();
779
      }
780
      function getIndicators(graph)
781
      {
782
        var url= urls.getIndicators+"?graph="+graph;
783
        var r = new qx.io.remote.Request(url,"GET","application/json");
784
        r.addEventListener("completed", function(e) { 
785
          r = e.getContent().items;
786
          var txt = "";
787
          for(var i = 0; i <= r.length; i++)
788
          {
789
            txt = "All";
790
            if (i < r.length)
791
            {
792
              txt = r[i][0];
793
            }
794
            var menu_bt = new qx.ui.menu.Button(txt);
795
            menu_bt.title = txt
796
            menu_bt.addEventListener("execute",function(e) { getExport(this.title); });
797
            indicator_menu.add(menu_bt);
798
          }
799
        });
800
        r.send();
801
      }
802

    
803
      // Default
804
      var now = getTime();
805
      start = now - 24 * 3600;
806
      duration = 24 * 3600;
807
      setUrl(start, duration);
808
      loadImage(url,l);
809
      getIndicators(graph);
810

    
811
      // Events
812
      tempoDelayRefresh();
813
      bt_refresh.addEventListener("execute",function(e) {
814
        var state = "activated";
815
        if (bt_refresh.hasState(state))
816
        {
817
          // bouton actif -> on le rend desactif
818
          bt_refresh.setBorder("outset");
819
          bt_refresh.removeState(state);
820
          // liberation timer
821
          tempoClearRefresh();
822
        }
823
        else
824
        {
825
          // bouton desactif -> on le rend actif
826
          bt_refresh.setBorder("inset");
827
          bt_refresh.addState(state);
828
          loadImage(url,l);
829
          // armement timer pour rafraichissement periodique
830
          tempoFireRefresh();
831
        }
832
      });
833

    
834
      bt_print.addEventListener("execute",function(e) {
835
        var w3 = undefined;
836

    
837
        function tempoFirePrint(Delay)
838
        {
839
          var a = setTimeout(tempoFallPrint, Delay);
840
        }
841

    
842
        function tempoFallPrint()
843
        {
844
          if ( w3 != undefined)
845
          {
846
            w3.close();
847
          }
848
        }
849

    
850
        var nb = document.images.length;
851

    
852
        // liste src
853
        var index_p = -1;
854
        var src_l = "";
855
        var src_tab = new Array;
856
        var j = 0;
857
        for (i = 0; i < nb; i++)
858
        {
859
          src_l = document.images[i].src;
860

    
861
          // correspondance selon parametres ?
862
          pos = src_l.indexOf("/rrdgraph.py");
863
          if (pos != -1)
864
          {
865
            pos = src_l.indexOf(host);
866
            if (pos != -1)
867
            {
868
              var graph_l = graph;
869
              var sc = " ";
870
              var rc = "+";
871
              while (graph_l.indexOf(sc) > 0)
872
              {
873
                graph_l = graph_l.replace(sc, rc);
874
              }
875

    
876
              var src_d = decodeURIComponent(src_l);
877
              pos = src_d.indexOf(graph_l);
878
            }
879
            if (pos != -1)
880
            {
881
              pos = src_l.indexOf(start);
882
            }
883
            if (pos != -1)
884
            {
885
              pos = src_l.indexOf(duration);
886
            }
887
            if (pos != -1)
888
            {
889
              src_tab[j] = encodeURIComponent(src_l);
890
              j += 1; 
891
            }
892
          }
893
        }
894

    
895
        // impression Ă  partir nouvelle fenĂȘtre
896
        var lh = src_tab.length;
897
        if (lh > 0)
898
        {
899
          var url = urls.graphsList;
900
          url += "?";
901
          for (i = 0; i < lh; i++)
902
          {
903
            if (i > 0)
904
            {
905
              url += "&";
906
            }
907
            url += i;
908
            url += "=";
909
            url += src_tab[i];
910
          }
911

    
912
          w3 = window.open(url);
913
          w3.onload = function(){
914
            w3.print();
915
            wDelay = 1000;
916
            tempoFirePrint(wDelay);
917
          }
918
        }
919
      });
920

    
921
      time_menu_bt.addEventListener("click", function(e)
922
      {
923
        if ( time_menu.isSeeable() )
924
        {
925
          time_menu.hide();
926
        }
927
        else
928
        {
929
          var el = this.getElement();
930
          time_menu.setLeft(qx.html.Location.getPageBoxLeft(el));
931
          time_menu.setTop(qx.html.Location.getPageBoxBottom(el));
932
          time_menu.show();
933
        }
934
        e.setPropagationStopped(true);
935
      });
936
      time_menu_bt.addEventListener("mousedown", function(e) { e.setPropagationStopped(true); });
937

    
938
      function setTail(hours)
939
      {
940
        now = getTime();
941
        start=now-hours*3600; 
942
        duration=hours*3600; 
943

    
944
        setUrl(start,duration);
945
        loadImage(url,l);
946
        bt_first.setEnabled(true);
947
        bt_prev.setEnabled(true);
948
        bt_zoomin.setEnabled(true);
949
        bt_zoomout.setEnabled(true);
950
      }
951
      function setStep(start_ts)
952
      {
953
        start = start_ts;
954
        setUrl(start, duration);
955

    
956
        loadImage(url,l);
957
        bt_first.setEnabled(true);
958
        bt_prev.setEnabled(true);
959
        bt_next.setEnabled(true);
960
        bt_last.setEnabled(true);
961
        bt_zoomin.setEnabled(true);
962
        bt_zoomout.setEnabled(true);
963
      }
964

    
965
      bt_first.addEventListener("execute", function(e) { updateGraphOnStartTime() });
966
      bt_prev.addEventListener("execute", function(e) { setStep(start-duration); });
967
      bt_next.addEventListener("execute", function(e) { setStep(start+duration); });
968
      bt_last.addEventListener("execute", function(e) { 
969
        now = getTime(); 
970
        setStep(now-duration); 
971
      });
972
      bt_zoomin.addEventListener("execute", function(e) { duration = duration / 2; setStep(start+duration/2); });
973
      bt_zoomout.addEventListener("execute", function(e) { 
974
        start = start - duration / 2; 
975
        duration = duration * 2; 
976
        setStep(start); 
977
      });
978

    
979
      indicator_menu_bt.addEventListener("click", function(e)
980
      {
981
        if ( indicator_menu.isSeeable() )
982
        {
983
          indicator_menu.hide();
984
        }
985
        else
986
        {
987
          var el = this.getElement();
988
          indicator_menu.setLeft(qx.html.Location.getPageBoxLeft(el));
989
          indicator_menu.setTop(qx.html.Location.getPageBoxBottom(el));
990
          indicator_menu.show();
991
        }
992
        e.setPropagationStopped(true);
993
      });
994
      indicator_menu_bt.addEventListener("mousedown", function(e) { e.setPropagationStopped(true); });
995

    
996
      function getExport(indicator)
997
      {
998
        var w4 = undefined;
999
        if (indicator != "")
1000
        {
1001
          var end = start + duration;
1002
          var url= urls.exportCSV+"?host="+encodeURIComponent(host)+"&graph="+graph+"&indicator="+indicator+"&start="+start+"&end="+end;
1003
          w4 = window.open(url);
1004
          w4.onload = function(){
1005
          }
1006
        }
1007
      }
1008

    
1009
      // Setup the UI
1010
      h1.add(bt_refresh);
1011
      h1.add(time_menu_bt);
1012
      h1.add(bt_first);
1013
      h1.add(bt_prev);
1014
      h1.add(bt_next);
1015
      h1.add(bt_last);
1016
      h1.add(bt_zoomin);
1017
      h1.add(bt_zoomout);
1018
      h1.add(indicator_menu_bt);
1019
      h1.add(bt_print);
1020
      h1.pack();
1021
      l2.add(l);
1022
      l2.add(h1);
1023
      w.add(l2);
1024

    
1025
      w.addEventListener("appear", function(e) {
1026
        wleft_c = this.getLeft();
1027
        wtop_c = this.getTop();
1028
      });
1029

    
1030
      w.addEventListener("disappear", function(e) {
1031
        var state = qx.client.History.getInstance().getState();
1032

    
1033
        // parametres courants
1034
        var cp = host+";"+encodeURIComponent(graph)+";"+wleft_c+";"+wtop_c+"+";
1035
        var pos = state.indexOf(cp);
1036
        if (pos >= 0)
1037
        {
1038
          // actualisation etat
1039
          var state = state.replace(cp, "");
1040
          qx.client.History.getInstance().addToHistory(state, this.tr("Vigilo Graphic"));
1041
        }
1042

    
1043
        // pour rafraichissement
1044
        var index_l = -1;
1045
        var cdi = qx.ui.core.ClientDocument.getInstance();
1046
        index_l = getIndexWindow(cdi, w);
1047
        if (index_l != -1)
1048
        {
1049
          // suppression element courant
1050
          c_l = cdi.getChildren()[index_l];
1051
          cdi.remove(c_l);
1052
        }
1053
      });
1054
      w._captionBar.addEventListener("mouseup", function(e) { 
1055
        var state = qx.client.History.getInstance().getState();
1056

    
1057
        var wleft = this.getLeft();
1058
        var wtop = this.getTop();
1059

    
1060
        // parametres courants et actuels
1061
        var cp = host+";"+encodeURIComponent(graph)+";"+wleft_c+";"+wtop_c+"+";
1062
        var ap = host+";"+encodeURIComponent(graph)+";"+wleft+";"+wtop+"+";
1063

    
1064
        var pos = state.indexOf(cp);
1065
        if (pos >= 0)
1066
        {
1067
          // actualisation etat
1068
          var state = state.replace(cp, ap);
1069
          qx.client.History.getInstance().addToHistory(state, this.tr("Vigilo Graphic"));
1070
          wleft_c = wleft;
1071
          wtop_c = wtop;
1072
        }
1073
      }, w);
1074
      w.addToDocument();
1075
      w.open();
1076
      wleft = parseInt(wleft);
1077
      wtop = parseInt(wtop);
1078
      if (wleft) { 
1079
        w.setLeft(wleft); 
1080
      } else {
1081
        // w.setRight() makes the close button fixed on the page even if you move the window
1082
        w.setLeft(qx.ui.core.ClientDocument.getInstance().getClientWidth() - 600);
1083
      }
1084
      if (wtop) { 
1085
        w.setTop(wtop); 
1086
      } else {
1087
        w.setTop(5);
1088
      }
1089

    
1090
      // Add to history if we are opening a new window (not on restore)
1091
      if (add_to_history) {
1092
        var state = qx.client.History.getInstance().getState();
1093
        var wleft = w.getLeft();
1094
        if (!wleft) { wleft = 0; }
1095
        var wtop = w.getTop();
1096
        if (!wtop) { wtop = 0; }
1097
        state += host+";"+encodeURIComponent(graph)+";"+wleft+";"+wtop+"+";
1098
        qx.client.History.getInstance().addToHistory(state, this.tr("Vigilo Graphic"));
1099
        qx.log.Logger.ROOT_LOGGER.debug("state: "+state);
1100
      }
1101
    },
1102
    
1103
        
1104
    /**
1105
     * On window close (verbatim from qooxdoo's skeleton)
1106
     *
1107
     * @type member
1108
     */
1109
    close : function()
1110
    {
1111
      this.base(arguments);
1112

    
1113
      // Prompt user
1114
      // return "Do you really want to close the application?";
1115
    },
1116

    
1117

    
1118
    /**
1119
     * Destructor (verbatim from qooxdoo's skeleton)
1120
     *
1121
     * @type member
1122
     */
1123
    terminate : function() {
1124
      this.base(arguments);
1125
    }
1126
  },
1127

    
1128

    
1129

    
1130

    
1131
  /*
1132
  *****************************************************************************
1133
     SETTINGS
1134
  *****************************************************************************
1135
  */
1136

    
1137
  settings : {
1138
    "vigigraph.resourceUri" : "./resource"
1139
  }
1140
});
1141

    
1142

    
1143
// vim: set tabstop=2 shiftwidth=2 expandtab :