UNPKG

@hpcc-js/comms

Version:
425 lines 18.4 kB
import { __assign, __awaiter, __extends, __generator } from "tslib"; import { Cache, StateObject, scopedLogger } from "@hpcc-js/util"; import { format as d3Format } from "d3-format"; import { EclService } from "../services/wsEcl"; import { WorkunitsService } from "../services/wsWorkunits"; import { Topology } from "./topology"; import { Workunit } from "./workunit"; import { QueryGraph } from "./queryGraph"; export { QueryGraph }; var logger = scopedLogger("@hpcc-js/comms/ecl/query.ts"); var siFormatter = d3Format("~s"); function isNumber(n) { return !isNaN(parseFloat(n)) && !isNaN(n - 0); } var QueryCache = /** @class */ (function (_super) { __extends(QueryCache, _super); function QueryCache() { return _super.call(this, function (obj) { return Cache.hash([obj.QueryId, obj.QuerySet]); }) || this; } return QueryCache; }(Cache)); var _queries = new QueryCache(); var Query = /** @class */ (function (_super) { __extends(Query, _super); function Query(optsConnection, querySet, queryID, queryDetails) { var _this = _super.call(this) || this; if (optsConnection instanceof WorkunitsService) { _this.wsWorkunitsService = optsConnection; } else { _this.wsWorkunitsService = new WorkunitsService(optsConnection); } _this.topology = Topology.attach(_this.wsWorkunitsService.opts()); _this.set(__assign({ QuerySet: querySet, QueryId: queryID }, queryDetails)); return _this; } Object.defineProperty(Query.prototype, "BaseUrl", { get: function () { return this.wsWorkunitsService.baseUrl; }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "properties", { get: function () { return this.get(); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Exceptions", { get: function () { return this.get("Exceptions"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "QueryId", { get: function () { return this.get("QueryId"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "QuerySet", { get: function () { return this.get("QuerySet"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "QueryName", { get: function () { return this.get("QueryName"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Wuid", { get: function () { return this.get("Wuid"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Dll", { get: function () { return this.get("Dll"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Suspended", { get: function () { return this.get("Suspended"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Activated", { get: function () { return this.get("Activated"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "SuspendedBy", { get: function () { return this.get("SuspendedBy"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Clusters", { get: function () { return this.get("Clusters"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "PublishedBy", { get: function () { return this.get("PublishedBy"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Comment", { get: function () { return this.get("Comment"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "LogicalFiles", { get: function () { return this.get("LogicalFiles"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "SuperFiles", { get: function () { return this.get("SuperFiles"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "IsLibrary", { get: function () { return this.get("IsLibrary"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "Priority", { get: function () { return this.get("Priority"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "WUSnapShot", { get: function () { return this.get("WUSnapShot"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "CompileTime", { get: function () { return this.get("CompileTime"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "LibrariesUsed", { get: function () { return this.get("LibrariesUsed"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "CountGraphs", { get: function () { return this.get("CountGraphs"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "ResourceURLCount", { get: function () { return this.get("ResourceURLCount"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "WsEclAddresses", { get: function () { return this.get("WsEclAddresses"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "WUGraphs", { get: function () { return this.get("WUGraphs"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "WUTimers", { get: function () { return this.get("WUTimers"); }, enumerable: false, configurable: true }); Object.defineProperty(Query.prototype, "PriorityID", { get: function () { return this.get("PriorityID"); }, enumerable: false, configurable: true }); Query.attach = function (optsConnection, querySet, queryId, state) { var retVal = _queries.get({ BaseUrl: optsConnection.baseUrl, QuerySet: querySet, QueryId: queryId }, function () { return new Query(optsConnection, querySet, queryId); }); if (state) { retVal.set(state); } return retVal; }; Query.prototype.wsEclService = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { if (!this._eclService) { this._eclService = this.topology.fetchServices({}).then(function (services) { var _a, _b, _c, _d; for (var _i = 0, _e = (_b = (_a = services === null || services === void 0 ? void 0 : services.TpEspServers) === null || _a === void 0 ? void 0 : _a.TpEspServer) !== null && _b !== void 0 ? _b : []; _i < _e.length; _i++) { var espServer = _e[_i]; for (var _f = 0, _g = (_d = (_c = espServer === null || espServer === void 0 ? void 0 : espServer.TpBindings) === null || _c === void 0 ? void 0 : _c.TpBinding) !== null && _d !== void 0 ? _d : []; _f < _g.length; _f++) { var binding = _g[_f]; if ((binding === null || binding === void 0 ? void 0 : binding.Service) === "ws_ecl") { var baseUrl = "".concat(binding.Protocol, "://").concat(globalThis.location.hostname, ":").concat(binding.Port); return new EclService({ baseUrl: baseUrl }); } } } return undefined; }); } return [2 /*return*/, this._eclService]; }); }); }; Query.prototype.fetchDetails = function () { return __awaiter(this, void 0, void 0, function () { var queryDetails; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.wsWorkunitsService.WUQueryDetails({ QuerySet: this.QuerySet, QueryId: this.QueryId, IncludeStateOnClusters: true, IncludeSuperFiles: true, IncludeWsEclAddresses: true, CheckAllNodes: false })]; case 1: queryDetails = _a.sent(); this.set(__assign({}, queryDetails)); return [2 /*return*/]; } }); }); }; Query.prototype.fetchRequestSchema = function () { return __awaiter(this, void 0, void 0, function () { var wsEclService, _a, e_1; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.wsEclService()]; case 1: wsEclService = _c.sent(); _c.label = 2; case 2: _c.trys.push([2, 4, , 5]); _a = this; return [4 /*yield*/, (wsEclService === null || wsEclService === void 0 ? void 0 : wsEclService.requestJson(this.QuerySet, this.QueryId))]; case 3: _a._requestSchema = (_b = _c.sent()) !== null && _b !== void 0 ? _b : []; return [3 /*break*/, 5]; case 4: e_1 = _c.sent(); // See: https://track.hpccsystems.com/browse/HPCC-29827 logger.debug(e_1); this._requestSchema = []; return [3 /*break*/, 5]; case 5: return [2 /*return*/]; } }); }); }; Query.prototype.fetchResponseSchema = function () { return __awaiter(this, void 0, void 0, function () { var wsEclService, _a, e_2; var _b; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.wsEclService()]; case 1: wsEclService = _c.sent(); _c.label = 2; case 2: _c.trys.push([2, 4, , 5]); _a = this; return [4 /*yield*/, (wsEclService === null || wsEclService === void 0 ? void 0 : wsEclService.responseJson(this.QuerySet, this.QueryId))]; case 3: _a._responseSchema = (_b = _c.sent()) !== null && _b !== void 0 ? _b : {}; return [3 /*break*/, 5]; case 4: e_2 = _c.sent(); // See: https://track.hpccsystems.com/browse/HPCC-29827 logger.debug(e_2); this._responseSchema = {}; return [3 /*break*/, 5]; case 5: return [2 /*return*/]; } }); }); }; Query.prototype.fetchSchema = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, Promise.all([this.fetchRequestSchema(), this.fetchResponseSchema()])]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Query.prototype.fetchSummaryStats = function () { return this.wsWorkunitsService.WUQueryGetSummaryStats({ Target: this.QuerySet, QueryId: this.QueryId }); }; Query.prototype.fetchGraph = function (GraphName, SubGraphId) { if (GraphName === void 0) { GraphName = ""; } if (SubGraphId === void 0) { SubGraphId = ""; } return this.wsWorkunitsService.WUQueryGetGraph({ Target: this.QuerySet, QueryId: this.QueryId, GraphName: GraphName, SubGraphId: SubGraphId }).then(function (response) { var _a; var graph = new QueryGraph(); var first = true; for (var _i = 0, _b = ((_a = response === null || response === void 0 ? void 0 : response.Graphs) === null || _a === void 0 ? void 0 : _a.ECLGraphEx) || []; _i < _b.length; _i++) { var graphItem = _b[_i]; if (first) { graph.load(graphItem.Graph); first = false; } else { graph.merge(graphItem.Graph); } } return graph; }); }; Query.prototype.fetchDetailsNormalized = function (request) { if (request === void 0) { request = {}; } var wu = Workunit.attach(this.wsWorkunitsService, this.Wuid); if (wu) { return Promise.all([this.fetchGraph(), wu.fetchDetailsMeta(), wu.fetchDetailsRaw(request)]).then(function (promises) { var graph = promises[0]; var meta = promises[1]; var metrics = promises[2]; var data = metrics.map(function (metric) { if (metric.Id[0] === "a" || metric.Id[0] === "e") { var item = graph.idx[metric.Id.substring(1)]; var _loop_1 = function (key) { if (key.charAt(0) !== "_" && key.charAt(0) === key.charAt(0).toUpperCase() && (typeof item[key] === "string" || typeof item[key] === "number" || typeof item[key] === "boolean")) { if (!metric.Properties.Property.some(function (row) { return row.Name === key; })) { var isNum = isNumber(item[key]); var rawValue = isNum ? parseFloat(item[key]) : item[key]; var formatted = item[key]; if (key.indexOf("Time") >= 0) { rawValue = rawValue / 1000000000; formatted = siFormatter(rawValue) + "s"; } metric.Properties.Property.push({ Name: key, RawValue: rawValue, Formatted: formatted }); } } }; for (var key in item) { _loop_1(key); } } return metric; }); return wu.normalizeDetails(meta, data); }); } return Promise.resolve({ meta: undefined, columns: undefined, data: undefined }); }; Query.prototype.submit = function (request) { return __awaiter(this, void 0, void 0, function () { var wsEclService; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, this.wsEclService()]; case 1: wsEclService = _b.sent(); try { return [2 /*return*/, (_a = wsEclService === null || wsEclService === void 0 ? void 0 : wsEclService.submit(this.QuerySet, this.QueryId, request).then(function (results) { for (var key in results) { results[key] = results[key].Row; } return results; })) !== null && _a !== void 0 ? _a : []]; } catch (e) { // See: https://track.hpccsystems.com/browse/HPCC-29827 logger.debug(e); return [2 /*return*/, []]; } return [2 /*return*/]; } }); }); }; Query.prototype.refresh = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, Promise.all([ this.fetchDetails(), this.fetchSchema() ])]; case 1: _a.sent(); return [2 /*return*/, this]; } }); }); }; Query.prototype.requestFields = function () { if (!this._requestSchema) return []; return this._requestSchema; }; Query.prototype.responseFields = function () { if (!this._responseSchema) return {}; return this._responseSchema; }; Query.prototype.resultNames = function () { var retVal = []; for (var key in this.responseFields()) { retVal.push(key); } return retVal; }; Query.prototype.resultFields = function (resultName) { if (!this._responseSchema[resultName]) return []; return this._responseSchema[resultName]; }; return Query; }(StateObject)); export { Query }; //# sourceMappingURL=query.js.map