summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-livestats/htdocs/luci-static/resources/livestats/GraphRPC.js
blob: 9ac291fd02308d8f50f3059f3f94f6f5ded510c2 (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
176
177
178
179
function Graph(container, id, options, transform) {
	if( !options ) options = { };

	this.id        = id;
	this.cols      = 100;
	this.type      = "line";
	this.options   = options;
	this.transform = transform;
	this.dataset   = {};

	var graph = document.createElement('div');
	var label = document.createElement('h2');
		label.innerHTML = options.title
			? options.title.replace("%s", id ) : id;

	container.appendChild( label );
	container.appendChild( graph );

	this.canvas = document.createElement('canvas');
	graph.appendChild( this.canvas );

	this.canvas.id = id;
	this.canvas.width  = ( options.width  || graph.offsetWidth - 20 );
	this.canvas.height = ( options.height || 300 );
}

Graph.prototype.addDataset = function(name, ds) {
	if( window.console ) console.debug("AddDataset: " + name);
	if( !this.layout ) {
		this.layout = new PlotKit.Layout( this.type, this.options );
	}

	if( !ds ) {
		ds = new Array();
		for( var i = 0; i < this.cols; i++ )
			ds[i] = new Array( i, 0 );
	}

	this.dataset[name] = ds;
	this.layout.addDataset(name, ds);
}

Graph.prototype.updateDataset = function(name, value) {
	if( window.console ) console.debug("UpdateDataset: " + name + " " + value);
	if( this.dataset[name] ) {
		var ds = this.dataset[name];

		for( var i = 1; i < this.cols; i++ )
			ds[i-1][1] = ds[i][1];

		value = Math.abs( parseFloat(value) || 0 );

		if( this.transform ) {
			value = ( ds[this.cols-1][1] > 0 )
				? this.transform(value, ds[this.cols-1][1]) : 0.01;
		}

		ds[this.cols-1][1] = value;
		this.layout.addDataset(name, ds);
	}
}

Graph.prototype.draw = function( options ) {
	if( this.layout ) {
		this.plotter = new PlotKit.CanvasRenderer(
			this.canvas, this.layout, this.options || options || {}
		);

		this.layout.evaluate();
		this.plotter.render();
	}
}

Graph.prototype.redraw = function() {
	if( this.layout && this.plotter ) {
		this.layout.evaluate();
		this.plotter.clear();
		this.plotter.render();
	}
}


function GraphRPC(container, uri, action, interval, datasources, options, transform) {
	this.ds        = datasources;
	this.uri       = uri
	this.action    = action;
	this.options   = options || { };
	this.container = container;
	this.transform = transform;
	this.proxy     = new MochiKit.JsonRpc.JsonRpcProxy(uri, [action]);
	this.graphs    = new Object();

	this.requestData();

	if( interval ) {
		var self = this;
		window.setInterval(function(){self.requestData()}, interval);
	}
}

GraphRPC.prototype.requestData = function() {
	var r = this.proxy[this.action](); var self = this;
	r.addCallback(function(r){ self.dispatchResponse(r) });
	r.addErrback(function(e){ throw('Error: ' + e) });
}

GraphRPC.prototype.dispatchResponse = function(response) {
	var instances;
	if( this.options.instances ) {
		instances = this.options.instances;
	}
	else {
		instances = new Array();
		for( var instance in response ) {
			instances[instances.length] = instance;
		}
	}

	for( var j = 0; j < instances.length; j++ ) {
		var instance = instances[j];

		if( this.options.separateDS ) {
			for( var i = 0; i < this.ds.length; i += 2 ) {
				var name = this.ds[i+1] || this.ds[i];
				var gid  = instance + '-' + name;
				var otle = this.options.title || instance;

				if( !this.graphs[gid] ) {
					this.options.title = otle.replace('%s', instance) + ': ' + name;
					this.graphs[gid] = new Graph(
						this.container, gid, this.options, this.transform
					);

					this.graphs[gid].addDataset(name);
					this.graphs[gid].draw();
					this.options.title = otle;
				}
				else
				{
					this.graphs[gid].updateDataset(
						name, instance
							? response[instance][this.ds[i]]
							: response[parseInt(this.ds[i])]
					);
					this.graphs[gid].redraw();
				}
			}
		}
		else {
			if( !this.graphs[instance] ) {
				this.graphs[instance] = new Graph(
					this.container, instance, this.options, this.transform
				);
				if( window.console ) console.debug("NG: " + instance);

				for( var i = 0; i < this.ds.length; i += 2 ) {
					var name = this.ds[i+1] || this.ds[i];
					if( window.console ) console.debug("ADS: " + name);
					this.graphs[instance].addDataset(name);
				}

				this.graphs[instance].draw();
			}
			else {
				for( var i = 0; i < this.ds.length; i += 2 ) {
					var name = this.ds[i+1] || this.ds[i];
					if( window.console ) console.debug("UDS: " + name + " " + response[instance][this.ds[i]]);
					this.graphs[instance].updateDataset(
						name, instance
							? response[instance][this.ds[i]]
							: response[parseInt(this.ds[i])]
					);
				}

				this.graphs[instance].redraw();
			}
		}
	}
}