This commit is contained in:
Dominik Werder
2024-09-19 13:19:42 +02:00
parent 049266bfe5
commit 4365d24280
8 changed files with 146 additions and 67 deletions

View File

@@ -1,5 +1,8 @@
pub mod aggregator;
pub mod container_events;
pub mod test;
pub mod timeweight;
#[cfg(test)]
mod test;
use super::binning as ___;

View File

@@ -0,0 +1,19 @@
use std::marker::PhantomData;
pub trait AggregatorTimeWeight {}
pub struct AggregatorNumeric<T> {
_t0: PhantomData<T>,
}
trait AggWithSame {}
impl AggWithSame for f64 {}
impl<T> AggregatorTimeWeight for AggregatorNumeric<T> where T: AggWithSame {}
impl AggregatorTimeWeight for AggregatorNumeric<f32> {}
impl AggregatorTimeWeight for AggregatorNumeric<u64> {}
// TODO do enum right from begin, using a SOA enum container.

View File

@@ -1,42 +1,82 @@
use super::aggregator::AggregatorNumeric;
use super::aggregator::AggregatorTimeWeight;
use super::___;
use crate::vecpreview::PreviewRange;
use crate::vecpreview::VecPreview;
use core::fmt;
use netpod::TsNano;
use serde::Deserialize;
use serde::Serialize;
use std::any;
use std::collections::VecDeque;
#[allow(unused)]
macro_rules! trace_init { ($($arg:tt)*) => ( if true { trace!($($arg)*); }) }
pub trait Container: Clone {}
pub trait Container: fmt::Debug + Clone + PreviewRange + Serialize + for<'a> Deserialize<'a> {
fn new() -> Self;
}
impl<T> Container for VecDeque<T> where T: EventValueType {}
pub trait EventValueType: Clone {
pub trait EventValueType: fmt::Debug + Clone {
type Container: Container;
type AggregatorTimeWeight: AggregatorTimeWeight;
}
impl<T> Container for VecDeque<T>
where
T: EventValueType + Serialize + for<'a> Deserialize<'a>,
{
fn new() -> Self {
VecDeque::new()
}
}
impl EventValueType for f32 {
type Container = VecDeque<Self>;
type AggregatorTimeWeight = AggregatorNumeric<Self>;
}
#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct ContainerEvents<EVT>
where
EVT: EventValueType,
{
tss: VecDeque<TsNano>,
// vals: VecDeque<EVT>,
vals: VecDeque<<EVT as EventValueType>::Container>,
vals: <EVT as EventValueType>::Container,
}
// TODO why does this already impl Serialize even though there is no bound for EVT?
// TODO try to actually instantiate and serialize in a test.
#[derive(Clone, Serialize, Deserialize)]
pub struct ContainerEvents2<EVT>
impl<EVT> ContainerEvents<EVT>
where
EVT: EventValueType,
{
tss: VecDeque<TsNano>,
vals: VecDeque<EVT>,
pub fn type_name() -> &'static str {
any::type_name::<Self>()
}
pub fn new() -> Self {
Self {
tss: VecDeque::new(),
vals: Container::new(),
}
}
pub fn len(&self) -> usize {
self.tss.len()
}
}
impl<EVT> fmt::Debug for ContainerEvents<EVT>
where
EVT: EventValueType,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let self_name = any::type_name::<Self>();
write!(
fmt,
"{self_name} {{ len: {:?}, tss: {:?}, vals {:?} }}",
self.len(),
VecPreview::new(&self.tss),
VecPreview::new(&self.vals),
)
}
}

View File

@@ -1,2 +1,14 @@
use super::container_events::ContainerEvents;
use super::___;
use netpod::log::*;
use std::any;
#[test]
fn test_use_serde() {
let x = ContainerEvents::<f32>::new();
let a: &dyn any::Any = &x;
assert_eq!(a.downcast_ref::<String>().is_some(), false);
assert_eq!(a.downcast_ref::<ContainerEvents<f32>>().is_some(), true);
let s = serde_json::to_string(&x).unwrap();
let _: ContainerEvents<f32> = serde_json::from_str(&s).unwrap();
}

View File

@@ -1,8 +1,9 @@
use super::super::container_events::EventValueType;
use super::___;
use err::thiserror;
use err::ThisError;
use futures_util::Stream;
use netpod::log::*;
use std::collections::VecDeque;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::Context;
@@ -11,6 +12,10 @@ use std::task::Poll;
#[allow(unused)]
macro_rules! trace_init { ($($arg:tt)*) => ( if true { trace!($($arg)*); }) }
#[derive(Debug, ThisError)]
#[cstm(name = "BinnedEventsTimeweight")]
pub enum Error {}
pub struct BinnedEventsTimeweight<EVT>
where
EVT: EventValueType,
@@ -18,7 +23,22 @@ where
_evt: PhantomData<EVT>,
}
impl<EVT> BinnedEventsTimeweight<EVT> where EVT: EventValueType {}
impl<EVT> BinnedEventsTimeweight<EVT>
where
EVT: EventValueType,
{
pub fn ingest(&mut self, evs: <EVT as EventValueType>::Container) -> Result<(), Error> {
// It is this type's task to find and store the one-before event.
// We then pass it to the aggregation.
// AggregatorTimeWeight needs a function for that.
// What about counting the events that actually fall into the range?
// Maybe that should be done in this type.
// That way we can pass the values and weights to the aggregation, and count the in-range here.
// This type must also "close" the current aggregation by passing the "last" and init the next.
// ALSO: need to keep track of the "lst". Probably best done in this type as well?
todo!()
}
}
pub struct BinnedEventsTimeweightStream {}

View File

@@ -2,6 +2,7 @@ use crate::timebin::TimeBinnerCommonV0Func;
use crate::timebin::TimeBinnerCommonV0Trait;
use crate::ts_offs_from_abs;
use crate::ts_offs_from_abs_with_anchor;
use crate::vecpreview::VecPreview;
use crate::IsoDateTime;
use crate::RangeOverlapInfo;
use crate::TimeBinnableType;
@@ -79,9 +80,9 @@ where
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let self_name = any::type_name::<Self>();
// if true {
// return fmt::Display::fmt(self, fmt);
// }
if true {
return fmt::Display::fmt(self, fmt);
}
if true {
write!(
fmt,
@@ -110,60 +111,15 @@ where
}
}
trait HasFrontBack<T> {
fn len(&self) -> usize;
fn front(&self) -> Option<&T>;
fn back(&self) -> Option<&T>;
}
impl<T> HasFrontBack<T> for VecDeque<T> {
fn len(&self) -> usize {
self.len()
}
fn front(&self) -> Option<&T> {
self.front()
}
fn back(&self) -> Option<&T> {
self.back()
}
}
struct VecPreview<'a, T> {
c: &'a dyn HasFrontBack<T>,
}
impl<'a, T> VecPreview<'a, T> {
fn new(c: &'a dyn HasFrontBack<T>) -> Self {
Self { c }
}
}
impl<'a, T> fmt::Display for VecPreview<'a, T>
where
T: fmt::Display,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
if self.c.len() == 0 {
write!(fmt, "()")
} else if self.c.len() == 1 {
write!(fmt, "{}", self.c.front().unwrap())
} else {
write!(fmt, "{}", self.c.front().unwrap())
}
}
}
impl<NTY> fmt::Display for BinsDim0<NTY>
where
NTY: fmt::Display,
NTY: fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let self_name = any::type_name::<Self>();
write!(
fmt,
"{self_name} {{ len: {:?}, ts1s: {}, ts2s {}, counts {}, mins {}, maxs {}, avgs {}, lsts {} }}",
"{self_name} {{ len: {:?}, ts1s: {:?}, ts2s {:?}, counts {:?}, mins {:?}, maxs {:?}, avgs {:?}, lsts {:?} }}",
self.len(),
VecPreview::new(&self.ts1s),
VecPreview::new(&self.ts2s),

View File

@@ -20,6 +20,7 @@ pub mod test;
pub mod testgen;
pub mod timebin;
pub mod transform;
pub mod vecpreview;
use channelevents::ChannelEvents;
use futures_util::Stream;

View File

@@ -0,0 +1,28 @@
use core::fmt;
use std::collections::VecDeque;
pub trait PreviewRange {
fn preview(&self) -> &dyn fmt::Debug;
}
impl<T> PreviewRange for VecDeque<T> {
fn preview(&self) -> &dyn fmt::Debug {
todo!()
}
}
pub struct VecPreview<'a> {
c: &'a dyn PreviewRange,
}
impl<'a> VecPreview<'a> {
pub fn new(c: &'a dyn PreviewRange) -> Self {
Self { c }
}
}
impl<'a> fmt::Debug for VecPreview<'a> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{:?}", self.c.preview())
}
}