solved duplicate points error

This commit is contained in:
2026-08-30 13:38:55 +02:00
parent 4c216abb1f
commit 67d8b50333
3 changed files with 33 additions and 6 deletions
+4 -4
View File
@@ -20,14 +20,15 @@ class Collector:
def collect_once(self) -> int:
samples = extract_samples(self.client.stations_data(), self.module_names)
updated = 0
for name, sample in samples.items():
try:
self.store.update(name, sample)
updated += bool(self.store.update(name, sample))
except Exception:
# One stale/duplicate module must not discard the other modules.
LOG.exception("Could not update %s RRD", name)
LOG.info("Collected %d Netatmo modules", len(samples))
return len(samples)
LOG.info("Collected %d Netatmo modules; updated %d RRDs", len(samples), updated)
return updated
def start(self) -> None:
if self._thread and self._thread.is_alive():
@@ -48,4 +49,3 @@ class Collector:
LOG.exception("Netatmo collection failed")
remaining = max(1, self.interval - (time.monotonic() - started))
self._stop.wait(remaining)
+15 -2
View File
@@ -1,6 +1,7 @@
from __future__ import annotations
import colorsys
import logging
import re
import tempfile
from pathlib import Path
@@ -10,6 +11,7 @@ import rrdtool
from .data import Sample
RRD_ERROR = rrdtool.OperationalError
LOG = logging.getLogger(__name__)
STEP = 600
HEARTBEAT = 1500
@@ -53,15 +55,26 @@ class RRDStore:
"RRA:AVERAGE:0.5:36:1464", "RRA:MIN:0.5:36:1464", "RRA:MAX:0.5:36:1464"]
rrdtool.create(str(path), *args)
def update(self, name: str, sample: Sample) -> None:
def update(self, name: str, sample: Sample) -> bool:
"""Store at most one sample per RRD step; return whether it was written."""
path = self.path(name)
last_timestamp = int(rrdtool.last(str(path)))
if sample.timestamp // STEP <= last_timestamp // STEP:
LOG.info(
"Skipping %s RRD update: sample interval already stored "
"(sample_timestamp=%d last_timestamp=%d)",
name, sample.timestamp, last_timestamp,
)
return False
schema = SCHEMAS[name]
fields = list(schema)
values = ["U" if sample.values.get(field) is None else str(sample.values[field]) for field in fields]
rrdtool.update(
str(self.path(name)),
str(path),
"--template", ":".join(fields),
f"{sample.timestamp}:{':'.join(values)}",
)
return True
def last(self, name: str, field: str) -> dict:
if name not in SCHEMAS or field not in SCHEMAS[name]: