UNPKG

diffusion

Version:

Diffusion JavaScript client

285 lines (284 loc) 9.89 kB
"use strict"; /** * @module Timeseries */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RangeQueryImpl = void 0; var error_reason_1 = require("./../../../errors/error-reason"); var errors_1 = require("./../../../errors/errors"); var require_non_null_1 = require("./../../../internal/util/require-non-null"); var bytes_impl_1 = require("./../../data/bytes-impl"); var range_1 = require("./../../features/time-series/range"); var range_query_parameters_1 = require("./../../features/time-series/range-query-parameters"); var response_success_1 = require("./../../util/response-success"); /** * Create range query parameters with a modified view range * * @param original the original range query parameters * @param op a modifier that is used to modify the {@link * RangeQueryParameters.viewRange} * @return new range query parameters */ function VIEW_RANGE(original, op) { return original.withViewRange(op(original.viewRange)); } /** * Create range query parameters with a modified edit range * * @param original the original range query parameters * @param op a modifier that is used to modify the {@link * RangeQueryParameters.editRange} * @return new range query parameters */ function EDIT_RANGE(original, op) { return original.withEditRange(op(original.editRange)); } /** * Implementation of the {@link RangeQuery} interface */ var RangeQueryImpl = /** @class */ (function () { /** * Create a new RangeQueryImpl instance * * @param context the command service for sending the range query to the server * @param valueType the data value type * @param parameters the range query parameters * @param mode the current mode of the range query */ function RangeQueryImpl(context, valueType, parameters, mode) { this.context = context; this.valueType = valueType; this.parameters = parameters; this.mode = mode; } /** * Create a default range query * * @param context the command service for sending the range query to the server * @return a range query with default parameters */ RangeQueryImpl.createDefault = function (context) { return new RangeQueryImpl(context, bytes_impl_1.BytesImpl, range_query_parameters_1.DEFAULT_RANGE_QUERY, VIEW_RANGE); }; /** * Create a new range query with a modified range * * The current {@link mode} determined whether the view range or edit range * are modified. * * @param op the operator that will modify the range * @return a new RangeQuery with a modified range */ RangeQueryImpl.prototype.withCurrentRange = function (op) { return new RangeQueryImpl(this.context, this.valueType, this.mode(this.parameters, op), this.mode); }; /** * @inheritdoc */ RangeQueryImpl.prototype.selectFrom = function (topicPath) { var _this = this; return new Promise(function (resolve, reject) { if (!topicPath) { reject(new Error('Topic path is null')); return; } var request = { topicPath: topicPath, parameters: _this.parameters }; _this.context.send(request, function (err, response) { if (!response_success_1.responseSuccess(err, response)) { reject(err); } else { try { resolve(response.createQueryResult(_this.valueType, _this.parameters.queryType.streamStructure)); } catch (err) { reject(error_reason_1.ErrorReason.INVALID_DATA); } } }); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.forValues = function () { return new RangeQueryImpl(this.context, this.valueType, this.parameters.withQueryType(range_query_parameters_1.QueryTypes.VALUES).withViewRange(range_1.DEFAULT_RANGE), VIEW_RANGE); }; /** * @inheritdoc */ RangeQueryImpl.prototype.forEdits = function () { return new RangeQueryImpl(this.context, this.valueType, this.parameters.withQueryType(range_query_parameters_1.QueryTypes.ALL_EDITS).withViewRange(range_1.DEFAULT_RANGE), VIEW_RANGE); }; /** * @inheritdoc */ RangeQueryImpl.prototype.editRange = function () { if (this.parameters.queryType !== range_query_parameters_1.QueryTypes.VALUES) { throw new errors_1.IllegalStateError('.editRange() cannot be applied to this edit range query'); } return new RangeQueryImpl(this.context, this.valueType, this.parameters, EDIT_RANGE); }; /** * @inheritdoc */ RangeQueryImpl.prototype.allEdits = function () { if (this.parameters.queryType === range_query_parameters_1.QueryTypes.VALUES) { throw new errors_1.IllegalStateError('.allEdits() cannot be applied to this value range query'); } return new RangeQueryImpl(this.context, this.valueType, this.parameters.withQueryType(range_query_parameters_1.QueryTypes.ALL_EDITS), EDIT_RANGE); }; /** * @inheritdoc */ RangeQueryImpl.prototype.latestEdits = function () { if (this.parameters.queryType === range_query_parameters_1.QueryTypes.VALUES) { throw new errors_1.IllegalStateError('.latestEdits() cannot be applied to this value range query'); } return new RangeQueryImpl(this.context, this.valueType, this.parameters.withQueryType(range_query_parameters_1.QueryTypes.LATEST_EDITS), EDIT_RANGE); }; /** * @inheritdoc */ RangeQueryImpl.prototype.limit = function (count) { return new RangeQueryImpl(this.context, this.valueType, this.parameters.withLimit(count), this.mode); }; /** * @inheritdoc */ RangeQueryImpl.prototype.as = function (newValueType) { require_non_null_1.requireNonNull(newValueType, 'Value type'); // allow datatypes to be provided if (newValueType && newValueType.valueClass) { return new RangeQueryImpl(this.context, newValueType.valueClass, this.parameters, this.mode); } else if (newValueType instanceof Function) { return new RangeQueryImpl(this.context, newValueType, this.parameters, this.mode); } else { throw new errors_1.IllegalArgumentError("Invalid value type: " + newValueType); } }; /** * @inheritdoc */ RangeQueryImpl.prototype.from = function (sequence) { return this.withCurrentRange(function (r) { return r.from(sequence); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.fromStart = function () { return this.withCurrentRange(function (r) { return r.fromStart(); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.fromLast = function (count) { return this.withCurrentRange(function (r) { return r.fromLast(count); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.fromLastMillis = function (timespan) { return this.withCurrentRange(function (r) { return r.fromLastMillis(timespan); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.to = function (sequence) { return this.withCurrentRange(function (r) { return r.to(sequence); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.toStart = function () { return this.withCurrentRange(function (r) { return r.toStart(); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.untilLast = function (count) { return this.withCurrentRange(function (r) { return r.untilLast(count); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.untilLastMillis = function (timespan) { return this.withCurrentRange(function (r) { return r.untilLastMillis(timespan); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.next = function (count) { return this.withCurrentRange(function (r) { return r.next(count); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.nextMillis = function (timespan) { return this.withCurrentRange(function (r) { return r.nextMillis(timespan); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.previous = function (count) { return this.withCurrentRange(function (r) { return r.previous(count); }); }; /** * @inheritdoc */ RangeQueryImpl.prototype.previousMillis = function (timespan) { return this.withCurrentRange(function (r) { return r.previousMillis(timespan); }); }; /** * Check if the range query is equal to another object * * @param other the other object * @return `true` if the other object is a `RangeQueryImpl` and the * parameters are equal */ RangeQueryImpl.prototype.equals = function (other) { if (other && other instanceof RangeQueryImpl) { return this.parameters.equals(other.parameters); } return false; }; /** * Convert the range query to a string * * @return a string representation of the parameters */ RangeQueryImpl.prototype.toString = function () { return this.parameters.toString(); }; return RangeQueryImpl; }()); exports.RangeQueryImpl = RangeQueryImpl;