Files
Davide Silvestri ad08d4be1d feat: Group-by aggregations (#5602)
* feat: show per-group aggregations in grouped grid views

Each group header shows the column's configured Summarize value computed
over that group's rows, column-aligned at every depth, visible whether
the group is collapsed or expanded. Reuses the footer aggregation config
and display components, and an interactive per-group picker that sets the
shared column aggregation. Gated behind the group_by_aggregations frontend
flag; backend returns values via the existing group-by-data response.

Not included (follow-up): realtime/live updates, performance and caching,
and consolidating the group-by-data and aggregations endpoints into one
request for grouped views.

* refactor: de-duplicate Decimal NaN handling in grid aggregations

Extract the repeated Decimal("NaN") -> "NaN" JSON-safety hack into a single
json_safe_aggregation_value helper, reused by the group-by per-group and totals
aggregations and the authed/public footer aggregation endpoints.

* fix: address copilot feedback on group-by aggregations

- Only show the "+ Summarize" picker for unconfigured columns; a configured
  column with no per-group value (e.g. distribution) now renders nothing
  instead of looking unconfigured.
- Document the aggregations_only and include_totals query params and the
  optional top-level aggregations field in the group-by-data OpenAPI schema.
- Mark row_count, sibling_index and row_offset optional (omitted in the lean
  aggregations_only mode).

* fix: de-flake replay-events last-seen-expired test

The test assumed RealtimeEvent id 1 never exists, but the id sequence isn't
reset between transactional tests, so the recorded event got id 1 when the
test ran first on an xdist worker and replay succeeded. Capture the real id,
delete it to simulate retention, and replay from it.

* feat: live, consolidated group aggregation updates

Group-header aggregation values and the footer total now update live and in
place on any change (cell edit, row create/delete, realtime), with no spinner.
In grouped mode they come from a single group-by-data request (aggregations_only
+ include_totals); the standalone /aggregations/ endpoint is no longer called
while grouped. Sort changes need no refresh (order-independent). Gated behind the
group_by_aggregations flag; flag-off behaviour is unchanged.

Deferred: per-group targeting (refreshes the loaded window), the optimistic +
realtime double request, and the 7.3.2 picker e2e.

* fix: dedupe and harden per-group aggregation refresh

Selecting an aggregation in a grouped view fired the group-by-data request
two or three times and could leave the footer total hidden behind a stuck
spinner until the page was refreshed.

- Clear the footer loading flag from refreshGroupByAggregations so the total
  shows on first select.
- Make refreshGroupByAggregations the single owner and supersede in-flight
  requests so a stale response can't overwrite newer values.
- Skip the duplicate fetch from updateFieldOptionsOfField and the footer
  watcher while a picker already owns the refresh.
- Right-align and shrink the banner aggregation value, and let the label slide
  behind the previous field instead of hiding it.

* chore: remove the group_by_aggregations feature flag

The per-group aggregations feature is finished, so enable it unconditionally
and drop the flag gating from the grid footer, group banners, and the
aggregation refresh action.

* test: drop obsolete group-by aggregation feature-flag specs

The group_by_aggregations flag is gone, so the two specs that asserted the
flag-disabled behaviour no longer apply.

* fix: stop the row context menu's Select row from clearing its own selection

The global click handler that clears a multi-select when clicking outside the
grid also fired for the row context menu's "Select row" click, wiping the
selection it had just made (most visible in group-by). Skip the clear when the
click lands inside a context menu; cell clicks, which should still clear, are
unaffected.

* fix: flaky test

* fix: show the spinner for updates as well

* fix: address copilot aggregation comment

* fix: avoid optimistic group aggregation values

* fix: address feedback

* fix: hide vert divider before primary field

* fix: only re-fetch when raw_aggregation_type changes
2026-07-06 08:20:54 +00:00

248 lines
7.0 KiB
Vue

<template>
<div
v-if="hasValue || editable"
ref="anchor"
class="grid-view-aggregation grid-view__group-by-banner-aggregation"
:class="{ 'read-only': !editable }"
:title="titleText"
@click.prevent="
editable && $refs.context.toggle($refs.anchor, 'top', 'right', 10)
"
>
<component
:is="viewAggregationType.getComponent()"
v-if="hasValue"
:aggregation-type="viewAggregationType"
:value="value"
:field="field"
:loading="loading"
/>
<div
v-else-if="loading && viewAggregationType"
class="grid-view__group-by-banner-aggregation-loading"
></div>
<!-- Only unconfigured columns offer the picker; a configured column with no
per-group value (e.g. distribution) shows nothing. -->
<div
v-else-if="!viewAggregationType"
class="grid-view-aggregation__empty grid-view__group-by-banner-aggregation-empty"
:class="{
'grid-view-aggregation__empty--active':
$refs.context && $refs.context.isOpen(),
}"
>
<i class="grid-view-aggregation__empty-icon iconoir-plus"></i>
{{ $t('common.summarize') }}
</div>
<Context ref="context">
<ul class="select__items">
<li
class="select__item select__item--no-options"
:class="{ active: !viewAggregationType }"
>
<a class="select__item-link" @click="selectAggregation('none')">
<div class="select__item-name">
<span class="select__item-name-text">{{
$t('common.none')
}}</span>
</div>
</a>
<i
v-if="!viewAggregationType"
class="select__item-active-icon iconoir-check"
></i>
</li>
<li
v-for="viewAggregation in viewAggregationTypes"
:key="viewAggregation.getType()"
class="select__item select__item--no-options"
:class="{ active: viewAggregation === viewAggregationType }"
>
<a
class="select__item-link"
@click="selectAggregation(viewAggregation.getType())"
>
<div class="select__item-name">
<span class="select__item-name-text">{{
viewAggregation.getName()
}}</span>
</div>
</a>
<i
v-if="viewAggregation === viewAggregationType"
class="select__item-active-icon iconoir-check"
></i>
</li>
</ul>
</Context>
</div>
</template>
<script>
export default {
name: 'GridViewGroupByAggregation',
props: {
view: {
type: Object,
required: true,
},
workspaceId: {
type: null,
default: null,
},
field: {
type: Object,
required: true,
},
storePrefix: {
type: String,
required: true,
},
// The raw, server-computed aggregation value for this group, or undefined when
// the field has no per-group value (e.g. an unsupported aggregation type).
rawValue: {
type: null,
default: undefined,
},
// This group's row count, used as the percentage/total context, mirroring the
// grid footer.
rowCount: {
type: Number,
default: 0,
},
aggregationRowCount: {
type: Number,
default: null,
},
loading: {
type: Boolean,
default: false,
},
},
emits: ['change'],
computed: {
fieldOptions() {
return this.$store.getters[
this.storePrefix + 'view/grid/getAllFieldOptions'
]
},
aggregationType() {
return this.fieldOptions[this.field.id]?.aggregation_type
},
viewAggregationType() {
if (!this.aggregationType) {
return null
}
try {
const aggregation = this.$registry.get(
'viewAggregation',
this.aggregationType
)
// A table-level-only aggregation (e.g. distribution) shares this field's
// footer config but has no per-group value, so resolve it to null here to
// keep the group cell empty instead of rendering against a stale value.
return aggregation.isAllowedInGroupBy() ? aggregation : null
} catch (_) {
return null
}
},
viewAggregationTypes() {
return this.$registry
.getOrderedList('viewAggregation')
.filter(
(agg) =>
agg.fieldIsCompatible(this.field) &&
agg.isAllowedInView() &&
agg.isAllowedInGroupBy()
)
},
fieldType() {
return this.$registry.get('field', this.field.type)
},
editable() {
return this.$hasPermission(
'database.table.view.update_field_options',
this.view,
this.workspaceId
)
},
hasValue() {
return this.viewAggregationType !== null && this.rawValue !== undefined
},
// Full label + value tooltip; the label is clipped when the column is narrow.
titleText() {
if (!this.hasValue) {
return null
}
return `${this.viewAggregationType.getShortName()} ${this.value}`
},
value() {
if (!this.viewAggregationType) {
return undefined
}
return this.viewAggregationType.getValue(this.rawValue, {
rowCount: this.aggregationRowCount ?? this.rowCount,
field: this.field,
fieldType: this.fieldType,
})
},
},
methods: {
async selectAggregation(newType) {
this.$refs.context.hide()
const values = {
aggregation_type: '',
aggregation_raw_type: '',
}
if (newType !== 'none') {
const selectedAggregation = this.$registry.get(
'viewAggregation',
newType
)
values.aggregation_type = newType
values.aggregation_raw_type = selectedAggregation.getRawType()
}
// Only the raw type is server-computed; a display-only change re-renders from
// the existing value, so skip the spinner and refetch (like the footer).
const rawTypeChanged =
values.aggregation_raw_type !==
(this.fieldOptions[this.field.id]?.aggregation_raw_type || '')
// Spin before the optimistic change so the new label never shows the old value.
if (rawTypeChanged) {
this.$store.dispatch(
this.storePrefix + 'view/grid/setGroupByAggregationsLoading',
{ fieldId: this.field.id }
)
}
try {
await this.$store.dispatch(
this.storePrefix + 'view/grid/updateFieldOptionsOfField',
{
field: this.field,
values,
readOnly: !this.editable,
skipAggregationRefresh: true,
}
)
} catch (error) {
if (rawTypeChanged) {
this.$store.dispatch(
this.storePrefix + 'view/grid/setGroupByAggregationsLoading',
{ loading: false }
)
}
throw error
}
// Only a raw-type change needs the server-computed values refetched.
if (rawTypeChanged) {
this.$emit('change')
}
},
},
}
</script>