Skip to content

Invalid aggregate function: none #5924

@sqwk

Description

@sqwk

Using ReportMetric::AGGREGATE_NONE as per https://docs.octobercms.com/4.x/extend/dashboards/data-sources.html#registering-metrics produces the error Invalid aggregate function: none

Some background: I am trying to display durations (time between a start_time and an end_time) on a widget using the new DataSource interface. Since aliases cannot be summed the following does not work:

        $this->registerMetric(new ReportMetric(
            self::METRIC_ENTRY_DURATION,
            'entry_duration',
            'Recorded Entry Duration',
            ReportMetric::AGGREGATE_SUM
        ));

        (...)

        $reportQueryBuilder->onConfigureQuery(
            function(Builder $query, ReportDimension $dimension, array $metrics) {
                $query->addSelect([
                    Db::raw('TIMESTAMPDIFF(SECOND, start_time, end_time) / 3600 AS entry_duration')
                ]);
            }
        );

as that will add SUM(entry_duration) to the query.

Instead I figured one could skip the aggregate function and do the sum as part of the query manually:

        $this->registerMetric(new ReportMetric(
            self::METRIC_ENTRY_DURATION,
            'id',
            'Recorded Entry Duration',
            ReportMetric::AGGREGATE_NONE
        ));

        (...)

        $reportQueryBuilder->onConfigureQuery(
            function(Builder $query, ReportDimension $dimension, array $metrics) {
                $query->addSelect([
                    Db::raw('SUM(TIMESTAMPDIFF(SECOND, start_time, end_time) / 3600) AS oc_metric_entry_duration')
                ]);
            }
        );

however, that produces the error above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions