Skip to content

Commit 8ff56a4

Browse files
committed
Refactor date range initialization to fetch earliest timestamp from API and update date picker accordingly.
We use the on-disk earliest timestamp to get the complete range. Signed-off-by: Adam Warner <[email protected]>
1 parent f9f48b9 commit 8ff56a4

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

scripts/js/queries.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
"use strict";
1111

12-
const beginningOfTime = 1_262_304_000; // Jan 01 2010, 00:00 in seconds
13-
const endOfTime = 2_147_483_647; // Jan 19, 2038, 03:14 in seconds
14-
let from = beginningOfTime;
15-
let until = endOfTime;
12+
// These values are provided by the API (/info/database).
13+
// We initialize them as null and populate them during page init.
14+
let beginningOfTime = null; // seconds since epoch (set from API: info/database.earliest_timestamp)
15+
let endOfTime = null; // seconds since epoch (set to end of today)
16+
let from = null;
17+
let until = null;
1618

1719
const dateformat = "LLL dd yyyy, HH:mm";
1820

@@ -40,39 +42,60 @@ function getDnssecConfig() {
4042
});
4143
}
4244

45+
// Fetch database info (earliest timestamp, sizes, ...) from the API and
46+
// initialize related globals.
47+
function getDatabaseInfo() {
48+
$.getJSON(document.body.dataset.apiurl + "/info/database", data => {
49+
// earliest_timestamp is provided in seconds since epoch
50+
beginningOfTime = Number(data.earliest_timestamp_disk);
51+
// Round down to nearest 5-minute segment (300 seconds)
52+
beginningOfTime = Math.floor(beginningOfTime / 300) * 300;
53+
54+
// endOfTime should be the start of tomorrow in seconds since epoch
55+
// We don't use 23:59:59 as the picker increments are set to 5 minutes
56+
endOfTime = luxon.DateTime.now().plus({ days: 1 }).startOf("day").toSeconds();
57+
58+
// If from/until were not provided via GET, default them
59+
from ??= beginningOfTime;
60+
until ??= endOfTime;
61+
62+
initDateRangePicker();
63+
});
64+
}
65+
4366
function initDateRangePicker() {
67+
const now = luxon.DateTime.now();
68+
const minDateDt = luxon.DateTime.fromSeconds(beginningOfTime);
69+
const maxDateDt = luxon.DateTime.fromSeconds(endOfTime);
70+
4471
$("#querytime").daterangepicker(
4572
{
4673
timePicker: true,
4774
timePickerIncrement: 5,
4875
timePicker24Hour: true,
4976
locale: { format: dateformat },
50-
startDate: luxon.DateTime.fromMillis(from * 1000), // convert to milliseconds since epoch
51-
endDate: luxon.DateTime.fromMillis(until * 1000), // convert to milliseconds since epoch
77+
// Use Luxon DateTime objects for the picker ranges/start/end. The
78+
// daterangepicker in this build expects Luxon DateTime or ISO strings.
79+
startDate: luxon.DateTime.fromSeconds(from),
80+
endDate: luxon.DateTime.fromSeconds(until),
5281
ranges: {
53-
"Last 10 Minutes": [luxon.DateTime.now().minus({ minutes: 10 }), luxon.DateTime.now()],
54-
"Last Hour": [luxon.DateTime.now().minus({ hours: 1 }), luxon.DateTime.now()],
55-
Today: [luxon.DateTime.now().startOf("day"), luxon.DateTime.now().endOf("day")],
56-
Yesterday: [
57-
luxon.DateTime.now().minus({ days: 1 }).startOf("day"),
58-
luxon.DateTime.now().minus({ days: 1 }).endOf("day"),
59-
],
60-
"Last 7 Days": [luxon.DateTime.now().minus({ days: 6 }), luxon.DateTime.now().endOf("day")],
61-
"Last 30 Days": [
62-
luxon.DateTime.now().minus({ days: 29 }),
63-
luxon.DateTime.now().endOf("day"),
64-
],
65-
"This Month": [luxon.DateTime.now().startOf("month"), luxon.DateTime.now().endOf("month")],
82+
"Last 10 Minutes": [now.minus({ minutes: 10 }), now],
83+
"Last Hour": [now.minus({ hours: 1 }), now],
84+
Today: [now.startOf("day"), maxDateDt],
85+
Yesterday: [now.minus({ days: 1 }).startOf("day"), now.minus({ days: 1 }).endOf("day")],
86+
"Last 7 Days": [now.minus({ days: 6 }), maxDateDt],
87+
"Last 30 Days": [now.minus({ days: 29 }), maxDateDt],
88+
"This Month": [now.startOf("month"), now.endOf("month")],
6689
"Last Month": [
67-
luxon.DateTime.now().minus({ months: 1 }).startOf("month"),
68-
luxon.DateTime.now().minus({ months: 1 }).endOf("month"),
90+
now.minus({ months: 1 }).startOf("month"),
91+
now.minus({ months: 1 }).endOf("month"),
6992
],
70-
"This Year": [luxon.DateTime.now().startOf("year"), luxon.DateTime.now().endOf("year")],
71-
"All Time": [
72-
luxon.DateTime.fromMillis(beginningOfTime * 1000),
73-
luxon.DateTime.fromMillis(endOfTime * 1000),
74-
], // convert to milliseconds since epoch
93+
"This Year": [now.startOf("year"), now.endOf("year")],
94+
"All Time": [minDateDt, maxDateDt],
7595
},
96+
// Don't allow selecting dates outside the database range
97+
minDate: minDateDt,
98+
maxDate: maxDateDt,
7699
opens: "center",
77100
showDropdowns: true,
78101
autoUpdateInput: true,
@@ -504,14 +527,15 @@ $(() => {
504527
const apiURL = getAPIURL(GETDict);
505528

506529
if ("from" in GETDict) {
507-
from = GETDict.from;
530+
from = Number(GETDict.from);
508531
}
509532

510533
if ("until" in GETDict) {
511-
until = GETDict.until;
534+
until = Number(GETDict.until);
512535
}
513536

514-
initDateRangePicker();
537+
// Fetch earliest timestamp from API and initialize date picker / table
538+
getDatabaseInfo();
515539

516540
table = $("#all-queries").DataTable({
517541
ajax: {

0 commit comments

Comments
 (0)