| 23 | use Symfony\Component\Console\Output\OutputInterface; |
| 24 | |
| 25 | class AnalyticsMigrator implements IMigrator { |
| 26 | private const EXPORT_FILE = 'analytics/user-data.json'; |
| 27 | private const VERSION = 2; |
| 28 | |
| 29 | public function __construct( |
| 30 | private IDBConnection $db, |
| 31 | private IL10N $l10n, |
| 32 | ) { |
| 33 | } |
| 34 | |
| 35 | public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void { |
| 36 | $uid = $user->getUID(); |
| 37 | $output->writeln('<info>Exporting Analytics data</info>'); |
| 38 | |
| 39 | $datasets = $this->fetchAllByUser('analytics_dataset', $uid); |
| 40 | $datasetIds = array_map(static fn (array $row): int => (int)$row['id'], $datasets); |
| 41 | |
| 42 | $reports = $this->fetchAllByUser('analytics_report', $uid); |
| 43 | $reportIds = array_map(static fn (array $row): int => (int)$row['id'], $reports); |
| 44 | |
| 45 | $payload = [ |
| 46 | 'datasets' => $datasets, |
| 47 | 'reports' => $reports, |
| 48 | 'dataloads' => $this->fetchAllByIds('analytics_dataload', 'dataset', $datasetIds), |
| 49 | 'facts' => $this->fetchAllByIds('analytics_facts', 'dataset', $datasetIds), |
| 50 | 'columns' => $this->fetchAllByIds('analytics_flex_columns', 'dataset_id', $datasetIds), |
| 51 | 'records' => $this->fetchAllByIds('analytics_flex_records', 'dataset_id', $datasetIds), |
| 52 | 'values' => $this->fetchAllByIds('analytics_flex_values', 'dataset_id', $datasetIds), |
| 53 | 'thresholds' => $this->fetchAllByIds('analytics_threshold', 'report', $reportIds), |
| 54 | 'panoramas' => $this->fetchAllByUser('analytics_panorama', $uid), |
| 55 | ]; |
| 56 | |
| 57 | $encoded = json_encode($payload, JSON_THROW_ON_ERROR); |
| 58 | $exportDestination->addFileContents(self::EXPORT_FILE, $encoded); |
| 59 | } |
| 60 | |
| 61 | public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void { |
| 62 | if (!$importSource->pathExists(self::EXPORT_FILE)) { |
| 63 | $output->writeln('<comment>No Analytics data found in archive</comment>'); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $raw = $importSource->getFileContents(self::EXPORT_FILE); |
| 68 | $payload = json_decode($raw, true, 512, JSON_THROW_ON_ERROR); |
| 69 | if (!is_array($payload)) { |
| 70 | throw new UserMigrationException('Invalid Analytics migration payload'); |
| 71 | } |
| 72 | |
| 73 | $uid = $user->getUID(); |
| 74 | $datasetMap = []; |
| 75 | $reportMap = []; |
| 76 | $reportDatasetMap = []; |
| 77 | foreach (($payload['reports'] ?? []) as $report) { |
| 78 | if (isset($report['id'], $report['dataset'])) { |
| 79 | $reportDatasetMap[(int)$report['id']] = (int)$report['dataset']; |
| 80 | } |
| 81 | } |
| 82 |
nothing calls this directly
no outgoing calls
no test coverage detected