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

Method execute

lib/Service/TransformationService.php:16–167  ·  view source on GitHub ↗
(array $source, array $definition, array $options = [])

Source from the content-addressed store, hash-verified

14 */
15class TransformationService {
16 public function execute(array $source, array $definition, array $options = []): array {
17 if (($definition['version'] ?? null) !== 1) {
18 throw new \InvalidArgumentException('Unsupported transformation version.');
19 }
20 if (($source['truncated'] ?? false) === true || ($source['queryProcessing']['pagination'] ?? false) === true) {
21 throw new \InvalidArgumentException('Calculations require the complete source result before pagination.');
22 }
23 $header = array_values($source['header'] ?? []);
24 $refs = array_values($source['columnRefs'] ?? array_map(static fn (int $i): string => 'source:' . $i, array_keys($header)));
25 if (count($header) !== count($refs) || count($refs) !== count(array_unique($refs))) {
26 throw new \InvalidArgumentException('The source column schema is invalid.');
27 }
28 $columns = [];
29 $measureNames = $source['keyFigures'] ?? [];
30 foreach ($refs as $index => $ref) {
31 $descriptor = $source['columns'][$index] ?? $source['sourceColumns'][$index] ?? [];
32 $role = $descriptor['role'] ?? (in_array($header[$index], $measureNames, true) ? 'measure' : 'dimension');
33 $columns[$ref] = [
34 ...$descriptor,
35 'ref' => $ref,
36 'name' => (string)$header[$index],
37 'role' => $role,
38 'aggregation' => $descriptor['defaultAggregation'] ?? 'sum',
39 'type' => $descriptor['type'] ?? ($role === 'measure' ? 'decimal' : 'text'),
40 ];
41 }
42 foreach (($definition['aggregations'] ?? []) as $ref => $aggregation) {
43 if (!isset($columns[$ref]) || $columns[$ref]['role'] !== 'measure'
44 || !in_array($aggregation, ['none', 'sum', 'avg', 'min', 'max', 'count', 'count_distinct'], true)) {
45 throw new \InvalidArgumentException('A source measure has an invalid aggregation.');
46 }
47 $columns[$ref]['aggregation'] = $aggregation;
48 }
49 $sourceRefs = $refs;
50 $calculations = $this->prepareCalculations($definition['calculations'] ?? [], $columns);
51 $rows = [];
52 foreach ($source['data'] ?? [] as $input) {
53 if (!is_array($input)) {
54 continue;
55 }
56 $row = [];
57 foreach ($sourceRefs as $index => $ref) {
58 $row[$ref] = $input[$index] ?? null;
59 }
60 foreach ($calculations as $calc) {
61 if ($calc['phase'] === 'before') {
62 $row[$calc['ref']] = $this->evaluate($calc['rpn'], $row);
63 }
64 }
65 $rows[] = $row;
66 }
67
68 $rowMeasureRefs = array_values(array_filter($sourceRefs, static fn (string $ref): bool => $columns[$ref]['role'] === 'measure'));
69 foreach ($calculations as $calc) {
70 if ($calc['phase'] === 'before') {
71 $rowMeasureRefs[] = $calc['ref'];
72 }
73 }

Callers

nothing calls this directly

Calls 4

prepareCalculationsMethod · 0.95
evaluateMethod · 0.95
aggregateMethod · 0.95
countFunction · 0.50

Tested by

no test coverage detected