Skip to content

Commit

Permalink
fixed date-time-presentation-setting; support numeric dates from the …
Browse files Browse the repository at this point in the history
…/stats api
  • Loading branch information
Erwin Dondorp committed Apr 11, 2022
1 parent 977d156 commit 14a1ae9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 47 deletions.
25 changes: 25 additions & 0 deletions saltgui/static/scripts/output/Output.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ export class Output {
return true;
}

static nDigits (pValue, pNrDigits) {
let digits = pValue.toString();
while (digits.length < pNrDigits) {
digits = "0" + digits;
}
return digits;
}

// reformat a date-time string
// supported formats:
Expand All @@ -163,6 +170,18 @@ export class Output {

const dateTimeRepresentation = Utils.getStorageItem("session", "datetime_representation", "utc");

if (typeof pDtStr === "number") {
pTimeOnly = pDtStr < 100 * 86400;
pDtStr = new Date(pDtStr * 1000);
}

if (typeof pDtStr === "object") {
// assume it is a Date
// 2019, Jan 26 19:05:22.808348
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
pDtStr = pDtStr.getUTCFullYear() + ", " + months[pDtStr.getUTCMonth()] + " " + pDtStr.getUTCDate() + " " + Output.nDigits(pDtStr.getUTCHours(), 2) + ":" + Output.nDigits(pDtStr.getUTCMinutes(), 2) + ":" + Output.nDigits(pDtStr.getUTCSeconds(), 2) + "." + Output.nDigits(pDtStr.getUTCMilliseconds(), 3);
}

// setting is not a number, return the original
let dateTimeFractionDigits = Number.parseInt(dateTimeFractionDigitsText, 10);
if (isNaN(dateTimeFractionDigits)) {
Expand Down Expand Up @@ -230,6 +249,12 @@ export class Output {
const localTZ = localDT.replace(/^.* /, "");
localDT = localDT.replace(/ [^ ]*$/, "");

if (milliSecondsSinceEpoch >= 86400 * 1000 && milliSecondsSinceEpoch < 100 * 86400 * 1000) {
const days = Math.trunc(milliSecondsSinceEpoch / (86400 * 1000)) + "d ";
utcDT = days + utcDT;
localDT = days + localDT;
}

let ret;
switch (dateTimeRepresentation) {
case "utc":
Expand Down
67 changes: 47 additions & 20 deletions saltgui/static/scripts/panels/Options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* global document */

import {Character} from "../Character.js";
import {Output} from "../output/Output.js";
import {OutputYaml} from "../output/OutputYaml.js";
import {Panel} from "./Panel.js";
import {Router} from "../Router.js";
Expand Down Expand Up @@ -47,7 +48,7 @@ export class OptionsPanel extends Panel {
],
[
"datetime-representation", "saltgui", "utc",
[["representation", "utc", "local", "utc+localtime", "local+utctime"]]
[["representation", "utc", "local", "utc-localtime:utc+localtime", "local-utctime:local+utctime"]]
],
["hide-jobs", "saltgui", "(none)"],

Expand Down Expand Up @@ -116,7 +117,7 @@ export class OptionsPanel extends Panel {
}

const radio = document.createElement("input");
radio.id = "option-" + pName + "-value-" + row[0] + "-" + itemValue.replace(/[+]/, "-");
radio.id = "option-" + pName + "-value-" + row[0] + "-" + itemValue;
radio.type = "radio";
radio.name = "option-" + pName + "-value-" + row[0];
radio.value = itemValue;
Expand Down Expand Up @@ -165,33 +166,61 @@ export class OptionsPanel extends Panel {
tbody.appendChild(tr);
}

static _enhanceSessionStart (pSessionStart) {
const startStr = new Date(pSessionStart * 1000);
return pSessionStart + "\n" + startStr;
static _enhanceSessionStart (pTd, pSessionStart) {
const line1 = Utils.createDiv(null, pSessionStart);

const line2s = Utils.createSpan();
Output.dateTimeStr(pSessionStart, line2s);
const line2d = Utils.createDiv();
line2d.appendChild(line2s);

pTd.innerHTML = "";
pTd.appendChild(line1);
pTd.appendChild(line2d);
}

static _enhanceSessionExpire (pSessionStart, pSessionExpire) {
const expireStr = new Date(pSessionExpire * 1000);
static _enhanceSessionExpire (pTd, pSessionExpire, pSessionStart) {

const line1 = Utils.createDiv(null, pSessionExpire);

const line2s = Utils.createSpan();
Output.dateTimeStr(pSessionExpire, line2s);
const line2d = Utils.createDiv();
line2d.appendChild(line2s);

const date = new Date(null);
if (pSessionStart && pSessionExpire) {
date.setSeconds(pSessionExpire - pSessionStart);
}
let durationStr = "";
const str1 = date.toISOString();
let line3 = null;
if (str1.startsWith("1970-01-01T")) {
// remove the date prefix and the millisecond suffix
durationStr = "\nduration is " + str1.substr(11, 8);
const durationStr = "duration is " + str1.substr(11, 8);
line3 = Utils.createDiv(null, durationStr);
}
let expiresInStr = "";

let line4 = null;
const leftMillis = pSessionExpire * 1000 - Date.now();
if (leftMillis < 0) {
expiresInStr = "\nexpired";
const expiresInStr = "expired";
line4 = Utils.createDiv(null, expiresInStr);
} else if (leftMillis < 86400000) {
const str2 = new Date(leftMillis).toISOString();
// remove the date prefix and the millisecond suffix
expiresInStr = "\nexpires in " + str2.substr(11, 8);
const expiresInStr = "expires in " + str2.substr(11, 8);
line4 = Utils.createDiv(null, expiresInStr);
}

pTd.innerHTML = "";
pTd.appendChild(line1);
pTd.appendChild(line2d);
if (line3) {
pTd.appendChild(line3);
}
if (line4) {
pTd.appendChild(line4);
}
return pSessionExpire + "\n" + expireStr + durationStr + expiresInStr;
}

onHide () {
Expand Down Expand Up @@ -233,14 +262,12 @@ export class OptionsPanel extends Panel {
value = category + "[" + name + "]";
}

const origValue = value;
const td = this.div.querySelector("#option-" + name + "-value");
if (category === "session" && name === "start") {
value = OptionsPanel._enhanceSessionStart(value);
OptionsPanel._enhanceSessionStart(td, value);
} else if (category === "session" && name === "expire") {
value = OptionsPanel._enhanceSessionExpire(sessionStart, value);
}
const td = this.div.querySelector("#option-" + name + "-value");
if (name === "perms") {
OptionsPanel._enhanceSessionExpire(td, value, sessionStart);
} else if (category === "session" && name === "perms") {
td.innerText = OutputYaml.formatYAML(value);
} else if (category === "session") {
td.innerText = value;
Expand All @@ -251,7 +278,7 @@ export class OptionsPanel extends Panel {
if (category === "session" && name === "expire") {
this.updateExpiresTimer = window.setInterval(() => {
// just redo the whole text-block
td.innerText = OptionsPanel._enhanceSessionExpire(sessionStart, origValue);
OptionsPanel._enhanceSessionExpire(td, value, sessionStart);
}, 1000);
}

Expand Down
39 changes: 12 additions & 27 deletions saltgui/static/scripts/panels/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class StatsPanel extends Panel {

this.updateStatsTimer = window.setInterval(() => {
this.onShowNow();
}, 3000);
}, 5000);
}

onShowNow () {
Expand All @@ -59,26 +59,11 @@ export class StatsPanel extends Panel {

// provide a shortened date format for cases
// where we see the timezone multiple times on one screen
static _shortenedDate (dateInMs) {
if (dateInMs === null) {
return dateInMs;
static _explainDateTime (pDateTimeInMs) {
if (pDateTimeInMs === null) {
return pDateTimeInMs;
}
// get the regular data fomat
let str = String(new Date(dateInMs * 1000));
// remove the named timezone part
str = str.replace(/ *[(].*[)]/, "");
return dateInMs + " (=" + str + ")";
}

static _shortenedInterval (intervalInMs) {
let str = new Date(intervalInMs * 1000).toISOString();
// remove the date prefix and the millisecond suffix
str = str.substr(11, 8);
// add the number of days (when there are any)
if (intervalInMs >= 86400) {
str = Math.floor(intervalInMs / 86400) + "d " + str;
}
return intervalInMs + " (=" + str + ")";
return pDateTimeInMs + " (=" + Output.dateTimeStr(pDateTimeInMs) + ")";
}

_handleStats (pStatsData) {
Expand Down Expand Up @@ -134,22 +119,22 @@ export class StatsPanel extends Panel {
// this turns the fields into strings (was number)
// we'll ignore that now

appData["Current Time"] = StatsPanel._shortenedDate(appData["Current Time"]);
appData["Current Time"] = StatsPanel._explainDateTime(appData["Current Time"]);

appData["Start Time"] = StatsPanel._shortenedDate(appData["Start Time"]);
appData["Start Time"] = StatsPanel._explainDateTime(appData["Start Time"]);

appData["Uptime"] = StatsPanel._shortenedInterval(appData["Uptime"]);
appData["Uptime"] = StatsPanel._explainDateTime(appData["Uptime"]);

const requests = appData["Requests"];
for (const key in requests) {
requests[key]["Start Time"] = StatsPanel._shortenedDate(requests[key]["Start Time"]);
requests[key]["End Time"] = StatsPanel._shortenedDate(requests[key]["End Time"]);
requests[key]["Start Time"] = StatsPanel._explainDateTime(requests[key]["Start Time"]);
requests[key]["End Time"] = StatsPanel._explainDateTime(requests[key]["End Time"]);
}

const slowQueries = appData["Slow Queries"];
for (const key in slowQueries) {
slowQueries[key]["Start Time"] = StatsPanel._shortenedDate(slowQueries[key]["Start Time"]);
slowQueries[key]["End Time"] = StatsPanel._shortenedDate(slowQueries[key]["End Time"]);
slowQueries[key]["Start Time"] = StatsPanel._explainDateTime(slowQueries[key]["Start Time"]);
slowQueries[key]["End Time"] = StatsPanel._explainDateTime(slowQueries[key]["End Time"]);
}
}

Expand Down

0 comments on commit 14a1ae9

Please sign in to comment.