Skip to content

Commit

Permalink
enhanced chart, smarter status refresh, and various fixes (#97)
Browse files Browse the repository at this point in the history
allow duplicate times and title tooltips in chart, refresh logs in settings only when changed, shorten datetime in log, fix cli check

* remove unnecessary check for duplicate results

* refactor formatDate()

* use the key

* better tooltip

* fix labels

* refresh logs only when changed, change datetime in log to match query log

* fix st

* skip results if none

* no seconds

* account for changed variable name

* fix whichSpeedtest
  • Loading branch information
ipitio authored Apr 26, 2024
1 parent 718bf6e commit 59a782a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 38 deletions.
14 changes: 6 additions & 8 deletions api_speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ function getRemainingTime()
$interval_seconds = -1;

if (file_exists('/opt/pihole/speedtestmod/schedule_check.sh')) {
$interval_seconds = speedtestExecute("grep 'interval_seconds=' /opt/pihole/speedtestmod/schedule_check.sh | cut -d'=' -f2")['data'];
$interval_seconds = speedtestExecute("grep 'INTERVAL_SECONDS=' /opt/pihole/speedtestmod/schedule_check.sh | cut -d'=' -f2")['data'];
}

// if interval_seconds is "nan", then schedule has never been set
Expand Down Expand Up @@ -352,16 +352,14 @@ function whichSpeedtest()
return 'official';
}

$version = speedtestExecute('/usr/bin/speedtest --version')['data'];
$version = speedtestExecute('/usr/bin/speedtest -h')['data'];

if (strpos($version, 'LibreSpeed') !== false) {
return 'LibreSpeed';
}

if (strpos($version, 'Python') !== false) {
if (strpos($version, 'sivel') !== false) {
return 'sivel\'s';
}

return 'LibreSpeed';
}

return 'official';
return 'no';
}
24 changes: 16 additions & 8 deletions scripts/pi-hole/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,10 @@ $(function () {

const codeBlock = (element, text, button, output) => {
if (element.find("pre").length > 0) {
element.find("pre code").text(text);
const existingText = element.find("pre code").text();
if (existingText !== text) {
element.find("pre code").text(text);
}
} else {
button.text("Hide " + output);
element.append(preCode(text));
Expand All @@ -543,7 +546,7 @@ $(function () {
// set in localStorage for use in other functions
localStorage.setItem("speedtest", speedtest);
} else {
localStorage.setItem("speedtest", "official");
localStorage.setItem("speedtest", "unknown");
}
})
.fail(function () {
Expand All @@ -554,6 +557,11 @@ $(function () {
const serviceStatus = () => {
whichSpeedtest();
const speedtestVersion = localStorage.getItem("speedtest") || "unknown";
let cliText = "Will use official CLI";
if (speedtestVersion !== "no") {
cliText = `Using ${speedtestVersion} CLI`;
}

$.ajax({
url: "api.php?getSpeedTestStatus",
dataType: "json",
Expand Down Expand Up @@ -609,24 +617,24 @@ $(function () {
let lastRunText = "Latest run is unavailable";
if (lastRun) {
lastRunText = `\nLatest run:\n${lastRun
.replace(/["{},]/g, "")
.replace(/\n\s*\n/g, "\n")
.replace(/^\n+|\s+$/g, "")}`;
.replaceAll(/["{},]/g, "")
.replaceAll(/\n\s*\n/g, "\n")
.replaceAll(/^\n+|\s+$/g, "")}`;
}

const statusText = `Using ${speedtestVersion} CLI\nSchedule is ${scheduleStatusText}\nNext run is${triggerText}\n${lastRunText}`;
const statusText = `${cliText}\nSchedule is ${scheduleStatusText}\nNext run is${triggerText}\n${lastRunText}`;
codeBlock(speedtestStatus, statusText, speedtestStatusBtn, "status");
})
.fail(function () {
const lastRunText = "Latest run is unavailable";
const statusText = `Using ${speedtestVersion} CLI\nSchedule is ${scheduleStatusText}\nNext run is${triggerText}\n${lastRunText}`;
const statusText = `${cliText}\nSchedule is ${scheduleStatusText}\nNext run is${triggerText}\n${lastRunText}`;
codeBlock(speedtestStatus, statusText, speedtestStatusBtn, "status");
});
})
.fail(function () {
const triggerText = speedtestTest.attr("value") ? "awaiting confirmation" : "unknown";
const lastRunText = "Latest run is unavailable";
const statusText = `Using ${speedtestVersion} CLI\nSchedule is unavailable\nNext run is ${triggerText}\n${lastRunText}`;
const statusText = `${cliText}\nSchedule is unavailable\nNext run is ${triggerText}\n${lastRunText}`;
codeBlock(speedtestStatus, statusText, speedtestStatusBtn, "status");
});
};
Expand Down
4 changes: 2 additions & 2 deletions scripts/pi-hole/js/speedresults.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ $(document).ready(function () {
{
render: function (data, type, _full, _meta) {
if (type === "display") {
data = moment.utc(data, "YYYY-MM-DD HH:mm:ss").local().format();
data = moment.utc(data, "YYYY-MM-DD HH:mm:ss").local().format("YYYY-MM-DD HH:mm");
}

return data;
Expand All @@ -58,7 +58,7 @@ $(document).ready(function () {
{
render: function (data, type, _full, _meta) {
if (type === "display") {
data = moment.utc(data, "YYYY-MM-DD HH:mm:ss").local().format();
data = moment.utc(data, "YYYY-MM-DD HH:mm:ss").local().format("YYYY-MM-DD HH:mm");
}

return data;
Expand Down
77 changes: 57 additions & 20 deletions scripts/pi-hole/js/speedtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ function createChart() {
Math.round(context?.parsed?.y ?? 0) + " " + (context?.dataset?.label ?? "") || null
);
},
title: function (context) {
const tick = context.slice(0, 1).shift().label ?? "";
const spaces = (tick.match(/ /g) || []).length;
const words = tick.split(" ");
let title = "Speedtest results";
switch (spaces) {
case 0:
title += " at " + words[0];
break;
case 1:
title += " on the " + words[0] + " at " + words[1];
break;
case 2:
title += " on " + words[0] + " " + words[1] + " at " + words[2];
break;
case 3:
title += " on " + words[0] + " " + words[1] + " " + words[2] + " at " + words[3];
break;
default:
break;
}

return title;
},
},
},
},
Expand Down Expand Up @@ -130,20 +154,6 @@ function createChart() {
});
}

function formatDate(itemdate, results) {
let output = "HH:mm";
if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {
return moment.utc(itemdate, "YYYY-MM-DD HH:mm:ss").local().format(output);
}

const first = moment(results.at(0).start_time, "YYYY-MM-DD HH:mm:ssZ");
if (moment.utc().diff(first, "hours") > 24) {
output = "Do HH:mm";
}

return moment.utc(itemdate, "YYYY-MM-DD HH:mm:ss").local().format(output);
}

function updateSpeedTestData() {
const daysIsTheSame = days === localStorage?.getItem("speedtest_days");
const typeIsTheSame = type === localStorage?.getItem("speedtest_chart_type");
Expand All @@ -160,15 +170,42 @@ function updateSpeedTestData() {
url: "api.php?getSpeedData=" + days,
dataType: "json",
}).done(function (results) {
results?.forEach(function (packet) {
// console.log(speedlabels.indexOf(formatDate(packet.start_time)));
if (speedlabels.indexOf(formatDate(packet.start_time, results)) === -1) {
speedlabels.push(formatDate(packet.start_time, results));
if (results !== null && results !== undefined && results.length > 0) {
// concat() can be used to make a shallow copy of the array
// aka duplicate its top level elements, or the references to its objects
// instead of using slice(0, 1) or slice(-1)
// shift() is used to remove and return the first element of the array
// pop() can be used to remove and return the last element of the array
// this is all to avoid using at(), which not supported in Safari
// and to avoid using [], which is looked down upon by the linter
const firstStartTime = results.slice(0, 1).shift().start_time;
const currDateTime = moment.utc();
const formats = {
YYYY: "YYYY MMM D HH:mm",
MM: "MMM D HH:mm",
DD: "Do HH:mm",
};
let dateFormat = "HH:mm";

for (const [key, value] of Object.entries(formats)) {
if (
moment(firstStartTime, "YYYY-MM-DD HH:mm:ss").format(key) !== currDateTime.format(key)
) {
dateFormat = value;
break;
}
}

results.forEach(function (packet) {
speedlabels.push(
moment.utc(packet.start_time, "YYYY-MM-DD HH:mm:ss").local().format(dateFormat)
);
uploadspeed.push(parseFloat(packet.upload));
downloadspeed.push(parseFloat(packet.download));
serverPing.push(parseFloat(packet.server_ping));
}
});
});
}

if (
speedChart &&
(!daysIsTheSame ||
Expand Down

0 comments on commit 59a782a

Please sign in to comment.