influx 5.0.6 → 5.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/src/backoff/backoff.d.ts +15 -15
- package/lib/src/backoff/backoff.js +2 -2
- package/lib/src/backoff/exponential.d.ts +52 -52
- package/lib/src/backoff/exponential.js +40 -40
- package/lib/src/builder.d.ts +265 -265
- package/lib/src/builder.js +366 -366
- package/lib/src/doc.d.ts +315 -315
- package/lib/src/doc.js +340 -340
- package/lib/src/grammar/ds.d.ts +45 -45
- package/lib/src/grammar/ds.js +56 -56
- package/lib/src/grammar/escape.d.ts +25 -25
- package/lib/src/grammar/escape.js +108 -108
- package/lib/src/grammar/index.d.ts +3 -3
- package/lib/src/grammar/index.js +8 -8
- package/lib/src/grammar/times.d.ts +74 -74
- package/lib/src/grammar/times.js +325 -325
- package/lib/src/host.d.ts +26 -26
- package/lib/src/host.js +34 -34
- package/lib/src/index.d.ts +659 -659
- package/lib/src/index.js +870 -870
- package/lib/src/pool.d.ts +149 -149
- package/lib/src/pool.js +344 -344
- package/lib/src/results.d.ts +118 -118
- package/lib/src/results.js +146 -146
- package/lib/src/schema.d.ts +58 -58
- package/lib/src/schema.js +117 -117
- package/package.json +2 -2
package/lib/src/grammar/times.js
CHANGED
|
@@ -1,325 +1,325 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var ds_1 = require("./ds");
|
|
4
|
-
/**
|
|
5
|
-
* Just a quick overview of what's going on in this file. It's a bit of a mess.
|
|
6
|
-
* Influx uses three time formats:
|
|
7
|
-
* - ISO times with nanoseconds when querying where an epoch is not provided
|
|
8
|
-
* - Unix timestamps when querying with an epoch (specifying the precision
|
|
9
|
-
* in the given time unit)
|
|
10
|
-
* - Its own time format for time literals.
|
|
11
|
-
*
|
|
12
|
-
* To complicate matters, Influx operates on nanosecond precisions
|
|
13
|
-
* by default, but we can't represent nanosecond timestamps in
|
|
14
|
-
* JavaScript numbers as they're 64 bit uints.
|
|
15
|
-
*
|
|
16
|
-
* As a result we have several utilities to convert between these different
|
|
17
|
-
* formats. When precision is required, we represent nanosecond timestamps
|
|
18
|
-
* as strings and wrap default dates in the INanoDate interface which
|
|
19
|
-
* lets the consumer read and write these more precise timestamps.
|
|
20
|
-
*
|
|
21
|
-
* Representing the timestamps as strings is definitely not a pure way to go
|
|
22
|
-
* about it, but importing an arbitrary-precision integer library adds
|
|
23
|
-
* bloat and is a massive hit to throughput. The operations we do do
|
|
24
|
-
* are pretty trivial, so we stick with manipulating strings
|
|
25
|
-
* and make sure to wash our hands when we're done.
|
|
26
|
-
*
|
|
27
|
-
* Vocabulary:
|
|
28
|
-
* Unix timestamp = 'timestamp', abbreviated as 'time'
|
|
29
|
-
* ISO timestamp = 'ISO time', abbreviated as 'ISO'
|
|
30
|
-
* Influx timestamp = 'Influx time', abbreviated as 'Influx'
|
|
31
|
-
*/
|
|
32
|
-
function leftPad(str, length, pad) {
|
|
33
|
-
if (pad === void 0) { pad = '0'; }
|
|
34
|
-
if (typeof str === 'number') {
|
|
35
|
-
str = String(str);
|
|
36
|
-
}
|
|
37
|
-
while (str.length < length) {
|
|
38
|
-
str = pad + str;
|
|
39
|
-
}
|
|
40
|
-
return str;
|
|
41
|
-
}
|
|
42
|
-
function rightPad(str, length, pad) {
|
|
43
|
-
if (pad === void 0) { pad = '0'; }
|
|
44
|
-
if (typeof str === 'number') {
|
|
45
|
-
str = String(str);
|
|
46
|
-
}
|
|
47
|
-
while (str.length < length) {
|
|
48
|
-
str += pad;
|
|
49
|
-
}
|
|
50
|
-
return str;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Precision is a map of available Influx time precisions.
|
|
54
|
-
* @type {Object.<String, String>}
|
|
55
|
-
* @example
|
|
56
|
-
* console.log(Precision.Hours); // => 'h'
|
|
57
|
-
* console.log(Precision.Minutes); // => 'm'
|
|
58
|
-
* console.log(Precision.Seconds); // => 's'
|
|
59
|
-
* console.log(Precision.Milliseconds); // => 'ms'
|
|
60
|
-
* console.log(Precision.Microseconds); // => 'u'
|
|
61
|
-
* console.log(Precision.Nanoseconds); // => 'ns'
|
|
62
|
-
*/
|
|
63
|
-
exports.Precision = Object.freeze({
|
|
64
|
-
Hours: 'h',
|
|
65
|
-
Microseconds: 'u',
|
|
66
|
-
Milliseconds: 'ms',
|
|
67
|
-
Minutes: 'm',
|
|
68
|
-
Nanoseconds: 'n',
|
|
69
|
-
Seconds: 's',
|
|
70
|
-
});
|
|
71
|
-
var MillisecondDateManipulator = (function () {
|
|
72
|
-
function MillisecondDateManipulator() {
|
|
73
|
-
}
|
|
74
|
-
MillisecondDateManipulator.prototype.format = function (date) {
|
|
75
|
-
return '"' + leftPad(date.getUTCFullYear(), 2)
|
|
76
|
-
+ '-' + leftPad(date.getUTCMonth() + 1, 2)
|
|
77
|
-
+ '-' + leftPad(date.getUTCDate(), 2)
|
|
78
|
-
+ ' ' + leftPad(date.getUTCHours(), 2)
|
|
79
|
-
+ ':' + leftPad(date.getUTCMinutes(), 2)
|
|
80
|
-
+ ':' + leftPad(date.getUTCSeconds(), 2)
|
|
81
|
-
+ '.' + leftPad(date.getUTCMilliseconds(), 3) + '"';
|
|
82
|
-
};
|
|
83
|
-
MillisecondDateManipulator.prototype.toTime = function (date, precision) {
|
|
84
|
-
var ms = date.getTime();
|
|
85
|
-
switch (precision) {
|
|
86
|
-
case 'n':
|
|
87
|
-
ms *= 1000;
|
|
88
|
-
case 'u':
|
|
89
|
-
ms *= 1000;
|
|
90
|
-
case 'ms':
|
|
91
|
-
return String(ms);
|
|
92
|
-
case 'h':
|
|
93
|
-
ms /= 60;
|
|
94
|
-
case 'm':
|
|
95
|
-
ms /= 60;
|
|
96
|
-
case 's':
|
|
97
|
-
ms /= 1000;
|
|
98
|
-
return String(Math.floor(ms));
|
|
99
|
-
default:
|
|
100
|
-
throw new Error("Unknown precision '" + precision + "'!");
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
MillisecondDateManipulator.prototype.isoToDate = function (timestamp) {
|
|
104
|
-
return new Date(timestamp);
|
|
105
|
-
};
|
|
106
|
-
MillisecondDateManipulator.prototype.timetoDate = function (timestamp, precision) {
|
|
107
|
-
switch (precision) {
|
|
108
|
-
case 'n':
|
|
109
|
-
timestamp /= 1000;
|
|
110
|
-
case 'u':
|
|
111
|
-
timestamp /= 1000;
|
|
112
|
-
case 'ms':
|
|
113
|
-
return new Date(timestamp);
|
|
114
|
-
case 'h':
|
|
115
|
-
timestamp *= 60;
|
|
116
|
-
case 'm':
|
|
117
|
-
timestamp *= 60;
|
|
118
|
-
case 's':
|
|
119
|
-
timestamp *= 1000;
|
|
120
|
-
return new Date(timestamp);
|
|
121
|
-
default:
|
|
122
|
-
throw new Error("Unknown precision '" + precision + "'!");
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
return MillisecondDateManipulator;
|
|
126
|
-
}());
|
|
127
|
-
var nsPer = {
|
|
128
|
-
ms: Math.pow(10, 6),
|
|
129
|
-
s: Math.pow(10, 9),
|
|
130
|
-
};
|
|
131
|
-
function nanoIsoToTime(iso) {
|
|
132
|
-
var _a = iso.split('.'), secondsStr = _a[0], decimalStr = _a[1];
|
|
133
|
-
if (decimalStr === undefined) {
|
|
134
|
-
decimalStr = '000000000';
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
decimalStr = rightPad(decimalStr.slice(0, -1), 9);
|
|
138
|
-
secondsStr += 'Z';
|
|
139
|
-
}
|
|
140
|
-
var seconds = Math.floor(new Date(secondsStr).getTime() / 1000);
|
|
141
|
-
return "" + seconds + decimalStr;
|
|
142
|
-
}
|
|
143
|
-
var nanoDateMethods = {
|
|
144
|
-
getNanoTimeFromISO: function () {
|
|
145
|
-
if (!this._cachedNanoISO) {
|
|
146
|
-
this._cachedNanoTime = nanoIsoToTime(this._nanoISO);
|
|
147
|
-
}
|
|
148
|
-
return this._cachedNanoTime;
|
|
149
|
-
},
|
|
150
|
-
toNanoISOStringFromISO: function () {
|
|
151
|
-
if (!this._cachedNanoISO) {
|
|
152
|
-
this._cachedNanoTime = nanoIsoToTime(this._nanoISO);
|
|
153
|
-
}
|
|
154
|
-
var base = this.toISOString().slice(0, -4); // slice of `123Z` milliseconds
|
|
155
|
-
return "" + base + this._cachedNanoTime.slice(-9) + "Z";
|
|
156
|
-
},
|
|
157
|
-
getNanoTimeFromStamp: function () {
|
|
158
|
-
return this._nanoTime;
|
|
159
|
-
},
|
|
160
|
-
toNanoISOStringFromStamp: function () {
|
|
161
|
-
var base = this.toISOString().slice(0, -4); // slice of `123Z` milliseconds
|
|
162
|
-
return "" + base + this._nanoTime.slice(-9) + "Z";
|
|
163
|
-
},
|
|
164
|
-
};
|
|
165
|
-
/**
|
|
166
|
-
* Covers a nanoseconds unix timestamp to a INanoDate for node-influx. The
|
|
167
|
-
* timestamp is provided as a string to prevent precision loss.
|
|
168
|
-
*
|
|
169
|
-
* Please see [A Moment for Times](https://node-influx.github.io/manual/
|
|
170
|
-
* usage.html#a-moment-for-times) for a more complete and eloquent explanation
|
|
171
|
-
* of time handling in this module.
|
|
172
|
-
*
|
|
173
|
-
* @param {String} timestamp
|
|
174
|
-
* @returns {INanoDate}
|
|
175
|
-
* @example
|
|
176
|
-
* const date = toNanoDate('1475985480231035600')
|
|
177
|
-
*
|
|
178
|
-
* // You can use the returned Date as a normal date:
|
|
179
|
-
* expect(date.getTime()).to.equal(1475985480231);
|
|
180
|
-
*
|
|
181
|
-
* // We decorate it with two additional methods to read
|
|
182
|
-
* // nanosecond-precision results:
|
|
183
|
-
* expect(date.getNanoTime()).to.equal('1475985480231035600');
|
|
184
|
-
* expect(date.toNanoISOString()).to.equal('2016-10-09T03:58:00.231035600Z');
|
|
185
|
-
*/
|
|
186
|
-
function toNanoDate(timestamp) {
|
|
187
|
-
var date = new Date(Math.floor(Number(timestamp) / nsPer.ms));
|
|
188
|
-
date._nanoTime = timestamp;
|
|
189
|
-
date.getNanoTime = nanoDateMethods.getNanoTimeFromStamp;
|
|
190
|
-
date.toNanoISOString = nanoDateMethods.toNanoISOStringFromStamp;
|
|
191
|
-
return date;
|
|
192
|
-
}
|
|
193
|
-
exports.toNanoDate = toNanoDate;
|
|
194
|
-
function asNanoDate(date) {
|
|
195
|
-
var d = date;
|
|
196
|
-
if (d.getNanoTime) {
|
|
197
|
-
return d;
|
|
198
|
-
}
|
|
199
|
-
return undefined;
|
|
200
|
-
}
|
|
201
|
-
var NanosecondsDateManipulator = (function () {
|
|
202
|
-
function NanosecondsDateManipulator() {
|
|
203
|
-
}
|
|
204
|
-
NanosecondsDateManipulator.prototype.format = function (date) {
|
|
205
|
-
return '"' + leftPad(date.getUTCFullYear(), 2)
|
|
206
|
-
+ '-' + leftPad(date.getUTCMonth() + 1, 2)
|
|
207
|
-
+ '-' + leftPad(date.getUTCDate(), 2)
|
|
208
|
-
+ ' ' + leftPad(date.getUTCHours(), 2)
|
|
209
|
-
+ ':' + leftPad(date.getUTCMinutes(), 2)
|
|
210
|
-
+ ':' + leftPad(date.getUTCSeconds(), 2)
|
|
211
|
-
+ '.' + date.getNanoTime().slice(-9) + '"';
|
|
212
|
-
};
|
|
213
|
-
NanosecondsDateManipulator.prototype.toTime = function (date, precision) {
|
|
214
|
-
var ms = date.getTime();
|
|
215
|
-
switch (precision) {
|
|
216
|
-
case 'u':
|
|
217
|
-
return date.getNanoTime().slice(0, -3);
|
|
218
|
-
case 'n':
|
|
219
|
-
return date.getNanoTime();
|
|
220
|
-
case 'h':
|
|
221
|
-
ms /= 60;
|
|
222
|
-
case 'm':
|
|
223
|
-
ms /= 60;
|
|
224
|
-
case 's':
|
|
225
|
-
ms /= 1000;
|
|
226
|
-
case 'ms':
|
|
227
|
-
return String(Math.floor(ms));
|
|
228
|
-
default:
|
|
229
|
-
throw new Error("Unknown precision '" + precision + "'!");
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
NanosecondsDateManipulator.prototype.isoToDate = function (timestamp) {
|
|
233
|
-
var date = new Date(timestamp);
|
|
234
|
-
date._nanoISO = timestamp;
|
|
235
|
-
date.getNanoTime = nanoDateMethods.getNanoTimeFromISO;
|
|
236
|
-
date.toNanoISOString = nanoDateMethods.toNanoISOStringFromISO;
|
|
237
|
-
return date;
|
|
238
|
-
};
|
|
239
|
-
NanosecondsDateManipulator.prototype.timetoDate = function (timestamp, precision) {
|
|
240
|
-
switch (precision) {
|
|
241
|
-
case 'h':
|
|
242
|
-
timestamp *= 60;
|
|
243
|
-
case 'm':
|
|
244
|
-
timestamp *= 60;
|
|
245
|
-
case 's':
|
|
246
|
-
timestamp *= 1000;
|
|
247
|
-
case 'ms':
|
|
248
|
-
timestamp *= 1000;
|
|
249
|
-
case 'u':
|
|
250
|
-
timestamp *= 1000;
|
|
251
|
-
case 'n':
|
|
252
|
-
var date = new Date(timestamp / nsPer.ms);
|
|
253
|
-
date._nanoTime = String(timestamp);
|
|
254
|
-
date.getNanoTime = nanoDateMethods.getNanoTimeFromStamp;
|
|
255
|
-
date.toNanoISOString = nanoDateMethods.toNanoISOStringFromStamp;
|
|
256
|
-
return date;
|
|
257
|
-
default:
|
|
258
|
-
throw new Error("Unknown precision '" + precision + "'!");
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
return NanosecondsDateManipulator;
|
|
262
|
-
}());
|
|
263
|
-
var milliManipulator = new MillisecondDateManipulator();
|
|
264
|
-
var nanoManipulator = new NanosecondsDateManipulator();
|
|
265
|
-
/**
|
|
266
|
-
* formatDate converts the Date instance to Influx's date query format.
|
|
267
|
-
* @private
|
|
268
|
-
*/
|
|
269
|
-
function formatDate(date) {
|
|
270
|
-
var nano = asNanoDate(date);
|
|
271
|
-
if (nano) {
|
|
272
|
-
return nanoManipulator.format(nano);
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
return milliManipulator.format(date);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
exports.formatDate = formatDate;
|
|
279
|
-
/**
|
|
280
|
-
* Converts a Date instance to a timestamp with the specified time precision.
|
|
281
|
-
* @private
|
|
282
|
-
*/
|
|
283
|
-
function dateToTime(date, precision) {
|
|
284
|
-
var nano = asNanoDate(date);
|
|
285
|
-
if (nano) {
|
|
286
|
-
return nanoManipulator.toTime(nano, precision);
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
return milliManipulator.toTime(date, precision);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
exports.dateToTime = dateToTime;
|
|
293
|
-
/**
|
|
294
|
-
* Converts an ISO-formatted data or unix timestamp to a Date instance. If
|
|
295
|
-
* the precision is finer than 'ms' the returned value will be a INanoDate.
|
|
296
|
-
* @private
|
|
297
|
-
*/
|
|
298
|
-
function isoOrTimeToDate(stamp, precision) {
|
|
299
|
-
if (precision === void 0) { precision = 'n'; }
|
|
300
|
-
if (typeof stamp === 'string') {
|
|
301
|
-
return nanoManipulator.isoToDate(stamp);
|
|
302
|
-
}
|
|
303
|
-
else {
|
|
304
|
-
return nanoManipulator.timetoDate(stamp, precision);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
exports.isoOrTimeToDate = isoOrTimeToDate;
|
|
308
|
-
/**
|
|
309
|
-
* Converts a timestamp to a string with the correct precision. Assumes
|
|
310
|
-
* that raw number and string instances are already in the correct precision.
|
|
311
|
-
* @private
|
|
312
|
-
*/
|
|
313
|
-
function castTimestamp(timestamp, precision) {
|
|
314
|
-
if (typeof timestamp === 'string') {
|
|
315
|
-
if (!ds_1.isNumeric(timestamp)) {
|
|
316
|
-
throw new Error("Expected numeric value for, timestamp, but got '" + timestamp + "'!");
|
|
317
|
-
}
|
|
318
|
-
return timestamp;
|
|
319
|
-
}
|
|
320
|
-
if (typeof timestamp === 'number') {
|
|
321
|
-
return String(timestamp);
|
|
322
|
-
}
|
|
323
|
-
return dateToTime(timestamp, precision);
|
|
324
|
-
}
|
|
325
|
-
exports.castTimestamp = castTimestamp;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var ds_1 = require("./ds");
|
|
4
|
+
/**
|
|
5
|
+
* Just a quick overview of what's going on in this file. It's a bit of a mess.
|
|
6
|
+
* Influx uses three time formats:
|
|
7
|
+
* - ISO times with nanoseconds when querying where an epoch is not provided
|
|
8
|
+
* - Unix timestamps when querying with an epoch (specifying the precision
|
|
9
|
+
* in the given time unit)
|
|
10
|
+
* - Its own time format for time literals.
|
|
11
|
+
*
|
|
12
|
+
* To complicate matters, Influx operates on nanosecond precisions
|
|
13
|
+
* by default, but we can't represent nanosecond timestamps in
|
|
14
|
+
* JavaScript numbers as they're 64 bit uints.
|
|
15
|
+
*
|
|
16
|
+
* As a result we have several utilities to convert between these different
|
|
17
|
+
* formats. When precision is required, we represent nanosecond timestamps
|
|
18
|
+
* as strings and wrap default dates in the INanoDate interface which
|
|
19
|
+
* lets the consumer read and write these more precise timestamps.
|
|
20
|
+
*
|
|
21
|
+
* Representing the timestamps as strings is definitely not a pure way to go
|
|
22
|
+
* about it, but importing an arbitrary-precision integer library adds
|
|
23
|
+
* bloat and is a massive hit to throughput. The operations we do do
|
|
24
|
+
* are pretty trivial, so we stick with manipulating strings
|
|
25
|
+
* and make sure to wash our hands when we're done.
|
|
26
|
+
*
|
|
27
|
+
* Vocabulary:
|
|
28
|
+
* Unix timestamp = 'timestamp', abbreviated as 'time'
|
|
29
|
+
* ISO timestamp = 'ISO time', abbreviated as 'ISO'
|
|
30
|
+
* Influx timestamp = 'Influx time', abbreviated as 'Influx'
|
|
31
|
+
*/
|
|
32
|
+
function leftPad(str, length, pad) {
|
|
33
|
+
if (pad === void 0) { pad = '0'; }
|
|
34
|
+
if (typeof str === 'number') {
|
|
35
|
+
str = String(str);
|
|
36
|
+
}
|
|
37
|
+
while (str.length < length) {
|
|
38
|
+
str = pad + str;
|
|
39
|
+
}
|
|
40
|
+
return str;
|
|
41
|
+
}
|
|
42
|
+
function rightPad(str, length, pad) {
|
|
43
|
+
if (pad === void 0) { pad = '0'; }
|
|
44
|
+
if (typeof str === 'number') {
|
|
45
|
+
str = String(str);
|
|
46
|
+
}
|
|
47
|
+
while (str.length < length) {
|
|
48
|
+
str += pad;
|
|
49
|
+
}
|
|
50
|
+
return str;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Precision is a map of available Influx time precisions.
|
|
54
|
+
* @type {Object.<String, String>}
|
|
55
|
+
* @example
|
|
56
|
+
* console.log(Precision.Hours); // => 'h'
|
|
57
|
+
* console.log(Precision.Minutes); // => 'm'
|
|
58
|
+
* console.log(Precision.Seconds); // => 's'
|
|
59
|
+
* console.log(Precision.Milliseconds); // => 'ms'
|
|
60
|
+
* console.log(Precision.Microseconds); // => 'u'
|
|
61
|
+
* console.log(Precision.Nanoseconds); // => 'ns'
|
|
62
|
+
*/
|
|
63
|
+
exports.Precision = Object.freeze({
|
|
64
|
+
Hours: 'h',
|
|
65
|
+
Microseconds: 'u',
|
|
66
|
+
Milliseconds: 'ms',
|
|
67
|
+
Minutes: 'm',
|
|
68
|
+
Nanoseconds: 'n',
|
|
69
|
+
Seconds: 's',
|
|
70
|
+
});
|
|
71
|
+
var MillisecondDateManipulator = (function () {
|
|
72
|
+
function MillisecondDateManipulator() {
|
|
73
|
+
}
|
|
74
|
+
MillisecondDateManipulator.prototype.format = function (date) {
|
|
75
|
+
return '"' + leftPad(date.getUTCFullYear(), 2)
|
|
76
|
+
+ '-' + leftPad(date.getUTCMonth() + 1, 2)
|
|
77
|
+
+ '-' + leftPad(date.getUTCDate(), 2)
|
|
78
|
+
+ ' ' + leftPad(date.getUTCHours(), 2)
|
|
79
|
+
+ ':' + leftPad(date.getUTCMinutes(), 2)
|
|
80
|
+
+ ':' + leftPad(date.getUTCSeconds(), 2)
|
|
81
|
+
+ '.' + leftPad(date.getUTCMilliseconds(), 3) + '"';
|
|
82
|
+
};
|
|
83
|
+
MillisecondDateManipulator.prototype.toTime = function (date, precision) {
|
|
84
|
+
var ms = date.getTime();
|
|
85
|
+
switch (precision) {
|
|
86
|
+
case 'n':
|
|
87
|
+
ms *= 1000;
|
|
88
|
+
case 'u':
|
|
89
|
+
ms *= 1000;
|
|
90
|
+
case 'ms':
|
|
91
|
+
return String(ms);
|
|
92
|
+
case 'h':
|
|
93
|
+
ms /= 60;
|
|
94
|
+
case 'm':
|
|
95
|
+
ms /= 60;
|
|
96
|
+
case 's':
|
|
97
|
+
ms /= 1000;
|
|
98
|
+
return String(Math.floor(ms));
|
|
99
|
+
default:
|
|
100
|
+
throw new Error("Unknown precision '" + precision + "'!");
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
MillisecondDateManipulator.prototype.isoToDate = function (timestamp) {
|
|
104
|
+
return new Date(timestamp);
|
|
105
|
+
};
|
|
106
|
+
MillisecondDateManipulator.prototype.timetoDate = function (timestamp, precision) {
|
|
107
|
+
switch (precision) {
|
|
108
|
+
case 'n':
|
|
109
|
+
timestamp /= 1000;
|
|
110
|
+
case 'u':
|
|
111
|
+
timestamp /= 1000;
|
|
112
|
+
case 'ms':
|
|
113
|
+
return new Date(timestamp);
|
|
114
|
+
case 'h':
|
|
115
|
+
timestamp *= 60;
|
|
116
|
+
case 'm':
|
|
117
|
+
timestamp *= 60;
|
|
118
|
+
case 's':
|
|
119
|
+
timestamp *= 1000;
|
|
120
|
+
return new Date(timestamp);
|
|
121
|
+
default:
|
|
122
|
+
throw new Error("Unknown precision '" + precision + "'!");
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return MillisecondDateManipulator;
|
|
126
|
+
}());
|
|
127
|
+
var nsPer = {
|
|
128
|
+
ms: Math.pow(10, 6),
|
|
129
|
+
s: Math.pow(10, 9),
|
|
130
|
+
};
|
|
131
|
+
function nanoIsoToTime(iso) {
|
|
132
|
+
var _a = iso.split('.'), secondsStr = _a[0], decimalStr = _a[1];
|
|
133
|
+
if (decimalStr === undefined) {
|
|
134
|
+
decimalStr = '000000000';
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
decimalStr = rightPad(decimalStr.slice(0, -1), 9);
|
|
138
|
+
secondsStr += 'Z';
|
|
139
|
+
}
|
|
140
|
+
var seconds = Math.floor(new Date(secondsStr).getTime() / 1000);
|
|
141
|
+
return "" + seconds + decimalStr;
|
|
142
|
+
}
|
|
143
|
+
var nanoDateMethods = {
|
|
144
|
+
getNanoTimeFromISO: function () {
|
|
145
|
+
if (!this._cachedNanoISO) {
|
|
146
|
+
this._cachedNanoTime = nanoIsoToTime(this._nanoISO);
|
|
147
|
+
}
|
|
148
|
+
return this._cachedNanoTime;
|
|
149
|
+
},
|
|
150
|
+
toNanoISOStringFromISO: function () {
|
|
151
|
+
if (!this._cachedNanoISO) {
|
|
152
|
+
this._cachedNanoTime = nanoIsoToTime(this._nanoISO);
|
|
153
|
+
}
|
|
154
|
+
var base = this.toISOString().slice(0, -4); // slice of `123Z` milliseconds
|
|
155
|
+
return "" + base + this._cachedNanoTime.slice(-9) + "Z";
|
|
156
|
+
},
|
|
157
|
+
getNanoTimeFromStamp: function () {
|
|
158
|
+
return this._nanoTime;
|
|
159
|
+
},
|
|
160
|
+
toNanoISOStringFromStamp: function () {
|
|
161
|
+
var base = this.toISOString().slice(0, -4); // slice of `123Z` milliseconds
|
|
162
|
+
return "" + base + this._nanoTime.slice(-9) + "Z";
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Covers a nanoseconds unix timestamp to a INanoDate for node-influx. The
|
|
167
|
+
* timestamp is provided as a string to prevent precision loss.
|
|
168
|
+
*
|
|
169
|
+
* Please see [A Moment for Times](https://node-influx.github.io/manual/
|
|
170
|
+
* usage.html#a-moment-for-times) for a more complete and eloquent explanation
|
|
171
|
+
* of time handling in this module.
|
|
172
|
+
*
|
|
173
|
+
* @param {String} timestamp
|
|
174
|
+
* @returns {INanoDate}
|
|
175
|
+
* @example
|
|
176
|
+
* const date = toNanoDate('1475985480231035600')
|
|
177
|
+
*
|
|
178
|
+
* // You can use the returned Date as a normal date:
|
|
179
|
+
* expect(date.getTime()).to.equal(1475985480231);
|
|
180
|
+
*
|
|
181
|
+
* // We decorate it with two additional methods to read
|
|
182
|
+
* // nanosecond-precision results:
|
|
183
|
+
* expect(date.getNanoTime()).to.equal('1475985480231035600');
|
|
184
|
+
* expect(date.toNanoISOString()).to.equal('2016-10-09T03:58:00.231035600Z');
|
|
185
|
+
*/
|
|
186
|
+
function toNanoDate(timestamp) {
|
|
187
|
+
var date = new Date(Math.floor(Number(timestamp) / nsPer.ms));
|
|
188
|
+
date._nanoTime = timestamp;
|
|
189
|
+
date.getNanoTime = nanoDateMethods.getNanoTimeFromStamp;
|
|
190
|
+
date.toNanoISOString = nanoDateMethods.toNanoISOStringFromStamp;
|
|
191
|
+
return date;
|
|
192
|
+
}
|
|
193
|
+
exports.toNanoDate = toNanoDate;
|
|
194
|
+
function asNanoDate(date) {
|
|
195
|
+
var d = date;
|
|
196
|
+
if (d.getNanoTime) {
|
|
197
|
+
return d;
|
|
198
|
+
}
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
var NanosecondsDateManipulator = (function () {
|
|
202
|
+
function NanosecondsDateManipulator() {
|
|
203
|
+
}
|
|
204
|
+
NanosecondsDateManipulator.prototype.format = function (date) {
|
|
205
|
+
return '"' + leftPad(date.getUTCFullYear(), 2)
|
|
206
|
+
+ '-' + leftPad(date.getUTCMonth() + 1, 2)
|
|
207
|
+
+ '-' + leftPad(date.getUTCDate(), 2)
|
|
208
|
+
+ ' ' + leftPad(date.getUTCHours(), 2)
|
|
209
|
+
+ ':' + leftPad(date.getUTCMinutes(), 2)
|
|
210
|
+
+ ':' + leftPad(date.getUTCSeconds(), 2)
|
|
211
|
+
+ '.' + date.getNanoTime().slice(-9) + '"';
|
|
212
|
+
};
|
|
213
|
+
NanosecondsDateManipulator.prototype.toTime = function (date, precision) {
|
|
214
|
+
var ms = date.getTime();
|
|
215
|
+
switch (precision) {
|
|
216
|
+
case 'u':
|
|
217
|
+
return date.getNanoTime().slice(0, -3);
|
|
218
|
+
case 'n':
|
|
219
|
+
return date.getNanoTime();
|
|
220
|
+
case 'h':
|
|
221
|
+
ms /= 60;
|
|
222
|
+
case 'm':
|
|
223
|
+
ms /= 60;
|
|
224
|
+
case 's':
|
|
225
|
+
ms /= 1000;
|
|
226
|
+
case 'ms':
|
|
227
|
+
return String(Math.floor(ms));
|
|
228
|
+
default:
|
|
229
|
+
throw new Error("Unknown precision '" + precision + "'!");
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
NanosecondsDateManipulator.prototype.isoToDate = function (timestamp) {
|
|
233
|
+
var date = new Date(timestamp);
|
|
234
|
+
date._nanoISO = timestamp;
|
|
235
|
+
date.getNanoTime = nanoDateMethods.getNanoTimeFromISO;
|
|
236
|
+
date.toNanoISOString = nanoDateMethods.toNanoISOStringFromISO;
|
|
237
|
+
return date;
|
|
238
|
+
};
|
|
239
|
+
NanosecondsDateManipulator.prototype.timetoDate = function (timestamp, precision) {
|
|
240
|
+
switch (precision) {
|
|
241
|
+
case 'h':
|
|
242
|
+
timestamp *= 60;
|
|
243
|
+
case 'm':
|
|
244
|
+
timestamp *= 60;
|
|
245
|
+
case 's':
|
|
246
|
+
timestamp *= 1000;
|
|
247
|
+
case 'ms':
|
|
248
|
+
timestamp *= 1000;
|
|
249
|
+
case 'u':
|
|
250
|
+
timestamp *= 1000;
|
|
251
|
+
case 'n':
|
|
252
|
+
var date = new Date(timestamp / nsPer.ms);
|
|
253
|
+
date._nanoTime = String(timestamp);
|
|
254
|
+
date.getNanoTime = nanoDateMethods.getNanoTimeFromStamp;
|
|
255
|
+
date.toNanoISOString = nanoDateMethods.toNanoISOStringFromStamp;
|
|
256
|
+
return date;
|
|
257
|
+
default:
|
|
258
|
+
throw new Error("Unknown precision '" + precision + "'!");
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
return NanosecondsDateManipulator;
|
|
262
|
+
}());
|
|
263
|
+
var milliManipulator = new MillisecondDateManipulator();
|
|
264
|
+
var nanoManipulator = new NanosecondsDateManipulator();
|
|
265
|
+
/**
|
|
266
|
+
* formatDate converts the Date instance to Influx's date query format.
|
|
267
|
+
* @private
|
|
268
|
+
*/
|
|
269
|
+
function formatDate(date) {
|
|
270
|
+
var nano = asNanoDate(date);
|
|
271
|
+
if (nano) {
|
|
272
|
+
return nanoManipulator.format(nano);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
return milliManipulator.format(date);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
exports.formatDate = formatDate;
|
|
279
|
+
/**
|
|
280
|
+
* Converts a Date instance to a timestamp with the specified time precision.
|
|
281
|
+
* @private
|
|
282
|
+
*/
|
|
283
|
+
function dateToTime(date, precision) {
|
|
284
|
+
var nano = asNanoDate(date);
|
|
285
|
+
if (nano) {
|
|
286
|
+
return nanoManipulator.toTime(nano, precision);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
return milliManipulator.toTime(date, precision);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
exports.dateToTime = dateToTime;
|
|
293
|
+
/**
|
|
294
|
+
* Converts an ISO-formatted data or unix timestamp to a Date instance. If
|
|
295
|
+
* the precision is finer than 'ms' the returned value will be a INanoDate.
|
|
296
|
+
* @private
|
|
297
|
+
*/
|
|
298
|
+
function isoOrTimeToDate(stamp, precision) {
|
|
299
|
+
if (precision === void 0) { precision = 'n'; }
|
|
300
|
+
if (typeof stamp === 'string') {
|
|
301
|
+
return nanoManipulator.isoToDate(stamp);
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
return nanoManipulator.timetoDate(stamp, precision);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
exports.isoOrTimeToDate = isoOrTimeToDate;
|
|
308
|
+
/**
|
|
309
|
+
* Converts a timestamp to a string with the correct precision. Assumes
|
|
310
|
+
* that raw number and string instances are already in the correct precision.
|
|
311
|
+
* @private
|
|
312
|
+
*/
|
|
313
|
+
function castTimestamp(timestamp, precision) {
|
|
314
|
+
if (typeof timestamp === 'string') {
|
|
315
|
+
if (!ds_1.isNumeric(timestamp)) {
|
|
316
|
+
throw new Error("Expected numeric value for, timestamp, but got '" + timestamp + "'!");
|
|
317
|
+
}
|
|
318
|
+
return timestamp;
|
|
319
|
+
}
|
|
320
|
+
if (typeof timestamp === 'number') {
|
|
321
|
+
return String(timestamp);
|
|
322
|
+
}
|
|
323
|
+
return dateToTime(timestamp, precision);
|
|
324
|
+
}
|
|
325
|
+
exports.castTimestamp = castTimestamp;
|