UNPKG

redis-time-series-ts

Version:
75 lines (74 loc) 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sample = void 0; const commandKeyword_1 = require("../enum/commandKeyword"); /** * A `key`-`value`-`timestamp` object */ class Sample { /** * Creates a Sample object * * @param key Key name for timeseries * @param value The value of the sample * @param timestamp The timestamp of the sample in ms * @returns the created Sample object * * @remarks * ``` * // Example * const tsSample = new Sample('temperature:2:32', 25, 1609682633000); * * tsSample.getKey(); // 'temperature:2:32' * tsSample.setKey('temperature:5:15'); // Sample('temperature:5:15', 25, 1609682633000) * tsSample.getValue(); // 25 * tsSample.setValue(27); // Sample('temperature:5:15', 27, 1609682633000) * tsSample.getTimestamp(); // 1609682633000 * tsSample.setTimestamp(1609682634000); // Sample('temperature:5:15', 27, 1609682634000) * tsSample.flatten(); // ['temperature:5:15', 1609682634000, 27] * ``` */ constructor(key, value, timestamp) { this.setKey(key); this.setValue(value); this.setTimestamp(timestamp); } getKey() { return this.key; } setKey(key) { this.key = key; return this; } getValue() { return this.value; } setValue(value) { this.value = value; return this; } getTimestamp() { return this.timestamp; } setTimestamp(timestamp) { this.timestamp = this.validateTimestamp(timestamp); return this; } flatten() { return [this.getKey(), this.getTimestamp(), this.getValue()]; } validateTimestamp(timestamp) { if (timestamp == null) { return commandKeyword_1.CommandKeyword.CURRENT_TIMESTAMP; } timestamp = Math.trunc(timestamp); if (this.isValidTimestamp(timestamp)) { return timestamp; } throw new Error(`wrong timestamp: ${timestamp}`); } isValidTimestamp(timestamp) { return new Date(timestamp).getTime() >= 0; } } exports.Sample = Sample;