summaryrefslogtreecommitdiff
path: root/src/examples/rrdJFlotFilter.html
blob: 6b7f2169c842c01d7060781a9f63f1137efae6fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!--
 Example HTML/javascript file that display the
 content of a RRD archive file in a graph
 using the Flot libraries. This page adds two examples
 to RRDJFlot.html of using RRDFilterOp to apply operations
 (here, summing) to several or all of the DS in the RRD.
 Part of the javascriptRRD package
 Copyright (c) 2010 Frank Wuerthwein, fkw@ucsd.edu
                    Igor Sfiligoi, isfiligoi@ucsd.edu

 Original repository: http://javascriptrrd.sourceforge.net/
 
 MIT License [http://www.opensource.org/licenses/mit-license.php]

-->

<!--
 This page requires Flot.

 Repository: http://code.google.com/p/flot/

-->

<html>
  
    <script type="text/javascript" src="../lib/binaryXHR.js"></script>
    <script type="text/javascript" src="../lib/rrdFile.js"></script>

    <!-- rrdFlot class needs the following four include files !-->
    <script type="text/javascript" src="../lib/rrdFlotSupport.js"></script>
    <script type="text/javascript" src="../lib/rrdFlot.js"></script>
    <script type="text/javascript" src="../lib/rrdFile.js"></script>
    <script type="text/javascript" src="../lib/rrdFilter.js"></script>
    <script type="text/javascript" src="../../flot/jquery.js"></script>
    <script type="text/javascript" src="../../flot/jquery.flot.js"></script>
    <script type="text/javascript" src="../../flot/jquery.flot.selection.js"></script>
  <head>
    <title>RRD Graphs with Flot</title>
  </head>

  <body>
    <h1 id="title">RRD Graphs with Flot, with Filter Operations</h1>

    RRD URL:
    <input type="text" id="input_fname" value="example1.rrd"
           onchange="fname_update()">
     <button onclick="fname_update()">Update</button>
    <hr>

    <table id="infotable" border=1>
        <tr><td colspan="21"><b>Javascript needed for this page to work</b></td></tr>
	<tr><td><b>RRD file</b></td><td id="fname" colspan="5">None</td></tr>
    </table>

    <div id="mygraph"></div>

    <script type="text/javascript">

      // Remove the Javascript warning
      document.getElementById("infotable").deleteRow(0);

      // fname and rrd_data are the global variable used by all the functions below
      fname=document.getElementById("input_fname").value;
      rrd_data=undefined;

      // This function updates the Web Page with the data from the RRD archive header
      // when a new file is selected
      function update_fname() {
        // Finally, update the file name and enable the update button
        document.getElementById("fname").firstChild.data=fname;

        var graph_opts={legend: { noColumns:4}};
        var ds_graph_opts={'Oscilator':{ color: "#ff8000", 
                                         lines: { show: true, fill: true, fillColor:"#ffff80"} },
                           'Idle':{ label: 'IdleJobs', color: "#00c0c0", 
                                    lines: { show: true, fill: true} },
                           'Running':{color: "#000000",yaxis:2}};

        
        // the rrdFlot object creates and handles the graph
        var f=new rrdFlot("mygraph",rrd_data,graph_opts,ds_graph_opts);
      }

      // Next three functions are for use in RRDFilterOp, 
      // which requires a list of functions, one for each DS 

      //Do nothing to the DS 
      function DoNothing(ds) {
        this.getName = function() {return ds;}
        this.getDSNames = function() {return [ds];}
        this.computeResult = function(val_list) {return val_list[0];}
      }

      //Sum two DSs
      function SumDS(ds1,ds2) {
        this.getName = function() {return ds1+"+"+ds2;}
        this.getDSNames = function() {return [ds1,ds2];}
        this.computeResult = function(val_list) {return val_list[0]+val_list[1];}
      }

      //Sums multiple DSs from a given list
      function MultiSumDS(list) {
        this.getName = function() {return "SumTotal";}
        this.getDSNames = function() {
           var i = 0;
           var name_list=[];
           for(i=0;i<list.length;i++){
              name_list.push(list[i].getName());
           }
           return name_list;}
        this.computeResult = function(val_list) {
           var val_sum=0; 
           var i=0;
           for(i=0;i<val_list.length;i++){
              val_sum+=val_list[i];
           }
           return val_sum;
        }
      }

      // This is the callback function that,
      // given a binary file object,
      // verifies that it is a valid RRD archive
      // and performs the update of the Web page
      function update_fname_handler(bf) {
          var i_rrd_data=undefined;
          try {
            var i_rrd_data=new RRDFile(bf);            
          } catch(err) {
            alert("File "+fname+" is not a valid RRD archive!");
          }

          if (i_rrd_data!=undefined) {
            rrd_data=i_rrd_data;
          
            //If only one DS, cannot sum anything 
             if (rrd_data.getNrDSs()<2) {
               alert("Not enough elements ("+rrd_data.getNrDSs()+") in RRD to sum!");
             }
            
             
             var op_list = []; //list of operations
             var DS_list = []; //list of DSs to sum in MultiSumDS
             var i = 0;
 
             //create a new rrdlist, which contains all original elements
             //    (kept the same by DoNothing())
             //    plus additional operated-on DSs from RRDFilterOp 
             for (i=0;i<rrd_data.getNrDSs();i++) {
                op_list.push(new DoNothing(rrd_data.getDS(i).getName()));
                DS_list.push(rrd_data.getDS(i))
             }
             op_list.push(new MultiSumDS(DS_list));
             op_list.push(new SumDS(rrd_data.getDS(0).getName(),rrd_data.getDS(1).getName()));

             rrd_data = new RRDFilterOp(rrd_data,op_list);

             update_fname()
          }
      }

      // this function is invoked when the RRD file name changes
      function fname_update() {
        fname=document.getElementById("input_fname").value;
        try {
          FetchBinaryURLAsync(fname,update_fname_handler);
        } catch (err) {
           alert("Failed loading "+fname+"\n"+err);
        }
      }

    </script>
  </body>
</html>