UNPKG

redis-time-series-ts

Version:
57 lines (56 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Aggregation = void 0; const aggregationType_1 = require("../enum/aggregationType"); const commandKeyword_1 = require("../enum/commandKeyword"); /** * A `aggregation-type`-`timeBucket` object */ class Aggregation { /** * Creates a new Aggregation object * * @param type The type of aggregation e.g. AggregationType.AVG | AggregationType.SUM etc * @param timeBucketInMs The time bucket for the aggregation * * @remarks * // Example * ``` * const aggregation = new Aggregation(AggregationType.AVG, 1000); * const timestampRange = new TimestampRange(date, date + 10000); * const samples = await redisTimeSeries.range("range1", timestampRange, undefined, aggregation); * * aggregation.getType() // avg * aggregation.getTimeBucketInMs() // 1000 * aggregation.flatten() // ['AGGREGATION', 'avg', 1000] * ``` */ constructor(type, timeBucketInMs) { this.type = this.validateType(type); this.timeBucketInMs = this.validateTimeBucket(timeBucketInMs); } getType() { return this.type; } getTimeBucketInMs() { return this.timeBucketInMs; } flatten() { return [commandKeyword_1.CommandKeyword.AGGREGATION, this.getType(), this.getTimeBucketInMs()]; } validateType(type) { const keyEnum = Object.keys(aggregationType_1.AggregationType).find(key => aggregationType_1.AggregationType[key] === type.toLowerCase()); if (keyEnum == null) { throw new Error(`aggregation type '${type}' not found`); } return aggregationType_1.AggregationType[keyEnum]; } validateTimeBucket(timeBucketInMs) { const truncatedTimeBucket = Math.trunc(timeBucketInMs); if (truncatedTimeBucket <= 0) { throw new Error(`invalid timestamp '${timeBucketInMs}'`); } return truncatedTimeBucket; } } exports.Aggregation = Aggregation;