Move binned type, add tests
This commit is contained in:
@@ -19,6 +19,12 @@ use std::any::Any;
|
||||
use std::collections::VecDeque;
|
||||
use std::{fmt, mem};
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! trace4 {
|
||||
($($arg:tt)*) => ();
|
||||
($($arg:tt)*) => (eprintln!($($arg)*));
|
||||
}
|
||||
|
||||
// TODO make members private
|
||||
#[derive(Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct BinsDim0<NTY> {
|
||||
@@ -51,17 +57,6 @@ where
|
||||
}
|
||||
|
||||
impl<NTY: ScalarOps> BinsDim0<NTY> {
|
||||
pub fn empty() -> Self {
|
||||
Self {
|
||||
ts1s: VecDeque::new(),
|
||||
ts2s: VecDeque::new(),
|
||||
counts: VecDeque::new(),
|
||||
mins: VecDeque::new(),
|
||||
maxs: VecDeque::new(),
|
||||
avgs: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, ts1: u64, ts2: u64, count: u64, min: NTY, max: NTY, avg: f32) {
|
||||
self.ts1s.push_back(ts1);
|
||||
self.ts2s.push_back(ts2);
|
||||
@@ -119,6 +114,19 @@ impl<NTY: ScalarOps> BinsDim0<NTY> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> Empty for BinsDim0<NTY> {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
ts1s: VecDeque::new(),
|
||||
ts2s: VecDeque::new(),
|
||||
counts: VecDeque::new(),
|
||||
mins: VecDeque::new(),
|
||||
maxs: VecDeque::new(),
|
||||
avgs: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> WithLen for BinsDim0<NTY> {
|
||||
fn len(&self) -> usize {
|
||||
self.ts1s.len()
|
||||
@@ -151,19 +159,6 @@ impl<NTY> RangeOverlapInfo for BinsDim0<NTY> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> Empty for BinsDim0<NTY> {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
ts1s: Default::default(),
|
||||
ts2s: Default::default(),
|
||||
counts: Default::default(),
|
||||
mins: Default::default(),
|
||||
maxs: Default::default(),
|
||||
avgs: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY: ScalarOps> AppendEmptyBin for BinsDim0<NTY> {
|
||||
fn append_empty_bin(&mut self, ts1: u64, ts2: u64) {
|
||||
self.ts1s.push_back(ts1);
|
||||
@@ -207,7 +202,8 @@ impl<NTY: ScalarOps> TimeBinnableType for BinsDim0<NTY> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
// TODO rename to BinsDim0CollectorOutput
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct BinsDim0CollectedResult<NTY> {
|
||||
#[serde(rename = "tsAnchor")]
|
||||
ts_anchor_sec: u64,
|
||||
@@ -219,17 +215,23 @@ pub struct BinsDim0CollectedResult<NTY> {
|
||||
ts1_off_ns: VecDeque<u64>,
|
||||
#[serde(rename = "ts2Ns")]
|
||||
ts2_off_ns: VecDeque<u64>,
|
||||
#[serde(rename = "counts")]
|
||||
counts: VecDeque<u64>,
|
||||
#[serde(rename = "mins")]
|
||||
mins: VecDeque<NTY>,
|
||||
#[serde(rename = "maxs")]
|
||||
maxs: VecDeque<NTY>,
|
||||
#[serde(rename = "avgs")]
|
||||
avgs: VecDeque<f32>,
|
||||
#[serde(skip_serializing_if = "crate::bool_is_false", rename = "finalisedRange")]
|
||||
#[serde(rename = "rangeFinal", default, skip_serializing_if = "crate::bool_is_false")]
|
||||
finalised_range: bool,
|
||||
#[serde(skip_serializing_if = "Zero::is_zero", rename = "missingBins")]
|
||||
#[serde(rename = "timedOut", default, skip_serializing_if = "crate::bool_is_false")]
|
||||
timed_out: bool,
|
||||
#[serde(rename = "missingBins", default, skip_serializing_if = "Zero::is_zero")]
|
||||
missing_bins: u32,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename = "continueAt")]
|
||||
#[serde(rename = "continueAt", default, skip_serializing_if = "Option::is_none")]
|
||||
continue_at: Option<IsoDateTime>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename = "finishedAt")]
|
||||
#[serde(rename = "finishedAt", default, skip_serializing_if = "Option::is_none")]
|
||||
finished_at: Option<IsoDateTime>,
|
||||
}
|
||||
|
||||
@@ -242,14 +244,30 @@ impl<NTY: ScalarOps> items_0::AsAnyRef for BinsDim0CollectedResult<NTY> {
|
||||
impl<NTY: ScalarOps> items_0::collect_c::Collected for BinsDim0CollectedResult<NTY> {}
|
||||
|
||||
impl<NTY> BinsDim0CollectedResult<NTY> {
|
||||
pub fn len(&self) -> usize {
|
||||
self.ts1_off_ms.len()
|
||||
}
|
||||
|
||||
pub fn ts_anchor_sec(&self) -> u64 {
|
||||
self.ts_anchor_sec
|
||||
}
|
||||
|
||||
pub fn ts1_off_ms(&self) -> &VecDeque<u64> {
|
||||
&self.ts1_off_ms
|
||||
}
|
||||
|
||||
pub fn ts2_off_ms(&self) -> &VecDeque<u64> {
|
||||
&self.ts2_off_ms
|
||||
}
|
||||
|
||||
pub fn counts(&self) -> &VecDeque<u64> {
|
||||
&self.counts
|
||||
}
|
||||
|
||||
pub fn range_final(&self) -> bool {
|
||||
self.finalised_range
|
||||
}
|
||||
|
||||
pub fn missing_bins(&self) -> u32 {
|
||||
self.missing_bins
|
||||
}
|
||||
@@ -265,6 +283,10 @@ impl<NTY> BinsDim0CollectedResult<NTY> {
|
||||
pub fn maxs(&self) -> &VecDeque<NTY> {
|
||||
&self.maxs
|
||||
}
|
||||
|
||||
pub fn avgs(&self) -> &VecDeque<f32> {
|
||||
&self.avgs
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY: ScalarOps> ToJsonResult for BinsDim0CollectedResult<NTY> {
|
||||
@@ -278,6 +300,7 @@ impl<NTY: ScalarOps> ToJsonResult for BinsDim0CollectedResult<NTY> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BinsDim0Collector<NTY> {
|
||||
timed_out: bool,
|
||||
range_complete: bool,
|
||||
@@ -305,6 +328,7 @@ impl<NTY: ScalarOps> CollectorType for BinsDim0Collector<NTY> {
|
||||
type Output = BinsDim0CollectedResult<NTY>;
|
||||
|
||||
fn ingest(&mut self, src: &mut Self::Input) {
|
||||
trace!("\n\n----------- BinsDim0Collector ingest\n{:?}\n\n", src);
|
||||
// TODO could be optimized by non-contiguous container.
|
||||
self.vals.ts1s.append(&mut src.ts1s);
|
||||
self.vals.ts2s.append(&mut src.ts2s);
|
||||
@@ -362,6 +386,7 @@ impl<NTY: ScalarOps> CollectorType for BinsDim0Collector<NTY> {
|
||||
maxs,
|
||||
avgs,
|
||||
finalised_range: self.range_complete,
|
||||
timed_out: self.timed_out,
|
||||
missing_bins,
|
||||
continue_at,
|
||||
finished_at,
|
||||
@@ -378,6 +403,73 @@ impl<NTY: ScalarOps> CollectableType for BinsDim0<NTY> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> items_0::collect_c::Collector for BinsDim0Collector<NTY>
|
||||
where
|
||||
NTY: ScalarOps,
|
||||
{
|
||||
fn len(&self) -> usize {
|
||||
self.vals.len()
|
||||
}
|
||||
|
||||
fn ingest(&mut self, item: &mut dyn items_0::collect_c::Collectable) {
|
||||
trace4!("\n\n••••••••••••••••••••••••••••\nINGEST\n{:?}\n\n", item);
|
||||
{
|
||||
{
|
||||
//let tyid = item.type_id();
|
||||
//let tyid = item.as_any_mut().type_id();
|
||||
//let tyid = format!("{:?}", item.type_id().to_owned());
|
||||
trace4!("ty 0: {:40?}", (item.as_any_mut() as &dyn Any).type_id());
|
||||
}
|
||||
trace4!("ty 1: {:40?}", std::any::TypeId::of::<BinsDim0<NTY>>());
|
||||
trace4!("ty 2: {:40?}", std::any::TypeId::of::<BinsDim0<i32>>());
|
||||
trace4!("ty 3: {:40?}", std::any::TypeId::of::<Box<BinsDim0<i32>>>());
|
||||
trace4!(
|
||||
"ty 4: {:?}",
|
||||
std::any::TypeId::of::<Box<dyn items_0::collect_c::Collectable>>()
|
||||
);
|
||||
trace4!(
|
||||
"ty 5: {:?}",
|
||||
std::any::TypeId::of::<&mut dyn items_0::collect_c::Collectable>()
|
||||
);
|
||||
trace4!("ty 6: {:?}", std::any::TypeId::of::<Box<dyn items_0::TimeBinned>>());
|
||||
}
|
||||
if let Some(item) = item.as_any_mut().downcast_mut::<BinsDim0<NTY>>() {
|
||||
trace4!("ingest plain");
|
||||
CollectorType::ingest(self, item)
|
||||
} else if let Some(item) = item.as_any_mut().downcast_mut::<Box<BinsDim0<NTY>>>() {
|
||||
trace4!("ingest boxed");
|
||||
CollectorType::ingest(self, item)
|
||||
} else if let Some(item) = item.as_any_mut().downcast_mut::<Box<dyn items_0::TimeBinned>>() {
|
||||
trace4!("ingest boxed dyn TimeBinned");
|
||||
if let Some(item) = item.as_any_mut().downcast_mut::<BinsDim0<NTY>>() {
|
||||
trace4!("ingest boxed dyn TimeBinned match");
|
||||
CollectorType::ingest(self, item)
|
||||
} else {
|
||||
warn!("BinsDim0Collector::ingest unexpected inner item");
|
||||
trace!("BinsDim0Collector::ingest unexpected inner item {:?}", item);
|
||||
}
|
||||
} else {
|
||||
warn!("BinsDim0Collector::ingest unexpected item");
|
||||
trace!("BinsDim0Collector::ingest unexpected item {:?}", item);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_range_complete(&mut self) {
|
||||
CollectorType::set_range_complete(self)
|
||||
}
|
||||
|
||||
fn set_timed_out(&mut self) {
|
||||
CollectorType::set_timed_out(self)
|
||||
}
|
||||
|
||||
fn result(&mut self) -> Result<Box<dyn items_0::collect_c::Collected>, Error> {
|
||||
match CollectorType::result(self) {
|
||||
Ok(res) => Ok(Box::new(res)),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BinsDim0Aggregator<NTY> {
|
||||
range: NanoRange,
|
||||
count: u64,
|
||||
@@ -510,7 +602,7 @@ impl<NTY: ScalarOps> TimeBinner for BinsDim0TimeBinner<NTY> {
|
||||
return;
|
||||
}
|
||||
if self.edges.len() < 2 {
|
||||
warn!("TimeBinnerDyn for {self_name} no more bin in edges A");
|
||||
warn!("TimeBinnerDyn for {self_name} no more bin in edges A\n{:?}\n\n", item);
|
||||
return;
|
||||
}
|
||||
// TODO optimize by remembering at which event array index we have arrived.
|
||||
@@ -522,7 +614,7 @@ impl<NTY: ScalarOps> TimeBinner for BinsDim0TimeBinner<NTY> {
|
||||
}) {
|
||||
self.cycle();
|
||||
if self.edges.len() < 2 {
|
||||
warn!("TimeBinnerDyn for {self_name} no more bin in edges B");
|
||||
warn!("TimeBinnerDyn for {self_name} no more bin in edges B\n{:?}\n\n", item);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -560,7 +652,7 @@ impl<NTY: ScalarOps> TimeBinner for BinsDim0TimeBinner<NTY> {
|
||||
if item.ends_after(agg.range().clone()) {
|
||||
self.cycle();
|
||||
if self.edges.len() < 2 {
|
||||
warn!("TimeBinnerDyn for {self_name} no more bin in edges C");
|
||||
warn!("TimeBinnerDyn for {self_name} no more bin in edges C\n{:?}\n\n", item);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
@@ -692,3 +784,21 @@ impl<NTY: ScalarOps> TimeBinned for BinsDim0<NTY> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> items_0::AsAnyMut for BinsDim0<NTY>
|
||||
where
|
||||
NTY: 'static,
|
||||
{
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<NTY> items_0::collect_c::Collectable for BinsDim0<NTY>
|
||||
where
|
||||
NTY: ScalarOps,
|
||||
{
|
||||
fn new_collector(&self) -> Box<dyn items_0::collect_c::Collector> {
|
||||
Box::new(BinsDim0Collector::<NTY>::new())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user