ReportDataQueryBuilder helps building queries for report data sources.

The report query builder doesn't load any extra dimension fields. They must be loaded by the report data source itself. However, the report query builder manages sorting and filtering by dimension fields, so they are expected to be loaded to the query by the data source.

The report data query builder doesn’t apply the group interval. It always groups data by the dimension field, irrespective of the group interval. However, for ReportDataSourceBase::GROUP_INTERVAL_FULL, grouping is not applied. This exception is because for the FULL group interval, the entire data set is considered as a single group.

Public Methods

public __construct()

public __construct(
    string $tableName,
    ReportDimension $dimension,
    array $metrics,
    ReportDataOrderRule $orderRule,
    array $dimensionFilters,
    int $limit,
    ?ReportDataPaginationParams $paginationParams,
    string $groupInterval,
    bool $hideEmptyDimensionValues,
    ?Carbon $startDate,
    ?Carbon $endDate,
    ?int $startTimestamp,
    ?string $dateColumnName,
    ?string $timestampColumnName,
    bool $totalsOnly
): void

Creates a new instance of the query builder.

public getFetchDataResult()

public getFetchDataResult(
    array $metricsConfiguration,
    ?string $recordUrlTemplate = null
): ReportFetchDataResult 

Fetches the report data based on the initialized query and configuration.

This method does the following:

  1. Initializes a query using the initQuery method.
  2. Executes the database queries and constructs a ReportFetchDataResult object.
  3. If pagination is set, it also calculates the total number of records.
  4. Computes metric totals based on the given metric configuration.

This method should be used if no extra configuration is required on the query returned by the initQuery method. If additional configurations are needed, create and configure the ReportFetchDataResult object directly, instead of using this method.

public getMetricTotals()

public getMetricTotals(array $metricsConfiguration): array 

getMetricTotals returns totals for metric with the metric totals or the relative bar enabled.

public getTotalRecords()

public getTotalRecords(): int 

getTotalRecords returns the total number of unpaginated records.

public initQuery()

public initQuery(): Illuminate\Database\Query\Builder 

initQuery initializes a Laravel query based on the provided configuration.

public onConfigureMetric()

public onConfigureMetric(callable $callback): void

Registers a callback to be called when the query is being built to configure a specific metric. Use this method to a metric column to the query. The added metric column name must have the format ocmetric[metric code]

If the callback returns true, the report data query builder doesn't add any additional SQL for the metric.

public onConfigureMetrics()

public onConfigureMetrics(callable $callback): void

Registers a callback to be called when the query is being built to configure metrics. Use this method to add joins to the query for metrics that belong to tables other than the one specified in the constructor.

public onConfigureQuery()

public onConfigureQuery(callable $callback): void

Registers a callback for the final query configuration. Use this method to add dimension fields or filters to the query. The callback must not add pagination filters. The same callback is used for calculating the total number of unpaginated records.