Writeback functionality in Grafana

I am trying to achieve writeback functionality in Grafana for InfluxDB datasource.
Writeback is where we can either Create or Update or Delete data in data and the changes reflect in the database.
Can anyone please help.
Thanks in advance.

Welcome

Maybe look at the data manipulation apache chart plugin for grafana

1 Like

@mittamanjusha There is an InfluxDB API example in the documentation

1 Like

I use InfluxDB, and Grafana too data manipulation plugin was a very useful tool @mikhailvolkov example was based on my small contribution, so if you get stuck somewhere feel free to ask me directly :wink:

2 Likes

Hi @mikhailvolkov
Thanks for the solution. I could achieve the results. Just have another quick question. So basically I have added elements with the text area.


So my question is I have to add about 10 new rows, should I be typing all the information 10 times or is there any way we can add the 10 rows at one go.

You can add and update elements dynamically using JavaScript Code:

Hi @mikhailvolkov , @yosiasz

I have been using Data Manipulation Panel for the writeback and it works perfectly fine.
Now the question is, can we do writeback in Grafana without REST API?
If yes, could you please provide the solution

1 Like

@mittamanjusha Data Source support is coming in the upcoming v3.1.

We just released v3 with refactoring internals and will work on the supporting Data Source next.

@mikhailvolkov

Is there any chance to use Data Manipulation panel without using REST API part?

1 Like

I believe I responded already about the Data Source support:

Hi @fercasjr
I’m new to the data manipulation plugin. So what I’m trying to achieve is to write data to influxdb using the data manipulation plugin in grafana. What I have done so far is I have connected my grafana to influxdbv2 datasource, I’m able to view the data stored in influxdbv2 using dashboard panels. What I can’t do is I can’t be able to write data back to influxdbv2. On the data manipulation plugin, I have created form elements to which I want to update the data and I have been able to add an update request payload which I will paste below. Code I got from the data manipulation documentation for Influxdb API. What I’m not able to do is on the Form Element I have created I can’t update the data I have entered to Influxdbv2. What might I be doing wrongly

/**
 * Set body
 */
const body = {}; //this is an empty object called body

/**
 * The following code gets each element ant if it has changed then updates the values and passes them to "body" object
 */
options.elements.forEach((element) => {
  if (!options.update.updatedOnly) {
    body[element.id] = element.value;
    return;
  }

  /**
   * Skip not updated elements
   */
  if (element.value === initial[element.id]) {
    return;
  }

  body[element.id] = element.value;
});

/**
 * Set URL
 * Important to declare precision for timestamp, by default is ns and in the following code we are generating a timestamp in ms
 */
const url = `http://localhost:8086/api/v2/write?org=oves&bucket=telegraf&precision=ms`;

/**
 * Fetch
 */
const resp = fetch(url, {
  method: "POST",
  headers: {
    "Content-Type": "text/plain; charset=utf-8",
    Accept: "application/csv",
    Authorization: "Token INFLUX_API_TOKEN",
  },

  /**
   * This is the metric to be written in influxDB.
   * The syntax is explained in the documentation "line-protocol"*
   */
  body: `mqtt_consumer,tag1=${body.element_id1} field1="${body.flid }"${Date.now()}`,
})
  .catch((error) => {
    console.error(error);
  })
  .then((resp) => {
    console.log(resp);
  });

The above is the code I’m using

what do you see when you do

  body = `mqtt_consumer,tag1=${body.element_id1} field1="${body.flid }"${Date.now()}`

  print(body)

@yosiasz That’s what I’m seeing

The flid is coming as null

Check this out, you have a huge array

curl --request POST \
"http://localhost:8086/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=ns" \
  --header "Authorization: Token YOUR_API_TOKEN" \
  --header "Content-Type: text/plain; charset=utf-8" \
  --header "Accept: application/json" \
  --data-binary '
    airSensors,sensor_id=TLM0201 temperature=73.97038159354763,humidity=35.23103248356096,co=0.48445310567793615 1630424257000000000
    airSensors,sensor_id=TLM0202 temperature=75.30007505999716,humidity=35.651929918691714,co=0.5141876544505826 1630424257000000000
    '

@yosiasz A quick question,
The link you’ve provided shows the way to write data to influxdb using either curl or Nodejs. My question is this, how will I use data manipulation with either of the two to be able to accept an input that will have the data I want to write or update?

@yosiasz on the left of the image, that’s what is giving the array, it’s querying data from influxdb

look at the following parameter

--data-binary 

it is an inline csv data, you are sending a big old fat array in

body: `mqtt_consumer,tag1=${body.element_id1}

language used curl or nodejs does not matter, the principle is the same under the hood. which is you are hitting an api with POST and the influxdb api endpoint expects the data to be formatted in a certain way regardless what language used.

@thuozander If you use Data Manipulation v3, values were moved from options to local state.

Please update options.elements to elements.

Release blog posts: 9 posts tagged with "Data Manipulation" | Volkov Labs

1 Like