Skip to content

Commit ca686d6

Browse files
committed
graph: Add ability to order by fields other than timestamp for aggregate entities
1 parent 8bc4645 commit ca686d6

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

graph/src/schema/api.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,12 @@ fn add_types_for_aggregation_types(
493493
input_schema: &InputSchema,
494494
) -> Result<(), APISchemaError> {
495495
for (name, agg_type) in input_schema.aggregation_types() {
496+
// Combine regular fields and aggregate fields for ordering
497+
let mut all_fields = agg_type.fields.to_vec();
498+
for agg in agg_type.aggregates.iter() {
499+
all_fields.push(agg.as_agg_field());
500+
}
501+
add_order_by_type(&mut api.document, name, &all_fields)?;
496502
add_aggregation_filter_type(api, name, agg_type)?;
497503
}
498504
Ok(())
@@ -686,13 +692,25 @@ impl FilterOps {
686692
s::Type::NamedType("OrderDirection".to_string()),
687693
),
688694
],
689-
FilterOps::Aggregation => vec![input_value(
690-
"interval",
691-
"",
692-
s::Type::NonNullType(Box::new(s::Type::NamedType(
693-
"Aggregation_interval".to_string(),
694-
))),
695-
)],
695+
FilterOps::Aggregation => vec![
696+
input_value(
697+
"interval",
698+
"",
699+
s::Type::NonNullType(Box::new(s::Type::NamedType(
700+
"Aggregation_interval".to_string(),
701+
))),
702+
),
703+
input_value(
704+
"orderBy",
705+
"",
706+
s::Type::NamedType(format!("{}_orderBy", type_name)),
707+
),
708+
input_value(
709+
"orderDirection",
710+
"",
711+
s::Type::NamedType("OrderDirection".to_string()),
712+
),
713+
],
696714
};
697715

698716
let mut args = vec![skip, first];

graph/src/schema/input/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ impl Aggregate {
824824

825825
/// The field needed for the finalised aggregation for hourly/daily
826826
/// values
827-
fn as_agg_field(&self) -> Field {
827+
pub fn as_agg_field(&self) -> Field {
828828
Field {
829829
name: self.name.clone(),
830830
field_type: self.field_type.clone(),

graphql/src/store/prefetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ impl<'a> Loader<'a> {
713713
// that causes unnecessary work in the database
714714
query.order = EntityOrder::Unordered;
715715
}
716-
// Aggregations are always ordered by (timestamp, id)
717-
if child_type.is_aggregation() {
716+
// Apply default timestamp ordering for aggregations if no custom order is specified
717+
if child_type.is_aggregation() && matches!(query.order, EntityOrder::Unordered) {
718718
let ts = child_type.field(kw::TIMESTAMP).unwrap();
719719
query.order = EntityOrder::Descending(ts.name.to_string(), ts.value_type);
720720
}

0 commit comments

Comments
 (0)