Skip to content

Commit

Permalink
Replace AbortSignal.timeout
Browse files Browse the repository at this point in the history
because one of the devices I want this to run on doesn't support that.
Also check heap usage on request for prometheus, rather than every
second.
  • Loading branch information
ToMe25 committed Dec 26, 2023
1 parent 1dc0f26 commit 5d157ad
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
Binary file modified data/gzip/index.js.gz
Binary file not shown.
16 changes: 15 additions & 1 deletion data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ update_time=Date.now()
}

function update(){
fetch('data.json',{method:'get',signal:AbortSignal.timeout(3000)})
var options={method:'GET'}
var timeout
if(typeof(AbortController)=='function'){
const abort=new AbortController();
options.signal=abort.signal
timeout=setTimeout(()=>abort.abort(),3000);
}

fetch('data.json',options)
.then((res)=>{
if(timeout!=undefined){
clearTimeout(timeout)
}
return res.json()
}).then((out)=>{
temp_elemen.innerText=out.temperature
Expand All @@ -29,6 +40,9 @@ time_element.innerText=time_element.dateTime=out.time
json_time=parseTimeString(out.time)
update_time=Date.now()
}).catch((err)=>{
if(timeout!=undefined){
clearTimeout(timeout)
}
console.error('Error: ',err)
})
}
Expand Down
16 changes: 15 additions & 1 deletion src/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ function init() {
* This function fetches new measurements from the ESP and updates the page.
*/
function update() {
fetch('data.json', { method: 'get', signal: AbortSignal.timeout(3000) })
var options = { method: 'GET' }
var timeout
if (typeof (AbortController) == 'function') {
const abort = new AbortController();
options.signal = abort.signal
timeout = setTimeout(() => abort.abort(), 3000);
}

fetch('data.json', options)
.then((res) => {
if (timeout != undefined) {
clearTimeout(timeout)
}
return res.json()
}).then((out) => {
temp_elemen.innerText = out.temperature
Expand All @@ -39,6 +50,9 @@ function update() {
json_time = parseTimeString(out.time)
update_time = Date.now()
}).catch((err) => {
if (timeout != undefined) {
clearTimeout(timeout)
}
console.error('Error: ', err)
})
}
Expand Down
9 changes: 1 addition & 8 deletions src/prometheus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
#include <sstream>
#include "fallback_log.h"

#ifdef ESP32// From what I could find this seems to be impossible on a ESP8266.
uint32_t prom::used_heap = 0;
#endif
#if ENABLE_WEB_SERVER == 1 && (ENABLE_PROMETHEUS_PUSH == 1 || ENABLE_PROMETHEUS_SCRAPE_SUPPORT == 1)
std::map<String, std::map<std::pair<WebRequestMethod, uint16_t>, uint64_t>> prom::http_requests_total;
#endif
Expand All @@ -36,11 +33,6 @@ void prom::setup() {
}

void prom::loop() {
#ifdef ESP32// From what I could find this seems to be impossible on a ESP8266.
// TODO move to getMetrics
used_heap = ESP.getHeapSize() - ESP.getFreeHeap();
#endif

#if ENABLE_PROMETHEUS_PUSH == 1
pushMetrics();
#endif
Expand Down Expand Up @@ -114,6 +106,7 @@ String prom::getMetrics(const bool openmetrics) {

// From what I could find this seems to be impossible on a ESP8266.
#ifdef ESP32
const uint64_t used_heap = ESP.getHeapSize() - ESP.getFreeHeap();
len += writeMetric(buffer + len, "process", "heap", "bytes",
"The amount of heap used on the ESP in bytes.", "gauge",
(double) used_heap, openmetrics);
Expand Down
3 changes: 0 additions & 3 deletions src/prometheus.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
* This header, and the source file with the same name, contain everything for the prometheus integration.
*/
namespace prom {
#ifdef ESP32// From what I could find this seems to be impossible on a ESP8266.
extern uint32_t used_heap;
#endif
#if ENABLE_WEB_SERVER == 1 && (ENABLE_PROMETHEUS_PUSH == 1 || ENABLE_PROMETHEUS_SCRAPE_SUPPORT == 1)
extern std::map<String, std::map<std::pair<WebRequestMethod, uint16_t>, uint64_t>> http_requests_total;
#endif
Expand Down

0 comments on commit 5d157ad

Please sign in to comment.