Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to order by fields other than timestamp for aggregation entities #5829

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading