| 49 | } |
| 50 | |
| 51 | public function importBundle(array $bundle, bool $demo = false): array { |
| 52 | BundleDefinition::validate($bundle); |
| 53 | $folders = $datasets = $reports = $panoramas = []; |
| 54 | $this->db->beginTransaction(); |
| 55 | try { |
| 56 | foreach ($bundle['folders'] ?? [] as $folder) { |
| 57 | $id = match ($folder['scope']) { |
| 58 | 'datasets' => $this->datasets->createGroup(), |
| 59 | 'panoramas' => $this->panoramas->createGroup(), |
| 60 | 'reports' => $this->reports->create($folder['name'], '', 0, 0, 0, '{}', 'table', '', '', '', ''), |
| 61 | }; |
| 62 | $this->requireId($id); |
| 63 | if ($folder['scope'] === 'datasets') $this->requireSuccess($this->datasets->rename($id, $folder['name'])); |
| 64 | if ($folder['scope'] === 'panoramas') $this->requireSuccess($this->panoramas->rename($id, $folder['name'])); |
| 65 | $folders[$folder['key']] = $id; |
| 66 | } |
| 67 | $definitions = []; |
| 68 | foreach ($bundle['datasets'] ?? [] as $dataset) { |
| 69 | $id = $this->datasets->create($dataset['name'], $dataset['dimension1'], $dataset['dimension2'], $dataset['value']); |
| 70 | $this->requireId($id); |
| 71 | if (isset($dataset['folder'])) $this->requireSuccess($this->datasets->updateGroup($id, $folders[$dataset['folder']])); |
| 72 | foreach ($dataset['data'] ?? [] as $row) $this->storage->create($id, $row[0], $row[1], $row[2]); |
| 73 | $datasets[$dataset['key']] = $id; |
| 74 | $definitions[$dataset['key']] = $dataset; |
| 75 | } |
| 76 | foreach ($bundle['reports'] as $report) { |
| 77 | $dataset = $definitions[$report['dataset']]; |
| 78 | $id = $this->reports->create($report['name'], $report['subheader'] ?? '', $folders[$report['folder'] ?? ''] ?? 0, |
| 79 | 2, $datasets[$report['dataset']], '{}', $report['visualization'], $report['chart'], |
| 80 | $dataset['dimension1'], $dataset['dimension2'], $dataset['value']); |
| 81 | $this->requireId($id); |
| 82 | $json = static fn(string $field) => isset($report[$field]) ? json_encode($report[$field], JSON_THROW_ON_ERROR) : null; |
| 83 | $this->requireSuccess($this->reports->updateOptions($id, $json('chartoptions'), $json('dataoptions'), $json('filteroptions'), $json('tableoptions'))); |
| 84 | foreach ($report['thresholds'] ?? [] as $threshold) { |
| 85 | $this->requireId($this->thresholds->create($id, $threshold['dimension'], $threshold['option'], $threshold['value'], $threshold['severity'], $threshold['coloring'])); |
| 86 | } |
| 87 | $reports[$report['key']] = $id; |
| 88 | } |
| 89 | foreach ($bundle['panoramas'] ?? [] as $panorama) { |
| 90 | $parent = $folders[$panorama['folder'] ?? ''] ?? 0; |
| 91 | $id = $this->panoramas->create(1, $parent); |
| 92 | $this->requireId($id); |
| 93 | $pages = $panorama['pages']; |
| 94 | foreach ($pages as $index => &$page) { |
| 95 | $page['page'] = $index; |
| 96 | foreach ($page['reports'] as &$tile) { |
| 97 | if ($tile['type'] === 0) $tile['value'] = $reports[$tile['value']]; |
| 98 | } |
| 99 | unset($tile); |
| 100 | } |
| 101 | unset($page); |
| 102 | $this->requireSuccess($this->panoramas->update($id, $panorama['name'], 1, $parent, $pages, BundleDefinition::mapFilters($panorama['filters'] ?? [], $reports))); |
| 103 | $panoramas[$panorama['key']] = $id; |
| 104 | } |
| 105 | $entry = $panoramas[$bundle['entrypoint'] ?? ''] ?? null; |
| 106 | if ($demo) { |
| 107 | $this->requireId($entry ?? 0); |
| 108 | $this->config->setUserValue($this->userId, 'analytics', 'business-demo-panorama', (string)$entry); |