-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
710 lines (606 loc) · 18.8 KB
/
index.js
File metadata and controls
710 lines (606 loc) · 18.8 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// Copyright 2025 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U.
// PROJECT: fiware-data-access
//
// This software and / or computer program has been developed by Telefónica Soluciones
// de Informática y Comunicaciones de España, S.A.U (hereinafter TSOL) and is protected
// as copyright by the applicable legislation on intellectual property.
//
// It belongs to TSOL, and / or its licensors, the exclusive rights of reproduction,
// distribution, public communication and transformation, and any economic right on it,
// all without prejudice of the moral rights of the authors mentioned above. It is expressly
// forbidden to decompile, disassemble, reverse engineer, sublicense or otherwise transmit
// by any means, translate or create derivative works of the software and / or computer
// programs, and perform with respect to all or part of such programs, any type of exploitation.
//
// Any use of all or part of the software and / or computer program will require the
// express written consent of TSOL. In all cases, it will be necessary to make
// an express reference to TSOL ownership in the software and / or computer
// program.
//
// Non-fulfillment of the provisions set forth herein and, in general, any violation of
// the peaceful possession and ownership of these rights will be prosecuted by the means
// provided in both Spanish and international law. TSOL reserves any civil or
// criminal actions it may exercise to protect its rights.
import express from 'express';
import { startFetcher } from './fetcher.js';
import { shutdownAgenda, initAgenda } from './lib/jobs.js';
import {
getFDAs,
fetchFDA,
executeQuery,
executeFDAQuery,
executeQueryStream,
executeFDAQueryStream,
createDA,
getFDA,
updateFDA,
deleteFDA,
getDAs,
getDA,
putDA,
deleteDA,
} from './lib/fda.js';
import { createIndex, disconnectClient } from './lib/utils/mongo.js';
import { destroyS3Client } from './lib/utils/aws.js';
import { closePgPools } from './lib/utils/pg.js';
import { config } from './lib/fdaConfig.js';
import {
initLogger,
getBasicLogger,
getInitialLogger,
} from './lib/utils/logger.js';
import { handleCdaQuery } from './lib/compat/cdaAdapter.js';
import {
validateAllowedFieldsBody,
validateForbiddenFieldsQuery,
parseBooleanQueryParam,
} from './lib/utils/utils.js';
import {
VALID_OUTPUT_TYPES,
DEFAULT_OUTPUT_TYPE,
rowsToCsv,
rowsToXlsx,
} from './lib/utils/outputFormat.js';
import {
onRequestStart,
onRequestFinish,
buildHealthPayload,
buildMetricsText,
getMetricsContentType,
} from './lib/metrics.js';
export const app = express();
const PORT = config.port;
const logger = getBasicLogger();
// Supported MIME types for /data, listed in server-default preference order.
const DATA_CONTENT_TYPES = [
'application/json',
'application/x-ndjson',
'text/csv',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
];
const DATA_ACCEPT_CONTENT_TYPE_TO_OUTPUT = {
'application/json': 'json',
'application/x-ndjson': 'ndjson',
'text/csv': 'csv',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xls',
'application/vnd.ms-excel': 'xls',
};
async function sendRowsByOutputType(res, rows, outputType) {
if (outputType === 'csv') {
const csv = rowsToCsv(rows);
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="results.csv"');
return res.send(csv);
}
if (outputType === 'xls') {
const buffer = await rowsToXlsx(rows);
res.setHeader(
'Content-Type',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
);
res.setHeader('Content-Disposition', 'attachment; filename="results.xlsx"');
return res.send(Buffer.from(buffer));
}
return res.json(rows);
}
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
const reqStartMs = onRequestStart();
const oldSend = res.send;
const oldJson = res.json;
function capture(body) {
try {
let str = typeof body === 'string' ? body : JSON.stringify(body);
if (str.length > config.logger.resSize) {
str = str.slice(0, config.logger.resSize) + '…[truncated]';
}
res.locals.responseBody = str;
} catch {
res.locals.responseBody = '[unserializable body]';
}
}
res.send = function (body) {
capture(body);
return oldSend.call(this, body);
};
res.json = function (body) {
capture(body);
return oldJson.call(this, body);
};
const start = Date.now();
res.on('finish', () => {
onRequestFinish(req, res, reqStartMs);
logger.info(
{
method: req.method,
path: req.originalUrl,
fiwareService: req.get('Fiware-Service'),
reqParams: `${JSON.stringify(req.params)}`,
reqQuery: `${JSON.stringify(req.query)}`,
reqBody: `${JSON.stringify(req.body)}`,
resCode: res.statusCode,
resMsg: res.statusMessage,
durationMs: Date.now() - start,
ip: req.ip,
userAgent: req.headers['user-agent'],
resSize: res.getHeader('Content-Length'),
resBody: res.locals.responseBody,
},
'API request completed',
);
});
next();
});
app.get('/health', async (req, res) => {
const payload = await buildHealthPayload();
res.status(200).json(payload);
});
app.get('/metrics', async (req, res) => {
const contentNegotiation = getMetricsContentType(req.get('Accept'));
if (!contentNegotiation.ok) {
return res.status(406).json({
error: 'NotAcceptable',
description:
'Accept header must allow application/openmetrics-text or text/plain',
});
}
res.setHeader('Content-Type', contentNegotiation.contentType);
const payload = await buildMetricsText();
return res.status(200).send(payload);
});
app.get('/:visibility/fdas', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility } = req.params;
if (!service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const fdas = await getFDAs(service, visibility, servicePath);
return res.status(200).json(fdas);
});
app.post('/:visibility/fdas', async (req, res) => {
const body = req.body ?? {};
validateAllowedFieldsBody(body, [
'id',
'query',
'description',
'refreshPolicy',
'timeColumn',
'objStgConf',
'cached',
]);
const {
id,
query,
description,
refreshPolicy,
timeColumn,
objStgConf,
cached,
} = body;
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility } = req.params;
const defaultDataAccessByConfig = config.defaultDataAccess?.enabled ?? true;
const defaultDataAccessEnabled =
req.query.defaultDataAccess === undefined
? defaultDataAccessByConfig
: parseBooleanQueryParam(
req.query.defaultDataAccess,
'defaultDataAccess',
true,
);
const cachedEnabled =
cached === undefined
? true
: parseBooleanQueryParam(cached, 'cached', true);
if (!id || !query || !service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const finalRefreshPolicy = refreshPolicy ?? { type: 'none' };
const finalObjStgConf = objStgConf ?? {};
await fetchFDA(
id,
query,
service,
visibility,
servicePath,
description,
finalRefreshPolicy,
timeColumn,
finalObjStgConf,
defaultDataAccessEnabled,
cachedEnabled,
);
return res.status(202).json({
id,
status: 'pending',
});
});
app.get('/:visibility/fdas/:fdaId', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId } = req.params;
if (!fdaId || !service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const fda = await getFDA(service, fdaId, visibility, servicePath);
return res.status(200).json(fda);
});
app.put('/:visibility/fdas/:fdaId', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId } = req.params;
if (!service || !fdaId || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
if (req.body && Object.keys(req.body).length > 0) {
return res.status(400).json({
error: 'BadRequest',
description: 'PUT /fdas does not accept a request body',
});
}
await updateFDA(service, fdaId, visibility, servicePath);
return res.status(202).json({
id: fdaId,
status: 'pending',
});
});
app.delete('/:visibility/fdas/:fdaId', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId } = req.params;
if (!service || !fdaId || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
await deleteFDA(service, fdaId, visibility, servicePath);
return res.sendStatus(204);
});
app.get('/:visibility/fdas/:fdaId/das', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId } = req.params;
if (!fdaId || !service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const das = await getDAs(service, fdaId, visibility, servicePath);
return res.status(200).json(das);
});
app.post('/:visibility/fdas/:fdaId/das', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId } = req.params;
const body = req.body ?? {};
validateAllowedFieldsBody(body, ['id', 'query', 'description', 'params']);
const { id, description, query, params } = body;
if (!fdaId || !id || !query || !service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
await createDA(
service,
fdaId,
id,
description,
query,
params,
visibility,
servicePath,
);
return res.sendStatus(201);
});
app.get('/:visibility/fdas/:fdaId/das/:daId', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId, daId } = req.params;
if (!service || !fdaId || !daId || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const da = await getDA(service, fdaId, daId, visibility, servicePath);
return res.status(200).json(da);
});
app.put('/:visibility/fdas/:fdaId/das/:daId', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId, daId } = req.params;
const body = req.body ?? {};
validateAllowedFieldsBody(body, ['query', 'description', 'params']);
const { description, query, params } = body;
if (!service || !fdaId || !daId || !query || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
await putDA(
service,
fdaId,
daId,
description,
query,
params,
visibility,
servicePath,
);
return res.sendStatus(204);
});
app.delete('/:visibility/fdas/:fdaId/das/:daId', async (req, res) => {
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
const { visibility, fdaId, daId } = req.params;
if (!service || !fdaId || !daId || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
await deleteDA(service, fdaId, daId, visibility, servicePath);
return res.sendStatus(204);
});
app.get('/:visibility/fdas/:fdaId/das/:daId/data', async (req, res) => {
const { visibility, fdaId, daId } = req.params;
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
validateForbiddenFieldsQuery(req.query, ['outputType', 'fresh']);
const matched = req.accepts(DATA_CONTENT_TYPES);
if (!matched) {
return res.status(406).json({
error: 'NotAcceptable',
description:
'Accept header must allow application/json, application/x-ndjson, text/csv, or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
}
const outputType = DATA_ACCEPT_CONTENT_TYPE_TO_OUTPUT[matched];
const queryParams = { ...req.query };
delete queryParams.outputType;
if (!fdaId || !daId || !service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const params = {
fdaId,
daId,
...queryParams,
};
if (outputType === 'ndjson' || outputType === 'csv') {
return executeQueryStream({
service,
visibility,
servicePath,
params,
req,
res,
format: outputType,
});
}
const rows = await executeQuery({
service,
visibility,
servicePath,
params,
});
return sendRowsByOutputType(res, rows, outputType);
});
app.get('/:visibility/fdas/:fdaId/data', async (req, res) => {
const { visibility, fdaId } = req.params;
const service = req.get('Fiware-Service');
const servicePath = req.get('Fiware-ServicePath');
if (Object.keys(req.query).length > 0) {
return res.status(400).json({
error: 'BadRequest',
description: 'FDA fresh query does not accept query parameters',
});
}
const matched = req.accepts(DATA_CONTENT_TYPES);
if (!matched) {
return res.status(406).json({
error: 'NotAcceptable',
description:
'Accept header must allow application/json, application/x-ndjson, text/csv, or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
}
const outputType = DATA_ACCEPT_CONTENT_TYPE_TO_OUTPUT[matched];
if (!fdaId || !service || !servicePath || !visibility) {
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
if (outputType === 'ndjson' || outputType === 'csv') {
return executeFDAQueryStream({
service,
visibility,
servicePath,
fdaId,
req,
res,
format: outputType,
});
}
const rows = await executeFDAQuery({
service,
visibility,
servicePath,
fdaId,
});
return sendRowsByOutputType(res, rows, outputType);
});
app.post('/plugin/cda/api/doQuery', async (req, res) => {
const startTime = Date.now();
const requestId = Math.random().toString(36).substring(7);
logger.info('[CDA REQUEST]', {
requestId,
method: req.method,
url: req.originalUrl,
headers: {
contentType: req.get('Content-Type'),
accept: req.get('Accept'),
fiwareService: req.get('Fiware-Service'),
},
query: req.query,
bodyKeys: Object.keys(req.body || {}),
paramKeys: Object.keys(req.body || {}).filter((k) => k.startsWith('param')),
timestamp: new Date().toISOString(),
});
logger.debug('[CDA REQUEST BODY STRINGIFIED]', {
requestId,
body: JSON.stringify(req.body),
});
const { path, dataAccessId } = req.body;
if (!path || !dataAccessId) {
logger.warn('[CDA BAD REQUEST]', {
requestId,
reason: 'Missing path or dataAccessId',
});
return res.status(400).json({
error: 'BadRequest',
description: 'Missing params in the request',
});
}
const rawOutputType = req.body.outputType || DEFAULT_OUTPUT_TYPE;
if (!VALID_OUTPUT_TYPES.includes(rawOutputType)) {
return res.status(400).json({
error: 'BadRequest',
description: `Invalid outputType '${rawOutputType}'. Allowed values: ${VALID_OUTPUT_TYPES.join(', ')}`,
});
}
try {
const result = await handleCdaQuery({
body: req.body,
outputType: rawOutputType,
});
logger.info('[CDA RESPONSE]', {
requestId,
status: 200,
durationMs: Date.now() - startTime,
rows: result?.resultset?.length,
totalRows: result?.queryInfo?.totalRows,
});
logger.debug('[CDA RESPONSE DETAIL]', {
requestId,
metadata: result?.metadata,
queryInfo: result?.queryInfo,
sampleRows: result?.resultset?.slice(0, 2),
});
return sendRowsByOutputType(res, result, rawOutputType);
} catch (err) {
logger.error('Error executing query:', err);
const status = err.status || 500;
return res.status(status).json({
error: err.type || 'InternalServerError',
description: err.message || 'Unexpected error executing query',
});
}
});
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
const status = err.status || 500;
if (status < 500) {
logger.warn(err);
} else {
logger.error(err);
}
return res.status(status).json({
error: err.type || 'InternalServerError',
description: err.message,
});
});
if (process.env.NODE_ENV !== 'test') {
startup()
.then(() => {
if (config.roles.apiServer) {
app.listen(PORT, () => {
logger.debug(`API Server listening at port ${PORT}`);
});
}
if (config.roles.fetcher) {
startFetcher().catch((err) => {
logger.error('[Fetcher] Failed to start', err);
process.exit(1);
});
}
})
.catch((err) => {
logger.error(`Startup failed: ${err}`);
process.exit(1);
});
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
}
async function startup() {
if (!config.roles.apiServer && !config.roles.fetcher) {
throw new Error('At least one FDA role must be enabled');
}
await createIndex();
initLogger(config);
if (config.roles.apiServer || config.roles.fetcher) {
await initAgenda();
}
getInitialLogger(config).fatal('[INIT]: Initializing app');
}
let shuttingDown = false;
async function shutdown() {
if (shuttingDown) return;
shuttingDown = true;
logger.info('[SHUTDOWN] Graceful shutdown started');
try {
if (config.roles.fetcher) {
await shutdownAgenda();
}
await disconnectClient();
await destroyS3Client();
await closePgPools();
logger.info('[SHUTDOWN] Completed');
process.exit(0);
} catch (err) {
logger.error('[SHUTDOWN] Failed', err);
process.exit(1);
}
}