Skip to content

Commit

Permalink
remove a bunch of boilerplate
Browse files Browse the repository at this point in the history
Signed-off-by: mimir-d <[email protected]>
  • Loading branch information
mimir-d committed Oct 8, 2024
1 parent c3ec622 commit ed3dcd0
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 463 deletions.
24 changes: 13 additions & 11 deletions src/output/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,23 @@ impl JsonEmitter {
}
}

fn serialize_artifact(&self, object: &spec::RootArtifact) -> serde_json::Value {
fn serialize_artifact(&self, object: &spec::RootImpl) -> serde_json::Value {
let now = chrono::Local::now();
let now_tz = now.with_timezone(&self.timezone);
let out_artifact = spec::Root {
let root = spec::Root {
artifact: object.clone(),
timestamp: now_tz,
seqno: self.next_sequence_no(),
};
serde_json::json!(out_artifact)
serde_json::json!(root)
}

fn next_sequence_no(&self) -> u64 {
self.seqno.fetch_add(1, atomic::Ordering::SeqCst);
self.seqno.load(atomic::Ordering::SeqCst)
}

pub async fn emit(&self, object: &spec::RootArtifact) -> Result<(), WriterError> {
pub async fn emit(&self, object: &spec::RootImpl) -> Result<(), WriterError> {
let serialized = self.serialize_artifact(object);
match self.writer {
WriterType::File(ref file) => file.write(&serialized.to_string()).await?,
Expand All @@ -132,8 +132,6 @@ mod tests {
use serde_json::json;

use super::*;
use crate::output as tv;
use tv::run::SchemaVersion;

#[tokio::test]
async fn test_emit_using_buffer_writer() -> Result<()> {
Expand All @@ -149,8 +147,11 @@ mod tests {
let writer = BufferWriter::new(buffer.clone());
let emitter = JsonEmitter::new(chrono_tz::UTC, WriterType::Buffer(writer));

let version = SchemaVersion::new();
emitter.emit(&version.to_artifact()).await?;
emitter
.emit(&spec::RootImpl::SchemaVersion(
spec::SchemaVersion::default(),
))
.await?;

let deserialized = serde_json::from_str::<serde_json::Value>(
buffer.lock().await.first().ok_or(anyhow!("no outputs"))?,
Expand Down Expand Up @@ -180,9 +181,10 @@ mod tests {
let buffer = Arc::new(Mutex::new(vec![]));
let writer = BufferWriter::new(buffer.clone());
let emitter = JsonEmitter::new(chrono_tz::UTC, WriterType::Buffer(writer));
let version = SchemaVersion::new();
emitter.emit(&version.to_artifact()).await?;
emitter.emit(&version.to_artifact()).await?;

let version = spec::RootImpl::SchemaVersion(spec::SchemaVersion::default());
emitter.emit(&version).await?;
emitter.emit(&version).await?;

let deserialized = serde_json::from_str::<serde_json::Value>(
buffer.lock().await.first().ok_or(anyhow!("no outputs"))?,
Expand Down
125 changes: 25 additions & 100 deletions src/output/measurement.rs → src/output/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::future::Future;
use std::sync::atomic;
use std::sync::Arc;

use chrono::DateTime;
use serde_json::Map;
use serde_json::Value;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -82,7 +81,7 @@ impl<'a> MeasurementSeries<'a> {
/// ```
pub async fn start(&self) -> Result<(), emitter::WriterError> {
self.emitter
.emit(&spec::TestStepArtifactDescendant::MeasurementSeriesStart(
.emit(&spec::TestStepArtifactImpl::MeasurementSeriesStart(
self.start.to_artifact(),
))
.await?;
Expand Down Expand Up @@ -110,12 +109,13 @@ impl<'a> MeasurementSeries<'a> {
/// # });
/// ```
pub async fn end(&self) -> Result<(), emitter::WriterError> {
let end =
MeasurementSeriesEnd::new(self.start.get_series_id(), self.current_sequence_no().await);
let end = spec::MeasurementSeriesEnd {
series_id: self.start.series_id.clone(),
total_count: self.current_sequence_no().await,
};

self.emitter
.emit(&spec::TestStepArtifactDescendant::MeasurementSeriesEnd(
end.to_artifact(),
))
.emit(&spec::TestStepArtifactImpl::MeasurementSeriesEnd(end))
.await?;

Ok(())
Expand All @@ -142,17 +142,18 @@ impl<'a> MeasurementSeries<'a> {
/// # });
/// ```
pub async fn add_measurement(&self, value: Value) -> Result<(), emitter::WriterError> {
let element = MeasurementSeriesElement::new(
self.current_sequence_no().await,
value,
&self.start,
None,
);
let element = spec::MeasurementSeriesElement {
index: self.current_sequence_no().await,
value: value.clone(),
timestamp: chrono::Local::now().with_timezone(&chrono_tz::Tz::UTC),
series_id: self.start.series_id.clone(),
metadata: None,
};
self.increment_sequence_no().await;

self.emitter
.emit(&spec::TestStepArtifactDescendant::MeasurementSeriesElement(
element.to_artifact(),
.emit(&spec::TestStepArtifactImpl::MeasurementSeriesElement(
element,
))
.await?;

Expand Down Expand Up @@ -185,19 +186,20 @@ impl<'a> MeasurementSeries<'a> {
value: Value,
metadata: Vec<(&str, Value)>,
) -> Result<(), emitter::WriterError> {
let element = MeasurementSeriesElement::new(
self.current_sequence_no().await,
value,
&self.start,
Some(Map::from_iter(
let element = spec::MeasurementSeriesElement {
index: self.current_sequence_no().await,
value: value.clone(),
timestamp: chrono::Local::now().with_timezone(&chrono_tz::Tz::UTC),
series_id: self.start.series_id.clone(),
metadata: Some(Map::from_iter(
metadata.iter().map(|(k, v)| (k.to_string(), v.clone())),
)),
);
};
self.increment_sequence_no().await;

self.emitter
.emit(&spec::TestStepArtifactDescendant::MeasurementSeriesElement(
element.to_artifact(),
.emit(&spec::TestStepArtifactImpl::MeasurementSeriesElement(
element,
))
.await?;

Expand Down Expand Up @@ -656,10 +658,6 @@ impl MeasurementSeriesStart {
metadata: self.metadata.clone(),
}
}

pub fn get_series_id(&self) -> &str {
&self.series_id
}
}

pub struct MeasurementSeriesStartBuilder {
Expand Down Expand Up @@ -744,62 +742,6 @@ impl MeasurementSeriesStartBuilder {
}
}

pub struct MeasurementSeriesEnd {
series_id: String,
total_count: u64,
}

impl MeasurementSeriesEnd {
pub(crate) fn new(series_id: &str, total_count: u64) -> MeasurementSeriesEnd {
MeasurementSeriesEnd {
series_id: series_id.to_string(),
total_count,
}
}

pub fn to_artifact(&self) -> spec::MeasurementSeriesEnd {
spec::MeasurementSeriesEnd {
series_id: self.series_id.clone(),
total_count: self.total_count,
}
}
}

pub struct MeasurementSeriesElement {
index: u64,
value: Value,
timestamp: DateTime<chrono_tz::Tz>,
series_id: String,
metadata: Option<Map<String, Value>>,
}

impl MeasurementSeriesElement {
pub(crate) fn new(
index: u64,
value: Value,
series: &MeasurementSeriesStart,
metadata: Option<Map<String, Value>>,
) -> MeasurementSeriesElement {
MeasurementSeriesElement {
index,
value: value.clone(),
timestamp: chrono::Local::now().with_timezone(&chrono_tz::Tz::UTC),
series_id: series.series_id.to_string(),
metadata,
}
}

pub fn to_artifact(&self) -> spec::MeasurementSeriesElement {
spec::MeasurementSeriesElement {
index: self.index,
value: self.value.clone(),
timestamp: self.timestamp,
series_id: self.series_id.clone(),
metadata: self.metadata.clone(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -937,23 +879,6 @@ mod tests {
Ok(())
}

#[test]
fn test_measurement_series_end_to_artifact() -> Result<()> {
let series_id = "series_id".to_owned();
let series = MeasurementSeriesEnd::new(&series_id, 1);

let artifact = series.to_artifact();
assert_eq!(
artifact,
spec::MeasurementSeriesEnd {
series_id: series_id.to_string(),
total_count: 1,
}
);

Ok(())
}

#[test]
fn test_validator() -> Result<()> {
let validator = Validator::builder(ValidatorType::Equal, 30.into())
Expand Down
4 changes: 2 additions & 2 deletions src/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod emitter;
mod error;
mod log;
mod macros;
mod measurement;
mod measure;
mod run;
mod state;
mod step;
Expand All @@ -25,7 +25,7 @@ pub use dut::*;
pub use emitter::*;
pub use error::*;
pub use log::*;
pub use measurement::*;
pub use measure::*;
pub use run::*;
pub use step::*;

Expand Down
Loading

0 comments on commit ed3dcd0

Please sign in to comment.