Changed endpoints and db queries, chart now shows suggested mins and maxes
This commit is contained in:
15
Database.go
15
Database.go
@@ -39,17 +39,10 @@ func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSnapshotRecordsFromDb(rowCount int) ([]*SnapshotRecord, error) {
|
func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
||||||
snapshots := make([]*SnapshotRecord, rowCount)
|
snapshots := make([]*SnapshotRecord, minuteAgo)
|
||||||
query := fmt.Sprintf("SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` ORDER BY `id` DESC LIMIT %v;",
|
query := "SELECT `%s`, `%s`, `%s`, `%s`, `%s` FROM `snapshots` ORDER BY `id` DESC WHERE %s > date_sub(now(), interval %v minute);"
|
||||||
"id",
|
rows, err := ClimateDb.Query(fmt.Sprintf(query, "id", "temp", "humidity", "co2", "time", "time", minuteAgo))
|
||||||
"temp",
|
|
||||||
"humidity",
|
|
||||||
"co2",
|
|
||||||
"time",
|
|
||||||
rowCount,
|
|
||||||
)
|
|
||||||
rows, err := ClimateDb.Query(query)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
31
charts.html
31
charts.html
@@ -15,11 +15,18 @@
|
|||||||
const tempColor = 'rgb(0,134,222)';
|
const tempColor = 'rgb(0,134,222)';
|
||||||
const co2Color = 'rgb(194,30,30)';
|
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() {
|
async function initChart() {
|
||||||
const ctx = document.getElementById('myChart').getContext('2d');
|
const ctx = document.getElementById('myChart').getContext('2d');
|
||||||
const data = await fetch("data/");
|
const {humidity, temp, co2} = getData();
|
||||||
console.log(data);
|
|
||||||
const {humidity, temp, co2} = transformData(await data.json());
|
|
||||||
const myChart = Chart.Line(ctx, {
|
const myChart = Chart.Line(ctx, {
|
||||||
data: {
|
data: {
|
||||||
datasets: [{
|
datasets: [{
|
||||||
@@ -65,8 +72,8 @@
|
|||||||
id: 'y-axis-1',
|
id: 'y-axis-1',
|
||||||
ticks: {
|
ticks: {
|
||||||
fontColor: co2Color,
|
fontColor: co2Color,
|
||||||
min: 400,
|
suggestedMin: 400,
|
||||||
max: 1100,
|
suggestedMax: 1100,
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
type: 'linear',
|
type: 'linear',
|
||||||
@@ -75,8 +82,8 @@
|
|||||||
id: 'y-axis-2',
|
id: 'y-axis-2',
|
||||||
ticks: {
|
ticks: {
|
||||||
fontColor: tempColor,
|
fontColor: tempColor,
|
||||||
min: 10,
|
suggestedMin: 10,
|
||||||
max: 35,
|
suggestedMax: 35,
|
||||||
},
|
},
|
||||||
gridLines: {
|
gridLines: {
|
||||||
drawOnChartArea: false,
|
drawOnChartArea: false,
|
||||||
@@ -88,8 +95,8 @@
|
|||||||
id: 'y-axis-3',
|
id: 'y-axis-3',
|
||||||
ticks: {
|
ticks: {
|
||||||
fontColor: humidityColor,
|
fontColor: humidityColor,
|
||||||
min: 15,
|
suggestedMin: 15,
|
||||||
max: 85,
|
suggestedMax: 85,
|
||||||
},
|
},
|
||||||
gridLines: {
|
gridLines: {
|
||||||
drawOnChartArea: false,
|
drawOnChartArea: false,
|
||||||
@@ -117,9 +124,9 @@
|
|||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
const newDatum = await fetch("data/1");
|
const newDatum = await fetch("data/1");
|
||||||
const snapshot = (await newDatum.json()).snapshots[0];
|
const snapshot = (await newDatum.json()).snapshots[0];
|
||||||
chart.data.datasets[0].data.push({x: snapshot.time, y: snapshot.humidity});
|
chart.data.datasets[0].data.splice(0, 1, {x: snapshot.time, y: snapshot.humidity});
|
||||||
chart.data.datasets[1].data.push({x: snapshot.time, y: snapshot.temp});
|
chart.data.datasets[1].data.splice(0, 1, {x: snapshot.time, y: snapshot.temp});
|
||||||
chart.data.datasets[2].data.push({x: snapshot.time, y: snapshot.co2});
|
chart.data.datasets[2].data.splice(0, 1, {x: snapshot.time, y: snapshot.co2});
|
||||||
chart.update(0);
|
chart.update(0);
|
||||||
}, 10 * 1000);
|
}, 10 * 1000);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,8 +36,9 @@ func startServer() {
|
|||||||
port := "8001"
|
port := "8001"
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/", showCharts).Methods("GET")
|
r.HandleFunc("/", showCharts).Methods("GET")
|
||||||
|
r.HandleFunc("/since/{mins}", showCharts).Methods("GET")
|
||||||
r.HandleFunc("/data/", sendData).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.HandleFunc("/", saveSnapshot).Methods("POST")
|
||||||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
@@ -46,13 +47,19 @@ func startServer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func showCharts(w http.ResponseWriter, r *http.Request) {
|
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) {
|
func sendData(w http.ResponseWriter, r *http.Request) {
|
||||||
var count int64 = 180
|
var count int64 = 60
|
||||||
if vars := mux.Vars(r); vars["count"] != "" {
|
if vars := mux.Vars(r); vars["mins"] != "" {
|
||||||
newCount, err := strconv.ParseInt(vars["count"], 10, 0)
|
newCount, err := strconv.ParseInt(vars["mins"], 10, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendInternalError(errors.New("bad snapshot count request"), w, r)
|
sendInternalError(errors.New("bad snapshot count request"), w, r)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user