Best Way to Store and Use Runtime Status Values
I have a client that would like to see what % of the previous X polls were successful. I've tried many things, but can't get what he really wants. Hopefully I'm missing something obvious.
We can look at the Runtime Status, or hit the /data-sources/status endpoint to get a snapshot of the last 10 successful and aborted polls. Any chance these are stored as a data point somewhere with the history?
Here's what we've looked into so far:
I know there's the internal data points which include the Poll Success Percent, but that goes back to the time the data source was re-started. That won't give us a good picture of the polls over the past 1 or 2 minutes.
I've also looked into the "previous sequential successful polls" but that seems to have a bug. See this post I had on it for more details: https://forum.mango-os.com/topic/4948/question-on-internal-metric-previous-sequential-successful-polls/
Right now we're using the Slave Monitor 1/0 value to know the % of time over the past x seconds Mango has been able to communicate with the Modbus device, but that's not really what the client wants. That doesn't tell him if individual polls aborted.
The only other solution I can think of is to hit the /data-sources/status API every x seconds, parse through the results and save them as data points.
If we did that, is there a way to hit the Mango API from a Scripting Data Source? That would make it easier to schedule the job and save results into a data point. It doesn't look like jQuery or an XMLHttpRequest can be used in the scripting data source. If possible, I'd like to avoid custom jobs and code being written outside of Mango.
It doesn't look like jQuery or an XMLHttpRequest can be used in the scripting data source.
But you can, thats the http builder.
I managed to use a global script to store momentJS, so no reason why you cannot do the same for jQuery.
Search forum posts for http get under my name. I posted a global script I wrote to do http requests for scripting and meta sources
Thank you, Fox!!!! I got a proof of concept working and will be off and running now.
For those that want a bit more info on what was done, here are the steps I took for this.
- Read Fox's post: https://forum.mango-os.com/topic/4834/how-to-get-json-data-via-api/2
- Read the help page on logging into Mango via the API: https://docs-v3.mango-os.com/mango-rest-api-authentication/
In my case we were trying to hit the Mango API via a scripting data source within Mango. We used HttpBuilder as Fox indicated and gave a good outline of in his post.
When determining the headers needed to send the request I mainly looked at the Mango help page linked to above.
At first I tried to hit the runtime status endpoint directly and was getting unauthorized status back. This was because the resource hitting the API wasn't logged in. I then added the logic to log into Mango (following the first login example on the Mango help doc).
Even after logging in via the API I still received 401 error when trying to hit the runtime status endpoint.
After flailing around unsuccessfully for a day I decided to try the JWT method at the bottom of the help page. That worked perfectly, and easily! After setting up the JWT for a user, I was able to use the generated token and access the runtime status endpoint with no issues.
One other quick note I forgot to include...
When I first tried this solution I was getting an error about a valid certificate path. I found this on Stack Overflow which resolve that issue: https://stackoverflow.com/questions/21076179/pkix-path-building-failed-and-unable-to-find-valid-certification-path-to-requ