From fff4ce74417a1b3047944ab2a7ad8cf3edc8111c Mon Sep 17 00:00:00 2001 From: Tobias Hopp Date: Sun, 21 May 2023 19:36:45 +0200 Subject: [PATCH] update --- .idea/runConfigurations/bin_www.xml | 1 + public/javascripts/main.js | 65 ++++++++++++++++++++++++----- public/stylesheets/style.css | 9 ++++ routes/index.js | 2 - views/index.pug | 19 ++++++++- 5 files changed, 82 insertions(+), 14 deletions(-) diff --git a/.idea/runConfigurations/bin_www.xml b/.idea/runConfigurations/bin_www.xml index 14bb4af..4bdd0b9 100644 --- a/.idea/runConfigurations/bin_www.xml +++ b/.idea/runConfigurations/bin_www.xml @@ -2,6 +2,7 @@ + diff --git a/public/javascripts/main.js b/public/javascripts/main.js index 8ec0739..cdb75d3 100644 --- a/public/javascripts/main.js +++ b/public/javascripts/main.js @@ -74,15 +74,15 @@ var chart = new Chart(ctx, { datasets: [{ label: 'PM2.5', data: [], - backgroundColor: 'rgba(150,160,6,0.2)', - borderColor: 'rgb(166,202,5)', + backgroundColor: 'rgba(99,113,10,0.5)', + borderColor: 'rgb(183,224,2)', borderWidth: 2, fill: false }, { label: 'PM10', data: [], - backgroundColor: 'rgba(0,156,144,0.2)', - borderColor: 'rgb(8,135,223)', + backgroundColor: 'rgba(161,2,189,0.5)', + borderColor: 'rgb(224,10,200)', borderWidth: 2, fill: false }] @@ -146,6 +146,13 @@ function addData(time, pm25, pm10) { // Aktualisiere das Chart chart.update(); } +function removeData() +{ + chart.data.labels.length = 0; + chart.data.datasets[0].data.length = 0; + chart.data.datasets[1].data.length = 0; + chart.update() +} @@ -153,7 +160,7 @@ setInterval(( ) => { //addData(new Date().toLocaleTimeString(), 1+Math.floor(Math.random() *2), 3+Math.floor(Math.random() * 2)); loadValues(); loadStatus(); -}, 2000); +}, 1000 * 60 * 3 ); function httpGetAsync(theUrl) @@ -173,16 +180,41 @@ function httpGetAsync(theUrl) let last_request = new Date(); last_request.setDate(last_request.getDate()-1); + function loadValues() { return new Promise( async () => { let data = await httpGetAsync("/data/" + last_request.getTime() ); - for( let entry of data ) + + let newData = []; + let last = 0; + for(let i = 0; i < data.length; i++) { - addData(new Date(entry.createdAt).toLocaleTimeString(), entry.pm25, entry.pm10) - pm25.innerText = "" + (Math.round(entry.pm25*100)/100); - pm10.innerText = "" + (Math.round(entry.pm10*100)/100); + if(last + 1000 * 60 * 15 < new Date(data[i].createdAt).getTime() ) + newData.push(data[i]); + last = new Date(data[i].createdAt).getTime(); } + + for( let entry of newData ) + { + let d = new Date(entry.createdAt); + addData(d.toLocaleDateString() + " " + d.toLocaleTimeString(), entry.pm25, entry.pm10) + } + + let entry = data[data.length-1]; + pm25.innerText = "" + (Math.round(entry.pm25*100)/100); + pm10.innerText = "" + (Math.round(entry.pm10*100)/100); + + if(entry.pm25 > 100 ) + pm25.style.color = "red"; + else + pm25.style.color = "black"; + + if(entry.pm10 > 100 ) + pm10.style.color = "red"; + else + pm10.style.color = "black"; + last_request = new Date(); }) } @@ -197,4 +229,17 @@ function loadStatus() }) } loadStatus(); -loadValues(); \ No newline at end of file +loadValues(); + + +let selector = document.getElementById("timespan_selector"); +selector.value = "24"; +selector.onchange = () => { + let val = Number.parseInt(selector.value); + + removeData(); + last_request = new Date(); + last_request.setHours(last_request.getHours()-val); + console.log("got", last_request); + loadValues(); +} \ No newline at end of file diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 3fde00f..71e52a0 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -24,6 +24,15 @@ h1 { overflow-x: hidden; } +#divLeft { + float:left; + font-size: 0.8em; +} #lastStatus { +} + +#divRight { + float:right; + margin-right: 2%; font-size: 0.8em; } \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 0a6e090..1c8f791 100644 --- a/routes/index.js +++ b/routes/index.js @@ -11,8 +11,6 @@ router.get('/', function(req, res, next) { router.get('/data/:timestamp', async (req,res) => { - let date = new Date(req.params.timestamp); - try { let entries = await PMEntry.find({ updatedAt: {$gt: req.params.timestamp} }); res.json(entries); diff --git a/views/index.pug b/views/index.pug index ad52c99..ab49db9 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,7 +1,22 @@ extends layout block content - h1 PM2.5 [] & PM10 [] - p#lastStatus + h1 PM2.5 [0] & PM10 [0] + div#divLeft + p#lastStatus + div#divRight + span Daten-Zeitspanne: + br + select#timespan_selector + option(value="1") 1 Stunde + option(value="5") 5 Stunden + option(value="12") 12 Stunden + option(value="24" selected) 1 Tag + option(value="72") 3 Tage + option(value="168") 7 Tage + option(value="672") 1 Monat (viele Daten!) + option(value="2016") 3 Monate (sehr viele Daten!) + option(value="8064") 1 Jahr (extrem viele Daten!) + option(value="99999999") Alles (unmengen an Daten!) div(style="width:100%; height:75vh;") canvas#chart