From 8c981ae37907cd65856afe22ea4e1a48da0435f2 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 23 May 2020 16:03:43 +0200 Subject: [PATCH] rrd: fix display interval, try to avoid numeric errors --- src/rrd/rrd.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/rrd/rrd.rs b/src/rrd/rrd.rs index 0088e3bb..1553e72c 100644 --- a/src/rrd/rrd.rs +++ b/src/rrd/rrd.rs @@ -2,7 +2,6 @@ use std::io::Read; use std::path::Path; use anyhow::{bail, Error}; -use serde_json::{json, Value}; use crate::api2::types::{RRDMode, RRDTimeFrameResolution}; @@ -49,7 +48,7 @@ impl RRD { let reso = timeframe as u64; - let end = reso*((epoch + reso -1)/reso); + let end = reso*(epoch/reso + 1); let start = end - reso*(RRD_DATA_ENTRIES as u64); let rrd_end = reso*(self.last_update/reso); @@ -140,7 +139,10 @@ impl RRD { RRDEntry { max: value, average: value, count: 1 } } else { let new_max = if max > value { max } else { value }; - let new_average = (average*(count as f64) + value)/(new_count as f64); + //let new_average = (average*(count as f64) + value)/(new_count as f64); + // Note: Try to avoid numeric errors + let new_average = average*((count as f64)/(new_count as f64)) + + value/(new_count as f64); RRDEntry { max: new_max, average: new_average, count: new_count } } }