I used this guide to iterate through table data and run a query for each item. I'm really rough with my js and I only want to run the command if my table data has "unacknowledged" as its state.
I can process these alarms individually but I have to filter them out of the table once processed. If I get 20 alarms I want to just hit "Process All" and let it be. The button works just fine but is attempting to process everything even if it has already been processed.
Assuming I understand correctly and you have the same code as the guide you provided; adding this lines of code to the button JS will achieve your goal:
// if the alarm is NOT unacknowledged
// do not run query and skip to next row
if (table1.data[i].state !== 'unacknowledged'){
runQuery(i + 1);
return;
}
The IF checks if the the state value is different than 'unacknowledged', if it is, it skips to the next row and stops executing the current row
Here is the full script of the guide with the above lines added to it (note lines 14 to 19):
var rows = table1.data;
var errors = "";
var total = rows.length;
function runQuery(i) {
// Update the Status text
Status.setValue("Progress: " + (i.toString() + "/" + total.toString()));
if (i >= rows.length) {
console.log("Finished running all queries");
return;
}
// if the alarm is NOT unacknowledged
// do not run query and skip to next row
if (table1.data[i].state !== 'unacknowledged'){
runQuery(i + 1);
return;
}
console.log("Running query for row", i);
query1.trigger({
additionalScope: { i: i }, // This is where we override the `i` variable
// You can use the argument to get the data with the onSuccess function
onSuccess: function (data) {
runQuery(i + 1);
},
onFailure: function (error) {
// Update the Errors text
errors += "Found error at line " + i.toString() + ": " + error + "\n\n";
Errors.setValue(errors);
runQuery(i + 1);
},
});
}
runQuery(0);
thank you very much. this works perfectly!
How would I go about getting the total number of rows displayed in the "progress" to equal just the unacknowledged alarms?
At this point I would just rewrite the code to make it cleaner but I don't wanna confuse you, just make the following changes.
Add lines 5,6
Change line 10
Add line 24
var rows = table1.data;
var errors = "";
var total = rows.length;
var totalToProcess = rows.filter(r => r.state === 'unacknowledged').length;
var procCnt = 0;
function runQuery(i) {
// Update the Status text
Status.setValue("Progress: " + (procCnt.toString() + "/" + total.toString()));
if (i >= rows.length) {
console.log("Finished running all queries");
return;
}
// if the alarm is NOT unacknowledged
// do not run query and skip to next row
if (table1.data[i].state !== 'unacknowledged'){
runQuery(i + 1);
return;
}
procCnt++;
console.log("Running query for row", i);
query1.trigger({
additionalScope: { i: i }, // This is where we override the `i` variable
// You can use the argument to get the data with the onSuccess function
onSuccess: function (data) {
runQuery(i + 1);
},
onFailure: function (error) {
// Update the Errors text
errors += "Found error at line " + i.toString() + ": " + error + "\n\n";
Errors.setValue(errors);
runQuery(i + 1);
},
});
}
runQuery(0);