Ver código fonte

Allow selecting a number of hours ago

I'm being lazy and doing a page reload rather than live updates.
Jason Tarka 4 anos atrás
pai
commit
3cfa63d0d9
3 arquivos alterados com 60 adições e 1 exclusões
  1. 9 0
      web-view/src/index.html
  2. 9 0
      web-view/src/js/consts.ts
  3. 42 1
      web-view/src/js/main.ts

+ 9 - 0
web-view/src/index.html

@@ -47,12 +47,21 @@
 		#gauges .gauge > .value {
 			stroke-width: 7px;
 		}
+
+
 	</style>
 </head>
 <body>
 	<div id="gauges"></div>
 	<div id="time-series-graphs"></div>
 
+	<div id="hour-selector">
+		<label>
+			Hours to show:
+			<select id="hours-ago"></select>
+		</label>
+	</div>
+
 	<!--suppress HtmlUnknownTarget -->
 	<script src="js/main.js" async></script>
 </body>

+ 9 - 0
web-view/src/js/consts.ts

@@ -1,3 +1,7 @@
+export const HOURS_QUERY:string = 'hours';
+export const HOURS_DEFAULT:number = 2;
+export const MAX_HOURS_AGO = 168;
+
 export type FieldInfo = {
 	key: string,
 	label: string,
@@ -87,3 +91,8 @@ export const FIELDS: FieldInfo[] = [
 ];
 
 export const FIELD_KEYS: string[] = FIELDS.map(f => f.key);
+
+/** The hours that can be selected in the dropdown */
+export const HOUR_OPTIONS:number[] = [
+	2, 4, 6, 12, 24, 48
+];

+ 42 - 1
web-view/src/js/main.ts

@@ -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(