use time::{Duration, OffsetDateTime};
use crate::domain::{CarbonIntensity, ForecastPoint};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowEstimator {
Central,
Prudent,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GreenWindow {
pub start: OffsetDateTime,
pub end: OffsetDateTime,
pub average: CarbonIntensity,
}
pub fn greenest_window(
points: &[ForecastPoint],
duration: Duration,
estimator: WindowEstimator,
) -> Option<GreenWindow> {
if points.len() < 2 || duration <= Duration::ZERO {
return None;
}
let step = points[1].at - points[0].at;
if step <= Duration::ZERO {
return None;
}
let value = |p: &ForecastPoint| match estimator {
WindowEstimator::Central => p.expected.value(),
WindowEstimator::Prudent => p.upper.value(),
};
let slots = (duration.whole_seconds() as f64 / step.whole_seconds() as f64).ceil() as usize;
if slots == 0 || points.len() < slots {
return None;
}
let mut window_sum: f64 = points[..slots].iter().map(value).sum();
let mut best_sum = window_sum;
let mut best_start = 0usize;
for i in slots..points.len() {
window_sum += value(&points[i]) - value(&points[i - slots]);
if window_sum < best_sum {
best_sum = window_sum;
best_start = i - slots + 1;
}
}
let average = CarbonIntensity::new(best_sum / slots as f64)?;
let start = points[best_start].at;
let end = points[best_start + slots - 1].at + step;
Some(GreenWindow {
start,
end,
average,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::{Methodology, ModelVersion, Region};
fn p(at: OffsetDateTime, g: f64, half: f64) -> ForecastPoint {
ForecastPoint::new(
at,
Region::National,
CarbonIntensity::new(g).unwrap(),
CarbonIntensity::new((g - half).max(0.0)).unwrap(),
CarbonIntensity::new(g + half).unwrap(),
Methodology::rte_direct(),
ModelVersion::new("climatology", 1),
)
}
#[test]
fn finds_lowest_average_window_on_expected() {
let t0 = OffsetDateTime::UNIX_EPOCH;
let step = Duration::minutes(15);
let values = [100.0, 90.0, 10.0, 12.0, 80.0];
let points: Vec<ForecastPoint> = values
.iter()
.enumerate()
.map(|(i, &g)| p(t0 + step * (i as i32), g, 5.0))
.collect();
let w = greenest_window(&points, Duration::minutes(30), WindowEstimator::Central).unwrap();
assert_eq!(w.start, t0 + step * 2);
assert!(w.average.value() < 12.0);
}
#[test]
fn prudent_estimator_optimises_upper_bound() {
let t0 = OffsetDateTime::UNIX_EPOCH;
let step = Duration::minutes(15);
let points = vec![
p(t0, 10.0, 50.0),
p(t0 + step, 30.0, 2.0),
p(t0 + step * 2, 80.0, 2.0),
];
let central =
greenest_window(&points, Duration::minutes(15), WindowEstimator::Central).unwrap();
assert_eq!(central.start, t0);
let prudent =
greenest_window(&points, Duration::minutes(15), WindowEstimator::Prudent).unwrap();
assert_eq!(prudent.start, t0 + step);
}
#[test]
fn none_when_series_too_short() {
let t0 = OffsetDateTime::UNIX_EPOCH;
let points = [p(t0, 50.0, 5.0)];
assert!(
greenest_window(&points, Duration::minutes(30), WindowEstimator::Central).is_none()
);
}
}