From 1303f5a83cf98a9a55456a33ac3d950f7c3a3d51 Mon Sep 17 00:00:00 2001 From: Valentina Schiavi Date: Tue, 30 Jul 2024 11:16:49 +0200 Subject: [PATCH] Unable to retrieve all data using API:/provided-mac-table-of-all-devices Fixes #231 --- server/index.js | 5 ++-- server/service/IndividualServicesService.js | 33 +++++++++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/server/index.js b/server/index.js index f628e49..56f589c 100644 --- a/server/index.js +++ b/server/index.js @@ -26,10 +26,11 @@ var expressAppConfig = oas3Tools.expressAppConfig(path.join(__dirname, 'api/open var app = expressAppConfig.getApp(); appCommons.setupExpressApp(app); -//TO FIX! -//global.databasePath ='d:/Working/VSCode/MacAddressTableRecorder/server/database/load.json' +//TO REMOVE, ONLY FOR DEBUG +//global.databasePath = 'D:/\VALE/\WorkindDoc/\MICROSERVIZI/\loadjson/\loadMATRRunning.json' global.databasePath = './database/load.json' + prepareElasticsearch().catch(err => { console.error(`Error preparing Elasticsearch : ${err}`); }).finally( diff --git a/server/service/IndividualServicesService.js b/server/service/IndividualServicesService.js index 8d35f86..03fdb31 100644 --- a/server/service/IndividualServicesService.js +++ b/server/service/IndividualServicesService.js @@ -889,17 +889,19 @@ function orderData(input) { return output; } + const PromptForProvidingAllMacTablesCausesReadingFromElasticSearch = async function () { return new Promise(async function (resolve, reject) { - let client = await elasticsearchService.getClient(false); - let indexAlias = await getIndexAliasAsync(); + const response = { 'application/json': [] }; try { + // Inizialize scroll operation let res2 = await client.search({ index: indexAlias, _source: 'mac-address', + scroll: '1m', // Keep window scroll opened for 1 minute body: { query: { match: { @@ -909,16 +911,28 @@ const PromptForProvidingAllMacTablesCausesReadingFromElasticSearch = async funct } }); - const response = { 'application/json': [] }; + let scrollId = res2.body._scroll_id; + let hits = res2.body.hits.hits; - const hits = res2.body.hits.hits; - for (const hit of hits) { - const source = hit._source['mac-address']; + // Continue to retrieve as long as there are documents. + while (hits.length > 0) { + for (const hit of hits) { + const source = hit._source['mac-address']; - for (const element of source) { - element["time-stamp-of-data"] = formatTimestamp(element["time-stamp-of-data"]); - response['application/json'].push(element); + for (const element of source) { + element["time-stamp-of-data"] = formatTimestamp(element["time-stamp-of-data"]); + response['application/json'].push(element); + } } + + // Next scroll request + res2 = await client.scroll({ + scroll_id: scrollId, + scroll: '1m' + }); + + scrollId = res2.body._scroll_id; + hits = res2.body.hits.hits; } if (Object.keys(response).length > 0) { @@ -932,6 +946,7 @@ const PromptForProvidingAllMacTablesCausesReadingFromElasticSearch = async funct }); }; + /** * Responses with a list of MAC tables of all connected devices. *