diff --git a/Database.go b/Database.go index 047ff76..08c48c9 100644 --- a/Database.go +++ b/Database.go @@ -39,17 +39,10 @@ func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error { return nil } -func getSnapshotRecordsFromDb(rowCount int) ([]*SnapshotRecord, error) { - snapshots := make([]*SnapshotRecord, rowCount) - query := fmt.Sprintf("SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` ORDER BY `id` DESC LIMIT %v;", - "id", - "temp", - "humidity", - "co2", - "time", - rowCount, - ) - rows, err := ClimateDb.Query(query) +func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) { + snapshots := make([]*SnapshotRecord, minuteAgo) + query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` ORDER BY `id` DESC WHERE %s > date_sub(now(), interval %v minute);" + rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time", "time", minuteAgo)) if err != nil { return nil, err } diff --git a/charts.html b/charts.html index 45dab28..476bcc0 100644 --- a/charts.html +++ b/charts.html @@ -15,11 +15,18 @@ const tempColor = 'rgb(0,134,222)'; const co2Color = 'rgb(194,30,30)'; + async function getData() { + let urlEndpoint = "data/"; + if (window.location.includes("since")) { + urlEndpoint += "since/" + window.location.slice(window.location.lastIndexOf("/") + 1); + } + const data = await fetch(urlEndpoint); + return transformData(await data.json()); + } + async function initChart() { const ctx = document.getElementById('myChart').getContext('2d'); - const data = await fetch("data/"); - console.log(data); - const {humidity, temp, co2} = transformData(await data.json()); + const {humidity, temp, co2} = getData(); const myChart = Chart.Line(ctx, { data: { datasets: [{ @@ -65,8 +72,8 @@ id: 'y-axis-1', ticks: { fontColor: co2Color, - min: 400, - max: 1100, + suggestedMin: 400, + suggestedMax: 1100, }, }, { type: 'linear', @@ -75,8 +82,8 @@ id: 'y-axis-2', ticks: { fontColor: tempColor, - min: 10, - max: 35, + suggestedMin: 10, + suggestedMax: 35, }, gridLines: { drawOnChartArea: false, @@ -88,8 +95,8 @@ id: 'y-axis-3', ticks: { fontColor: humidityColor, - min: 15, - max: 85, + suggestedMin: 15, + suggestedMax: 85, }, gridLines: { drawOnChartArea: false, @@ -117,9 +124,9 @@ setInterval(async () => { const newDatum = await fetch("data/1"); const snapshot = (await newDatum.json()).snapshots[0]; - chart.data.datasets[0].data.push({x: snapshot.time, y: snapshot.humidity}); - chart.data.datasets[1].data.push({x: snapshot.time, y: snapshot.temp}); - chart.data.datasets[2].data.push({x: snapshot.time, y: snapshot.co2}); + chart.data.datasets[0].data.splice(0, 1, {x: snapshot.time, y: snapshot.humidity}); + chart.data.datasets[1].data.splice(0, 1, {x: snapshot.time, y: snapshot.temp}); + chart.data.datasets[2].data.splice(0, 1, {x: snapshot.time, y: snapshot.co2}); chart.update(0); }, 10 * 1000); } diff --git a/climate-server.go b/climate-server.go index 3aaf3d8..9b2c83f 100644 --- a/climate-server.go +++ b/climate-server.go @@ -36,8 +36,9 @@ func startServer() { port := "8001" r := mux.NewRouter() r.HandleFunc("/", showCharts).Methods("GET") + r.HandleFunc("/since/{mins}", showCharts).Methods("GET") r.HandleFunc("/data/", sendData).Methods("GET") - r.HandleFunc("/data/{count}", sendData).Methods("GET") + r.HandleFunc("/data/since/{mins}", sendData).Methods("GET") r.HandleFunc("/", saveSnapshot).Methods("POST") r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) http.Handle("/", r) @@ -46,13 +47,19 @@ func startServer() { } func showCharts(w http.ResponseWriter, r *http.Request) { - http.ServeFile(w, r, "charts.html") + if countStr := mux.Vars(r)["mins"]; countStr != "" { + if _, err := strconv.ParseInt(countStr, 10, 0); err != nil { + http.Redirect(w, r, "/", 303) + } + } else { + http.ServeFile(w, r, "charts.html") + } } func sendData(w http.ResponseWriter, r *http.Request) { - var count int64 = 180 - if vars := mux.Vars(r); vars["count"] != "" { - newCount, err := strconv.ParseInt(vars["count"], 10, 0) + var count int64 = 60 + if vars := mux.Vars(r); vars["mins"] != "" { + newCount, err := strconv.ParseInt(vars["mins"], 10, 0) if err != nil { sendInternalError(errors.New("bad snapshot count request"), w, r) return