-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutput.php
More file actions
200 lines (166 loc) · 6.12 KB
/
Output.php
File metadata and controls
200 lines (166 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace Keboola\GoogleAnalyticsExtractor\Extractor;
use Keboola\Component\Manifest\ManifestManager;
use Keboola\Component\Manifest\ManifestManager\Options\OutTable\ManifestOptions;
use Keboola\Component\Manifest\ManifestManager\Options\OutTable\ManifestOptionsSchema;
use Keboola\Csv\CsvFile;
use Keboola\GoogleAnalyticsExtractor\GoogleAnalytics\Result;
class Output
{
private string $dataDir;
private Usage $usage;
private array $options;
private string $outputBucket;
private bool $useLegacyManifest;
public function __construct(
string $dataDir,
string $outputBucket,
bool $useLegacyManifest = true,
array $options = [],
) {
$this->dataDir = $dataDir;
$this->outputBucket = $outputBucket;
$this->useLegacyManifest = $useLegacyManifest;
$this->usage = new Usage($dataDir);
$this->options = $options;
}
public function getUsage(): Usage
{
return $this->usage;
}
public function writeProfiles(CsvFile $csv, array $profiles): CsvFile
{
foreach ($profiles as $profile) {
$csv->writeRow([
'id' => $profile['id'],
'name' => $profile['name'],
'webPropertyId' => $profile['webPropertyId'],
'webPropertyName' => $profile['webPropertyName'],
'accountId' => $profile['accountId'],
'accountName' => $profile['accountName'],
]);
}
return $csv;
}
public function writeProperties(CsvFile $csv, array $properties): CsvFile
{
foreach ($properties as $property) {
$csv->writeRow([
'propertyKey' => $property['propertyKey'],
'propertyName' => $property['propertyName'],
'accountKey' => $property['accountKey'],
'accountName' => $property['accountName'],
]);
}
return $csv;
}
private function createHeaderRowFromQuery(array $query, string $type = 'idProfile'): array
{
$dimensions = array_map(function ($item) {
return str_replace('ga:', '', $item['name']);
}, $query['query']['dimensions']);
$metrics = array_map(function ($item) {
$metric = $item['expression'] ?? $item['name'];
return str_replace('ga:', '', $metric);
}, $query['query']['metrics']);
return array_merge(
['id', $type],
$dimensions,
$metrics,
);
}
public function createReport(array $query): CsvFile
{
return $this->createCsvFile($query['outputTable']);
}
public function createSampleReportJson(array $query, array $report): array
{
$columns = $this->createHeaderRowFromQuery($query);
$rows = [];
$profileId = $query['query']['viewId'];
foreach ($report['data'] as $result) {
$rows[] = array_combine($columns, $this->createReportRow($result, $profileId));
}
return $rows;
}
private function createReportRow(Result $reportDataItem, string $profileId): array
{
$metrics = $this->formatResultKeys($reportDataItem->getMetrics());
$dimensions = $this->formatResultKeys($reportDataItem->getDimensions());
if (isset($dimensions['date'])) {
$time = strtotime($dimensions['date']) ?: 0;
$dimensions['date'] = date('Y-m-d', $time);
}
$pKey = $this->getPrimaryKey($profileId, $dimensions);
return array_merge(
[$pKey, $profileId],
array_values($dimensions),
array_values($metrics),
);
}
public function writeReport(CsvFile $csv, array $report, string $profileId): CsvFile
{
/** @var Result $result */
foreach ($report['data'] as $result) {
$csv->writeRow($this->createReportRow($result, $profileId));
}
return $csv;
}
private function getPrimaryKey(string $profileId, array $dimensions): string
{
// Backward compatibility with data with old (buggy) PKs
if (isset($dimensions['date'])) {
if ($this->isConflictingPrimaryKey() && strtotime($dimensions['date']) < strtotime('2018-03-27')) {
return sha1($profileId . implode('', $dimensions));
}
}
return sha1($profileId . implode('-', $dimensions));
}
private function isConflictingPrimaryKey(): bool
{
return !isset($this->options['nonConflictPrimaryKey']) || $this->options['nonConflictPrimaryKey'] === false;
}
private function formatResultKeys(array $metricsOrDimensions): array
{
$res = [];
foreach ($metricsOrDimensions as $k => $v) {
$res[str_replace('ga:', '', $k)] = $v;
}
return $res;
}
public function createCsvFile(string $name): CsvFile
{
$outTablesDir = $this->dataDir . '/out/tables';
if (!is_dir($outTablesDir)) {
mkdir($outTablesDir, 0777, true);
}
$filename = $this->dataDir . '/out/tables/' . $name . '.csv';
if (!file_exists($filename)) {
touch($filename);
}
return new CsvFile($filename);
}
public function createManifest(
string $name,
array $query,
?array $primaryKey = null,
bool $incremental = false,
string $type = 'idProfile',
): void {
$manifestManager = new ManifestManager($this->dataDir);
$manifestOptions = new ManifestOptions();
$manifestOptions->setIncremental($incremental)
->setDestination(sprintf('%s.%s', $this->outputBucket, $query['outputTable']));
$columns = $this->createHeaderRowFromQuery($query, $type);
foreach ($columns as $column) {
$manifestOptions->addSchema(new ManifestOptionsSchema(
$column,
['base' => ['type' => 'STRING']],
true,
isset($primaryKey) && in_array($column, $primaryKey, true),
));
}
$manifestManager->writeTableManifest($name, $manifestOptions, $this->useLegacyManifest);
}
}