Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network.js: minor consistency changes #1413

Merged
merged 1 commit into from
Jun 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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