Code Monkey home page Code Monkey logo

influxdb-nodejs's Introduction

influxdb-nodejs

Build Status Coverage Status npm Github Releases

An InfluxDB Node.js Client.

Installation

$ npm install influxdb-nodejs

Examples

View the ./examples directory for working examples.

API

API

New Client

// no auth
const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
// normal auth (user and password will be added to URL query parameters)
const Influx = require('influxdb-nodejs');
const client = new Influx('http://user:[email protected]:8086/mydb');
// basic auth (will be used http basic auth)
const Influx = require('influxdb-nodejs');
const client = new Influx('http://user:[email protected]:8086/mydb?auth=basic');

Write point

const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
// i --> integer
// s --> string
// f --> float
// b --> boolean
const fieldSchema = {
  use: 'i',
  bytes: 'i',
  url: 's',
};
const tagSchema = {
  spdy: ['speedy', 'fast', 'slow'],
  method: '*',
  // http stats code: 10x, 20x, 30x, 40x, 50x
  type: ['1', '2', '3', '4', '5'],
};
client.schema('http', fieldSchema, tagSchema, {
  // default is false
  stripUnknown: true,
});
client.write('http')
  .tag({
    spdy: 'fast',
    method: 'GET',
    type: '2',  
  })
  .field({
    use: 300,
    bytes: 2312,
    url: 'https://github.com/vicanso/influxdb-nodejs',
  })
  .then(() => console.info('write point success'))
  .catch(console.error);

Query influxdb with multi where condition

const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
client.query('http')
  .where('spdy', '1')
  .where('method', ['GET', 'POST'])
  .where('use', 300, '>=')
  .then(console.info)
  .catch(console.error);
// => influx ql: select * from "http" where "spdy" = '1' and "use" >= 300 and ("method" = 'GET' or "method" = 'POST')

Query influxdb using functon

const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
client.query('http')
  .where('spdy', '1')
  .addFunction('count', 'url')
  .then(console.info)
  .catch(console.error);
// => select count("url") from "http" where "spdy" = '1'

client.query('http')
  .where('spdy', '1')
  .addFunction('bottom', 'use', 5)
  .then(console.info)
  .catch(console.error);
// select bottom("use",5) from "http" where "spdy" = '1'

Write points to influxdb in queue

const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
function loginStatus(account, ip, type) {
  client.write('login')
    .tag({
      type,  
    })
    .field({
      account,
      ip,  
    })
    .queue();
  if (client.writeQueueLength >= 10) {
    client.syncWrite()
      .then(() => console.info('sync write queue success'))
      .catch(err => console.error(`sync write queue fail, ${err.message}`));
  }
}

setInterval(() => {
  loginStatus('vicanso', '127.0.0.1', 'vip');
}, 5000);

Sub query

const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
client.query('http')
  .addFunction('max', 'use')
  .addGroup('type')
  .subQuery()
  .addFunction('sum', 'max')
  .then((data) => {
    // { name: 'http', columns: [ 'time', 'sum' ], values: [ [ '1970-01-01T00:00:00Z', 904 ] ] }
    console.info(data.results[0].series[0]);
  }).catch(console.error);

Multi query

const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
const reader = client.query('request');
reader.set({
  limit: 2,
});
reader.multiQuery();
reader.measurement = 'login';
reader.set({
  limit: 1,
  tz: 'America/Chicago',
});
reader.set({
  format: 'json',
});
reader.then(data => {
  console.info(JSON.stringify(data));
}).catch(console.error);

Use influxdb for express

const express = require('express');
const app = express();
const _ = require('lodash');
const Influx = require('influxdb-nodejs');
const client = new Influx('http://127.0.0.1:8086/mydb');
const onHeaders = require('on-headers');

// set the http stats schema
client.schema('http', {
  use: 'integer',
  code: 'integer',
  bytes: 'integer',
  url: 'string',
});
client.on('writeQueue', () => {
  // sync write queue if the length is 100
  if (client.writeQueueLength === 100) {
    client.syncWrite()
      .then(() => {
        console.info('sync write success');
      })
      .catch(console.error);
  }
});

function httpStats(req, res, next) {
  const start = Date.now();
  onHeaders(res, () => {
    const code = res.statusCode;
    const use = Date.now() - start;
    const method = req.method;
    const bytes = parseInt(res.get('Content-Length') || 0, 10);
    const tags = {
      spdy: _.sortedIndex([100, 300, 1000, 3000], use),
      type: code / 100 | 0,
      method,
    };
    const fields = {
      use,
      code,
      bytes,
      url: req.url,
      route: req.route.path
    };
    // use queue for better performance
    client.write('http')
      .tag(tags)
      .field(fields)
      .queue();
  });
  next();
}

client.createDatabase().catch(err => {
  console.error('create database fail err:', err);
});

app.use(httpStats);

app.use((req, res, next) => {
  setTimeout(next, _.random(0, 5000));
});

app.get('/users/me', (req, res) => {
  res.json({
    account: 'vicanso',
    name: 'Tree Xie',
  });
});

app.get('/book/:id', (req, res) => {
  const {
    id,
  } = req.params;
  res.json({
    id: id,
    name: 'my book',
    author: 'vicanso',
  });
});

app.get('/order/:id', (req, res) => {
  res.status(400).json({
    error: 'The id is not valid',
  });
});

app.get('/author/:id', (req, res) => {
  res.status(500).json({
    error: 'The database is disconnected',
  });
});

let server;
const finish = () => {
  console.info(`listen on http://127.0.0.1:${server.address().port}/`);
};
if (process.env.PORT) {
  server = app.listen(process.env.PORT, finish);
} else {
  server = app.listen(finish);
}

Influxdb Charts

HTTP Spdy(experss demo)

HTTP Type(experss demo)

HTTP Error(experss demo)

Comparison

  • influx It's complex for me. Before developing this module, I used influx, which was not straightforward; and its batch function can not be saved as queue. What's more, the function of query is too simple, just like I write influx ql.

  • influent I have never used this module, but I have read its API. In my opinion, this module is not so convenient.

License

MIT

influxdb-nodejs's People

Contributors

cehlen avatar codex- avatar dependabot[bot] avatar drproteus avatar jakubknejzlik avatar mattiash avatar snipa22 avatar vicanso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

influxdb-nodejs's Issues

Precedence of condition is not happening

Hi vicanso ,

Following are the condition added to the query. 
qry.condition("ALARM_SEVERITY = 'Major' or ALARM_SEVERITY = 'Minor'");
qry.condition("STATUS='Raise'")

The expected query should be
select * from ALARM_DETAILS WHERE (ALARM_SEVERITY='Major' or ALARM_SEVERITY='Minor') and STATUS='Raise';

But query returns all other STATUS also.

Are you handling the precedence of the conditions?

Write error with tags

Seems to be an issue with writing tags.

InfluxDB version 1.2.2

'use strict';

const influx = require('influxdb-nodejs');

const influxCli = new influx('http://localhost:8086/test');

influxCli.writePoint('test_measurement',
  { testBoolean: true, testString: 'test', testInteger: 1, testFloat: 0.1 },
  { testTag: 'test', testAnothertag: 'test' }
).then(() => console.info('write point success')).catch(err => console.error(err));

This result with an error: [TypeError: this is not a typed array.]

The same request is OK when no tags are provided.

[Question] Retention policy

Any example how to work with different retention policies?

Like this:

$ influx
> create database mydb
> create retention policy rp_1 on mydb duration 1h replication 1
> create retention policy rp_2 on mydb duration 2h replication 1
curl http://localhost:8086/write?db=mydb&rp=rp_1 --data-binary SOMEDATA
curl http://localhost:8086/write?db=mydb&rp=rp_2 --data-binary SOMEDATA

Thanks

ECONNRESET error during big queue

I'm trying to insert a big list from MySQL to InfluxDB but always receive the following error:

  Error: read ECONNRESET
    at TCP.onread (net.js:657:25)
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read',
  response: undefined

Code:

module.exports = {
    setDataQueue(timestamp, objID, list) {
        // console.log(Date.parse(timestamp));
        db.write("opdata_item")
        .time(timestamp, "ms")
        .tag({ objID })
        .field(list)
        .queue();
    },
    startQueue() {
        return new Promise(function (resolve, reject) {
            console.log("STARTING INFLUX QUEUE");
            db.syncWrite()
            .then(() => resolve("write point success"))
            .catch(error => reject(error));
        });
    }
}

I'm converting data from a single day (around 180.000 values) and have to convert data from over 10 years, if I try to convert only one hour (around 7.500 values) everything work fine.

Is there a simple way to perform big queues?

epoch not passed to Influx.query

It seems that the "epoch" value is not passed to the Influx.query.

More precisely, in influx.js, function query accepts 3 parameters: query(q, db, epoch).
However, when called by function syncQuery in client.js, only the first parameter is passed, therefore the epoch is undefined and ignore when influxDB is queried.

One way to solve this is to modify the following line in client.js, line 752:
return influx.query(arr.join(';')).then((data) => {

shall become
return influx.query(arr.join(';'), null, internal(this).options.epoch).then((data) => {

However, I'm not sure that this is the right approach.

Show Tag Values

Is there some way to perform the followingquery:

"SHOW TAG VALUES FROM "default"."my_stat" WITH KEY IN ("mac", "name")"

?

Connect to InfluxDB cluster

Hello,
I am relatively new to this Lib, I was hoping has anyone tried connecting a client to a cluster?
or is it possible to connect to InfluxDB cluster?

question about timestamp

I use queue to do batch insert points. The timestamp seems be updated by the queue itself.

queue() { const internalData = internal(this); const queue = internalData.queue; /* istanbul ignore if */ if (!queue) { throw new Error('queue function is undefined'); } this.time(util.getTime(internalData.precision)); queue(this.toJSON()); return this; } }

util.getTime(internalData.precision) seems reset the previous timestamp. I think it is not proper. Current logical caused lost the preset timestamps.

Add comma separated multi statement

Refering to Influxdb Multi statement

It would be awesome to have a QL that would handle mutli statement over the HTTP API without doing like this :

var QL = []; QL.push(client.query("temperature").addField("value").where("device", query.device).where('time',query.to.unix()*1000000000, "<").toSelect()) QL.push(client.query("temperature").addField("value").where("device", query.device).where('time', query.to.unix()*1000000000, "<").toSelect()) var datas = await client.queryPost(QL.join(";"),"test")

Values that end in i are not being converted to string.

Per: https://github.com/vicanso/influxdb-nodejs/blob/master/lib/influx.js#L28

It appears that strings that end in an i, are not being converted to strings. I'm not entirely sure why this is like this, except perhaps for debugging, as it causes errors as follows:
influxdb-nodejs writer data:{"measurement":"shares","tags":{"foundBlock":false,"shareType":"valid","paymentType":"pplns","poolID":2,"blockDiff":6664436299,"bitcoin":0,"worker_tag":"mini","payout_tag":""},"fields":{"round":1219169,"shares":1000,"worker":"mini","payout":""},"time":1483862223761,"precision":"ms"} +16s
influxdb-nodejs POST /write, data:"shares,foundBlock=false,shareType=valid,paymentType=pplns,poolID=2,blockDiff=6664436299,bitcoin=0,worker_tag=mini,payout_tag=round=1219169,shares=1000,worker=mini,payout="" 1483862223761", query:{"db":"","u":"","p":"","precision":"ms"} +1ms

As you can see, the worker field, is not being properly escaped, which is causing it to throw invalid boolean errors.

I'm not sure if this is a debug thing (I assume it is?), but I don't want to submit a patch without knowing what it's there for.

Basic Auth

I'm looking for a way to use basic authentication. Have i overlooked a detail or isn't it currently possible?

Electron OK?

Do you foresee any problems using this package with Electron?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.