Project

General

Profile

Download (5.59 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, backgroundupdate) {
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( window.interfaces, function( key, value ) {
32
		myData[value]['interfacename'] = graph_interfacenames[value];
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
			var sizeLabel = $( "#traffic-graph-size option:selected" ).text();
69

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

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

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

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

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

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

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

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

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

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

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

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

    
113
					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>';
114

    
115
				}
116

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

    
119
				return content;
120

    
121
			});
122

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

    
126
	});
127

    
128
	var refreshGraphFunction = function(){
129
		d3.json("ifstats.php")
130
		.header("Content-Type", "application/x-www-form-urlencoded")
131
		.post('if='+window.interfaces.join('|'), function(error, json) { //TODO all ifs again
132

    
133
			if (error) {
134

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

    
141
			}
142

    
143
			if (json.error) {
144

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

    
151
			}
152

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

    
155
			$.each(json, function( key, ifVals ) {
156
				label = $('#traffic-chart-' + key + ' svg > .interface-label');
157
				$(label).text(ifVals.name);
158

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

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

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

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

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

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

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

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

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

    
196

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

    
200
				charts[key].update();			
201

    
202
			});
203

    
204
		});
205

    
206
	}
207
	
208
	if(backgroundupdate) {
209
		updateTimerIds = setInterval(refreshGraphFunction, refreshInterval * 1000);
210
	} else {
211
		//only update the graphs when tab is active in window to save resources and prevent build up
212
		updateIds = Visibility.every(refreshInterval * 1000, refreshGraphFunction);
213
	}
214
}
(4-4/4)