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

[Question] Infinity Datasource with Update Request #440

Closed mschweig closed 4 months ago

mschweig commented 4 months ago

Hello,

how can I use the Infinity Datasource to do a Update Request with the specified field from the Form Elements?

For example: Form Element Custom Select is "command1"

When clicking on "Submit" a GET/POST request should be carried out via Infinity Datasource to the desired API like that:

https://demo-api?param=command1

Is that possible?

Thanks Best regards.

vitPinchuk commented 4 months ago

@mschweig Thank you for your question.

Use Infinity Datasource. For example, I use endpoint: https://jsonplaceholder.typicode.com/posts

image

Add 3 element for Title, Body, and for user Id

image

Here is my Initial request settings image

image

And an example of creating an update request with the specified field from form elements:

Update Action -> Data Source Data Source -> Infinity

custom code example

if (context.panel.response) {
  context.grafana.notifySuccess(['Update', 'Values updated successfully.']);
  context.panel.initialRequest();
} else {
  context.grafana.notifyError(['Update', 'An error occurred updating values.']);
}

image

Please create a payload using the panel elements. The code editor should return the appropriate payload. Create payload:

let payload = {
  title: '',
  body: '',
  userId: ''
}
context.panel.elements.forEach((element) => {
  console.log('console >> element', element)
  if (element.id === "postTitle") {
    payload.title = element.value
  }

  if (element.id === "postBody") {
    payload.body = element.value
  }

  if (element.id === "userId") {
    payload.userId = element.value
  }

})

return payload

image

Next, we should use payload in request via Infinity Data Source. Use for that Query Editor field in Update Request Method: POST
URL: https://jsonplaceholder.typicode.com/posts And Headers, Body, Request params have URL options
Body Type: Raw Body Content Type: JSON Body Content (for example):

{
  "title": "${payload.title}",
  "body": "${payload.body}",
  "userId": "${payload.userId}"
}

Here we use the payload we got from the Create Payload code editor. image

And after click "Submit" button I see in Network my payload in body:

image

In the same way we can add parameters from the payload to URL As example:
https://jsonplaceholder.typicode.com/posts?param=${payload.title}

image

mikhail-vl commented 4 months ago

@vitPinchuk Well done! Could you please create a blog post how to use Business forms with Infinity Data Source based on this content.

mschweig commented 4 months ago

Hi @vitPinchuk thanks, that's amazing! I had to update to 4.1.0, it was not working with prior versions.

I also combined Infinity with the base64 Plugin to show Images from HTTP APIs, what a great add-on :)