PluginProbe
WebTotem Security / 2.3.14
WebTotem Security v2.3.14
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / htdocs / js / wtsec_Chart.js

wtsec_Chart.js in WebTotem Security 2.3.14, at htdocs/js/wtsec_Chart.js

572 lines 18.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 async function drawLineChart(dataset) {
2 var diagram = document.getElementById('wtotem_chart_diagram');
3 var days = diagram.dataset.days;
4
5 const attacksAccessor = (d) => d.attacks;
6 const blockedAccessor = (d) => d.blocked;
7 const biggerAccessor = (d) =>
8 d.attacks > d.blocked ? d.attacks : d.blocked;
9 const dateParser = (days <= 1) ? d3.timeParse("%Y-%m-%d %H:%m:%M"): d3.timeParse("%Y-%m-%d");
10 const xAccessor = (d) => dateParser(d.date);
11
12
13 const chartWrapper = document.getElementById('line-chart')
14 let dimensions = {
15 width: chartWrapper.offsetWidth,
16 height: 251,
17 margin: {
18 top: 15,
19 right: 15,
20 bottom: 40,
21 left: 60,
22 },
23 };
24 dimensions.boundedWidth =
25 dimensions.width - dimensions.margin.left - dimensions.margin.right;
26 dimensions.boundedHeight =
27 dimensions.height - dimensions.margin.top - dimensions.margin.bottom;
28
29 const wrapper = d3
30 .select("#line-chart")
31 .append("svg")
32 .attr("width", dimensions.width)
33 .attr("height", dimensions.height);
34
35 const bounds = wrapper
36 .append("g")
37 .style(
38 "transform",
39 `translate(${dimensions.margin.left - 10}px, ${
40 dimensions.margin.top
41 }px)`
42 );
43
44 const yScale = d3
45 .scaleLinear()
46 .domain(d3.extent(dataset, biggerAccessor))
47 .range([dimensions.boundedHeight, 0]);
48 const xScale = d3
49 .scaleTime()
50 .domain(d3.extent(dataset, xAccessor))
51 .range([0, dimensions.boundedWidth]);
52
53 function make_y_gridlines() {
54 return d3.axisLeft(yScale).ticks(5);
55 }
56
57 bounds
58 .append("g")
59 .attr("class", "grid")
60 .call(
61 make_y_gridlines().tickSize(-dimensions.boundedWidth).tickFormat("")
62 );
63
64 const area1 = d3
65 .area()
66 .x((d) => xScale(xAccessor(d)))
67 .y0(dimensions.height)
68 .y1((d) => yScale(attacksAccessor(d)));
69
70 const area2 = d3
71 .area()
72 .x((d) => xScale(xAccessor(d)))
73 .y0(dimensions.height)
74 .y1((d) => yScale(blockedAccessor(d)));
75
76 bounds
77 .append("linearGradient")
78 .attr("id", "area-gradient1")
79 .attr("gradientUnits", "userSpaceOnUse")
80 .attr("x1", 0)
81 .attr("y1", yScale(0))
82 .attr("x2", 0)
83 .attr("y2", yScale(100))
84 .selectAll("stop")
85 .data([
86 { offset: "0%", color: "#d46c6a", opacity: 0.04 },
87 { offset: "100%", color: "#d46c6a", opacity: 1 },
88 ])
89 .enter()
90 .append("stop")
91 .attr("offset", function (d) {
92 return d.offset;
93 })
94 .attr("stop-color", function (d) {
95 return d.color;
96 })
97 .attr("stop-opacity", function (d) {
98 return d.opacity;
99 });
100
101 bounds
102 .append("linearGradient")
103 .attr("id", "area-gradient2")
104 .attr("gradientUnits", "userSpaceOnUse")
105 .attr("x1", 0)
106 .attr("y1", yScale(0))
107 .attr("x2", 0)
108 .attr("y2", yScale(100))
109 .selectAll("stop")
110 .data([
111 { offset: "0%", color: "#bace3d", opacity: 0.02 },
112 { offset: "100%", color: "#bace3d", opacity: 1 },
113 ])
114 .enter()
115 .append("stop")
116 .attr("offset", function (d) {
117 return d.offset;
118 })
119 .attr("stop-color", function (d) {
120 return d.color;
121 })
122 .attr("stop-opacity", function (d) {
123 return d.opacity;
124 });
125
126 const lineGenerator1 = d3
127 .line()
128 .x((d) => xScale(xAccessor(d)))
129 .y((d) => yScale(attacksAccessor(d)));
130 const lineGenerator2 = d3
131 .line()
132 .x((d) => xScale(xAccessor(d)))
133 .y((d) => yScale(attacksAccessor(d)));
134
135 const line1 = bounds
136 .append("path")
137 .datum(dataset)
138 .attr("d", lineGenerator1(dataset))
139 .attr("class", "area")
140 .attr("d", area1)
141 .style("fill", "url(#area-gradient1)");
142
143 const line2 = bounds
144 .append("path")
145 .datum(dataset)
146 .attr("d", lineGenerator2(dataset))
147 .attr("class", "area")
148 .attr("d", area2)
149 .style("fill", "url(#area-gradient2)");
150
151 const yAxisGenerator = d3.axisLeft().scale(yScale);
152 // const xAxisGenerator = d3.axisBottom().scale(xScale);
153
154 let dateFormat = (days <= 1) ? d3.timeFormat("%H:%M") : (days > 31) ? d3.timeFormat("%b %Y") : d3.timeFormat("%b %d");
155 const xAxisGenerator = (days <= 1) ? d3.axisBottom().scale(xScale).ticks(d3.timeHour.every(2)).tickFormat(dateFormat) :
156 (days > 31) ? d3.axisBottom().scale(xScale).ticks(d3.timeMonth.every(1)).tickFormat(dateFormat) :
157 (days <= 7) ? d3.axisBottom().scale(xScale).ticks(d3.timeDay.every(1)).tickFormat(dateFormat) :
158 d3.axisBottom().scale(xScale).ticks(9).tickFormat(dateFormat);
159 //const xAxisGenerator = d3.axisBottom().scale(xScale).ticks(ticks).tickFormat(dateFormat);
160
161 const yAxis = bounds.append("g").attr("class", "axis").call(yAxisGenerator);
162 const xAxis = bounds
163 .append("g")
164 .call(xAxisGenerator)
165 .attr("class", "axis chart-date")
166 .style("transform", `translateY(${dimensions.boundedHeight}px)`);
167
168 const listeningRect = bounds
169 .append("rect")
170 .attr("class", "listening-rect")
171 .attr("width", dimensions.boundedWidth)
172 .attr("height", dimensions.boundedHeight)
173 .on("mousemove", onMouseMove)
174 .on("mouseleave", onMouseLeave);
175
176 const tooltipAttacks = d3.select("#tooltipAttacks");
177 const tooltipBlocked = d3.select("#tooltipBlocked");
178 const tooltipCircle1 = bounds
179 .append("circle")
180 .attr("class", "tooltip-circle")
181 .attr("r", 4)
182 .attr("stroke", "#F3F5F6")
183 .attr("fill", "#1D293F")
184 .attr("stroke-width", 2)
185 .style("opacity", 0);
186 const tooltipCircle2 = bounds
187 .append("circle")
188 .attr("class", "tooltip-circle")
189 .attr("r", 4)
190 .attr("stroke", "#F3F5F6")
191 .attr("fill", "#1D293F")
192 .attr("stroke-width", 2)
193 .style("opacity", 0);
194 function onMouseMove() {
195 const mousePosition = d3.mouse(this);
196 const hoveredDate = xScale.invert(mousePosition[0]);
197
198 const getDistanceFromHoveredDate = (d) =>
199 Math.abs(xAccessor(d) - hoveredDate);
200 const closestIndex = d3.scan(
201 dataset,
202 (a, b) =>
203 getDistanceFromHoveredDate(a) - getDistanceFromHoveredDate(b)
204 );
205 const closestDataPoint = dataset[closestIndex];
206
207 const closestXValue = xAccessor(closestDataPoint);
208 const closestAttacksValue = attacksAccessor(closestDataPoint);
209 const closestBlockedValue = blockedAccessor(closestDataPoint);
210
211 const x = xScale(closestXValue) + dimensions.margin.left + 8;
212 const yAttacks =
213 yScale(closestAttacksValue) + dimensions.margin.top + 10;
214 const yBlocked =
215 yScale(closestBlockedValue) + dimensions.margin.top + 10;
216
217 tooltipAttacks.style(
218 "transform",
219 `translate(` +
220 `calc( -50% + ${x}px),` +
221 `calc(-100% + ${yAttacks}px)` +
222 `)`
223 );
224 tooltipBlocked.style(
225 "transform",
226 `translate(` +
227 `calc( -50% + ${x}px),` +
228 `calc(-100% + ${yBlocked}px)` +
229 `)`
230 );
231
232 tooltipAttacks.style("opacity", 1);
233 tooltipBlocked.style("opacity", 1);
234
235 tooltipAttacks.select("#countAttacks").html(closestAttacksValue);
236 tooltipBlocked.select("#countBlocked").html(closestBlockedValue);
237 tooltipCircle1
238 .attr("cx", xScale(closestXValue))
239 .attr("cy", yScale(closestAttacksValue))
240 .style("opacity", 1);
241 tooltipCircle2
242 .attr("cx", xScale(closestXValue))
243 .attr("cy", yScale(closestBlockedValue))
244 .style("opacity", 1);
245 }
246
247 function onMouseLeave() {
248 tooltipAttacks.style("opacity", 0);
249 tooltipBlocked.style("opacity", 0);
250
251 tooltipCircle1.style("opacity", 0);
252 tooltipCircle2.style("opacity", 0);
253 }
254 }
255
256 const drawWafChart = (data) => {
257 drawLineChart(data);
258 };
259
260 if(typeof waf_chart == "object"){
261 drawWafChart(waf_chart);
262 }
263
264 var resizeTimerFirewallChart;
265 window.onresize = function (event) {
266 clearTimeout(resizeTimerFirewallChart);
267 resizeTimerFirewallChart = setTimeout(function () {
268 var s = d3.select("#line-chart").selectAll("svg")
269 s = s.remove();
270 drawLineChart(waf_chart);
271 }, 10);
272 };
273
274 // server-status
275 async function drawServerStatusChart(id, elementSelector, tooltipSelector, tooltipValueSelector, color, dataset) {
276 const dataAccessor = (d) => d.value;
277
278 let diagram = document.querySelector(elementSelector);
279 let days = diagram.dataset.days;
280
281 const dateParser = (days <= 1) ? d3.timeParse("%Y-%m-%d %H:%m:%M"): d3.timeParse("%Y-%m-%d");
282
283 const xAccessor = (d) => dateParser(d.date);
284
285 const chartWrapper = document.querySelector(elementSelector);
286 let dimensions = {
287 width: chartWrapper.offsetWidth,
288 height: 251,
289 margin: {
290 top: 15,
291 right: 15,
292 bottom: 40,
293 left: 60,
294 },
295 };
296 dimensions.boundedWidth =
297 dimensions.width - dimensions.margin.left - dimensions.margin.right;
298 dimensions.boundedHeight =
299 dimensions.height - dimensions.margin.top - dimensions.margin.bottom;
300
301 const wrapper = d3
302 .select(elementSelector)
303 .append("svg")
304 .attr("width", dimensions.width)
305 .attr("height", dimensions.height);
306
307 const bounds = wrapper
308 .append("g")
309 .style(
310 "transform",
311 `translate(${dimensions.margin.left - 10}px, ${
312 dimensions.margin.top
313 }px)`
314 );
315
316 const yScale = d3
317 .scaleLinear()
318 .domain(d3.extent(dataset, dataAccessor))
319 .range([dimensions.boundedHeight, 0]);
320 const xScale = d3
321 .scaleTime()
322 .domain(d3.extent(dataset, xAccessor))
323 .range([0, dimensions.boundedWidth]);
324
325 function make_y_gridlines() {
326 return d3.axisLeft(yScale).ticks(5);
327 }
328
329 bounds
330 .append("g")
331 .attr("class", "grid")
332 .call(
333 make_y_gridlines().tickSize(-dimensions.boundedWidth).tickFormat("")
334 );
335
336 const area1 = d3
337 .area()
338 .x((d) => xScale(xAccessor(d)))
339 .y0(dimensions.height)
340 .y1((d) => yScale(dataAccessor(d)));
341
342 const gradientAreaId = "area-gradient-" + id;
343
344 bounds
345 .append("linearGradient")
346 .attr("id", gradientAreaId)
347 .attr("gradientUnits", "userSpaceOnUse")
348 .attr("x1", 0)
349 .attr("y1", yScale(0))
350 .attr("x2", 0)
351 .attr("y2", yScale(100))
352 .selectAll("stop")
353 .data([
354 { offset: "0%", color: color, opacity: 0 },
355 { offset: "100%", color: color, opacity: 0.6 },
356 ])
357 .enter()
358 .append("stop")
359 .attr("offset", function (d) {
360 return d.offset;
361 })
362 .attr("stop-color", function (d) {
363 return d.color;
364 })
365 .attr("stop-opacity", function (d) {
366 return d.opacity;
367 });
368
369 const lineGenerator1 = d3
370 .line()
371 .x((d) => xScale(xAccessor(d)))
372 .y((d) => yScale(dataAccessor(d)));
373
374 const line1 = bounds
375 .append("path")
376 .datum(dataset)
377 .attr("d", lineGenerator1(dataset))
378 .attr("class", "area")
379 .attr("d", area1)
380 .style("fill", "url(#"+gradientAreaId+")");
381
382 const yAxisGenerator = d3.axisLeft().scale(yScale);
383 // const xAxisGenerator = d3.axisBottom().scale(xScale);
384 let dateFormat = (days <= 1) ? d3.timeFormat("%H:%M") : (days > 31) ? d3.timeFormat("%b %Y") : d3.timeFormat("%b %d");
385 const xAxisGenerator = (days <= 1) ? d3.axisBottom().scale(xScale).ticks(d3.timeHour.every(2)).tickFormat(dateFormat) :
386 (days > 31) ? d3.axisBottom().scale(xScale).ticks(d3.timeMonth.every(1)).tickFormat(dateFormat) :
387 (days <= 7) ? d3.axisBottom().scale(xScale).ticks(d3.timeDay.every(1)).tickFormat(dateFormat) :
388 d3.axisBottom().scale(xScale).ticks(9).tickFormat(dateFormat);
389
390 const yAxis = bounds.append("g").attr("class", "axis").call(yAxisGenerator);
391 const xAxis = bounds
392 .append("g")
393 .call(xAxisGenerator)
394 .attr("class", "axis")
395 .style("transform", `translateY(${dimensions.boundedHeight}px)`);
396
397 const listeningRect = bounds
398 .append("rect")
399 .attr("class", "listening-rect")
400 .attr("width", dimensions.boundedWidth)
401 .attr("height", dimensions.boundedHeight)
402 .on("mousemove", onMouseMove)
403 .on("mouseleave", onMouseLeave);
404
405 const tooltipData = d3.select(tooltipSelector);
406 const tooltipCircle1 = bounds
407 .append("circle")
408 .attr("class", "tooltip-circle")
409 .attr("r", 4)
410 .attr("stroke", "#F3F5F6")
411 .attr("fill", "#1D293F")
412 .attr("stroke-width", 2)
413 .style("opacity", 0);
414
415 function onMouseMove() {
416 const mousePosition = d3.mouse(this);
417 const hoveredDate = xScale.invert(mousePosition[0]);
418
419 const getDistanceFromHoveredDate = (d) =>
420 Math.abs(xAccessor(d) - hoveredDate);
421 const closestIndex = d3.scan(
422 dataset,
423 (a, b) =>
424 getDistanceFromHoveredDate(a) - getDistanceFromHoveredDate(b)
425 );
426 const closestDataPoint = dataset[closestIndex];
427
428 const closestXValue = xAccessor(closestDataPoint);
429 const closestValue = dataAccessor(closestDataPoint);
430
431 const x = xScale(closestXValue) + dimensions.margin.left + 8;
432 const yAttacks =
433 yScale(closestValue) + dimensions.margin.top + 10;
434
435 tooltipData.style(
436 "transform",
437 `translate(` +
438 `calc( -50% + ${x}px),` +
439 `calc(-100% + ${yAttacks}px)` +
440 `)`
441 );
442
443 tooltipData.style("opacity", 1);
444
445 tooltipData.select(tooltipValueSelector).html(closestValue);
446 tooltipCircle1
447 .attr("cx", xScale(closestXValue))
448 .attr("cy", yScale(closestValue))
449 .style("opacity", 1);
450 }
451
452 function onMouseLeave() {
453 tooltipData.style("opacity", 0);
454 tooltipCircle1.style("opacity", 0);
455 }
456 }
457
458 const ramChartSelector = "#ram-chart";
459 const drawRamChart = (data) => {
460 const ramTooltipSelector = "#tooltipRam";
461 const ramTooltipValueSelector = "#countRam";
462 const ramChartColor = "#3d50df";
463 drawServerStatusChart("ram", ramChartSelector, ramTooltipSelector, ramTooltipValueSelector, ramChartColor, data);
464 }
465
466
467 if(typeof serverStatusRam == "object"){
468 drawRamChart(serverStatusRam);
469 }
470
471
472 const cpuChartSelector = "#cpu-chart";
473 const drawCpuChart = (data) => {
474 const cpuTooltipSelector = "#tooltipCpu";
475 const cpuTooltipValueSelector = "#countCpu";
476 const cpuChartColor = "#6d3594";
477 drawServerStatusChart("cpu", cpuChartSelector, cpuTooltipSelector, cpuTooltipValueSelector, cpuChartColor, data);
478 }
479
480 if(typeof serverStatusCpu == "object") {
481 drawCpuChart(serverStatusCpu);
482 }
483
484
485 var resizeTimerRamChart;
486 var resizeTimerCpuChart;
487
488 window.onresize = function (event) {
489
490 if (document.querySelector("#ram-chart") !== null) {
491 clearTimeout(resizeTimerRamChart);
492 resizeTimerRamChart = setTimeout(function () {
493 var s1 = d3.select("#ram-chart").selectAll("svg")
494 s1 = s1.remove();
495 drawRamChart(serverStatusRam);
496 }, 10);
497 };
498
499 if (document.querySelector("#cpu-chart") !== null) {
500 clearTimeout(resizeTimerCpuChart);
501 resizeTimerCpuChart = setTimeout(function () {
502 var s2 = d3.select("#cpu-chart").selectAll("svg")
503 s2 = s2.remove();
504 drawCpuChart(serverStatusCpu);
505 }, 10);
506 };
507 }
508
509
510
511 // disk-chart
512
513 async function drawDiskChart_(elementSelector, data) {
514 // set the dimensions and margins of the graph
515 var width = 150
516 height = 150
517 margin = 1
518
519 // The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
520 var radius = Math.min(width, height) / 2 - margin
521
522 // append the svg object to the div called 'my_dataviz'
523 var svg = d3.select(elementSelector)
524 .append("svg")
525 .attr("width", width)
526 .attr("height", height)
527 .append("g")
528 .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
529
530 // Create dummy data
531 // var data = {a: 9, b: 20, c:30, d:8, e:12}
532
533 // set the color scale
534 var color = d3.scaleOrdinal()
535 .domain(data)
536 .range(["#5E6977", "#3D50DF"])
537
538 // Compute the position of each group on the pie:
539 var pie = d3.pie()
540 .value(function(d) {return d.value; })
541 var data_ready = pie(d3.entries(data))
542
543 // Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
544 svg
545 .selectAll('whatever')
546 .data(data_ready)
547 .enter()
548 .append('path')
549 .attr('d', d3.arc()
550 .innerRadius(30) // This is the size of the donut hole
551 .outerRadius(radius)
552 )
553 .attr('fill', function(d){ return(color(d.data.key)) })
554 .attr("stroke", "white")
555 .style("stroke-width", "15px")
556 .style("opacity", 1)
557
558 }
559
560
561 const drawDiskChart = (data) => {
562 const diskData = {use: data['use'], free: data['free']};
563 const discChartSelector = "#disk-chart";
564 drawDiskChart_(discChartSelector, diskData);
565 };
566
567 if(typeof serverStatusDiscUsage == "object") {
568 drawDiskChart(serverStatusDiscUsage);
569 }
570
571
572