Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
76 lines (65 loc) · 2.04 KB
/
Copy pathmain.ts
File metadata and controls
76 lines (65 loc) · 2.04 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
6
7D5C
3
64
65
66
67
68
69
70
71
72
73
74
75
76
import _ from 'lodash';
import * as mongo from './external/mongo';
import extract from './stages/extract';
import transform from './stages/transform';
import load from './stages/load';
import { STAGE } from './index';
import Timer from './timer';
import Logger from './logger';
const timer = Timer();
const logger = Logger('Main', timer);
type ETLSummary = {
extract?: any;
transform?: any;
load?: any;
duration?: number;
errors: string[];
stages: STAGE[];
};
async function recordSummary(summary: ETLSummary) {
if (mongo.collections.runs) {
await mongo.collections.runs.insertOne(_.clone(summary));
}
}
async function main(stages: STAGE[]) {
timer.start();
const summary: ETLSummary = { stages, errors: [] };
let activeStage;
try {
logger.info('#####', 'STARTING ETL');
logger.info('Stages to run:', ...stages);
if (stages.includes(STAGE.EXTRACT) && stages.includes(STAGE.LOAD) && !stages.includes(STAGE.TRANSFORM)) {
throw new Error(
'EXTRACT and LOAD requested without TRANSFORM stage. Stopping this run to protect against accidentally loading different data than was extracted. You likely meant to use --all to run all stages.',
);
}
await mongo.connect();
if (stages.includes(STAGE.EXTRACT)) {
activeStage = STAGE.EXTRACT;
const extractSummary = await extract();
summary.extract = extractSummary;
}
if (stages.includes(STAGE.TRANSFORM)) {
activeStage = STAGE.TRANSFORM;
summary.transform = await transform();
}
if (stages.includes(STAGE.LOAD)) {
activeStage = STAGE.LOAD;
summary.load = await load();
}
} catch (e) {
if (activeStage) {
logger.error(`ETL threw an error during ${activeStage}:`, e);
} else {
logger.error(`ETL threw an error:`, e);
}
summary.errors.push((e as Error).message);
} finally {
summary.duration = timer.time();
await recordSummary(summary);
await mongo.close();
logger.info('Summary', summary);
logger.info('#####', 'FINISHED ETL');
}
}
export default main;