From 096e0abdb7d710075a1c84115ecd70ccdbe17969 Mon Sep 17 00:00:00 2001 From: Dominik Werder Date: Sat, 21 Sep 2024 21:20:42 +0200 Subject: [PATCH] WIP --- .../items_2/src/binning/container_events.rs | 2 +- .../binning/timeweight/timeweight_events.rs | 141 ++++++++++++------ 2 files changed, 98 insertions(+), 45 deletions(-) diff --git a/crates/items_2/src/binning/container_events.rs b/crates/items_2/src/binning/container_events.rs index d2378f1..09e0a83 100644 --- a/crates/items_2/src/binning/container_events.rs +++ b/crates/items_2/src/binning/container_events.rs @@ -25,7 +25,7 @@ pub trait Container: fmt::Debug + Clone + PreviewRange + Serialize + for<'a fn pop_front(&mut self) -> Option; } -pub trait EventValueType: fmt::Debug + Clone { +pub trait EventValueType: fmt::Debug + Clone + PartialOrd { type Container: Container; type AggregatorTimeWeight: AggregatorTimeWeight; } diff --git a/crates/items_2/src/binning/timeweight/timeweight_events.rs b/crates/items_2/src/binning/timeweight/timeweight_events.rs index 7612d63..8a8f345 100644 --- a/crates/items_2/src/binning/timeweight/timeweight_events.rs +++ b/crates/items_2/src/binning/timeweight/timeweight_events.rs @@ -29,13 +29,13 @@ pub enum Error { type MinMax = (EventSingle, EventSingle); -struct InnerA { +struct InnerB { range: BinnedRange, cnt: u64, _t1: PhantomData, } -impl InnerA +impl InnerB where EVT: EventValueType, { @@ -117,25 +117,6 @@ where Ok(()) } - fn ingest_with_lst_nominmax( - &mut self, - evs: ContainerEvents, - lst: &mut EventSingle, - minmax: &mut Option>, - ) -> Result<(), Error> { - // TODO how to handle the min max? I don't take event data yet out of the container. - todo!("minmax handle"); - if let Some(ts0) = evs.ts_first() { - if ts0 < self.range.nano_beg() { - Err(Error::AnotherBeforeRange) - } else { - self.ingest_with_lst_ge_range_beg(evs, lst) - } - } else { - Err(Error::EmptyContainerInnerHandler) - } - } - fn ingest_with_lst_minmax( &mut self, evs: ContainerEvents, @@ -156,13 +137,54 @@ where } } +struct InnerA { + inner_b: InnerB, + minmax: Option<(EventSingle, EventSingle)>, +} + +impl InnerA +where + EVT: EventValueType, +{ + fn ingest_with_lst_nominmax( + &mut self, + evs: ContainerEvents, + lst: &mut EventSingle, + minmax: &mut Option>, + ) -> Result<(), Error> { + // TODO how to handle the min max? I don't take event data yet out of the container. + todo!("minmax handle"); + if let Some(ts0) = evs.ts_first() { + if ts0 < self.inner_b.range.nano_beg() { + Err(Error::AnotherBeforeRange) + } else { + self.inner_b.ingest_with_lst_ge_range_beg(evs, lst) + } + } else { + Err(Error::EmptyContainerInnerHandler) + } + } + + fn ingest_with_lst(&mut self, evs: ContainerEvents, lst: &mut EventSingle) -> Result<(), Error> { + if let Some(minmax) = self.minmax.as_mut() { + // TODO + // Maybe? + self.inner_b.ingest_with_lst_minmax(evs, lst, minmax)?; + todo!() + } else { + // self.ingest_with_lst_without_minmax(evs, lst) + // TODO we have lst, so expect that this next event gives us minmax. + todo!() + } + } +} + pub struct BinnedEventsTimeweight where EVT: EventValueType, { inner_a: InnerA, lst: Option>, - minmax: Option<(EventSingle, EventSingle)>, } impl BinnedEventsTimeweight @@ -172,41 +194,76 @@ where pub fn new(range: BinnedRange) -> Self { Self { inner_a: InnerA:: { - range, - cnt: 0, - _t1: PhantomData, + inner_b: InnerB { + range, + cnt: 0, + _t1: PhantomData, + }, + minmax: None, }, lst: None, - minmax: None, + } + } + + fn apply_min_max(ev: &EventSingle, minmax: &mut MinMax) { + if ev.val < minmax.0.val { + minmax.0 = ev.clone(); + } + if ev.val > minmax.1.val { + minmax.1 = ev.clone(); } } fn ingest_event_without_lst(&mut self, ev: EventSingle) -> Result<(), Error> { - let range = &self.inner_a.range; + let range = &self.inner_a.inner_b.range; let beg = range.nano_beg(); let end = range.nano_end(); if ev.ts < end { + self.lst = Some(ev.clone()); if ev.ts >= beg { - self.minmax = Some((ev.clone(), ev.clone())); - self.inner_a.cnt += 1; + self.inner_a.minmax = Some((ev.clone(), ev.clone())); + self.inner_a.inner_b.cnt += 1; } - self.lst = Some(ev); + } else { + todo!("must cycle forward, can probably not produce a bin at all until then") } Ok(()) } - fn ingest_without_lst(&mut self, mut evs: ContainerEvents) -> Result<(), Error> { + fn _________ingest_with_lst_without_minmax( + &mut self, + mut evs: ContainerEvents, + lst: &mut EventSingle, + ) -> Result<(), Error> { if let Some(ev) = evs.event_next() { - self.ingest_event_without_lst(ev)?; - } - if let Some(lst) = self.lst.as_mut() { - if let Some(minmax) = self.minmax.as_mut() { - self.inner_a.ingest_with_lst_minmax(evs, lst, minmax) + if ev.ts >= self.inner_a.inner_b.range.nano_end() { + // need to cycle, then apply the event again... + todo!("cycle and reapply"); } else { - self.inner_a.ingest_with_lst_nominmax(evs, lst, &mut self.minmax) + // self.ingest_event_with_lst_without_minmax(ev, lst)?; + Ok(()) } } else { - Err(Error::NoLstAfterFirst) + Ok(()) + } + } + + fn ingest_without_lst(&mut self, mut evs: ContainerEvents) -> Result<(), Error> { + if let Some(ev) = evs.event_next() { + if ev.ts >= self.inner_a.inner_b.range.nano_end() { + // need to cycle, then apply the event again... + // TODO write that retry as a loop with iter limit. + todo!("cycle and reapply"); + } else { + self.ingest_event_without_lst(ev)?; + if let Some(lst) = self.lst.as_mut() { + self.inner_a.ingest_with_lst(evs, lst) + } else { + Err(Error::NoLstAfterFirst) + } + } + } else { + Ok(()) } } @@ -214,13 +271,9 @@ where // and with respect to the last container, if any. fn ingest_ordered(&mut self, evs: ContainerEvents) -> Result<(), Error> { if let Some(lst) = self.lst.as_mut() { - if let Some(minmax) = self.minmax.as_mut() { - self.inner_a.ingest_with_lst_minmax(evs, lst, minmax) - } else { - self.inner_a.ingest_with_lst_nominmax(evs, lst, &mut self.minmax) - } + self.inner_a.ingest_with_lst(evs, lst) } else { - if self.minmax.is_some() { + if self.inner_a.minmax.is_some() { Err(Error::NoLstButMinMax) } else { self.ingest_without_lst(evs)