Project

General

Profile

Download (5.27 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * part of pfSense (https://www.pfsense.org)
3
 * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
4
 * All rights reserved.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

    
19
function draw_graph(refreshInterval, then) {
20

    
21
	d3.select("div[id^=nvtooltip-]").remove();
22
	d3.select(".interface-label").remove();
23

    
24
	var invert = localStorage.getItem('invert');
25
	var size = localStorage.getItem('size');
26

    
27
	startTime = 120 * refreshInterval;
28
	then.setSeconds(then.getSeconds() - startTime);
29
	var thenTime = then.getTime();
30

    
31
	$.each( JSON.parse(localStorage.getItem('interfaces')), function( key, value ) {
32

    
33
		latest[value + 'in'] = 0;
34
		latest[value + 'out'] = 0;
35

    
36
		var stepTime = thenTime;
37

    
38
		//initialize first 120 graph points to zero
39
		for (i = 1; i < 120; i++) {
40

    
41
			stepTime = stepTime + (1000 * refreshInterval);
42

    
43
			myData[value].forEach(function(entry) {
44
				entry.values.push({
45
					x: stepTime,
46
					y: 0
47
				});
48
			});
49

    
50
		}
51

    
52
		nv.addGraph(function() {
53

    
54
			charts[value] = nv.models.lineChart()
55
						.useInteractiveGuideline(true)
56
						.color(d3.scale.category20().range())
57
						.rightAlignYAxis(true)
58
						.margin({top: 0, left:25, bottom: 30, right: 45});
59

    
60
			charts[value]
61
				.x(function(d,i) { return d.x });
62

    
63
			charts[value].xAxis
64
				.tickFormat(function (d) {
65
					return d3.time.format('%M:%S')(new Date(d));
66
				});
67

    
68
			//TODO change to localStorage.getItem('sizeLabel');
69
			var sizeLabel = $( "#traffic-graph-size option:selected" ).text();
70

    
71
			d3.select('#traffic-chart-' + value + ' svg')
72
				.append("text")
73
				.attr('class', 'interface-label')
74
				.attr("x", 20)             
75
				.attr("y", 20)
76
				.attr("font-size", 18)
77
				.text(value);
78

    
79
			charts[value].yAxis
80
		    	.tickFormat(d3.format('.2s'))
81
		    	.showMaxMin(false);
82

    
83
			d3.select('#traffic-chart-' + value + ' svg')
84
				.datum(myData[value])
85
		    	.transition().duration(500)
86
		    	.call(charts[value]);
87

    
88
			nv.utils.windowResize(charts[value].update);
89

    
90
			//custom tooltip contents
91
			charts[value].interactiveLayer.tooltip.contentGenerator(function(data) {
92

    
93
				var units = 'b/s';
94
				console.log(localStorage.getItem('size'));
95
				if(localStorage.getItem('size') === "1") {
96
					units = 'B/s'
97
				}
98

    
99
				var content = '<h3>' + d3.time.format('%Y-%m-%d %H:%M:%S')(new Date(data.value)) + '</h3><table><tbody>';
100

    
101
				for ( var v = 0; v < data.series.length; v++ ){
102

    
103
					var rawValue = data.series[v].value;
104

    
105
					if((invert === "true") && data.series[v].key.includes("(out)")) {
106
						rawValue = 0 - rawValue;
107
					}
108

    
109
					var sValue = d3.formatPrefix(rawValue);
110

    
111
					//TODO change unit based on unit size
112
					var formattedVal = sValue.scale(rawValue).toFixed(2) + ' ' + sValue.symbol + units;
113

    
114
					content += '<tr><td class="legend-color-guide"><div style="background-color: ' + data.series[v].color + '"></div></td><td>' + data.series[v].key + '</td><td class="value"><strong>' + formattedVal + '</strong></td></tr>';
115

    
116
				}
117

    
118
				content += '</tbody></table>';
119

    
120
				return content;
121

    
122
			});
123

    
124
			return charts[value];
125
		});
126

    
127
	});
128

    
129
	//only update the graphs when tab is active in window to save resources and prevent build up
130
	updateIds = Visibility.every(refreshInterval * 1000, function(){
131

    
132
		d3.json("ifstats.php")
133
		.header("Content-Type", "application/x-www-form-urlencoded")
134
		.post('if='+JSON.parse(localStorage.getItem('interfaces')).join('|'), function(error, json) { //TODO all ifs again
135

    
136
			if (error) {
137

    
138
				Visibility.stop(updateIds);
139
				$(".traffic-widget-chart").remove();
140
				$("#traffic-chart-error").show().html('<strong>Error</strong>: ' + error);
141
				return console.warn(error);
142

    
143
			}
144

    
145
			if (json.error) {
146

    
147
				Visibility.stop(updateIds);
148
				$(".traffic-widget-chart").remove();
149
				$("#traffic-chart-error").show().html('<strong>Error</strong>: ' + json.error);
150
				return console.warn(json.error);
151

    
152
			}
153

    
154
			now = new Date(Date.now());
155

    
156
			$.each(json, function( key, ifVals ) {
157

    
158
				if(!myData[key][0].first) {
159

    
160
					var trafficIn = ((ifVals[0].values[1] * size) - latest[ifVals[0].key]) / refreshInterval;
161
					var trafficOut = ((ifVals[1].values[1] * size) - latest[ifVals[1].key]) / refreshInterval;
162

    
163
					if((localStorage.getItem('invert') === "true")) {
164
						trafficOut = 0 - trafficOut;
165
					}
166

    
167
					myData[key][0].values.push({
168
						x: now.getTime(),
169
						y: trafficIn
170
					});
171

    
172
					myData[key][1].values.push({
173
						x: now.getTime(),
174
						y: trafficOut
175
					});
176

    
177
				} else {
178
					myData[key][0].values.push({
179
						x: now.getTime(),
180
						y: 0
181
					});
182

    
183
					myData[key][1].values.push({
184
						x: now.getTime(),
185
						y: 0
186
					});
187
				}
188

    
189
				latest[ifVals[0].key] = ifVals[0].values[1] * size;
190
				latest[ifVals[1].key] = ifVals[1].values[1] * size;
191

    
192
				myData[key][0].first = false;
193
				myData[key][1].first = false;
194

    
195

    
196
				myData[key][0].values.shift();
197
				myData[key][1].values.shift();
198

    
199
				charts[key].update();			
200

    
201
			});
202

    
203
		});
204

    
205
	});
206

    
207
}
(4-4/4)