Add "Clients (over time)" graph (needs FTL on branch "new/clientsovertime")

This commit is contained in:
DL6ER
2017-09-22 21:11:57 +02:00
parent 2e6adda93f
commit 076700fc92
3 changed files with 202 additions and 1 deletions

View File

@@ -7,7 +7,7 @@
// Define global variables
/* global Chart */
var timeLineChart, queryTypeChart, forwardDestinationChart;
var queryTypePieChart, forwardDestinationPieChart;
var queryTypePieChart, forwardDestinationPieChart, clientsChart;
function padNumber(num) {
return ("00" + num).substr(-2,2);
@@ -261,6 +261,86 @@ function updateForwardedOverTime() {
});
}
function updateClientsOverTime() {
$.getJSON("api.php?overTimeDataClients&getClientNames", function(data) {
if("FTLnotrunning" in data)
{
return;
}
// convert received objects to arrays
data.over_time = objectToArray(data.over_time);
var timestamps = data.over_time[0];
var plotdata = data.over_time[1];
var labels = [];
var key, i, j;
for (key in data.clients)
{
if (!{}.hasOwnProperty.call(data.clients, key)) continue;
if(key.indexOf("|") > -1)
{
var idx = key.indexOf("|");
key = key.substr(0, idx);
}
labels.push(key);
}
// Get colors from AdminLTE
var colors = [];
$.each($.AdminLTE.options.colors, function(key, value) { colors.push(value); });
var v = [], c = [], k = [];
// Remove possibly already existing data
clientsChart.data.labels = [];
clientsChart.data.datasets[0].data = [];
for (i = 1; i < clientsChart.data.datasets.length; i++)
{
clientsChart.data.datasets[i].data = [];
}
// Collect values and colors, and labels
clientsChart.data.datasets[0].backgroundColor = colors[0];
clientsChart.data.datasets[0].pointRadius = 0;
clientsChart.data.datasets[0].pointHitRadius = 5;
clientsChart.data.datasets[0].pointHoverRadius = 5;
clientsChart.data.datasets[0].label = labels[0];
for (i = clientsChart.data.datasets.length; i < plotdata[0].length; i++)
{
clientsChart.data.datasets.push({data: [], backgroundColor: colors[i], pointRadius: 0, pointHitRadius: 5, pointHoverRadius: 5, label: labels[i]});
}
// Add data for each dataset that is available
for (j in timestamps)
{
if (!{}.hasOwnProperty.call(timestamps, j)) continue;
for (key in plotdata[j])
{
if (!{}.hasOwnProperty.call(plotdata[j], key)) continue;
clientsChart.data.datasets[key].data.push(1e-2*plotdata[j][key]);
}
var d = new Date(1000*parseInt(timestamps[j]));
clientsChart.data.labels.push(d);
}
$("#clients .overlay").hide();
clientsChart.update();
}).done(function() {
// Reload graph after 10 minutes
failures = 0;
setTimeout(updateClientsOverTime, 600000);
}).fail(function() {
failures++;
if(failures < 5)
{
// Try again after 1 minute only if this has not failed more
// than five times in a row
setTimeout(updateClientsOverTime, 60000);
}
});
}
function updateForwardDestinationsPie() {
$.getJSON("api.php?getForwardDestinations=unsorted", function(data) {
@@ -625,6 +705,69 @@ $(document).ready(function() {
{
ctx = document.getElementById("forwardDestinationChart").getContext("2d");
forwardDestinationChart = new Chart(ctx, {
type: "line",
data: {
labels: [],
datasets: [{ data: [] }]
},
options: {
tooltips: {
enabled: true,
mode: "x-axis",
callbacks: {
title: function(tooltipItem, data) {
var label = tooltipItem[0].xLabel;
var time = label.match(/(\d?\d):?(\d?\d?)/);
var h = parseInt(time[1], 10);
var m = parseInt(time[2], 10) || 0;
var from = padNumber(h)+":"+padNumber(m-5)+":00";
var to = padNumber(h)+":"+padNumber(m+4)+":59";
return "Client activity from "+from+" to "+to;
},
label: function(tooltipItems, data) {
return data.datasets[tooltipItems.datasetIndex].label + ": " + (100.0*tooltipItems.yLabel).toFixed(1) + "%";
}
}
},
legend: {
display: false
},
scales: {
xAxes: [{
type: "time",
time: {
unit: "hour",
displayFormats: {
hour: "HH:mm"
},
tooltipFormat: "HH:mm"
}
}],
yAxes: [{
ticks: {
mix: 0.0,
max: 1.0,
beginAtZero: true,
callback: function(value, index, values) {
return Math.round(value*100) + " %";
}
},
stacked: true
}]
},
maintainAspectRatio: true
}
});
// Pull in data via AJAX
updateClientsOverTime();
}
// Create / load "Top Clients over Time" only if authorized
if(document.getElementById("clientsChart"))
{
ctx = document.getElementById("clientsChart").getContext("2d");
clientsChart = new Chart(ctx, {
type: "line",
data: {
labels: [],