MCPcopy Create free account
hub / github.com/Rello/analytics / resolveTopN

Method resolveTopN

lib/Service/FlexibleStorageService.php:784–828  ·  view source on GitHub ↗

@param list ,aggregation:string}> $projection */

(mixed $option, FlexibleColumnResolver $resolver, array $projection, bool $aggregate)

Source from the content-addressed store, hash-verified

782
783 /** @param list<array{column:array<string,mixed>,aggregation:string}> $projection */
784 private function resolveTopN(mixed $option, FlexibleColumnResolver $resolver, array $projection, bool $aggregate): ?array {
785 if ($option === null || $option === [] || (is_array($option) && ($option['type'] ?? 'none') === 'none')) {
786 return null;
787 }
788 if (!$aggregate || !is_array($option)) {
789 throw new FlexibleStorageException('invalid_top_n', 'Top-N requires an aggregated query.');
790 }
791 $dimension = $resolver->resolve((string)($option['dimension'] ?? ''));
792 $measure = $resolver->resolve((string)($option['measure'] ?? ''));
793 $selected = array_column(array_column($projection, 'column'), 'ref');
794 $projectionByRef = [];
795 foreach ($projection as $item) {
796 $projectionByRef[$item['column']['ref']] = $item;
797 }
798 $type = strtolower((string)($option['type'] ?? ''));
799 $number = (int)($option['number'] ?? 0);
800 if (
801 $dimension['role'] !== 'dimension'
802 || $measure['role'] !== 'measure'
803 || !in_array($dimension['ref'], $selected, true)
804 || !in_array($measure['ref'], $selected, true)
805 || !in_array($type, ['top', 'flop'], true)
806 || $number < 1
807 || $number > 1000
808 ) {
809 throw new FlexibleStorageException('invalid_top_n', 'Top-N requires projected dimension and measure references, top/flop type, and a number from 1 to 1000.');
810 }
811 if (!$this->hasNumericAggregateResult($projectionByRef[$measure['ref']])) {
812 throw new FlexibleStorageException('invalid_top_n', 'The Top-N ranking measure must produce a numeric result.', ['column' => $measure['ref']]);
813 }
814 if (($option['others'] ?? false) === true) {
815 foreach ($projection as $item) {
816 if ($item['column']['role'] === 'measure' && !$this->hasNumericAggregateResult($item)) {
817 throw new FlexibleStorageException('invalid_top_n', 'Grouping remaining Top-N rows requires numeric measure results.', ['column' => $item['column']['ref']]);
818 }
819 }
820 }
821 return [
822 'dimension' => $dimension,
823 'measure' => $measure,
824 'type' => $type,
825 'number' => $number,
826 'others' => ($option['others'] ?? false) === true,
827 ];
828 }
829
830 /** @param array{column:array<string,mixed>,aggregation:string} $item */
831 private function hasNumericAggregateResult(array $item): bool {

Callers 1

queryMethod · 0.95

Calls 2

resolveMethod · 0.45

Tested by

no test coverage detected