Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
refactor extent to work on Range
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Aug 3, 2023
1 parent b30a57a commit f168436
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
33 changes: 18 additions & 15 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,6 @@ impl<'a, P: Props> Event<'a, P> {
}
}

pub fn ts(&self) -> Option<Timestamp> {
self.extent().map(|ts| *ts.end())
}

pub fn tss(&self) -> Option<Timestamp> {
self.extent().and_then(|ts| ts.start().cloned())
}

pub fn extent(&self) -> Option<&Extent> {
self.ts.as_ref()
}
Expand All @@ -71,16 +63,27 @@ impl<'a, P: Props> Event<'a, P> {
&'kv self,
mut for_each: F,
) {
if let Some(ref ts) = self.ts {
if let Some(start) = ts.start() {
for_each(TSS_KEY.to_key(), start.to_value());
let mut reserved = || {
if let Some(ref ts) = self.ts {
if let Some(ts) = ts.to_point() {
for_each(TS_KEY.to_key(), ts.to_value())?;
} else {
let ts = ts.as_span();

for_each(TSS_KEY.to_key(), ts.start.to_value())?;
for_each(TS_KEY.to_key(), ts.end.to_value())?;
}
}

for_each(TS_KEY.to_key(), ts.end().to_value());
}
for_each(TPL_KEY.to_key(), self.tpl.to_value())?;
for_each(MSG_KEY.to_key(), Msg::new_ref(self).to_value())?;

ControlFlow::Continue(())
};

for_each(TPL_KEY.to_key(), self.tpl.to_value());
for_each(MSG_KEY.to_key(), Msg::new_ref(self).to_value());
if let ControlFlow::Break(()) = reserved() {
return;
}

self.props.for_each(for_each);
}
Expand Down
44 changes: 22 additions & 22 deletions core/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{cmp, fmt, ops::RangeInclusive, str, str::FromStr, time::Duration};
use core::{cmp, fmt, ops::Range, str, str::FromStr, time::Duration};

use crate::{
empty::Empty,
Expand Down Expand Up @@ -62,14 +62,14 @@ pub struct Extent(ExtentInner);

#[derive(Clone)]
enum ExtentInner {
Point(Timestamp),
Span(RangeInclusive<Timestamp>),
Point(Range<Timestamp>),
Span(Range<Timestamp>),
}

impl fmt::Debug for Extent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
ExtentInner::Point(ts) => fmt::Debug::fmt(&ts, f),
ExtentInner::Point(ref ts) => fmt::Debug::fmt(&ts.start, f),
ExtentInner::Span(ref ts) => fmt::Debug::fmt(ts, f),
}
}
Expand All @@ -78,40 +78,40 @@ impl fmt::Debug for Extent {
impl fmt::Display for Extent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
ExtentInner::Point(ts) => fmt::Display::fmt(&ts, f),
ExtentInner::Point(ref ts) => fmt::Display::fmt(&ts.end, f),
ExtentInner::Span(ref ts) => {
fmt::Display::fmt(ts.start(), f)?;
write!(f, "..=")?;
fmt::Display::fmt(ts.end(), f)
fmt::Display::fmt(&ts.start, f)?;
write!(f, "..")?;
fmt::Display::fmt(&ts.end, f)
}
}
}
}

impl Extent {
pub fn point(ts: Timestamp) -> Extent {
Extent(ExtentInner::Point(ts))
Extent(ExtentInner::Point(ts..ts))
}

pub fn span(ts: RangeInclusive<Timestamp>) -> Extent {
if ts.start() == ts.end() {
Extent::point(*ts.end())
pub fn span(ts: Range<Timestamp>) -> Extent {
if ts.start == ts.end {
Extent::point(ts.end)
} else {
Extent(ExtentInner::Span(ts))
}
}

pub fn start(&self) -> Option<&Timestamp> {
pub fn as_span(&self) -> &Range<Timestamp> {
match self.0 {
ExtentInner::Point(_) => None,
ExtentInner::Span(ref ts) => Some(ts.start()),
ExtentInner::Point(ref ts) => ts,
ExtentInner::Span(ref ts) => ts,
}
}

pub fn end(&self) -> &Timestamp {
pub fn to_point(&self) -> Option<&Timestamp> {
match self.0 {
ExtentInner::Point(ref ts) => ts,
ExtentInner::Span(ref ts) => ts.end(),
ExtentInner::Point(ref ts) => Some(&ts.start),
ExtentInner::Span(_) => None,
}
}
}
Expand All @@ -128,14 +128,14 @@ impl<'a> From<&'a Timestamp> for Extent {
}
}

impl From<RangeInclusive<Timestamp>> for Extent {
fn from(span: RangeInclusive<Timestamp>) -> Extent {
impl From<Range<Timestamp>> for Extent {
fn from(span: Range<Timestamp>) -> Extent {
Extent::span(span)
}
}

impl<'a> From<&'a RangeInclusive<Timestamp>> for Extent {
fn from(span: &'a RangeInclusive<Timestamp>) -> Extent {
impl<'a> From<&'a Range<Timestamp>> for Extent {
fn from(span: &'a Range<Timestamp>) -> Extent {
Extent::span(span.clone())
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::{
any::Any,
fmt,
future::Future,
ops::{ControlFlow, RangeInclusive},
ops::{ControlFlow, Range},
};

use emit_core::{
Expand Down Expand Up @@ -386,7 +386,7 @@ impl EventExtent for Timestamp {
}
}

impl EventExtent for RangeInclusive<Timestamp> {
impl EventExtent for Range<Timestamp> {
fn to_extent(&self) -> Option<Extent> {
Some(Extent::span(self.clone()))
}
Expand Down Expand Up @@ -424,7 +424,7 @@ pub fn __private_span_start() -> Option<Timestamp> {
pub fn __private_span_end(start: Option<Timestamp>) -> Option<Extent> {
let end = ambient::get().now();

start.and_then(|start| end.map(|end| Extent::span(start..=end)))
start.and_then(|start| end.map(|end| Extent::span(start..end)))
}

#[repr(transparent)]
Expand Down
2 changes: 1 addition & 1 deletion targets/otlp/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl emit_core::target::Target for OtlpLogsTarget {
fn event<P: emit_core::props::Props>(&self, evt: &emit_core::event::Event<P>) {
let time_unix_nano = evt
.extent()
.map(|ts| ts.end())
.map(|ts| ts.as_span().end)
.map(|ts| ts.to_unix().as_nanos() as u64)
.unwrap_or_default();

Expand Down

0 comments on commit f168436

Please sign in to comment.