API Monitor: Using Eval to access returned JSON data #200
-
I am trying to create a monitor that will call an api route that returns a JSON object. I want to parse that JSON object, looks for a specific node in the collection and retrieve a value. Let me use the following (reduced) example from github status api
I wrote a couple of different EVAL blocks, but neither would save. Once I started removing the JSON object access it saved. So I'm guessing it's either not supported or I writing it incorrectly. Is this actually possible? Is there any documentation on what JavaScript is allowed?Attempt #1 - using find()
Attempt #2 - using a loop
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@dug42 The eval is validated against For your use case you will have to add validations Attempt 1 - using find() (function (statusCode, responseTime, responseDataBase64) {
const resp = JSON.parse(atob(responseDataBase64));
let node_key = "Webhooks";
let status = "DOWN";
if(statusCode >= 200 && statusCode <= 399) {
if (responseTime > 2000) {
status = "DEGRADED";
} else if(!!resp.components) {
let webhook_component = resp.components.find(component => component.name === node_key);
let web_status = webhook_component ? webhook_component.status : null;
if (webhook_component === undefined || web_status === null) {
status = "DOWN";
} else if (web_status === "operational") {
status = "UP";
} else if (web_status === "partial_outage") {
status = "DEGRADED";
} else if (web_status === "major_outage") {
status = "DOWN";
}
};
};
return {
status: status,
latency: responseTime
};
}) Attempt 2 - using loop (function (statusCode, responseTime, responseDataBase64) {
const resp = JSON.parse(atob(responseDataBase64));
let node_key = "Webhooks";
let status = "DOWN";
if(statusCode >= 200 && statusCode <= 399) {
if (responseTime > 2000) {
status = "DEGRADED";
} else if(!!resp.components) {
for (let i = 0; i < resp.components.length; i++) {
let component = resp.components[i];
if (component.name === node_key) {
if (component.status === null || component.status === undefined) {
status = "DOWN";
} else if (component.status === "operational") {
status = "UP";
} else if (component.status === "partial_outage") {
status = "DEGRADED";
} else if (component.status === "major_outage") {
status = "DOWN";
}
break;
};
};
};
};
return {
status: status,
latency: responseTime
};
}) I have also updated the documentation. |
Beta Was this translation helpful? Give feedback.
@dug42 The eval is validated against
(200, 1000, "e30=")
which translates to(200, 1000, '{}')
within the function. So whatever function you write should be able to handle these values otherwise you won't be able to save the monitor.For your use case you will have to add validations
Attempt 1 - using find()