VolkovLabs / business-forms

The Business Forms panel is a conceptually new plugin for Grafana. It is the first plugin that allows inserting and updating application data, as well as modifying configuration directly from your Grafana dashboard.
https://docs.volkovlabs.io
Apache License 2.0
86 stars 10 forks source link

Issue Sending Data to MSSQL Datasource #499

Closed kvecc closed 2 months ago

kvecc commented 2 months ago

I am using Azure Managed Grafana, and it is connected to my MSSQL database. I am able to query it to populate elements on my form, but I haven't been able to send data back to it using the Datasource option for the Update Request. It is a very simple form only sending two pieces of information, so I was hoping I could avoid having to create an API for my database.

My Update code is shown below: image

This is the error message I get when trying to submit my form data: image

mikhail-vl commented 2 months ago

@kvecc Check you payload to make sure that you have collection_status and collection_interval. If they are undefined or equal to 0, because of the if statement, they may not be a part of the payload.

console.error(payload);

I believe the error means that one of those variables was not replaced correctly from the payload.

kvecc commented 2 months ago

@mikhail-vl my collection_status variable returns a 1 or 0, does this mean it will not register the variable if it is supposed to be returning a 0? Also, I have my two elements tied to values from my initial query, if I do not change one of the elements, will it still return the default value populated by my initial query?

mikhail-vl commented 2 months ago

@kvecc Remove the if statement from the payload to set values in any case.

Yes, payload will have values for all the elements based on the code.

kvecc commented 2 months ago

@mikhail-vl I tried removing the if statement, but I'm still getting the same error

asimonok commented 2 months ago

@kvecc please replace single quotes to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

from

rawSql: '${payload.collection_status}'

to

rawSql: `${payload.collection_status}`

Please let us know if there is anything else or if it doesn't work for you

kvecc commented 2 months ago

@asimonok I tried the following two pieces of code and got the corresponding errors

1) image image

2) image image

asimonok commented 2 months ago

@kvecc the first option is valid. You should wrap string values into single quotes VALUES (${payload.collection_status}, '${payload.collection_interval}')

kvecc commented 2 months ago

@asimonok when I try to format my code like that, it gives me formatting errors

image

asimonok commented 2 months ago

@kvecc the closing bracket } for first parameter is missing.

correct version

`INSERT INTO data_collection_status (collection_status, data_interval) VALUES (${payload.collection_status}, '${payload.collection_interval}')`
kvecc commented 2 months ago

@asimonok thank you for pointing that out, didn't realize I had deleted that closing bracket, that correction has fixed my issue and I was able to successfully insert data back into my MSSQL database, thanks again for your help!

mikhail-vl commented 2 months ago

@kvecc Thank you for confirming. We updated blog posts accordingly.