Retrieving data in JavaScript

We provide a JavaScript function query that fetches time series data from InfluxDB server and returns an Array object.

Background

This function uses a built-in function fetch. Consequently, following the nature of fetch, it returns a Promise object.

Provided function

query(domain, apiKey, timeFilter, {...})

Download

To download the source, please use the Raw or Download ZIP button on the GitHubGist page.

Description

query fetches time series data from domain according to the arguments and converts the returned data into an Array object. Below are the required parameters.

  • domain is the domain part of the URL.

  • apiKey is used for authenticating with the server.

{...} specifies additional filters and the output format of the table.

timeFilter

InfluxQL time filter expression, the default value undefined ignores time.

device

InfluxQL regular expression to select devices, the default value "//" matches all devices

location

InfluxQL regular expression to select locations, the default value "//" matches all locations

sensor

InfluxQL regular expression to select sensors, the default value "//" matches all sensors

includeNetworkSensors
true

allows returning network-related data

false (default)

filters out all network-related data

channel

InfluxQL regular expression to select channels, the default value "//" matches all channels

aggFunc

InfluxQL aggregate function evaluated on the selected data, default is no aggregation

aggInterval

InfluxQL time interval value, used with aggFunc

doUnstack
true (default)

return time series into individual columns

false

return all time series in a single column

convertTimestamp
true (default)

convert timestamps into date time objects

false

return timestamps in unix time in milliseconds

database

Database name to retrieve the data from, default is "main"

Demonstration script

The following NodeJS excerpt demonstrates the main functionality of query function with our demo.decentlab.com server. Before running the script, please download it from our gist repository by clicking the Raw button and save as decentlab.js.

You can also try the following example on JSFiddle.

> dl = require("./decentlab.js");
> await dl.query(
  "demo.decentlab.com",
  "eyJrIjoiclhMRFFvUXFzQXpKVkZydm52b0VMRVg3M3U2b3VqQUciLCJuIjoiZGF0YS1xdWVyeS1hcGktZGVtby0yIiwiaWQiOjF9",
  {
    timeFilter: "time > '2020-01-01' AND time < '2020-01-05'",
    device: "/^(3001|6414)$/",
    sensor: "/sht30|mb7389/",
    aggFunc: "mean",
    aggInterval: "12h",
    doUnstack: false,
  })

The above example will retrieve the data from demo.decentlab.com server, matching the following criteria:

  • devices with ID 3001 or 6414

  • sensor names containing either sht30 or mb7389

  • compute the mean of groups divided by 12-hour interval

  • received between 2020-01-01 and 2020-01-05

The output:

[
  [ 'timestamp', 'series', 'value', 'unit' ],
  [
    2020-01-01T00:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    31.61432235270641,
    '%'
  ],
  [
    2020-01-01T00:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    20.87331490512996,
    '°C'
  ],
  [
    2020-01-01T00:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3189.8802816901407,
    'mm'
  ],
  [ 2020-01-01T00:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-01T12:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    31.190076549426497,
    '%'
  ],
  [
    2020-01-01T12:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    20.905384527351792,
    '°C'
  ],
  [
    2020-01-01T12:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3184.8263888888887,
    'mm'
  ],
  [ 2020-01-01T12:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-02T00:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    30.88009095236713,
    '%'
  ],
  [
    2020-01-02T00:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    20.86234428006967,
    '°C'
  ],
  [
    2020-01-02T00:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3194.1901408450703,
    'mm'
  ],
  [ 2020-01-02T00:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-02T12:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    30.75081909784793,
    '%'
  ],
  [
    2020-01-02T12:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    20.898224473107046,
    '°C'
  ],
  [
    2020-01-02T12:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3188.1478873239435,
    'mm'
  ],
  [ 2020-01-02T12:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-03T00:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    30.78511965974529,
    '%'
  ],
  [
    2020-01-03T00:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    20.874379564945936,
    '°C'
  ],
  [
    2020-01-03T00:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3192.784722222222,
    'mm'
  ],
  [ 2020-01-03T00:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-03T12:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    31.265757059416938,
    '%'
  ],
  [
    2020-01-03T12:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    21.049211405271144,
    '°C'
  ],
  [
    2020-01-03T12:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3182.4405594405594,
    'mm'
  ],
  [ 2020-01-03T12:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-04T00:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    32.440659359290635,
    '%'
  ],
  [
    2020-01-04T00:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    20.96320456414299,
    '°C'
  ],
  [
    2020-01-04T00:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3190.2013888888887,
    'mm'
  ],
  [ 2020-01-04T00:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ],
  [
    2020-01-04T12:00:00.000Z,
    '3001.sensirion-sht30-humidity',
    33.12882005374565,
    '%'
  ],
  [
    2020-01-04T12:00:00.000Z,
    '3001.sensirion-sht30-temperature',
    21.08088447224977,
    '°C'
  ],
  [
    2020-01-04T12:00:00.000Z,
    '6414.maxbotix-mb7389-distance',
    3185.950704225352,
    'mm'
  ],
  [ 2020-01-04T12:00:00.000Z, '6414.maxbotix-mb7389-trials', 15, '' ]
]