|
|
@@ -6,6 +6,7 @@ import {getDatabase, onValue, orderByChild, query, ref, startAt} from 'firebase/
|
|
|
import {Reading} from './types';
|
|
|
import {createGauges, createGraphs} from './graphs';
|
|
|
import {processData} from './processData';
|
|
|
+import {HOUR_OPTIONS, HOURS_DEFAULT, HOURS_QUERY, MAX_HOURS_AGO} from './consts';
|
|
|
|
|
|
const app = initializeApp(firebaseConfig);
|
|
|
const rtdb = getDatabase(app);
|
|
|
@@ -13,7 +14,7 @@ const rtdb = getDatabase(app);
|
|
|
document.body.onload = init;
|
|
|
|
|
|
async function init() {
|
|
|
- const readings = await getRecentData(12);
|
|
|
+ const readings = await getRecentData(getHoursAgo());
|
|
|
const values = processData(readings);
|
|
|
|
|
|
createGauges(
|
|
|
@@ -25,6 +26,46 @@ async function init() {
|
|
|
values,
|
|
|
document.getElementById('time-series-graphs')
|
|
|
);
|
|
|
+
|
|
|
+ showHourSelection();
|
|
|
+}
|
|
|
+
|
|
|
+function getHoursAgo():number {
|
|
|
+ const query = new URLSearchParams(document.location.search)
|
|
|
+ .get(HOURS_QUERY);
|
|
|
+ if(!query) {
|
|
|
+ return HOURS_DEFAULT;
|
|
|
+ }
|
|
|
+
|
|
|
+ let hours = parseInt(query);
|
|
|
+ if(isNaN(hours) || hours > MAX_HOURS_AGO) {
|
|
|
+ return HOURS_DEFAULT;
|
|
|
+ }
|
|
|
+
|
|
|
+ return hours;
|
|
|
+}
|
|
|
+
|
|
|
+function showHourSelection() {
|
|
|
+ const options:HTMLSelectElement =
|
|
|
+ document.getElementById('hours-ago') as HTMLSelectElement;
|
|
|
+
|
|
|
+ const current = getHoursAgo();
|
|
|
+
|
|
|
+ for(let h of HOUR_OPTIONS) {
|
|
|
+ const opt = document.createElement('option');
|
|
|
+ opt.value = h.toString();
|
|
|
+ opt.textContent = `Last ${h} hours`;
|
|
|
+ if(h === current) {
|
|
|
+ opt.selected = true;
|
|
|
+ }
|
|
|
+ options.add(opt);
|
|
|
+ }
|
|
|
+
|
|
|
+ options.addEventListener('change', () => {
|
|
|
+ const selection = options.value;
|
|
|
+ const newUrl = `${document.location.href.split('?')[0]}?hours=${selection}`;
|
|
|
+ document.location.href = newUrl;
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
async function getRecentData(
|