redis-time-series-ts
Version:
Javascript RedisTimeSeries client
58 lines (57 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TimestampRange = void 0;
const commandKeyword_1 = require("../enum/commandKeyword");
/**
* A `from` - `to` timestamp object
*/
class TimestampRange {
/**
* Creates a timestamp range object
*
* @param from timestamp to begin from (milliseconds)
* @param to timestamp to end at (milliseconds)
*
* @remarks
* ```
* // Example
* const oneDayAgo = new Date().getTime() - 24 * 3600 * 1000
* const now = new Date().getTime()
* const tsRange = new TimestampRange(oneDayAgo, now)
* tsRange.getFrom() // oneDayAgo
* tsRange.getTo() // now
* tsRange.setFrom(oneDayAgo - 24 * 3600 * 1000) // twoDaysAgo
* tsRange.getTo(oneDayAgo) // oneDayAgo
* tsRange.flatten() // [twoDaysAgo, oneDayAgo]
* ```
*/
constructor(from, to) {
this.setFrom(from);
this.setTo(to);
}
getFrom() {
return this.from;
}
setFrom(from) {
this.from = from == null ? commandKeyword_1.CommandKeyword.MIN_TIMESTAMP : this.validateTimestamp(from);
return this;
}
getTo() {
return this.to;
}
setTo(to) {
this.to = to == null ? commandKeyword_1.CommandKeyword.MAX_TIMESTAMP : this.validateTimestamp(to);
return this;
}
flatten() {
return [this.getFrom(), this.getTo()];
}
validateTimestamp(timestamp) {
timestamp = Math.trunc(timestamp);
if (new Date(timestamp).getTime() >= 0) {
return timestamp;
}
throw new Error(`invalid timestamp ${timestamp}`);
}
}
exports.TimestampRange = TimestampRange;