Code Monkey home page Code Monkey logo

influx-ql's Introduction

Influx-ql

Build Status Coverage Status npm Github Releases

Influxdb-nodejs An Influxdb Node.js Client depends on Influx-ql.

Get influx ql

const QL = require('influx-ql');
const ql = new QL('mydb');
ql.measurement = 'http';
ql.RP = 'default';
ql.addField('status', 'spdy', 'fetch time');
ql.start = '2016-01-01';
ql.end = '-3h';
ql.limit = 10;
ql.slimit = 5;
ql.order = 'desc';
ql.offset = 10;
ql.soffset = 5;
ql.tz = 'America/Chicago';
ql.where('code', 400);
ql.where('"use" <= 30');
// select "fetch time","spdy","status" from "mydb"."default"."http" where "code" = 400 and "use" <= 30 and time <= now() - 3h and time >= '2016-01-01' order by time desc limit 10 slimit 5 offset 10 soffset 5 tz('America/Chicago')
ql.toSelect();

The enhance where function

const QL = require('influx-ql');
const ql = new QL('mydb');
ql.measurement = 'http';

// select * from "mydb".."http" where "spdy" = '1'
ql.where("spdy", "1");
ql.emptyConditions();

// select * from "mydb".."http" where ("spdy" = '1' or "spdy" = '2')
ql.where("spdy", ["1", "2"]);
ql.emptyConditions();

// select * from "mydb".."http" where "use" >= 300
ql.where("use", 300, ">=");
ql.emptyConditions();

// select * from "mydb".."http" where ("spdy" = '1' and "method" = 'GET')
ql.where({spdy: "1", method: "GET"});
ql.emptyConditions();

// select * from "mydb".."http" where ("spdy" != '1' and "method" != 'GET')
ql.where({spdy: "1", method: "GET"}, '!=');
ql.emptyConditions();

// select * from "mydb".."http" where ("spdy" = '1' or "method" = 'GET')
ql.where({spdy: "1", method: "GET"}, "or");
ql.emptyConditions();

// select * from "mydb".."http" where (spdy = '1' and method = 'GET')
ql.where("spdy = '1' and method = 'GET'");
ql.emptyConditions();

// select * from "mydb".."http" where "spdy" = /1|2/
ql.where({spdy: /1|2/});
ql.emptyConditions();

// select * from "mydb".."http" where "method" = 'GET' or "spdy" = '1'
ql.where('spdy', '1');
ql.where('method', 'GET');
ql.relation = 'or';

// select sum("max") from (select max("fetch time") from "mydb".."http" group by "spdy")
ql.clean();
ql.measurement = 'http';
ql.addFunction('max', 'fetch time');
ql.addGroup('spdy');
ql.subQuery();
ql.addFunction('sum', 'max');

Installation

$ npm i influx-ql

Examples

View the ./examples directory for working examples.

API

API

License

MIT

influx-ql's People

Contributors

azerothian avatar dependabot[bot] avatar vicanso avatar yura415 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

influx-ql's Issues

Getting QL object out of influx sql stattement

Is it possible to get QL object back from influx SQL statement?

This would help in many use cases where we want to analyze the incoming SQL statements before it goes to influx database.

Error when grouping by time with offset

When grouping by time with an offset the query returns a error.

group by "time(86400s, 120m)"
ERR: difference aggregate requires a GROUP BY interval

If I remove the " " before and after the time parameter

group by time(86400s, 120m)

the query returns correctly.

When grouping by time without offset works even with " "

Influx Version 1.5.1

"or" operator for condition

Trying to query select one,two,three from stats where myCond='40002' or myCond='40007' and time <= 1463142720s and time >= 1463142660s

Tried with

const reader = client.query('stats');
reader.addField('one', 'two', 'three');
reader.start = start;
reader.condition("myCond='40002' or myCond='40007'");

In this case the condition is ignored. If I only enter one condition without "or" operator it is considered and included in the query.

How can I add several conditions on the same field as one would do with "or" operator?

Cannot query for value equality e.g. "field" | 1023 = "field"

If you try to use .where to specify a condition based on field or tag value then influx-ql will add single quotes to it and therefore comparison fails.

For example if you want to query integer values by bitwise operator you need to have ability to skip single quoting .where value.

const typeMask = 1023
const ql = new QL()
ql.measurement = "test";
ql.where(
  {
    type_mask: '"type_mask"',
  },
  `| ${typeMask} =`,
)
console.log(ql.toSelect())
/* 
  log output: select * from "test" where "type_mask" | 1023 = '"type_mask"'
  โœ“ correct output: select * from "test" where "type_mask" | 1023 = "type_mask"
*/

Expression in addField not working

const QL = require("influx-ql");
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField('"use" - 2');
console.info(ql.toSelect());

Above code is not working (expression is wrapped in double quote). See the below generated query.
select ""use" - 2" from "mydb".."http"
Where as if the expression is '"use" + 2', then it works.
select "use" + 2 from "mydb".."http"

Support for javascript Date as ql.start and ql.end, GROUP by time interval and nested functions

Currently ql.start and ql.end don't work with javascript Date Object. I think it would be nice to add support for them, even if I pass a date string it isn't quoted in query result:

Javascript:

    var end = new Date();
    var start = new Date(Date.now() - (24*60*60*1000)); //last day
    var ql = new QL();
    ql.measurement = point.measurement;
    ql.addFunction('mean', 'value');
    ql.condition(point.tags);
    ql.start = start.toString();
    ql.end = end.toString();

    var query = ql.toSelect();

    console.log(query);

Output:

select mean("value") from "temperature" where ("location0" = 'garage' and "device" = '_thermo1') and time <= Thu Jan 25 2018 16:19:46 GMT+0100 (CET) and time >= Tue Jan 23 2018 04:19:46 GMT+0100 (CET)

There is also no support for GROUP BY time intervals (Example: GROUP BY time(interval[unit])) and for nested functions (Eample DIFFERENCE(first(value))

UPDATE:

At the moment I have been able to use Javascript dates by using a function to convert timestamp to nano string:

function dateToNano(date){
  return (date.getTime() * 1000000).toString();
}

......
    var end = new Date();
    var start = new Date(Date.now() - (24*60*60*1000)); //last day

    var ql = new QL();
    ql.measurement = point.measurement;
    ql.addFunction('mean', 'value');
    ql.condition(point.tags);
    ql.start = dateToNano(start);
    ql.end = dateToNano(end);

    var query = ql.toSelect();

Composed Functions

Hi how can i do this query:

select count(distinct(userId)) as value from heartbeat where time > now() - {} GROUP BY time({}) fill(0)

My porblem is with composed function:

count(distinct(userId))

Duplicates fields when using subQuery()

I'm expecting many issues when using subQuery() method.

The fields I have checked are duplicated are ql.start ql.end ql.addGroup ql.tz`

If I set those values in the subQuery those values are added to the main query too and this is not the expected behaviour.

Example

Expected:

select sum(value) as "value"
from (
select difference(value) as "value" 
from "temperature"
where "tag1" = 'max' and time <= 1553096174000000000 and time >= 1521560174000000000 tz('Europe/Rome')
) 
group by time(1d) fill(none)

Got:

select sum(value) as "value"
from (
select difference(value) as "value" 
from "temperature"
where "tag1" = 'max' and time <= 1553096174000000000 and time >= 1521560174000000000 
group by time(1d) fill(none) <-- duplicated
tz('Europe/Rome') 
) 
where time <= 1553096174000000000 and time >= 1521560174000000000 <-- duplicated
group by time(1d) fill(none)
tz('Europe/Rome') <-- duplicated

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.