use crate::{values::SubResource, Resource, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AggregateFunction {
Sum,
Count,
Avg,
Min,
Max,
}
impl AggregateFunction {
pub fn as_str(&self) -> &'static str {
match self {
AggregateFunction::Sum => "sum",
AggregateFunction::Count => "count",
AggregateFunction::Avg => "avg",
AggregateFunction::Min => "min",
AggregateFunction::Max => "max",
}
}
pub fn from_name(name: &str) -> Option<Self> {
match name {
"sum" => Some(AggregateFunction::Sum),
"count" => Some(AggregateFunction::Count),
"avg" | "average" | "mean" => Some(AggregateFunction::Avg),
"min" => Some(AggregateFunction::Min),
"max" => Some(AggregateFunction::Max),
_ => None,
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Aggregate {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default)]
pub property: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expression: Option<crate::expression::Expression>,
pub function: AggregateFunction,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum GroupGranularity {
#[default]
Exact,
Day,
Month,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AggregateGrouping {
pub property: String,
#[serde(default)]
pub granularity: GroupGranularity,
#[serde(default)]
pub tz_offset_minutes: i64,
#[serde(default)]
pub limit: Option<usize>,
}
pub const DEFAULT_GROUP_LIMIT: usize = 100;
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Aggregation {
pub aggregates: Vec<Aggregate>,
#[serde(default)]
pub group_by: Option<AggregateGrouping>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub now_ms: Option<i64>,
}
impl Aggregation {
pub fn is_empty(&self) -> bool {
self.aggregates.is_empty()
}
}
#[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AggregateGroup {
pub key: String,
pub value: Option<f64>,
pub count: usize,
}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AggregateOutcome {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub property: Option<String>,
pub function: AggregateFunction,
pub value: Option<f64>,
pub count: usize,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub groups: Vec<AggregateGroup>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub groups_truncated: bool,
}
pub fn value_as_number(value: &Value) -> Option<f64> {
match value {
Value::Integer(int) => Some(*int as f64),
Value::Float(float) => Some(*float),
Value::Timestamp(stamp) => Some(*stamp as f64),
Value::Boolean(bool) => Some(if *bool { 1.0 } else { 0.0 }),
Value::String(string) | Value::Slug(string) | Value::Markdown(string) => {
string.trim().parse::<f64>().ok()
}
Value::Date(date) => days_from_iso_date(date).map(|days| (days * 86_400_000) as f64),
_ => None,
}
}
pub fn group_key(resource: &Resource, grouping: &AggregateGrouping) -> Option<String> {
let value = resource.get(&grouping.property).ok()?;
match value {
Value::ResourceArray(items) => items.first().map(|item| match item {
SubResource::Subject(subject) => subject.to_string(),
SubResource::Nested(_) => String::new(),
}),
Value::Timestamp(stamp) => Some(bucket_instant(
*stamp,
grouping.granularity,
grouping.tz_offset_minutes,
)),
Value::Date(date) => Some(match grouping.granularity {
GroupGranularity::Month => date.get(0..7).unwrap_or(date).to_string(),
_ => date.to_string(),
}),
other => Some(other.to_string()),
}
}
fn bucket_instant(millis: i64, granularity: GroupGranularity, tz_offset_minutes: i64) -> String {
if granularity == GroupGranularity::Exact {
return millis.to_string();
}
let shifted = millis + tz_offset_minutes * 60_000;
let days = shifted.div_euclid(86_400_000);
let (year, month, day) = civil_from_days(days);
match granularity {
GroupGranularity::Month => format!("{year:04}-{month:02}"),
_ => format!("{year:04}-{month:02}-{day:02}"),
}
}
fn civil_from_days(days: i64) -> (i64, u32, u32) {
let z = days + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as u64;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let y = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = (doy - (153 * mp + 2) / 5 + 1) as u32;
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
(if m <= 2 { y + 1 } else { y }, m, d)
}
fn days_from_iso_date(date: &str) -> Option<i64> {
let mut parts = date.split('-');
let year: i64 = parts.next()?.parse().ok()?;
let month: i64 = parts.next()?.parse().ok()?;
let day: i64 = parts.next()?.get(0..2).unwrap_or("").parse().ok()?;
if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
return None;
}
Some(days_from_civil(year, month, day))
}
fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
let y = if month <= 2 { year - 1 } else { year };
let era = if y >= 0 { y } else { y - 399 } / 400;
let yoe = y - era * 400;
let mp = if month > 2 { month - 3 } else { month + 9 };
let doy = (153 * mp + 2) / 5 + day - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
era * 146_097 + doe - 719_468
}
#[derive(Debug, Default, Clone)]
pub struct Accumulator {
sum: f64,
min: Option<f64>,
max: Option<f64>,
pub count: usize,
}
impl Accumulator {
pub fn add(&mut self, number: f64) {
self.sum += number;
self.count += 1;
self.min = Some(self.min.map_or(number, |min| min.min(number)));
self.max = Some(self.max.map_or(number, |max| max.max(number)));
}
pub fn count_row(&mut self) {
self.count += 1;
}
pub fn finish(&self, function: AggregateFunction) -> Option<f64> {
match function {
AggregateFunction::Count => Some(self.count as f64),
_ if self.count == 0 => None,
AggregateFunction::Sum => Some(self.sum),
AggregateFunction::Avg => Some(self.sum / self.count as f64),
AggregateFunction::Min => self.min,
AggregateFunction::Max => self.max,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn buckets_instants_in_the_callers_timezone() {
let stamp = 1_785_454_200_000;
assert_eq!(
bucket_instant(stamp, GroupGranularity::Day, 0),
"2026-07-30"
);
assert_eq!(
bucket_instant(stamp, GroupGranularity::Day, 120),
"2026-07-31"
);
assert_eq!(
bucket_instant(stamp, GroupGranularity::Month, 120),
"2026-07"
);
assert_eq!(
bucket_instant(stamp, GroupGranularity::Day, -300),
"2026-07-30"
);
}
#[test]
fn buckets_instants_before_the_epoch() {
assert_eq!(bucket_instant(-1, GroupGranularity::Day, 0), "1969-12-31");
}
#[test]
fn civil_days_round_trip() {
for (year, month, day) in [
(1970, 1, 1),
(2000, 2, 29),
(2026, 7, 30),
(1969, 12, 31),
(2100, 3, 1),
] {
let days = days_from_civil(year, month, day);
assert_eq!(civil_from_days(days), (year, month as u32, day as u32));
}
}
#[test]
fn reads_numbers_out_of_the_values_that_have_them() {
assert_eq!(value_as_number(&Value::Integer(7)), Some(7.0));
assert_eq!(value_as_number(&Value::Float(2.5)), Some(2.5));
assert_eq!(value_as_number(&Value::Timestamp(1000)), Some(1000.0));
assert_eq!(value_as_number(&Value::String(" 12.5 ".into())), Some(12.5));
assert_eq!(value_as_number(&Value::String("n/a".into())), None);
assert_eq!(
value_as_number(&Value::Date("1970-01-02".into())),
Some(86_400_000.0)
);
}
#[test]
fn an_empty_accumulator_has_no_value_but_still_counts_zero() {
let acc = Accumulator::default();
assert_eq!(acc.finish(AggregateFunction::Sum), None);
assert_eq!(acc.finish(AggregateFunction::Avg), None);
assert_eq!(acc.finish(AggregateFunction::Count), Some(0.0));
}
#[test]
fn accumulates_each_function() {
let mut acc = Accumulator::default();
for number in [4.0, 8.0, 3.0] {
acc.add(number);
}
assert_eq!(acc.finish(AggregateFunction::Sum), Some(15.0));
assert_eq!(acc.finish(AggregateFunction::Avg), Some(5.0));
assert_eq!(acc.finish(AggregateFunction::Min), Some(3.0));
assert_eq!(acc.finish(AggregateFunction::Max), Some(8.0));
assert_eq!(acc.finish(AggregateFunction::Count), Some(3.0));
}
#[test]
fn counting_rows_needs_no_values() {
let mut acc = Accumulator::default();
acc.count_row();
acc.count_row();
assert_eq!(acc.finish(AggregateFunction::Count), Some(2.0));
}
}