diff --git a/Cargo.toml b/Cargo.toml index 79acaaf..0ba9703 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,7 +50,7 @@ tracing-subscriber = { version = "0.3", features = ["ansi", "env-filter"] } cli-table = "0.4.9" [features] -default = ["tokio", "default-tls"] +default = ["tokio", "default-tls", "integration-tests"] tokio = ["backoff/tokio", "dep:tokio"] async-std = ["backoff/async-std", "dep:async-std"] default-tls = ["reqwest/default-tls"] diff --git a/src/datasets/model.rs b/src/datasets/model.rs index aa2ab15..773e32e 100644 --- a/src/datasets/model.rs +++ b/src/datasets/model.rs @@ -591,14 +591,6 @@ pub struct QueryResult { /// The tables that were queried. pub tables: Vec, - // /// The datasets that were queried. - // #[serde(default, deserialize_with = "deserialize_null_default")] - // pub dataset_names: Vec, - // /// The events that matched the query. - // #[serde(default, deserialize_with = "deserialize_null_default")] - // pub matches: Vec, - // /// The time series buckets. - // pub buckets: Timeseries, /// The ID of the query that generated this result when it was saved on the /// server. This is only set when the query was send with the `SaveKind` /// option specified. diff --git a/src/datasets/model/table.rs b/src/datasets/model/table.rs index bf4985e..3d6f0fb 100644 --- a/src/datasets/model/table.rs +++ b/src/datasets/model/table.rs @@ -267,12 +267,13 @@ impl Table { &self.columns } - pub fn rows(&self) -> usize { - self.columns.iter().map(Vec::len).max().unwrap_or(0) + /// Returns the maximum length of the first column + pub fn len(&self) -> usize { + self.columns.first().map(Vec::len).unwrap_or_default() } pub fn get_row(&self, row: usize) -> Option { - if self.rows() > row { + if self.len() > row { Some(Row { table: self, row }) } else { None @@ -301,7 +302,7 @@ impl<'table> Iterator for RowIter<'table> { } fn size_hint(&self) -> (usize, Option) { - let size = self.table.rows(); + let size = self.table.len(); (size - self.row, Some(size - self.row)) } @@ -309,15 +310,15 @@ impl<'table> Iterator for RowIter<'table> { where Self: Sized, { - self.table.rows() - self.row + self.table.len() - self.row } fn last(self) -> Option where Self: Sized, { - if self.table.rows() > 0 { - self.table.get_row(self.table.rows() - 1) + if self.table.len() > 0 { + self.table.get_row(self.table.len() - 1) } else { None } @@ -331,6 +332,19 @@ pub struct Row<'table> { } impl<'table> Row<'table> { + /// Returns the value of the row by name + pub fn get_field(&self, field: &str) -> Option<&JsonValue> { + let mut index = None; + + for (i, f) in self.table.fields.iter().enumerate() { + if f.name() == field { + index = Some(i); + break; + } + } + + self.get(index?) + } /// Returns the value of the row. pub fn get(&self, column: usize) -> Option<&JsonValue> { self.table.columns.get(column).and_then(|c| c.get(self.row)) diff --git a/tests/cursor.rs b/tests/cursor.rs index 94656b2..e8844fa 100644 --- a/tests/cursor.rs +++ b/tests/cursor.rs @@ -118,9 +118,13 @@ async fn test_cursor_impl(ctx: &mut Context) { .await .unwrap(); assert!(apl_query_result.saved_query_id.is_some()); - assert_eq!(1000, apl_query_result.matches.len()); + assert_eq!(1000, apl_query_result.tables[0].len()); - let mid_row_id = &apl_query_result.matches[500].row_id; + let table = &apl_query_result.tables[0]; + + let row = table.get_row(500).unwrap(); + + let mid_row_id = &row.get_field("row_id").unwrap(); let apl_query_result = ctx .client @@ -138,5 +142,6 @@ async fn test_cursor_impl(ctx: &mut Context) { .await .unwrap(); assert!(apl_query_result.saved_query_id.is_some()); - assert_eq!(500, apl_query_result.matches.len()); + assert_eq!(500, apl_query_result.tables.len()); + assert_eq!(500, apl_query_result.tables[0].len()); } diff --git a/tests/datasets.rs b/tests/datasets.rs index af8d3ec..60e5d7a 100644 --- a/tests/datasets.rs +++ b/tests/datasets.rs @@ -1,6 +1,5 @@ #![cfg(feature = "integration-tests")] use axiom_rs::{datasets::*, Client}; -use chrono::{Duration, Utc}; use futures::StreamExt; use serde_json::json; use std::{env, time::Duration as StdDuration}; @@ -43,7 +42,7 @@ impl AsyncTestContext for Context { #[test_context(Context)] #[tokio::test] async fn test_datasets(ctx: &mut Context) -> Result<(), Box> { - Ok(test_datasets_impl(ctx).await?) + test_datasets_impl(ctx).await } #[cfg(feature = "async-std")] #[test_context(Context)] @@ -79,7 +78,7 @@ async fn test_datasets_impl(ctx: &mut Context) -> Result<(), Box Result<(), Box 0); + assert!(!info.fields.is_empty()); // Run another query but using APL. let apl_query_result = ctx @@ -181,12 +180,8 @@ async fn test_datasets_impl(ctx: &mut Context) -> Result<(), Box