Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Elastic Search error in findNotConnectedElements #158

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions server/service/IndividualServicesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,10 @@ const RequestForDeleteEquipmentIntoElasticSearch = async function (mountName) {
} else {
resolve();
}
console.log('Remove mountName={' + mountName + '}');
}
catch (error) {
console.error('Failed to remove mountName={' + mountName + '}');
reject(error)
}
});
Expand All @@ -298,7 +300,7 @@ const findNotConnectedElements = async function (listJsonES, listJsonMD) {
if (missingElements.length > 0) {
// Create a new JSON object with the result
const resultJSON = {
"mountName - list": missingElements
"mountName-list": missingElements
};
resolve(resultJSON);
}
Expand Down Expand Up @@ -369,22 +371,23 @@ exports.updateCurrentConnectedEquipment = async function (user, originator, xCor
//list of equipment that was connected (mac-address data in ES) but now that are not connected
const listJsonDisconnectedEq = await findNotConnectedElements(oldConnectedListFromES, newConnectedListFromMwdi);

//Write new "mountName - list" list into ES
if (areEqualArray(oldConnectedListFromES, newConnectedListFromMwdi) == false) {
result = await RequestForWriteListConnectedEquipmentIntoElasticSearch(newConnectedListFromMwdi);
}

if (listJsonDisconnectedEq != null) {
listDisconnectedEq = listJsonDisconnectedEq["mountName-list"];
console.log("Number of disconnected equipment:" + listDisconnectedEq + "=> remove mac-adress data from ES");
console.log("Number of disconnected equipment:" + listDisconnectedEq.length + "=> remove mac-adress data from ES");

//remove mac-address data in ES of equipment that are that are no longer connected
if (Array.isArray(listDisconnectedEq)) {
for (const elementToRemove of listDisconnectedEq) {
const mountName = listES.indexOf(elementToRemove);
RequestForDeleteEquipmentIntoElasticSearch(mountName);
RequestForDeleteEquipmentIntoElasticSearch(elementToRemove);
}
}
}
//Write new "mountName - list" list into ES
if (areEqualArray(oldConnectedListFromES, newConnectedListFromMwdi) == false) {
result = await RequestForWriteListConnectedEquipmentIntoElasticSearch(newConnectedListFromMwdi);
}

resolve(newConnectedListFromMwdi);
}
catch (error) {
Expand Down Expand Up @@ -516,9 +519,6 @@ const EmbeddingCausesRequestForListOfDevicesAtMwdi = async function (user, origi
//TO FIX
let auth = "Basic YWRtaW46YWRtaW4=";

if (operationKey === 'Operation key not yet provided.')
operationKey = "siaeTest";


let httpRequestHeader = new RequestHeader(
user,
Expand Down Expand Up @@ -554,7 +554,6 @@ const EmbeddingCausesRequestForListOfDevicesAtMwdi = async function (user, origi
});

console.log("Number of connected devices = " + response.data['mountName-list'].length);
response.data
resolve(response.data);
} catch (error) {
resolve(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ let slidingWindow = [];
let deviceList = [];
let lastDeviceListIndex = -1;
let print_log_level = 2;
let stop = false;
var handle = 0;



async function sendRequest(device, user, originator, xCorrelator, traceIndicator, customerJourney) {
Expand Down Expand Up @@ -87,6 +90,7 @@ function getNextDeviceListIndex() {
lastDeviceListIndex = -1;
} else if (lastDeviceListIndex >= (deviceList.length - 1)) {
lastDeviceListIndex = 0;
stop = true;
} else {
lastDeviceListIndex += 1;
}
Expand Down Expand Up @@ -115,13 +119,21 @@ function addNextDeviceListElementInWindow() {
printLog('+++++ addNextDeviceListElementInWindow: newDeviceListIndex = -1 +++++', print_log_level >= 3)
return false
}
if (checkDeviceExistsInSlidingWindow(deviceList[newDeviceListIndex]) != DEVICE_NOT_PRESENT) {
printLog('+++++ Element ' + deviceList[newDeviceListIndex] + ' (indice: ' + newDeviceListIndex + ') already exists in Sliding Window +++++', print_log_level >= 3)
} else {
slidingWindow.push(prepareObjectForWindow(newDeviceListIndex));
elementAdded = true;

if (stop != true) {
if (checkDeviceExistsInSlidingWindow(deviceList[newDeviceListIndex]) != DEVICE_NOT_PRESENT) {
printLog('+++++ Element ' + deviceList[newDeviceListIndex] + ' (indice: ' + newDeviceListIndex + ') already exists in Sliding Window +++++', print_log_level >= 3)
} else {
slidingWindow.push(prepareObjectForWindow(newDeviceListIndex));
elementAdded = true;
}
}
else {
break;
}

} while (!elementAdded);
return elementAdded;
return true;
} catch (error) {
console.log("Error in addNextDeviceListElementInWindow (" + error + ")")
Expand Down Expand Up @@ -186,9 +198,9 @@ function printLog(text, print_log) {
* When time-to-live achieves zero another request gets done and ttl reset to the original value.
* When even all the retries achieve zero the sliding window element is discarded from both the lists.
*/
async function startTtlChecking() {
function startTtlChecking() {
try {
async function upgradeTtl() {
function upgradeTtl() {
for (let index = 0; index < slidingWindow.length; index++) {
slidingWindow[index].ttl -= 1;
if (slidingWindow[index].ttl == 0) {
Expand All @@ -200,6 +212,11 @@ async function startTtlChecking() {
printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1);
requestMessage(slidingWindow.length - 1);
}
else {
printLog(printListDevice('Device List', deviceList), print_log_level >= 2);
printLog('Sliding Window IS EMPTY', print_log_level >= 1);
}

} else {
slidingWindow[index].ttl = responseTimeout;
slidingWindow[index].retries -= 1;
Expand All @@ -208,8 +225,16 @@ async function startTtlChecking() {
}
}
}
if (slidingWindow.length == 0) {
clearInterval(handle);
handle = 0; // I just do this so I know I've cleared the interval
stop = false;
MATRCycle(2);
}
}
setInterval(upgradeTtl, 1000);

handle = setInterval(upgradeTtl, 1000);

} catch (error) {
console.log("Error in startTtlChecking (" + error + ")");
debugger;
Expand All @@ -228,6 +253,10 @@ async function requestMessage(index) {
return;
}

if (slidingWindow.length == 0) {
return;
}

//TO FIX
let user = "User Name";
let originator = "Resolver";
Expand All @@ -240,22 +269,33 @@ async function requestMessage(index) {
if (retObj.ret.code != 200) {
//errore
let elementIndex = checkDeviceExistsInSlidingWindow(retObj['node-id']);
if (slidingWindow[elementIndex].retries == 0) {
printLog('Error (' + retObj.ret.code + ' - ' + retObj.ret.message + ') from element (II time) ' + retObj['node-id'] + ' --> Dropped from Sliding Window', print_log_level >= 2);
slidingWindow.splice(elementIndex, 1);
if (addNextDeviceListElementInWindow()) {
printLog('Add element ' + slidingWindow[slidingWindow.length - 1]['node-id'] + ' in Sliding Window and send request...', print_log_level >= 2);
requestMessage(slidingWindow.length - 1);
if (elementIndex == DEVICE_NOT_PRESENT) {
printLog('Response from element ' + retObj['node-id'] + ' not more present in Sliding Window. Ignore that.', print_log_level >= 2);
}
else {
if (slidingWindow[elementIndex].retries == 0) {
printLog('Error (' + retObj.ret.code + ' - ' + retObj.ret.message + ') from element (II time) ' + retObj['node-id'] + ' --> Dropped from Sliding Window', print_log_level >= 2);
slidingWindow.splice(elementIndex, 1);
if (addNextDeviceListElementInWindow()) {
printLog('Add element ' + slidingWindow[slidingWindow.length - 1]['node-id'] + ' in Sliding Window and send request...', print_log_level >= 2);
printLog(printListDevice('Device List', deviceList), print_log_level >= 2);
printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1);
requestMessage(slidingWindow.length - 1);
}
else {
printLog(printListDevice('Device List', deviceList), print_log_level >= 2);
printLog('Sliding Window IS EMPTY', print_log_level >= 1);
}

} else {
printLog('Error (' + retObj.ret.code + ' - ' + retObj.ret.message + ') from element (I time) ' + retObj['node-id'] + ' Resend the request....', print_log_level >= 2);
slidingWindow[elementIndex].ttl = responseTimeout;
slidingWindow[elementIndex].retries -= 1;
requestMessage(elementIndex);
}
printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1);
} else {
printLog('Error (' + retObj.ret.code + ' - ' + retObj.ret.message + ') from element (I time) ' + retObj['node-id'] + ' Resend the request....', print_log_level >= 2);
slidingWindow[elementIndex].ttl = responseTimeout;
slidingWindow[elementIndex].retries -= 1;
requestMessage(elementIndex);
}
} else {
//retrun OK
//return OK
printLog('****************************************************************************************************', print_log_level >= 2);
let elementIndex = checkDeviceExistsInSlidingWindow(retObj['node-id']);
if (elementIndex == DEVICE_NOT_PRESENT) {
Expand All @@ -264,11 +304,16 @@ async function requestMessage(index) {
printLog('Response from element ' + retObj['node-id'] + ' --> Dropped from Sliding Window. Timestamp: ' + Date.now(), print_log_level >= 2);
slidingWindow.splice(elementIndex, 1);
if (addNextDeviceListElementInWindow()) {
printLog('Add element ' + slidingWindow[slidingWindow.length - 1]['node-id'] + ' in Sliding Window and send request...', print_log_level >= 2);
printLog('Add element ' + slidingWindow[slidingWindow.length - 1]['node-id'] + ' in Sliding Window and send request...', print_log_level >= 2);
printLog(printListDevice('Device List', deviceList), print_log_level >= 2);
printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1);
requestMessage(slidingWindow.length - 1);
}
else {
printLog(printListDevice('Device List', deviceList), print_log_level >= 2);
printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1);
requestMessage(slidingWindow.length - 1);
}

}
printLog('****************************************************************************************************', print_log_level >= 2);
}
Expand All @@ -295,6 +340,16 @@ async function extractProfileConfiguration(uuid) {
* deviceList is present the procedure will starts immediatly
**/
module.exports.embeddingCausesCyclicRequestsForUpdatingMacTableFromDeviceAtMatr = async function (logging_level) {
MATRCycle(2);
}

async function MATRCycle(logging_level) {

printLog('*****************************************************************', print_log_level >= 1);
printLog(' ', print_log_level >= 1);
printLog(' START MATR CYCLE ', print_log_level >= 1);
printLog(' ', print_log_level >= 1);
printLog('*****************************************************************', print_log_level >= 1);

slidingWindowSizeDb = await extractProfileConfiguration("matr-1-0-0-integer-p-000")
responseTimeout = await extractProfileConfiguration("matr-1-0-0-integer-p-001")
Expand Down Expand Up @@ -322,11 +377,10 @@ module.exports.embeddingCausesCyclicRequestsForUpdatingMacTableFromDeviceAtMatr
printLog('Element ' + slidingWindow[i]['node-id'] + ' send request...', print_log_level >= 2);
}

printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1);
printLog(printList('Sliding Window - MAIN', slidingWindow), print_log_level >= 1);
startTtlChecking();
return true;
}
catch (error) {
console.error("Error on MATR cycle");
console.error("Error on MATR cycle: " + error);
}
}
Loading