MetricsQuery

Struct MetricsQuery 

Source
pub struct MetricsQuery { /* private fields */ }
Expand description

Builder for constructing queries against a metrics repository.

MetricsQuery provides a fluent API for filtering metrics by various criteria including time ranges, tags, and analyzer types. Queries are constructed incrementally and executed asynchronously.

§Example

use term_guard::repository::MetricsQuery;

let results = repository.load().await
    .after(start_timestamp)
    .before(end_timestamp)
    .with_tag("environment", "production")
    .for_analyzers(vec!["completeness", "size"])
    .execute()
    .await?;

for (key, context) in results {
    println!("Metrics at {}: {:?}", key.timestamp, context.all_metrics());
}

Implementations§

Source§

impl MetricsQuery

Source

pub fn new(repository: Arc<dyn MetricsRepository>) -> Self

Creates a new query for the given repository.

§Arguments
  • repository - The repository to query against
Source

pub fn before(self, timestamp: i64) -> Self

Filters results to metrics before the specified timestamp.

§Arguments
  • timestamp - Unix timestamp in milliseconds (exclusive)
§Example
let end_time = chrono::Utc::now().timestamp_millis();
let query = repository.load().await.before(end_time);
Source

pub fn after(self, timestamp: i64) -> Self

Filters results to metrics after the specified timestamp.

§Arguments
  • timestamp - Unix timestamp in milliseconds (inclusive)
§Example
let start_time = (chrono::Utc::now() - chrono::Duration::days(7)).timestamp_millis();
let query = repository.load().await.after(start_time);
Source

pub fn between(self, start: i64, end: i64) -> Self

Filters results to metrics within a time range.

§Arguments
  • start - Start timestamp in milliseconds (inclusive)
  • end - End timestamp in milliseconds (exclusive)
§Example
let query = repository.load().await.between(start_time, end_time);
Source

pub fn with_tag(self, key: impl Into<String>, value: impl Into<String>) -> Self

Filters results to metrics with a specific tag.

§Arguments
  • key - The tag key
  • value - The tag value
§Example
let query = repository.load().await
    .with_tag("environment", "production")
    .with_tag("region", "us-west-2");
Source

pub fn with_tags<I, K, V>(self, tags: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<String>,

Filters results to metrics with multiple tags.

§Arguments
  • tags - Iterator of (key, value) pairs
§Example
let tags = vec![
    ("environment", "production"),
    ("dataset", "users"),
];
let query = repository.load().await.with_tags(tags);
Source

pub fn for_analyzers<I, S>(self, analyzers: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Filters results to specific analyzer types.

Only metrics from the specified analyzers will be included in the results.

§Arguments
  • analyzers - List of analyzer names
§Example
let query = repository.load().await
    .for_analyzers(vec!["completeness", "size", "mean"]);
Source

pub fn limit(self, limit: usize) -> Self

Limits the number of results returned.

§Arguments
  • limit - Maximum number of results
§Example
let query = repository.load().await.limit(100);
Source

pub fn validate(&self) -> Result<()>

Validates query parameters for correctness.

§Returns

Returns an error if the query parameters are invalid.

Source

pub fn offset(self, offset: usize) -> Self

Sets the offset for pagination.

§Arguments
  • offset - Number of results to skip
§Example
// Get results 100-200
let query = repository.load().await.offset(100).limit(100);
Source

pub fn sort(self, order: SortOrder) -> Self

Sets the sort order for results.

§Arguments
  • order - The sort order to use
§Example
use term_guard::repository::query::SortOrder;

let query = repository.load().await.sort(SortOrder::Ascending);
Source

pub async fn execute(self) -> Result<Vec<(ResultKey, AnalyzerContext)>>

Executes the query and returns the results.

§Returns

A vector of (ResultKey, AnalyzerContext) pairs matching the query criteria, sorted by timestamp according to the specified sort order.

§Errors

Returns an error if the query execution fails (e.g., I/O error, invalid query).

§Example
let results = repository.load().await
    .after(start_time)
    .with_tag("environment", "production")
    .execute()
    .await?;

for (key, context) in results {
    println!("Timestamp: {}", key.timestamp);
    println!("Metrics: {:?}", context.all_metrics());
}
Source

pub async fn count(self) -> Result<usize>

Returns a count of metrics matching the query criteria without loading them.

This is more efficient than executing the query and counting results when only the count is needed.

§Errors

Returns an error if the count operation fails.

Source

pub async fn exists(self) -> Result<bool>

Checks if any metrics match the query criteria.

§Returns

Returns true if at least one metric matches, false otherwise.

§Errors

Returns an error if the check operation fails.

Source

pub fn get_before(&self) -> Option<i64>

Accessor methods for DataFusion integration

Source

pub fn get_after(&self) -> Option<i64>

Source

pub fn get_tags(&self) -> &HashMap<String, String>

Source

pub fn get_analyzers(&self) -> &Option<Vec<String>>

Source

pub fn get_limit(&self) -> Option<usize>

Source

pub fn get_offset(&self) -> Option<usize>

Source

pub fn get_sort_order(&self) -> SortOrder

Source

pub fn is_ascending(&self) -> bool

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,