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

284 lines
7.8 KiB
Vue

<template>
<div
ref="fieldContextAnchor"
class="grid-view-aggregation"
:class="{
'read-only': !userCanMakeAggregations,
}"
@click.prevent="
userCanMakeAggregations &&
$refs[`fieldContext`].toggle($refs.fieldContextAnchor, 'top', 'right', 10)
"
>
<component
:is="viewAggregationType.getComponent()"
v-if="viewAggregationType"
:loading="loading"
:aggregation-type="viewAggregationType"
:value="value"
:field="field"
/>
<div
v-else-if="userCanMakeAggregations"
class="grid-view-aggregation__empty"
:class="{
'grid-view-aggregation__empty--active':
$refs.fieldContext && $refs.fieldContext.isOpen(),
}"
>
<i class="grid-view-aggregation__empty-icon iconoir-plus"></i>
{{ $t('common.summarize') }}
</div>
<Context ref="fieldContext">
<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>
import { mapGetters } from 'vuex'
export default {
name: 'GridViewFieldFooter',
props: {
database: {
type: Object,
required: true,
},
field: {
type: Object,
required: true,
},
view: {
type: Object,
required: true,
},
storePrefix: {
type: String,
required: true,
},
},
data() {
return { pendingValueUpdate: false }
},
computed: {
// Group headers reuse this column's aggregation config in grouped mode.
groupAggregationsEnabled() {
return this.$store.getters[this.storePrefix + 'view/grid/isGroupByMode']
},
userCanMakeAggregations() {
return this.$hasPermission(
'database.table.view.update_field_options',
this.view,
this.database.workspace.id
)
},
aggregationType() {
return this.fieldOptions[this.field.id]?.aggregation_type
},
aggregationRawType() {
return this.fieldOptions[this.field.id]?.aggregation_raw_type
},
value() {
if (
this.viewAggregationType &&
this.fieldAggregationData[this.field.id] !== undefined
) {
const { value } = this.fieldAggregationData[this.field.id]
return this.viewAggregationType.getValue(value, {
rowCount: this.rowCount,
field: this.field,
fieldType: this.fieldType,
})
}
return undefined
},
loading() {
return this.fieldAggregationData[this.field.id]?.loading
},
viewAggregationType() {
if (!this.aggregationType) {
return null
}
try {
return this.$registry.get('viewAggregation', this.aggregationType)
} catch (_) {
// A stale/unknown aggregation type must not crash the grid.
return null
}
},
viewAggregationTypes() {
return this.$registry
.getOrderedList('viewAggregation')
.filter(
(agg) => agg.fieldIsCompatible(this.field) && agg.isAllowedInView()
)
},
fieldType() {
return this.$registry.get('field', this.field.type)
},
fieldAggregationData() {
return this.$store.getters[
this.storePrefix + 'view/grid/getAllFieldAggregationData'
]
},
fieldOptions() {
return this.$store.getters[
this.storePrefix + 'view/grid/getAllFieldOptions'
]
},
rowCount() {
return this.$store.getters[this.storePrefix + 'view/grid/getCount']
},
},
watch: {
aggregationRawType(value) {
if (!value) {
return
}
if (this.pendingValueUpdate) {
return
}
// Skip while a picker already owns the grouped refresh (its spinner is on);
// remote changes leave it off and still refresh here.
if (
this.groupAggregationsEnabled &&
this.$store.getters[
this.storePrefix + 'view/grid/getGroupByAggregationsLoading'
](this.field.id)
) {
return
}
this.$store.dispatch(
this.storePrefix + 'view/grid/fetchAllFieldAggregationData',
{
view: this.view,
}
)
},
},
/*beforeCreate() {
this.$options.computed = {
...(this.$options.computed || {}),
...mapGetters({
fieldAggregationData:
this.$options.propsData.storePrefix +
'view/grid/getAllFieldAggregationData',
fieldOptions:
this.$options.propsData.storePrefix + 'view/grid/getAllFieldOptions',
rowCount: this.$options.propsData.storePrefix + 'view/grid/getCount',
}),
}
},*/
methods: {
async selectAggregation(newType) {
this.$refs.fieldContext.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 context menu).
const rawTypeChanged =
values.aggregation_raw_type !==
(this.fieldOptions[this.field.id]?.aggregation_raw_type || '')
const syncGroups = this.groupAggregationsEnabled
if (syncGroups && rawTypeChanged) {
// Spin before the optimistic change so the new label never shows the old value.
this.$store.dispatch(
this.storePrefix + 'view/grid/setGroupByAggregationsLoading',
{ fieldId: this.field.id }
)
}
// Prevent the watcher to trigger while value is not yet saved on server
this.pendingValueUpdate = true
try {
await this.$store.dispatch(
this.storePrefix + 'view/grid/updateFieldOptionsOfField',
{
field: this.field,
values,
readOnly: !this.userCanMakeAggregations,
skipAggregationRefresh: syncGroups,
}
)
} catch (error) {
if (syncGroups && rawTypeChanged) {
this.$store.dispatch(
this.storePrefix + 'view/grid/setGroupByAggregationsLoading',
{ loading: false }
)
}
throw error
} finally {
this.pendingValueUpdate = false
}
if (syncGroups && rawTypeChanged) {
this.$store.dispatch(
this.storePrefix + 'view/grid/refreshGroupByAggregations',
{
view: this.view,
fields: this.$store.getters['field/getAll'],
fieldId: this.field.id,
}
)
}
},
},
}
</script>