Skip to content

Commit

Permalink
graph: Add ability to order by fields other than timestamp for aggreg…
Browse files Browse the repository at this point in the history
…ate entities
  • Loading branch information
incrypto32 committed Feb 18, 2025
1 parent 8bc4645 commit d1af6ed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
32 changes: 25 additions & 7 deletions graph/src/schema/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ fn add_types_for_aggregation_types(
input_schema: &InputSchema,
) -> Result<(), APISchemaError> {
for (name, agg_type) in input_schema.aggregation_types() {
// Combine regular fields and aggregate fields for ordering
let mut all_fields = agg_type.fields.to_vec();
for agg in agg_type.aggregates.iter() {
all_fields.push(agg.as_agg_field());
}
add_order_by_type(&mut api.document, name, &all_fields)?;
add_aggregation_filter_type(api, name, agg_type)?;
}
Ok(())
Expand Down Expand Up @@ -686,13 +692,25 @@ impl FilterOps {
s::Type::NamedType("OrderDirection".to_string()),
),
],
FilterOps::Aggregation => vec![input_value(
"interval",
"",
s::Type::NonNullType(Box::new(s::Type::NamedType(
"Aggregation_interval".to_string(),
))),
)],
FilterOps::Aggregation => vec![
input_value(
"interval",
"",
s::Type::NonNullType(Box::new(s::Type::NamedType(
"Aggregation_interval".to_string(),
))),
),
input_value(
"orderBy",
"",
s::Type::NamedType(format!("{}_orderBy", type_name)),
),
input_value(
"orderDirection",
"",
s::Type::NamedType("OrderDirection".to_string()),
),
],
};

let mut args = vec![skip, first];
Expand Down
2 changes: 1 addition & 1 deletion graph/src/schema/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ impl Aggregate {

/// The field needed for the finalised aggregation for hourly/daily
/// values
fn as_agg_field(&self) -> Field {
pub fn as_agg_field(&self) -> Field {
Field {
name: self.name.clone(),
field_type: self.field_type.clone(),
Expand Down
4 changes: 2 additions & 2 deletions graphql/src/store/prefetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,8 @@ impl<'a> Loader<'a> {
// that causes unnecessary work in the database
query.order = EntityOrder::Unordered;
}
// Aggregations are always ordered by (timestamp, id)
if child_type.is_aggregation() {
// Apply default timestamp ordering for aggregations if no custom order is specified
if child_type.is_aggregation() && matches!(query.order, EntityOrder::Default) {
let ts = child_type.field(kw::TIMESTAMP).unwrap();
query.order = EntityOrder::Descending(ts.name.to_string(), ts.value_type);
}
Expand Down

0 comments on commit d1af6ed

Please sign in to comment.