From 3faf8de131731dcead6f9e08fc6c29a070859b5d Mon Sep 17 00:00:00 2001 From: Dominik Werder Date: Wed, 14 May 2025 16:27:39 +0200 Subject: [PATCH] Date helper --- Cargo.toml | 3 ++- src/netpod.rs | 45 +++++++++++++++++--------------------------- src/range/evrange.rs | 17 +++++++++++++---- src/ttl.rs | 4 ++++ 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index adcdd1f..7c0d932 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "daqbuf-netpod" version = "0.0.4" authors = ["Dominik Werder "] -edition = "2021" +edition = "2024" [lib] path = "src/netpod.rs" @@ -15,6 +15,7 @@ humantime = "2.1.0" humantime-serde = "1.1.1" bytes = "1.8.0" chrono = { version = "0.4.19", features = ["serde"] } +time = { version = "0.3.41", features = ["macros", "parsing"] } futures-util = "0.3.14" tracing = "0.1.37" url = "2.5.3" diff --git a/src/netpod.rs b/src/netpod.rs index 2a716b1..5fdedbc 100644 --- a/src/netpod.rs +++ b/src/netpod.rs @@ -77,7 +77,7 @@ pub mod log_macros_branch { pub use branch_info as info; pub use branch_trace as trace; pub use branch_warn as warn; - pub use tracing::{self, event, span, Level}; + pub use tracing::{self, Level, event, span}; } pub mod log_macros { @@ -134,13 +134,13 @@ pub mod log_macros { } pub mod log { - pub use tracing::{self, event, span, Level}; + pub use tracing::{self, Level, event, span}; pub use tracing::{debug, error, info, trace, warn}; } pub mod log_direct { pub use crate::{debug, error, info, trace, warn}; - pub use tracing::{self, event, span, Level}; + pub use tracing::{self, Level, event, span}; } use bytes::Bytes; @@ -165,12 +165,12 @@ use std::net::SocketAddr; use std::path::PathBuf; use std::pin::Pin; use std::str::FromStr; -use std::sync::atomic; -use std::sync::atomic::AtomicPtr; use std::sync::LazyLock; use std::sync::Once; use std::sync::RwLock; use std::sync::RwLockWriteGuard; +use std::sync::atomic; +use std::sync::atomic::AtomicPtr; use std::task::Context; use std::task::Poll; use std::time::Duration; @@ -258,6 +258,7 @@ autoerr::create_error_v1!( UnknownSeriesKind(i64), BadInt(#[from] std::num::ParseIntError), ChronoParse(#[from] chrono::ParseError), + TimeParse(#[from] time::error::Parse), HumantimeDurationParse(#[from] humantime::DurationError), MissingQueryParameters, MissingSeries, @@ -720,9 +721,9 @@ impl From> for String { mod string_fix_impl_serde { use crate::StringFix; - use serde::de::Visitor; use serde::Deserialize; use serde::Serialize; + use serde::de::Visitor; use std::fmt; impl Serialize for StringFix { @@ -1021,11 +1022,7 @@ pub struct Cluster { impl Cluster { pub fn decompress_default(&self) -> bool { - if self.is_central_storage { - false - } else { - true - } + if self.is_central_storage { false } else { true } } pub fn scylla_st(&self) -> Option<&ScyllaConfig> { @@ -1436,19 +1433,11 @@ impl ByteOrder { } pub fn is_le(&self) -> bool { - if let Self::Little = self { - true - } else { - false - } + if let Self::Little = self { true } else { false } } pub fn is_be(&self) -> bool { - if let Self::Big = self { - true - } else { - false - } + if let Self::Big = self { true } else { false } } } @@ -1837,9 +1826,9 @@ impl fmt::Display for DtNano { mod dt_nano_serde { use super::DtNano; use de::Visitor; - use serde::de; use serde::Deserialize; use serde::Serialize; + use serde::de; use std::fmt; impl Serialize for DtNano { @@ -1927,9 +1916,9 @@ mod ts_nano_ser { use chrono::TimeZone; use chrono::Utc; use de::Visitor; - use serde::de; use serde::Deserialize; use serde::Serialize; + use serde::de; use std::fmt; impl Serialize for TsNano { @@ -2676,11 +2665,7 @@ impl BinnedRange { } else { let f1 = (bin_len_req.ms() - v1.ms()) / bin_len_req.ms(); let f2 = (v2.ms() - bin_len_req.ms()) / bin_len_req.ms(); - if f1 < f2 { - v1 - } else { - v2 - } + if f1 < f2 { v1 } else { v2 } } } else { DtMs::from_ms_u64(v1.ms()) @@ -3103,6 +3088,10 @@ impl TsMs { Self(self.0 + 1) } + pub fn add_dt_ms(&self, dt: DtMs) -> Self { + Self(self.0 + dt.0) + } + pub fn fmt(&self) -> TsMsFmt { TsMsFmt { ts: self.clone() } } diff --git a/src/range/evrange.rs b/src/range/evrange.rs index 93d7f83..3cc18f7 100644 --- a/src/range/evrange.rs +++ b/src/range/evrange.rs @@ -1,12 +1,12 @@ -use crate::query::PulseRangeQuery; -use crate::query::TimeRangeQuery; -use crate::timeunits::SEC; use crate::AppendToUrl; use crate::Dim0Kind; use crate::Error; use crate::FromUrl; -use crate::TsNano; use crate::MS; +use crate::TsNano; +use crate::query::PulseRangeQuery; +use crate::query::TimeRangeQuery; +use crate::timeunits::SEC; use chrono::DateTime; use chrono::TimeZone; use chrono::Utc; @@ -92,6 +92,15 @@ impl NanoRange { } } + pub fn from_strings(beg: &str, end: &str) -> Result { + // let format = time::format_description!("[year]-[month]-[day]T[hour]:[minute]:[second]Z"); + // let beg = time::UtcDateTime::parse(beg, &format)?; + // let beg = beg.into(); + let beg = beg.parse::>()?; + let end = end.parse::>()?; + Ok(Self::from_date_time(beg, end)) + } + pub fn from_ms_u64(beg: u64, end: u64) -> Self { Self { beg: MS * beg, diff --git a/src/ttl.rs b/src/ttl.rs index cba260f..ba9c503 100644 --- a/src/ttl.rs +++ b/src/ttl.rs @@ -68,6 +68,10 @@ impl RetentionTime { } pub fn to_index_db_i32(&self) -> i32 { + self.to_index_db_u16() as i32 + } + + pub fn to_index_db_u16(&self) -> u16 { match self { RetentionTime::Short => 2, RetentionTime::Medium => 4,