@opendash/plugin-openware
Version:
open.WARE Plugin for open.DASH
313 lines • 15.8 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
import { equals, } from "opendash";
var DataAdapter = /** @class */ (function () {
function DataAdapter(config) {
this.host = "localhost:4567";
this.secure = "";
this.websocket = null;
this.loaded = false;
this.tags = [];
this.allowedIds = new Set();
this.host = config.host || this.host;
this.secure = config.secure ? "s" : "";
}
DataAdapter.prototype.onContext = function (context) {
this.context = context;
};
DataAdapter.prototype.onUser = function (user) {
if (!equals(this.user, user)) {
this.user = user;
this.init();
}
};
DataAdapter.prototype.onSource = function (source, descendents) {
var tags = __spreadArrays([source.tag], descendents.map(function (src) { return src.tag; })).filter(Boolean)
.filter(function (v, i, a) { return a.indexOf(v) === i; });
if (!equals(tags, this.tags)) {
this.tags = tags;
this.init();
}
};
DataAdapter.prototype.fetchValues = function (item, options) {
if (options === void 0) { options = {}; }
return __awaiter(this, void 0, void 0, function () {
var id, source, start, end, url, data, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
id = encodeURIComponent(item.id);
source = encodeURIComponent(item.source);
start = options.start || 0;
end = options.end || Date.now();
url = "/api/data/historical/" + source + "/" + id + "/" + start + "/" + end;
return [4 /*yield*/, this.fetch(url)];
case 1:
data = _a.sent();
if (!Array.isArray(data.values)) {
throw new Error("response.data.values must be an Array");
}
return [2 /*return*/, data.values];
case 2:
error_1 = _a.sent();
return [2 /*return*/, []];
case 3: return [2 /*return*/];
}
});
});
};
DataAdapter.prototype.fetchDimensionValues = function (item, dimension, options) {
if (options === void 0) { options = {}; }
var _a, _b, _c, _d, _e, _f, _g, _h;
return __awaiter(this, void 0, void 0, function () {
var id, source, start, end, qs, url, data, url, data, error_2;
return __generator(this, function (_j) {
switch (_j.label) {
case 0:
_j.trys.push([0, 5, , 6]);
id = encodeURIComponent(item.id);
source = encodeURIComponent(item.source);
start = options.start || 0;
end = options.end || Date.now();
if (!(((_a = options) === null || _a === void 0 ? void 0 : _a.aggregationType) && ((_b = options) === null || _b === void 0 ? void 0 : _b.aggregationSplits))) return [3 /*break*/, 2];
qs = new URLSearchParams({
dimension: dimension.toString(),
splits: (_c = options) === null || _c === void 0 ? void 0 : _c.aggregationSplits.toString(),
operation: (_d = options) === null || _d === void 0 ? void 0 : _d.aggregationType,
});
url = "/api/transform/aggregation/" + source + "/" + id + "/" + start + "/" + end + "?" + qs.toString();
return [4 /*yield*/, this.fetch(url)];
case 1:
data = _j.sent();
console.log(data);
if (!Array.isArray((_f = (_e = data) === null || _e === void 0 ? void 0 : _e.result) === null || _f === void 0 ? void 0 : _f.values)) {
throw new Error("response.data.result.values must be an Array");
}
return [2 /*return*/, (_h = (_g = data) === null || _g === void 0 ? void 0 : _g.result) === null || _h === void 0 ? void 0 : _h.values.map(function (x) { return ({
date: x.date,
value: x.value[0],
}); })];
case 2:
url = "/api/data/historical/" + source + "/" + id + "/" + start + "/" + end;
return [4 /*yield*/, this.fetch(url)];
case 3:
data = _j.sent();
if (!Array.isArray(data.values)) {
throw new Error("response.data.values must be an Array");
}
return [2 /*return*/, data.values.map(function (x) { return ({
date: x.date,
value: x.value[dimension],
}); })];
case 4: return [3 /*break*/, 6];
case 5:
error_2 = _j.sent();
console.error(error_2);
return [2 /*return*/, []];
case 6: return [2 /*return*/];
}
});
});
};
DataAdapter.prototype.update = function (item) {
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); });
};
DataAdapter.prototype.init = function () {
return __awaiter(this, void 0, void 0, function () {
var _i, _a, tag, url, data, error_3;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_b.trys.push([0, 7, , 8]);
if (!this.context || !this.user || this.tags.length === 0) {
return [2 /*return*/];
}
if (this.loaded) {
this.context.reset();
this.allowedIds.clear();
}
else {
this.loaded = true;
}
_i = 0, _a = this.tags;
_b.label = 1;
case 1:
if (!(_i < _a.length)) return [3 /*break*/, 5];
tag = _a[_i];
url = "/api/data/items/" + this.user.username + "/" + tag;
return [4 /*yield*/, this.fetch(url)];
case 2:
data = _b.sent();
return [4 /*yield*/, this.handleItems(data)];
case 3:
_b.sent();
_b.label = 4;
case 4:
_i++;
return [3 /*break*/, 1];
case 5:
this.context.setLoading(false);
return [4 /*yield*/, this.initWebsocket()];
case 6:
_b.sent();
return [3 /*break*/, 8];
case 7:
error_3 = _b.sent();
console.error(error_3);
return [3 /*break*/, 8];
case 8: return [2 /*return*/];
}
});
});
};
DataAdapter.prototype.initWebsocket = function (reconnect) {
if (reconnect === void 0) { reconnect = false; }
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
if (this.websocket) {
this.websocket.close();
}
this.websocket = new WebSocket("ws" + this.secure + "://" + this.host + "/subscription");
this.websocket.onopen = function () {
_this.websocket.send(JSON.stringify({
action: "subscribe",
session: _this.user.session,
sources: _this.tags,
}));
_this.websocket.onmessage = function (msg) {
try {
var message = JSON.parse(msg.data);
if (_this.tags.includes(message.user)) {
_this.handleItems([message]).then(function (ok) {
console.info("ws -> data");
}, function (error) {
console.error("ws -> data error:", error);
});
}
}
catch (e) {
console.error(e);
}
};
_this.websocket.onclose = function () { };
};
window.addEventListener("focus", function (e) {
if (!(_this.websocket.readyState === _this.websocket.OPEN ||
_this.websocket.readyState === _this.websocket.CONNECTING)) {
_this.initWebsocket(true);
}
});
return [2 /*return*/];
});
});
};
DataAdapter.prototype.handleItems = function (data) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function () {
var items, _i, data_1, item;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
items = data
.map(function (item) {
for (var _i = 0, _a = item.valueTypes; _i < _a.length; _i++) {
var typeItem = _a[_i];
typeItem.name = typeItem.name || "no name";
}
if (item.meta.visible === false) {
return null;
}
return {
id: item.id,
name: item.name || "no name",
source: item.user,
valueTypes: item.valueTypes,
meta: item.meta,
};
})
.filter(function (item) { return item !== null; });
return [4 /*yield*/, this.context.setItems(items)];
case 1:
_d.sent();
for (_i = 0, data_1 = data; _i < data_1.length; _i++) {
item = data_1[_i];
if (((_a = item) === null || _a === void 0 ? void 0 : _a.id) && ((_b = item) === null || _b === void 0 ? void 0 : _b.user) && ((_c = item.values) === null || _c === void 0 ? void 0 : _c[0])) {
this.context.setValue(
// @ts-ignore
{ source: item.user, id: item.id }, item.values[0]);
}
}
return [2 /*return*/];
}
});
});
};
DataAdapter.prototype.fetch = function (url) {
return __awaiter(this, void 0, void 0, function () {
var response, data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, fetch("http" + this.secure + "://" + this.host + url, {
headers: {
"OD-SESSION": this.user.session,
},
})];
case 1:
response = _a.sent();
return [4 /*yield*/, response.json()];
case 2:
data = _a.sent();
return [2 /*return*/, data];
}
});
});
};
return DataAdapter;
}());
export { DataAdapter };
//# sourceMappingURL=DataAdapter.js.map