Constructor
new QL()
Example
const QL = require('influx-ql');
const ql = new QL('mydb');
ql.measurement = 'http';
ql.RP = 'two-weeks';
ql.addField('status', 'spdy', 'fetch time');
ql.start = '2016-01-01';
ql.end = '-3h';
ql.limit = 10;
ql.order = 'desc';
ql.offset = 10;
ql.tz = 'America/Chicago';
ql.addGroup('spdy');
ql.addGroup('time(30s)');
ql.condition('code', 400);
ql.condition('use', 30, '<=');
ql.fill = 0;
console.info(ql.toSelect());
Members
database
Set the database for influx ql
Example
const ql = new QL();
ql.database = 'mydb';
database
Get the database
Example
console.info(ql.database);
end
Set influx ql end time
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.end = '-1h';
console.info(ql.toSelect());
// => select * from "mydb".."http" where time <= now() - 1h
end
Get influx ql end time
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.end = '-1h';
console.info(ql.end);
// => '-1h';
fill
Set the influx query result fill value for time intervals that have no data
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy');
ql.fill = 0;
console.info(ql.toSelect());
// => select * from "mydb".."http" group by "spdy" fill(0)
fill
Get the influx query result fill value for time intervals that have no data
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy');
ql.fill = 0;
console.info(ql.fill);
// => 0
into
Get the measurement for select into
Example
console.info(ql.into);
into
Set the measurement for select into
Example
const ql = new QL('mydb');
ql.mearsurement = 'http';
ql.rp = 'my-rp';
ql.intoDatabase = 'mydb copy';
ql.into = 'http copy';
ql.intoRP = 'my-rp-copy';
intoDatabase
Get the database for select into
Example
console.info(ql.intoDatabase);
intoDatabase
Set the database for select into
Example
const ql = new QL('mydb');
ql.mearsurement = 'http';
ql.rp = 'my-rp';
ql.intoDatabase = 'mydb copy';
ql.into = 'http copy';
ql.intoRP = 'my-rp-copy';
intoRP
Get the rp for select into
Example
console.info(ql.intoRP);
intoRP
Set the rp for select into
Example
const ql = new QL('mydb');
ql.mearsurement = 'http';
ql.rp = 'my-rp';
ql.intoDatabase = 'mydb copy';
ql.into = 'http copy';
ql.intoRP = 'my-rp-copy';
limit
Set influx ql query result point limit
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.limit = 10;
console.info(ql.toSelect());
// => select * from "mydb".."http" limit 10
limit
Get influx ql query result point limit
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.limit = 10;
console.info(ql.limit);
// => 10
measurement
Set influx ql measurement
Example
const ql = new QL('mydb');
ql.measurement = 'http';
console.info(ql.toSelect());
// => select * from "mydb".."http"
measurement
Get influx ql measurement
Example
const ql = new QL('mydb');
ql.measurement = 'http';
console.info(ql.measurement);
// => 'http'
offset
Set influx ql query offset of the result
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.offset = 10;
console.info(ql.toSelect());
// => select * from "mydb".."http" offset 10
offset
Get influx ql query offset of the result
Example
const ql = new QL('mydb');
ql.measurement = 'http';
console.info(ql.offset);
// => 0
ql.offset = 10;
console.info(ql.offset);
// => 10
order
Get the influx query result order of time
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy');
console.info(ql.order);
// => undefined
ql.order = 'desc';
// => 'desc'
order
Set the influx query result order of time
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy');
ql.order = 'desc';
console.info(ql.toSelect());
// => select * from "mydb".."http" group by "spdy" order by time desc
relation
Get influx ql default where relation
Example
const ql = new QL('mydb');
console.info(ql.relation);
// => 'and'
relation
Set influx ql default where relation
Example
const ql = new QL('mydb');
console.info(ql.relation);
// => and
ql.relation = 'or';
console.info(ql.relation);
// => or
RP
Get influx ql retention policy
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.RP = 'two-weeks';
console.info(ql.RP);
// => two-weeks
RP
Set influx ql retention policy
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.RP = 'two-weeks';
console.info(ql.toSelect());
// => select * from "mydb"."two-weeks"."http"
slimit
Set influx query result series limit
Example
const ql = new QL('mydb');
ql.slimit = 3;
console.info(ql.toSelect());
// => select * from "mydb" slimit 3
slimit
Get influx query result series limit
Example
const ql = new QL('mydb');
ql.slimit = 3;
console.info(ql.slimit);
// => 3
soffset
Set influx ql offset series in the query results
Example
const ql = new QL('mydb');
ql.soffset = 10;
console.info(ql.toSelect());
// => select * from "mydb" soffset 10
soffset
Get influx ql offset series in the query results
Example
const ql = new QL('mydb');
console.info(ql.soffset);
// => 0
ql.soffset = 10;
console.info(ql.soffset);
// => 10
start
Get influx ql start time
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.start = '-3h';
console.info(ql.start);
// => '-3h';
start
Set influx ql start time
Examples
const ql = new QL('mydb');
ql.measurement = 'http';
ql.start = '-3h';
console.info(ql.toSelect());
// => select * from "mydb".."http" where time >= now() - 3h
const ql = new QL('mydb');
ql.measurement = 'http';
ql.start = '2015-08-18T00:00:00Z';
console.info(ql.toSelect());
// => select * from "mydb".."http" where time >= '2015-08-18T00:00:00Z'
const ql = new QL('mydb');
ql.measurement = 'http';
ql.start = '1388534400s;
console.info(ql.toSelect());
// => select * from "mydb".."http" where time >= 1388534400s
tz
Get influx ql time zone
Example
const ql = new QL('mydb');
ql.tz = 'America/Chicago'
console.info(ql.tz);
// => 'America/Chicago'
tz
Set influx ql time zone
Example
const ql = new QL('mydb');
ql.tz = 'America/Chicago';
console.info(ql.tz);
// => America/Chicago
Methods
addField(field)
Add the field of the query result
Examples
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField('status', 'spdy', 'fetch time');
console.info(ql.toSelect());
// => select "fetch time","spdy","status" from "mydb".."http"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField({
'fetch time': 'ft',
});
console.info(ql.toSelect());
// => select "fetch time" as "ft" from "mydb".."http"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField('"use" + 2');
console.info(ql.toSelect());
// => select "use" + 2 from "mydb".."http"
Parameters:
Name | Type | Description |
---|---|---|
field |
String | field's name |
Returns:
QL
addFunction(type, field, field, options) → {QL}
Add influx ql function
Examples
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count', 'use');
ql.addFunction('mean', 'use');
ql.addGroup('spdy');
console.info(ql.toSelect());
// => select count("use"),mean("use") from "mydb".."http" group by "spdy"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction("bottom", 'use', 3);
console.info(ql.toSelect());
// => select bottom("use",3) from "mydb".."http"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count("use")');
console.info(ql.toSelect());
// => select count("use") from "mydb".."http"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count("use")', {
alias: 'countUse',
});
console.info(ql.toSelect());
// => select count("use") as "countUse" from "mydb".."http"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count', 'use', {
alias: 'countUse',
});
console.info(ql.toSelect());
// => select count("use") as "countUse" from "mydb".."http"
Parameters:
Name | Type | Description |
---|---|---|
type |
String | function name |
field |
Any | function param |
field |
Any | function param |
options |
Object | set the alias for the function, eg: {"alias": "the alias name"} |
Returns:
- Type
- QL
addGroup(tag) → {QL}
Add influx ql group by
Examples
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy', 'method');
ql.addFunction('count', 'use');
console.info(ql.toSelect());
// => select count("use") from "mydb".."http" group by "method","spdy"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy', 'time(1m)');
ql.addFunction('count', 'use');
console.info(ql.toSelect());
// => select count("use") from "mydb".."http" group by "method","time(1m)"
Parameters:
Name | Type | Description |
---|---|---|
tag |
String | tag's name |
Returns:
- Type
- QL
addMeasurement(measurement) → {QL}
Add influx ql measurement
Examples
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count', 'use');
console.info(ql.toSelect());
// => select count("use") from "mydb".."http"
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addMeasurement('https');
ql.addFunction('count', 'use');
console.info(ql.toSelect());
// => select count("use") from "mydb".."http","mydb".."https"
Parameters:
Name | Type | Description |
---|---|---|
measurement |
String | measurement name |
Returns:
- Type
- QL
clean()
Clean all the influx ql condition
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField('fetch time');
ql.addGroup('spdy');
ql.clean();
console.info(ql.toSelect());
// => select * from "mydb".."http"
emptyConditions() → {QL}
Empty the influx ql where condition
Example
const ql = new QL();
ql.measurement = 'http';
ql.condition({
code: 500,
spyd: '1',
});
console.info(ql.toSelect());
// => select * from "http" where ("code" = 500 and "spdy" = '1')
ql.emptyConditions();
console.info(ql.toSelect());
// => select * from "http"
Returns:
- Type
- QL
emptyFields()
Remove all fields of the query result
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField('status', 'spdy', 'fetch time');
ql.emptyFields();
console.info(ql.toSelect());
// => select * from "mydb".."http"
Returns:
QL
emptyFunctions() → {QL}
Remove all influx ql functions
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count', 'use');
ql.addFunction('mean', 'use');
ql.emptyFunctions();
ql.addGroup('spdy');
console.info(ql.toSelect());
// => select * from "mydb".."http" group by "spdy"
Returns:
- Type
- QL
emptyGroups() → {QL}
Empty influx ql group by
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy', 'method');
ql.emptyGroups();
console.info(ql.toSelect());
// => select * from "mydb".."http"
Returns:
- Type
- QL
emptyMeasurements() → {QL}
Empty influx ql measurements
Example
const ql = new QL('mydb');
ql.addMeasurement('http', 'https');
ql.emptyMeasurements();
console.info(ql.toSelect());
// => select * from "mydb"
Returns:
- Type
- QL
multiQuery()
Multi query
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('max', 'fetch time');
ql.addGroup('spdy');
ql.multiQuery();
ql.addFunction('sum', 'max');
console.info(ql.toSelect());
// => select max("fetch time") from "mydb".."http" group by "spdy";
select sum("max") from "mydb".."http"
removeField(field)
Remove the field of the query result
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addField('status', 'spdy', 'fetch time');
ql.removeField('status');
console.info(ql.toSelect());
// => select "fetch time","spdy" from "mydb".."http"
Parameters:
Name | Type | Description |
---|---|---|
field |
String | field's name |
Returns:
QL
removeFunction(type, field, field) → {QL}
Remove influx ql function
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('count', 'use');
ql.addFunction('mean', 'use');
ql.removeFunction('count', 'use');
ql.addGroup('spdy');
ql.addGroup('time(6h)');
console.info(ql.toSelect());
// => select mean("use") from "mydb".."http" group by "spdy"
Parameters:
Name | Type | Description |
---|---|---|
type |
String | function name |
field |
Any | function param |
field |
Any | function param |
Returns:
- Type
- QL
removeGroup(tag) → {QL}
Remove influx ql group by
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addGroup('spdy', 'method');
ql.removeGroup('spdy')
ql.addFunction('count', 'use');
console.info(ql.toSelect());
// => select count("use") from "mydb".."http" group by "method"
Parameters:
Name | Type | Description |
---|---|---|
tag |
String | tag's name |
Returns:
- Type
- QL
removeMeasurement(measurement) → {QL}
Remove influx ql measurement
Example
const ql = new QL('mydb');
ql.addMeasurement('foo', 'bar');
ql.removeMeasurement('foo')
ql.addFunction('count', 'use');
console.info(ql.toSelect());
// => select count("use") from "mydb".."bar"
Parameters:
Name | Type | Description |
---|---|---|
measurement |
String | measurement name |
Returns:
- Type
- QL
subQuery()
Set the sub query string, it will get the current ql for sub query and clean the query options
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.addFunction('max', 'fetch time');
ql.addGroup('spdy');
ql.subQuery();
ql.addFunction('sum', 'max');
console.info(ql.toSelect());
// => select sum("max") from (select max("fetch time") from "mydb".."http" group by "spdy")
toSelect() → {String}
Get the influx select ql
Example
const ql = new QL('mydb');
ql.measurement = 'http';
ql.RP = 'two-weeks';
ql.addField('status', 'spdy', 'fetch time');
ql.start = '2016-01-01';
ql.end = '-3h';
ql.limit = 10;
ql.order = 'desc';
ql.offset = 10;
ql.addGroup('spdy');
ql.condition('code', 400);
ql.condition('use', 30, '<=');
ql.fill = 0;
console.info(ql.toSelect());
Returns:
- Type
- String
where(key, value, relation, operator) → {QL}
Add the influx ql where condition, alias for condition
Examples
const ql = new QL();
ql.measurement = 'http';
ql.where('code', 500);
console.info(ql.toSelect());
// => select * from "http" where "code" = 500
const ql = new QL();
ql.measurement = 'http';
ql.where({
code: 500,
spdy: '1',
});
console.info(ql.toSelect());
// => select * from "http" where ("code" = 500 and "spdy" = '1')
const ql = new QL();
ql.measurement = 'http';
ql.where('spdy', ['1', '2']);
console.info(ql.toSelect());
// => select * from "mydb".."http" where ("spdy" = '1' or "spdy" = '2')
const ql = new QL();
ql.measurement = 'http';
ql.where({
code: 500,
spdy: '1',
}, '!=');
console.info(ql.toSelect());
// => select * from "http" where ("code" != 500 and "spdy" != '1')
const ql = new QL();
ql.measurement = 'http';
ql.where('code', /5\d{2}/);
console.info(ql.toSelect());
// => select * from "http" where "code" = /5\d{2}/
Parameters:
Name | Type | Description |
---|---|---|
key |
String | the condition key |
value |
String | the condition value |
relation |
String | the multi condition relation |
operator |
String | the conditon operator, default is '=' |
Returns:
- Type
- QL