Skip to content

Commit

Permalink
Merge pull request #1413 from pi-hole/XhmikosR-patch-1
Browse files Browse the repository at this point in the history
network.js: minor consistency changes
  • Loading branch information
PromoFaux authored Jun 1, 2020
2 parents 105d880 + ff16f48 commit ace21fb
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions scripts/pi-hole/js/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

var tableApi;

var APIstring = "api_db.php?network";
var API_STRING = "api_db.php?network";

// How many IPs do we show at most per device?
var MAXIPDISPLAY = 3;

var DAY_IN_SECONDS = 24 * 60 * 60;

function handleAjaxError(xhr, textStatus) {
if (textStatus === "timeout") {
alert("The server took too long to send the data.");
Expand Down Expand Up @@ -56,32 +58,31 @@ function mixColors(ratio, rgb1, rgb2) {
}

function parseColor(input) {
var m;
m = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
if (m) {
return [m[1], m[2], m[3]];
var match = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);

if (match) {
return [match[1], match[2], match[3]];
}
}

$(function () {
tableApi = $("#network-entries").DataTable({
rowCallback: function (row, data) {
var color,
iconClasses,
lastQuery = parseInt(data.lastQuery);
var color;
var iconClasses;
var lastQuery = parseInt(data.lastQuery);
var diff = getTimestamp() - lastQuery;
var networkRecent = $(".network-recent").css("background-color");
var networkOld = $(".network-old").css("background-color");
var networkOlder = $(".network-older").css("background-color");
var networkNever = $(".network-never").css("background-color");

if (lastQuery > 0) {
var diff = getTimestamp() - lastQuery;
if (diff <= 86400) {
// Last query came in within the last 24 hours (24*60*60 = 86400)
if (diff <= DAY_IN_SECONDS) {
// Last query came in within the last 24 hours
// Color: light-green to light-yellow
var ratio = Number(diff) / 86400;
var lightgreen = parseColor(networkRecent);
var lightyellow = parseColor(networkOld);
color = rgbToHex(mixColors(ratio, lightgreen, lightyellow));
var ratio = Number(diff) / DAY_IN_SECONDS;
color = rgbToHex(mixColors(ratio, parseColor(networkRecent), parseColor(networkOld)));
iconClasses = "fas fa-check";
} else {
// Last query was longer than 24 hours ago
Expand Down Expand Up @@ -146,7 +147,7 @@ $(function () {
"<'row'<'col-sm-4'l><'col-sm-8'p>>" +
"<'row'<'col-sm-12'<'table-responsive'tr>>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>",
ajax: { url: APIstring, error: handleAjaxError, dataSrc: "network" },
ajax: { url: API_STRING, error: handleAjaxError, dataSrc: "network" },
autoWidth: false,
processing: true,
order: [[5, "desc"]],
Expand Down

0 comments on commit ace21fb

Please sign in to comment.