-
Notifications
You must be signed in to change notification settings - Fork 68
/
DataWarehouseInitializer.php
325 lines (293 loc) · 8.28 KB
/
DataWarehouseInitializer.php
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace OpenXdmod;
use Exception;
use CCR\DB\iDatabase;
use ETL\Configuration\EtlConfiguration;
use ETL\EtlOverseer;
use ETL\EtlOverseerOptions;
use ETL\Utilities;
use FilterListBuilder;
class DataWarehouseInitializer
{
/**
* @var \Log
*/
protected $logger;
/**
* HPcDB database.
*
* @var iDatabase
*/
protected $hpcdbDb;
/**
* MoD warehouse database.
*
* @var iDatabase
*/
protected $warehouseDb;
/**
* Aggregation units.
*
* @var array
*/
protected $aggregationUnits = array(
'day',
'month',
'quarter',
'year'
);
/**
* Name of the aggregate database.
*
* @var string
*/
protected $aggDbName = 'modw_aggregates';
/**
* Default aggregation start date.
*
* @var string
*/
protected $aggregationStartDate;
/**
* Default aggregation end date.
*
* @var string
*/
protected $aggregationEndDate;
/**
* Default append value.
*
* True if aggregation data should be appended.
*
* @var bool
*/
protected $append;
/**
* @param iDatabase $hpcdbDb The HPcDB database.
* @param iDatabase $warehouseDb The MoD warehouse database.
*/
public function __construct(
iDatabase $hpcdbDb,
iDatabase $warehouseDb
) {
$this->hpcdbDb = $hpcdbDb;
$this->warehouseDb = $warehouseDb;
$this->logger = \Log::singleton('null');
}
/**
* Set the logger.
*
* @param \Log $logger A logger instance.
*/
public function setLogger(\Log $logger)
{
$this->logger = $logger;
}
/**
* Set the name of the aggregate database.
*
* @param string $dbName
*/
public function setAggregateDatabaseName($dbName)
{
$this->aggDbName = $dbName;
}
/**
* Ingest all data needed for the data warehouse.
*
* @param string $startDate
* @param string $endDate
*/
public function ingestAll($startDate = null, $endDate = null)
{
$this->logger->debug('Ingesting all data');
if ($startDate !== null) {
$this->logger->debug('Start date: ' . $startDate);
}
if ($endDate !== null) {
$this->logger->debug('End date: ' . $endDate);
}
$this->ingestAllShredded($startDate, $endDate);
$this->ingestAllStaging($startDate, $endDate);
$this->ingestAllHpcdb($startDate, $endDate);
}
/**
* Ingest shredded job data.
*
* @param string $startDate
* @param string $endDate
*/
public function ingestAllShredded($startDate = null, $endDate = null)
{
$this->logger->debug('Ingesting shredded data to staging tables');
$this->runEtlPipeline('staging-ingest-common');
$this->runEtlPipeline('staging-ingest-jobs');
}
/**
* Ingest staging data to the HPcDB.
*
* @param string $startDate
* @param string $endDate
*/
public function ingestAllStaging($startDate = null, $endDate = null)
{
$this->logger->debug('Ingesting staging data to HPCDB');
$this->runEtlPipeline('hpcdb-ingest-common');
$this->runEtlPipeline('hpcdb-ingest-jobs');
}
/**
* Ingest HPcDB data to the MoD warehouse.
*
* @param string $startDate
* @param string $endDate
*/
public function ingestAllHpcdb($startDate = null, $endDate = null)
{
$this->logger->debug('Ingesting HPCDB data to modw');
if ($startDate !== null || $endDate !== null) {
$params = array();
if ($startDate !== null) {
$params['start-date'] = $startDate . ' 00:00:00';
}
if ($endDate !== null) {
$params['end-date'] = $endDate . ' 23:59:59';
}
$this->runEtlPipeline(
'hpcdb-prep-xdw-job-ingest-by-date-range',
$params
);
} else {
$this->runEtlPipeline('hpcdb-prep-xdw-job-ingest-by-new-jobs');
}
// Use current time from the database in case clocks are not
// synchronized.
$lastModifiedStartDate
= $this->hpcdbDb->query('SELECT NOW() AS now FROM dual')[0]['now'];
$this->runEtlPipeline(
'hpcdb-xdw-ingest',
array('last-modified-start-date' => $lastModifiedStartDate)
);
}
/**
* Initialize aggregate database.
*
* This function should be called before all other aggregation
* functions.
*
* @param string $startDate
* @param string $endDate
*/
public function initializeAggregation($startDate = null, $endDate = null)
{
/**
* This is staying around until xsede can be updated to not require this to be changed.
* As this is called from supremm aggregation still.
*/
return;
}
/**
* Create aggregate job data.
*
* @param string $lastModifiedStartDate Last modified start date used to
* determine which jobs will be aggregated.
*/
public function aggregateAllJobs($lastModifiedStartDate)
{
$this->runEtlPipeline(
'jobs-xdw-aggregate',
array('last-modified-start-date' => $lastModifiedStartDate)
);
$filterListBuilder = new FilterListBuilder();
$filterListBuilder->setLogger($this->logger);
$filterListBuilder->buildRealmLists('Jobs');
}
/**
* Aggregate a fact table.
*
* This is staying around until supremm can be updated to etlv2
*
* @param string $aggregator Aggregator class name.
* @param string $startDate Aggregation start date.
* @param string $endDate Aggregation end date.
* @param bool $append True if aggregation data should be appended.
*/
public function aggregate(
$aggregator,
$startDate,
$endDate,
$append = true
) {
$this->logger->info(array(
'message' => 'start',
'class' => get_class($this),
'function' => __FUNCTION__,
'aggregator' => $aggregator,
'start_date' => $startDate,
'end_date' => $endDate,
'append' => $append,
));
foreach ($this->aggregationUnits as $aggUnit) {
$this->logger->info("Aggregating by $aggUnit");
$agg = new $aggregator($aggUnit);
$agg->setLogger($this->logger);
$agg->execute(
$this->warehouseDb,
$this->aggDbName,
$startDate,
$endDate,
$append
);
}
$this->logger->info("Building filter lists");
$agg->updateFilters();
$this->logger->info(array(
'message' => 'end',
'class' => get_class($this),
'function' => __FUNCTION__,
));
}
/**
* Run an ETL pipeline.
*
* @param string $name Pipeline or "section" to run.
* @param array $params Parameters to be passed to used to construct
* EtlOverseerOptions.
*/
private function runEtlPipeline($name, $params = array())
{
$this->logger->debug(
sprintf(
'Running ETL pipeline "%s" with parameters %s',
$name,
json_encode($params)
)
);
$etlConfig = new EtlConfiguration(
CONFIG_DIR . '/etl/etl.json',
null,
$this->logger,
array('default_module_name' => 'xdmod')
);
$etlConfig->initialize();
Utilities::setEtlConfig($etlConfig);
$scriptOptions = array_merge(
array(
'default-module-name' => 'xdmod',
'process-sections' => array($name),
),
$params
);
$this->logger->debug(
sprintf(
'Running ETL pipeline with script options %s',
json_encode($scriptOptions)
)
);
$overseerOptions = new EtlOverseerOptions(
$scriptOptions,
$this->logger
);
$overseer = new EtlOverseer($overseerOptions, $this->logger);
$overseer->execute($etlConfig);
}
}