diff --git a/src/types.rs b/src/types.rs index e99ad5a..f203f45 100644 --- a/src/types.rs +++ b/src/types.rs @@ -3,6 +3,7 @@ use anyhow; // A simple type alias so as to DRY. pub type Result = anyhow::Result; +pub type TimeSerie = Vec<(u64, f64)>; #[derive(Clone)] pub struct PrometheusConfig { @@ -39,3 +40,5 @@ pub struct QueryConfig { pub to: u64, pub step: u64, } + + diff --git a/src/utils.rs b/src/utils.rs index f3ed790..55e2435 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,7 +2,7 @@ use bytes::{buf::Reader, Buf}; use hyper::{body, Body, Client, Method, Request, StatusCode}; use std::{collections::HashMap, io::Read}; -use crate::types; +use crate::types::{self, TimeSerie}; pub fn print_buf(mut reader: Reader) { let mut dst = [0; 1024]; @@ -82,6 +82,54 @@ pub async fn post_with_headers( let body = hyper::body::aggregate(res).await?; let reader = body.reader(); - // Err(anyhow::format_err!("bad")) Ok((status, reader)) } + + +// TODO: cover this function with tests +pub fn interpolate_nans_and_gaps_with_zeros(ts: &TimeSerie, from: u64, to: u64, step: u64) -> TimeSerie { + // add zeros in the begining + let mut result: TimeSerie = Vec::new(); + let mut t = from; + + if ts.len() == 0 { + while t + step < to { + result.push((t, 0.0)); + t += step; + } + return result; + } + + while t + step < ts[0].0 { + result.push((t, 0.0)); + t += step; + } + + let mut i = 0usize; + while i + 1 < ts.len() { + let mut my_t = ts[i].0; + let v = if f64::is_nan(ts[i].1) { 0.0 } else { ts[i].1 }; + result.push((my_t, v)); + + let next_t = ts[i + 1].0; + + while my_t + step < next_t { + my_t += step; + result.push((my_t, 0.0)); + } + + i += 1; + } + + let mut t = ts.last().unwrap().0; + let v = if f64::is_nan(ts.last().unwrap().1) { 0.0 } else { ts.last().unwrap().1 }; + + result.push((t, v)); + + while t + step < to { + t += step; + result.push((t, 0.0)); + } + + return result; +}