-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathQueryController.js
More file actions
99 lines (89 loc) · 3.33 KB
/
Copy pathQueryController.js
File metadata and controls
99 lines (89 loc) · 3.33 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
/**
* @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.
*/
const { LogManager, updateAndSendExpressResponseFromNativeError } = require('@aliceo2/web-ui');
const { AbortError, throwIfQueryAborted } = require('../utils/queryCancellation');
/**
* Gateway for all calls that are to query InfoLogger database
*/
class QueryController {
/**
* Setup QueryController to be used in the API router
* @param {SQLDataSource} queryService - service to be used to query information on the logs
*/
constructor(queryService) {
/**
* @type {SQLDataSource}
*/
this._queryService = queryService;
this._logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'ilg'}/query-ctrl`);
}
/**
* Given InfoLogger parameters, use the query service to retrieve logs requested
* @param {Request} req - HTTP request object with "query" information on object
* @param {Response} res - HTTP response object to provide information on request
* @returns {void}
*/
async getLogs(req, res) {
const abortController = new AbortController();
const { signal } = abortController;
try {
const { body: { criterias, options } } = req;
if (!criterias || Object.keys(criterias).length === 0) {
res.status(400).json({ error: 'Invalid query parameters provided' });
return;
}
let responseInProgress = true;
res.on('finish', () => {
responseInProgress = false;
});
res.on('close', () => {
if (responseInProgress) {
abortController.abort();
}
});
const logs = await this._queryService.queryFromFilters(criterias, options, signal);
throwIfQueryAborted(signal);
res.status(200).json(logs);
} catch (error) {
if (signal.aborted || error instanceof AbortError) {
this._logger.infoMessage('Query was cancelled by the client');
return;
}
this._logger.errorMessage(error.toString());
updateAndSendExpressResponseFromNativeError(res, error);
}
}
/**
* API endpoint for retrieving total number of logs grouped by severity for a given runNumber
* (Used within FLP)
* @param {Request} req - HTTP request object with "query" information on object
* @param {Response} res - HTTP response object to provide information on request
* @returns {void}
*/
async getQueryStats(req, res) {
const { runNumber } = req.query;
if (!runNumber || isNaN(runNumber)) {
res.status(400).json({ error: 'Invalid runNumber provided' });
} else {
try {
const stats = await this._queryService.queryGroupCountLogsBySeverity(runNumber);
res.status(200).json(stats);
} catch (error) {
this._logger.errorMessage(error.toString());
updateAndSendExpressResponseFromNativeError(res, error);
}
}
}
}
exports.QueryController = QueryController;