-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
99 lines (91 loc) · 2.9 KB
/
main.ts
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
import '@-/util/src/app/read-dotenv-side-effect';
import {runApp} from '@-/util/src/app/run';
import {createApp} from '@-/util/src/app/app';
import {getConfig} from './config/config';
import {createInitialContext} from './context/context';
import {Database} from './database/database';
import {readdir} from 'fs/promises';
import {join} from 'path';
import {transformLogsLinesToParsedLines} from './transforms/parsed-lines';
import {
aggregateKicksPerDay,
aggregateKicksPerHour,
transformParsedLinesToKicks,
} from './transforms/kicks';
import {processLogFile} from './logs/process-file';
import {renderReport} from './report/render-report';
import {aggregateParsedLinesPerDay} from './transforms/parsed-lines-per-day';
import {aggregateUpdatesHandlingPerDay} from './transforms/update-handle';
import {
calculateUniqueChats,
calculateUniqueUsers,
} from './transforms/unique-count';
runApp({
withTimeout: false,
createApp: ({logger}) =>
createApp({
getLogger: () => logger,
getConfig,
setupLoggerByConfig: ({logger}) => {
logger.setDebug(true);
},
getInitialContext: createInitialContext,
preInit: async ({app}) => {
app.registerComponent({
name: 'database',
getComponent: ({logger}) => new Database({logger}),
});
await Promise.resolve();
},
}),
afterReady: async ({app, logger, context}) => {
const {
database,
config: {logsDirPath, logsFilesPattern},
} = context;
const patternRegexp = new RegExp(
'^' +
logsFilesPattern.replace(/[*.]/g, (full) => {
switch (full) {
case '*':
return '.*';
case '.':
return '\\.';
default:
throw new Error('unsupported replace');
}
}) +
'$',
);
const names = await readdir(logsDirPath);
const logNames = names.filter((name) => patternRegexp.test(name));
// Add all available logs to the database
await Promise.all(
logNames.map((logName) => {
return processLogFile({
path: join(logsDirPath, logName),
database,
}).catch((error) => {
logger.error('logProcessing', {fileName: logName}, {error});
});
}),
);
await database.onLinesSaved();
// Run transforms
await Promise.all([transformLogsLinesToParsedLines({context})]);
await Promise.all(
[
transformParsedLinesToKicks,
aggregateParsedLinesPerDay,
aggregateUpdatesHandlingPerDay,
calculateUniqueChats,
calculateUniqueUsers,
].map((fun) => fun({context})),
);
await Promise.all([aggregateKicksPerHour].map((fun) => fun({context})));
await Promise.all([aggregateKicksPerDay].map((fun) => fun({context})));
await renderReport({context});
context.needDumpDatabaseOnStop = true;
await app.stop({withTimeout: false});
},
});