feat: Add print to stdout to data generator (#2512)

This adds a flag to the data generator to print samples to standard out. It disables logging output so that only the line protocol is output.
pull/24376/head
Paul Dix 2021-09-12 17:00:36 -04:00 committed by Paul Dix
parent 914c6e712b
commit 32f2410597
2 changed files with 34 additions and 4 deletions

View File

@ -18,8 +18,6 @@ use tracing::info;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let help = r#"IOx data point generator
Examples:
@ -60,6 +58,10 @@ Logging:
.takes_value(true)
.required(true),
)
.arg(Arg::with_name("PRINT")
.long("print")
.help("Print the generated line protocol from a single sample collection to the terminal")
)
.arg(
Arg::with_name("OUTPUT")
.short("o")
@ -135,6 +137,11 @@ Logging:
)
.get_matches();
let disable_log_output = matches.is_present("PRINT");
if !disable_log_output {
tracing_subscriber::fmt::init();
}
let spec_filename = matches
.value_of("SPECIFICATION")
// This should never fail if clap is working properly
@ -181,8 +188,10 @@ Logging:
);
PointsWriterBuilder::new_api(host, org, bucket, token, create_bucket, org_id).await?
} else if matches.is_present("PRINT") {
PointsWriterBuilder::new_std_out()
} else {
panic!("One of --output or --host must be provided.");
panic!("One of --print or --output or --host must be provided.");
};
let result = iox_data_generator::generate::<rand::rngs::SmallRng>(
@ -197,7 +206,11 @@ Logging:
.await;
match result {
Ok(total_points) => eprintln!("Submitted {} total points", total_points),
Ok(total_points) => {
if !disable_log_output {
eprintln!("Submitted {} total points", total_points);
}
}
Err(e) => panic!("Execution failed: \n{}", e),
}

View File

@ -95,6 +95,7 @@ enum PointsWriterConfig {
},
#[cfg(test)]
Vector(BTreeMap<String, Arc<Mutex<Vec<u8>>>>),
Stdout,
}
impl PointsWriterBuilder {
@ -157,6 +158,13 @@ impl PointsWriterBuilder {
})
}
/// Write points to stdout
pub fn new_std_out() -> Self {
Self {
config: PointsWriterConfig::Stdout,
}
}
/// Generate points but do not write them anywhere
pub fn new_no_op(perform_write: bool) -> Self {
Self {
@ -193,6 +201,7 @@ impl PointsWriterBuilder {
.or_insert_with(|| Arc::new(Mutex::new(Vec::new())));
InnerPointsWriter::Vec(Arc::clone(v))
}
PointsWriterConfig::Stdout => InnerPointsWriter::Stdout,
};
PointsWriter { inner_writer }
@ -225,6 +234,7 @@ enum InnerPointsWriter {
},
#[cfg(test)]
Vec(Arc<Mutex<Vec<u8>>>),
Stdout,
}
impl InnerPointsWriter {
@ -278,6 +288,13 @@ impl InnerPointsWriter {
.expect("Should be able to write to vec");
}
}
Self::Stdout => {
for point in points {
point
.write_data_point_to(std::io::stdout())
.expect("should be able to write to stdout");
}
}
}
Ok(())
}