Getting error when trying to write to Influxdb

I am trying to write from Grafana to Influxdb using Flux language. The part of the code responsible for the writing is shown below, where I am trying to write to existing bucket “qpu1-ops” new “_measurement”, “tag”, two “_field” - Time and PD. First I format “data”, then write it to influxdb. Could anyone let me know if I am mixing up format? Thank you!


data =
  array.from(
    rows: [
    {
      _time: "now()",
      _measurement: "test",
      tag1: "a",
      _field: "Time",
      _value: "1701453855968",
    },
    {
      _time: "now()",
      _measurement: "test_measurement",
      tag1: "a",
      _field: "PD",
      _value: "5",
    },
  ],
  )
  
  data
    |> to(bucket: "qpu1-ops")

Hi and Welcome,
is there a tutorial your following for this? If not what language or library are you using to interact with influxdb , or is this example all in the data explorer web interface ?

Hi, I am using Data Manipulation plugin for Grafana (Data Manipulation plugin for Grafana | Grafana Labs), with JavaScript as the language. I am trying to follow some tutorials from the website of this plugin as well as general information on communication with influxdb. But unfortunately the information is sparse and I have to guess a lot.

@Evgeny197 What error are you getting?

error: “invalid: error @2:3-2:8: undefined identifier array”, status: 500

Oh, you probably just need to import the array package:

import "array"

data =
  array.from(
    rows: [
    {
      _time: "now()",
      _measurement: "test",
      tag1: "a",
      _field: "Time",
      _value: "1701453855968",
    },
    {
      _time: "now()",
      _measurement: "test_measurement",
      tag1: "a",
      _field: "PD",
      _value: "5",
    },
  ],
  )
  
  data
    |> to(bucket: "qpu1-ops")

Thank you so much @scott . It is writing now to the database. Now the question - how do I pass variable into the _value field? it gives me errors when i put in variable names declared in JavaScript part of the code (outside Flux block). I also tried to pass on current time using now(), and it also gives me an error.

What does your JS code look like? Without seeing it, the guidance I would offer is structuring the Flux query string as a template literal and interpolate your JS variables into the Flux template. For example:

var myVariable = "foo"

var query = `import "array"

data =
  array.from(
    rows: [
    {
      _time: "now()",
      _measurement: "test",
      tag1: "${myVariable}",
      _field: "Time",
      _value: "1701453855968",
    },
    {
      _time: "now()",
      _measurement: "test_measurement",
      tag1: "${myVariable}",
      _field: "PD",
      _value: "5",
    },
  ],
  )
  
  data
    |> to(bucket: "qpu1-ops")
`

The code is as follows:

const payload = {};
const val = 0.79;

elements.forEach((element) => {
  if (!element.value) {
    return;
  }
  payload[element.id] = element.value;
});

return {
  query: `import "array"
  data =
    array.from(
      rows: [
      {
        _time: now(),
        _measurement: "test_measurement",
        _field: "Time",
        _value: "_time",
      },
      {
        _time: now(),
        _measurement: "test_measurement",
        _field: "PD",
        _value: val,
      },
    ],
    )
  
  data
    |> to(bucket: "qpu1-ops")`,
};

I also tried

....
        _field: "PD",
        _value: payload.PD,
...

@scott it works with the variable, thank you!
Now the last question - how do i write into database the current time stamp? if I use

        _field: "Time",
        _value: "${now()}",

it gives me an error: ReferenceError: now is not defined

I figured that one out:

var localeDateTime = (new Date()).toLocaleString();
return {
  query: `import "array"
  data =
    array.from(
      rows: [
      {
        _time: now(),
        _measurement: "test_measurement",
        _field: "Time",
        _value: "${localeDateTime}",
      },
   ]
  )
  data
    |> to(bucket: "qpu1-ops")`,
};

Thanks again!!!

1 Like

A followup - is there a way NOT to write _start and _stop columns into influxdb? They crowd the data without any need for it.

Can you elaborate on why? I just re-read your code and noticed you don’t appear to be using the native time columns, but your own. Do you need to know a time delta between source and the time the observation was written into influxdb or something?

If you just need timestamps displayed in your local time zone, influxdb (or JavaScript) can do that pretty easily. And probably halve the storage footprint and increase the performance of queries too.

More info here