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

Class AnalyticsMigrator

lib/UserMigration/AnalyticsMigrator.php:24–452  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected