-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathQCModel.js
More file actions
188 lines (161 loc) · 7.79 KB
/
QCModel.js
File metadata and controls
188 lines (161 loc) · 7.79 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
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { readFileSync } from 'fs';
import { LogManager } from '@aliceo2/web-ui';
import { openFile, toJSON } from 'jsroot';
import { Kafka, logLevel } from 'kafkajs';
import { CcdbService } from './services/ccdb/CcdbService.js';
import { IntervalsService } from './services/Intervals.service.js';
import { StatusService } from './services/Status.service.js';
import { JsonFileService } from './services/JsonFileService.js';
import { QcObjectService } from './services/QcObject.service.js';
import { FilterService } from './services/FilterService.js';
import { BookkeepingService } from './services/BookkeepingService.js';
import { AliEcsSynchronizer } from './services/external/AliEcsSynchronizer.js';
import { LayoutController } from './controllers/LayoutController.js';
import { StatusController } from './controllers/StatusController.js';
import { ObjectController } from './controllers/ObjectController.js';
import { FilterController } from './controllers/FilterController.js';
import { UserController } from './controllers/UserController.js';
import { config } from './config/configProvider.js';
import { LayoutRepository } from './repositories/LayoutRepository.js';
import { UserRepository } from './repositories/UserRepository.js';
import { ChartRepository } from './repositories/ChartRepository.js';
import { initDatabase } from './database/index.js';
import { SequelizeDatabase } from './database/SequelizeDatabase.js';
import { objectGetByIdValidationMiddlewareFactory }
from './middleware/objects/objectGetByIdValidationMiddlewareFactory.js';
import { objectsGetValidationMiddlewareFactory } from './middleware/objects/objectsGetValidationMiddlewareFactory.js';
import { objectGetContentsValidationMiddlewareFactory }
from './middleware/objects/objectGetContentsValidationMiddlewareFactory.js';
import { RunModeService } from './services/RunModeService.js';
import { KafkaConfigDto } from './dtos/KafkaConfigurationDto.js';
import { QcdbDownloadService } from './services/QcdbDownload.service.js';
const LOG_FACILITY = `${process.env.npm_config_log_label ?? 'qcg'}/model-setup`;
/**
* Model initialization for the QCG application
* @param {WebSocket} ws - web-ui websocket server implementation
* @param {EventEmitter} eventEmitter - Event emitter instance for inter-service communication
* @returns {Promise<object>} Multiple services and controllers that are to be used by the QCG application
*/
export const setupQcModel = async (ws, eventEmitter) => {
const logger = LogManager.getLogger(LOG_FACILITY);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJSON = JSON.parse(readFileSync(`${__dirname}/../package.json`));
const jsonFileService = new JsonFileService(config.dbFile || `${__dirname}/../db.json`);
const databaseConfig = config.database || {};
if (Object.keys(databaseConfig).length > 0) {
try {
const sequelizeDatabase = new SequelizeDatabase(databaseConfig);
await initDatabase(sequelizeDatabase, { forceSeed: config?.database?.forceSeed, drop: config?.database?.drop });
logger.infoMessage('Database initialized successfully');
} catch (error) {
logger.errorMessage(`Database initialization failed: ${error.message}`);
}
} else {
logger.warnMessage('No database configuration found, skipping database initialization');
}
if (config?.kafka?.enabled) {
try {
const validConfig = await KafkaConfigDto.validateAsync(config.kafka);
const { clientId, brokers, consumerGroups } = validConfig;
const kafkaClient = new Kafka({
clientId,
brokers,
retry: { retries: Infinity },
logLevel: logLevel.NOTHING,
});
const aliEcsSynchronizer = new AliEcsSynchronizer(kafkaClient, consumerGroups, eventEmitter);
aliEcsSynchronizer.start();
} catch (error) {
logger.errorMessage(`Kafka initialization/connection failed: ${error.message}`);
}
}
const layoutRepository = new LayoutRepository(jsonFileService);
const userRepository = new UserRepository(jsonFileService);
const chartRepository = new ChartRepository(jsonFileService);
const userController = new UserController(userRepository);
const layoutController = new LayoutController(layoutRepository);
const statusService = new StatusService({ version: packageJSON?.version ?? '-' }, { qc: config.qc ?? {} });
const statusController = new StatusController(statusService);
const qcdbDownloadService = new QcdbDownloadService(config.ccdb);
const ccdbService = CcdbService.setup(config.ccdb);
statusService.dataService = ccdbService;
const qcObjectService = new QcObjectService(ccdbService, chartRepository, { openFile, toJSON });
qcObjectService.refreshCache();
const intervalsService = new IntervalsService();
const bookkeepingService = new BookkeepingService(config.bookkeeping);
try {
await bookkeepingService.connect();
} catch (error) {
logger.errorMessage(`Failed connecting to Bookkeeping: ${error.message || error}`);
}
const filterService = new FilterService(bookkeepingService, config);
const runModeService = new RunModeService(config.bookkeeping, bookkeepingService, ccdbService, eventEmitter, ws);
const objectController = new ObjectController(qcObjectService, runModeService, qcdbDownloadService);
const filterController = new FilterController(filterService, runModeService);
const objectGetByIdValidation = objectGetByIdValidationMiddlewareFactory(filterService);
const objectsGetValidation = objectsGetValidationMiddlewareFactory(filterService);
const objectGetContentsValidation = objectGetContentsValidationMiddlewareFactory(filterService);
initializeIntervals(intervalsService, qcObjectService, filterService, runModeService);
return {
userController,
layoutController,
statusService,
statusController,
objectController,
intervalsService,
filterController,
layoutRepository,
jsonFileService,
objectGetByIdValidation,
objectsGetValidation,
objectGetContentsValidation,
};
};
/**
* Method to register services at the start of the server
* @param {Intervals} intervalsService - wrapper for storing intervals
* @param {QcObjectService} qcObjectService - service for retrieving information on qc objects
* @param {FilterService} filterService - service for retrieving run types information from Bookkeeping
* @param {RunModeService} runModeService - service for monitoring the status of runs
* @returns {void}
*/
function initializeIntervals(intervalsService, qcObjectService, filterService, runModeService) {
intervalsService.register(
qcObjectService.refreshCache.bind(qcObjectService),
qcObjectService.getCacheRefreshRate(),
);
if (filterService.runTypesRefreshInterval > 0) {
intervalsService.register(
filterService.getRunTypes.bind(filterService),
filterService.runTypesRefreshInterval,
);
}
if (runModeService.refreshInterval > 0) {
intervalsService.register(
runModeService.refreshRunsCache.bind(runModeService),
runModeService.refreshInterval,
);
}
if (filterService.dataPassesRefreshInterval > 0) {
intervalsService.register(
filterService.getDataPasses.bind(runModeService),
filterService.dataPassesRefreshInterval,
);
}
}