@senx/warp10
Version:
Warp 10 NodeJS library
217 lines (216 loc) • 6.44 kB
TypeScript
import { DataPoint } from './DataPoint';
export declare enum TimeUnits {
US = 1000,
MS = 1,
NS = 1000000
}
export interface GTS {
c: string;
v: any[];
a: {
[key: string]: string;
};
l: {
[key: string]: string;
};
la: number;
}
export interface W10Data {
ts: number;
value: any;
loc?: {
lat: number;
long: number;
};
elev?: number;
}
export interface FormattedGTS {
name: string;
labels: {
[key: string]: string;
};
attributes: {
[key: string]: string;
};
data: W10Data[];
}
/**
*
*/
export declare class Warp10 {
private url;
private client;
private LOG;
private _endpoint;
private _timeUnit;
private _headers?;
private _timeout;
/**
* Create new Warp 10 connector.
*
* @param params - \{ endpoint: string; debug?: boolean; silent?: boolean, timeUnit: TimeUnits \}
* endpoint - Warp 10 endpoint, without <code>/api/v0</code> at the end.
* debug - Enable debug
* silent - Do not produce logs
* timeUnit - Platform timeUnit @see TimeUnits
* headers - custom HTTP headers
* timeout - http Timeout
*
* @example
* ```
* // standard constructor
* const w10 = new Warp10({endpoint: 'https://sandbox.senx.io'});
* // builder pattern
* const w10 = new Warp10().endpoint('https://sandbox.senx.io').timeUnit(TimeUnits.US);
* ```
*/
constructor(params?: {
endpoint?: string;
debug?: boolean;
silent?: boolean;
timeUnit?: TimeUnits;
headers?: {
[key: string]: string;
};
timeout?: number;
});
endpoint(endpoint: string | undefined): this;
headers(headers: {
[key: string]: string;
}): this;
debug(debug: boolean): this;
silent(silent: boolean): this;
timeUnit(timeUnit: TimeUnits): this;
timeout(to: number): this;
private send;
/**
* Build got request options from defined options
*
* @param path - request path
* @param method - request method
* @param warpToken - the X-Warp10-Token, if any
*/
private getOptions;
/**
* Execute a WarpScript against a Warp 10 instance
*
* @param warpScript - WarpScript to execute
*
* @example
* ```
* // Prints "[4]":
* console.log(await w10.exec('2 2 +'))
* ```
*/
exec(warpScript: string): Promise<{
result: any[];
meta: {
elapsed: number;
ops: number;
fetched: number;
};
}>;
/**
* Fetch data against a Warp 10 instance
*
* @param readToken - Read token
* @param className - ClassName, could be a regexp starting with '\~' (ie: '~io.warp10.*' )
* @param labels - Labels key value map. Could be a regexp starting with '\~' (ie: \{ 'myLabel': '~sensor_.*' \} )
* @param start - ISO8601 UTC Date
* @param stop - ISO8601 UTC Date if 'start' is a ISO8601 date. Timespan (in platform timeunit format) if 'start' is a timestamp
* @param format - Output format: text' | 'fulltext' | 'json' | 'tsv' | 'fulltsv' | 'pack' | 'raw' | 'formatted', default is 'formatted'
* @param dedup - Deduplicates data (default is true)
*
* @example
* ```
* // fetch raw data between 2 dates
* console.log(await w10.fetch(readToken, '~io.warp10.*', {}, '2019-11-11T12:34:43.388409Z', '2019-11-21T12:34:43.388409Z', 'json'));
*
* // Fetch data with a time span
* console.log(await w10.fetch(readToken, '~.*', {}, '2019-11-21T12:34:43.388409Z', 86400000000 * 5));
* ```
*/
fetch(readToken: string, className: string, labels: {
[key: string]: string;
}, start: string, stop: any, format?: 'text' | 'fulltext' | 'json' | 'tsv' | 'fulltsv' | 'pack' | 'raw' | 'formatted', dedup?: boolean): Promise<{
result: any[];
meta: {
elapsed: number;
ops: number;
fetched: number;
};
}>;
/**
* Update datapoints
*
* @param writeToken - Write token
* @param dataPoints
*
* @example
* ```
* console.log(await w10.update(writeToken, [
* {timestamp: moment.utc().valueOf() * 1000, className: 'io.warp10.test', labels: {key: 'value'}, value: 54},
* '1380475081000000// io.warp10.test{key=value} T',
* '1566893344654882/48.81:-4.147/124 io.warp10.test{key=value} [8.2 151 152 1568189745655509/40.6:-74/14 ]',
* ]));
* ```
*/
update(writeToken: string, dataPoints: (DataPoint | string)[]): Promise<{
response: string | undefined;
count: number;
}>;
/**
*
* @param deleteToken - Delete token
* @param className - ClassName, could be a regexp starting with '\~' (ie: '~io.warp10.*' )
* @param labels - Labels key value map. Could be a regexp starting with '\~' (ie: \{ 'myLabel': '~sensor_.*' \} )
* @param start - ISO8601 UTC Date
* @param end - ISO8601 UTC Date
* @param deleteAll - Default is 'false'
*
* @example
* ```
* // delete data between 2 dates
* console.log(await w10.delete(deleteToken, '~io.warp10.test*', {key: 'value'}, '2019-11-11T12:34:43.388409Z', '2019-11-21T12:34:43.388409Z'));
*
* // delete all
* console.log(await w10.delete(deleteToken, '~io.warp10.test*', {key: 'value'}, '', '', true));
* ```
*
*/
delete(deleteToken: string, className: string, labels: {
[key: string]: string;
}, start: string, end: string, deleteAll?: boolean): Promise<{
result: string;
}>;
/**
* Update Meta
* @param writeToken - Write token
* @param meta - Metadata key value map to update
*
* @example
* ```
* // write meta
* console.log(await w10.meta(writeToken, [{
* className: 'io.warp10.test',
* labels: {key: 'value'},
* attributes: {attr: 'value'}
* }]));
* ```
*/
meta(writeToken: string, meta: {
className: string;
labels: {
[key: string]: string;
};
attributes: {
[key: string]: string;
};
}[]): Promise<{
response: string;
count: number;
}>;
private formatLabels;
private static formatValues;
private formatGTS;
}