Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.24 KB | None | 0 0
  1. import { createConnection } from "mysql";
  2. import { Utils } from "tyx";
  3. import fs = require("fs");
  4. import * as _ from "lodash";
  5.  
  6. const DBS = "dbadmin:gUYZ6anpLEF3gksC@dev.db.n5-nederman.com:3306/iot_dev";
  7. let sensors = [];
  8.  
  9. main().then(() => console.log("done"));
  10.  
  11. const TO_CONVERT = []; // ["label", "onoff"];
  12.  
  13. async function main() {
  14. let tokens = DBS.split(/:|@|\/|;/);
  15. let db = createConnection({
  16. user: tokens[0],
  17. password: tokens[1],
  18. host: tokens[2],
  19. port: parseInt(tokens[3]),
  20. database: tokens[4]
  21. });
  22.  
  23. let dashboardFilter = [] ||
  24. [
  25. "0271d26d-fa5c-481c-aeab-ea72e1ca7e62"
  26. // "3139ad2d-b080-4459-874c-f406d71b5682",
  27. // "6a180455-75e0-4d48-beb4-8dc815ed2ccd",
  28. // "26895f91-c75d-48b2-b2ac-5321aff8b4aa",
  29.  
  30. ];
  31.  
  32. let data = await new Promise<any>((resolve, reject) => {
  33. db.query(`SELECT * FROM dashboard`
  34. + (dashboardFilter.length ? ` WHERE dashboardId IN ('${dashboardFilter.join("','")}')` : ""), (err, res) => {
  35. if (err) reject(err); else resolve(res);
  36. });
  37. });
  38.  
  39. let res: any = { layout: {}, widget: {} };
  40. for (let dash of data) {
  41. console.log("dash.sensors", dash.sensors);
  42. sensors = dash.sensors;
  43. try { JSON.parse(dash.definition); }
  44. catch (err) { console.error(dash.dashboardId); }
  45.  
  46. let json: any;
  47. try { json = JSON.parse(dash.definition); }
  48. catch (err) {
  49. console.error(err);
  50. continue;
  51. }
  52.  
  53. // if (json.version) throw new TypeError("New dashboard format");
  54. fs.writeFileSync(`src/demo/dashboard/org/old_${dash.dashboardId}.json`, JSON.stringify(json, null, 2));
  55.  
  56. // /* set the new structure of the dashboard */
  57. // let allSensors = {};
  58. // visit(0, json, res, allSensors);
  59. // json = convertLayout(json);
  60.  
  61. // /* set the sensors attached in the widgets in binding properties */
  62. // sensors = [];
  63. // getSensors(json);
  64. // json["binding"] = sensors;
  65.  
  66. // /* update the old widgets */
  67. // json = updateOldWidgets(json);
  68. /* update the units on widgets */
  69. // json = updateUnits(json);
  70. json = updateLayout(json);
  71.  
  72. json.type = "dashboard";
  73. json.version = 2;
  74. fs.writeFileSync(`src/demo/dashboard/data/new_${dash.dashboardId}.json`, JSON.stringify(json, null, 2));
  75. // try {
  76. // await new Promise<any>((resolve, reject) => {
  77. // db.query(`UPDATE dashboard SET definition = ? WHERE dashboardId = ?`, [JSON.stringify(json), dash.dashboardId], (err, data) => {
  78. // if (err) reject(err); else resolve(res);
  79. // });
  80. // });
  81. // console.log(`Updated: [${dash.dashboardId}]`);
  82. // } catch (err) {
  83. // console.error(err);
  84. // }
  85. }
  86. // res.layoutKeys = summ(res.layout);
  87. // res.widgetKeys = summ(res.widget);
  88. // console.log(res);
  89.  
  90. fs.writeFileSync("src/demo/dashboards.json", JSON.stringify(res, null, 2));
  91. db.destroy();
  92. }
  93.  
  94. function updateLayout(definition) {
  95.  
  96.  
  97. if (!definition) { return null; }
  98. for (let i = 0; i < _.get(definition, "children", definition).length; i++) {
  99.  
  100. /* if there are children in the element, check them */
  101. if (definition.children) {
  102.  
  103. /* if element has other children */
  104. if (!_.isEmpty(definition.children[i].children)) {
  105. updateLayout(definition.children[i]);
  106. }
  107. }
  108.  
  109. if (definition.children[i]["config"]) {
  110. if (definition.children[i]["component"] !== "multiLineChart") {
  111. // if (definition.children[i]["layout"].name) {
  112. console.log("sensors", sensors);
  113. definition.children[i]["layout"].label = sensors.find(sensor => sensor.sensorId === definition.children[i]["binding"].sensorId).label;
  114. delete definition.children[i]["layout"].name;
  115. // }
  116. }
  117. }
  118. }
  119. return definition;
  120. }
  121.  
  122. function updateUnits(definition) {
  123.  
  124. if (!definition) { return null; }
  125. for (let i = 0; i < _.get(definition, "children", definition).length; i++) {
  126.  
  127. /* if there are children in the element, check them */
  128. if (definition.children) {
  129.  
  130. /* if element has other children */
  131. if (!_.isEmpty(definition.children[i].children)) {
  132. updateUnits(definition.children[i]);
  133. }
  134. }
  135.  
  136. if (definition.children[i]["config"]) {
  137. if (definition.children[i]["component"] !== "multiLineChart") {
  138. if (definition.children[i]["config"].unit && definition.children[i]["config"].unit !== " ") {
  139. definition.children[i]["config"].unit = definition.children[i]["config"].unit.trim();
  140. }
  141. } else {
  142. if (definition.children[i]["config"].sensors) {
  143. definition.children[i]["config"].sensors.map(sensor => {
  144. if (sensor.config && sensor.config.unit !== " ") {
  145. sensor.config.unit = sensor.config.unit.trim();
  146. }
  147. });
  148. }
  149. }
  150. }
  151. }
  152. return definition;
  153. }
  154.  
  155. function updateOldWidgets(definition) {
  156. if (!definition) { return null; }
  157. for (let i = 0; i < _.get(definition, "children", definition).length; i++) {
  158.  
  159. /* if there are children in the element, check them */
  160. if (definition.children) {
  161.  
  162. /* if element has other children */
  163. if (!_.isEmpty(definition.children[i].children))
  164. updateOldWidgets(definition.children[i]);
  165. }
  166.  
  167. /* if widget is old one, set the default metadata to false and set modified = config */
  168. if (definition.children[i]["type"] === "widget" && !definition.children[i]["layout"]["defaultMetadata"]) {
  169. definition.children[i]["layout"]["defaultMetadata"] = "false";
  170. definition.children[i]["modified"] = definition.children[i]["config"];
  171. }
  172.  
  173. /* if widget is old one, set the name and description in config and set modified = config */
  174. if (definition.children[i]["config"] && !definition.children[i]["config"]["name"]) {
  175. definition.children[i]["config"]["name"] = definition.children[i]["layout"]["name"];
  176. definition.children[i]["config"]["description"] = definition.children[i]["layout"]["description"];
  177. definition.children[i]["modified"] = definition.children[i]["config"];
  178. }
  179.  
  180. /* if element line-chart, set the type to mean(Average) and set modified = config */
  181. if (definition.children[i]["config"] &&
  182. (definition.children[i]["component"] === "simpleLineChart" ||
  183. definition.children[i]["component"] === "highstock" ||
  184. definition.children[i]["component"] === "zoomableSpline" ||
  185. definition.children[i]["component"] === "areaRange" ||
  186. definition.children[i]["component"] === "barChart")
  187. && !definition.children[i]["config"]["type"]) {
  188. definition.children[i]["config"]["type"] = "mean";
  189. definition.children[i]["modified"] = definition.children[i]["config"];
  190. }
  191. }
  192. return definition;
  193. }
  194.  
  195. function getSensors(definition) {
  196. if (!definition) { return null; }
  197. for (let i = 0; i < _.get(definition, "children", definition).length; i++) {
  198.  
  199. /* if there are children in the element, check them */
  200. if (definition.children) {
  201.  
  202. /* if element is multiLine and there are sensors attached on it */
  203. if (definition.children[i]["component"] === "multiLineChart" && definition.children[i]["config"])
  204. getSensors(definition.children[i]["config"]["sensors"]);
  205.  
  206. /* if element has other children */
  207. else if (!_.isEmpty(definition.children[i].children))
  208. getSensors(definition.children[i]);
  209. }
  210.  
  211. /* if element is list of the sensors attached on multiLine, sensors always have { binding: { sensorId: '' }} */
  212. if (!definition.children) {
  213. if (definition[i].binding) sensors.push(definition[i].binding.sensorId);
  214. else sensors.push(definition[i].sensorId);
  215. }
  216.  
  217. /* if element not multiLine, check if it has attached */
  218. else if (definition.children[i]["component"] !== "multiLineChart") {
  219. if (_.get(definition, "children[" + i + "].binding", false))
  220. sensors.push(definition.children[i].binding.sensorId);
  221. }
  222. }
  223. }
  224.  
  225. function visit(level: number, node: any, res: any, allSensors: any) {
  226. if (!node) return;
  227. let name = node.component;
  228. let group = res.layout;
  229.  
  230. if (node.tenantId || node.plantId || node.systemId || node.sensorId || res.widget[name]
  231. || name === "mediaUpload"
  232. || name === "textEditor"
  233. || name === "sensorsTable"
  234. || name === "alarmsTable"
  235. || name === "section?"
  236. || name === "label"
  237. ) {
  238. if (res.layout[name]) {
  239. res.widget[name] = res.layout[name];
  240. res.layout[name] = null;
  241. }
  242. group = res.widget;
  243. }
  244.  
  245. let cp = group[name] = group[name] || { component: name, count: 0, keys: {} };
  246. cp.count++;
  247. let ks = Object.keys(node);
  248. ks.forEach(k => { cp.keys[k] = cp.keys[k] || 0; cp.keys[k]++; });
  249.  
  250. for (let i = 0; i < node.children.length; i++) {
  251. let n = node.children[i];
  252. let g = visit(level + 1, n, res, allSensors);
  253. let cv = (g === res.layout) ? convertLayout(n) : convertWidget(n, allSensors);
  254. node.children[i] = cv;
  255. }
  256. return group;
  257. }
  258.  
  259. function convertLayout(node: any) {
  260. let res = {
  261. type: "layout",
  262. version: undefined,
  263. component: node.component,
  264. id: Utils.uuid(),
  265. binding: undefined,
  266. layout: {},
  267. // temp: {},
  268. children: node.children
  269. };
  270. for (let key in node) {
  271. if (res[key]) continue;
  272. if (["animation", "isTemplate"].indexOf(key) > -1) {
  273. // res.temp[key] = node[key];
  274. continue;
  275. } else {
  276. res.layout[key] = node[key];
  277. }
  278. }
  279. return res;
  280. }
  281.  
  282. function color(val: string) {
  283. if (!val || !val.startsWith || !val.startsWith("rgb(")) return val;
  284. let rgb = "#" + val.replace("rgb(", "")
  285. .replace(")", "")
  286. .split(",")
  287. .map(v => +v)
  288. .map(v => v.toString(16))
  289. .map(v => v.length === 1 ? "0" + v : v)
  290. .join("").toUpperCase();
  291. return rgb;
  292. }
  293.  
  294. function convertWidget(node: any, allSensors: any) {
  295. if (TO_CONVERT.length && TO_CONVERT.indexOf(node.component) === -1)
  296. return node;
  297.  
  298. let res = {
  299. type: "widget",
  300. component: node.component,
  301. id: Utils.uuid(),
  302. binding: undefined,
  303. layout: {},
  304. config: {} as any,
  305. children: node.children
  306. };
  307.  
  308. if (node.component === "onoff") {
  309. res.config.ranges = [];
  310. if (node.errorRange) {
  311. res.config.ranges.push({
  312. indicator: node.errorRange.errorValueIndicator,
  313. color: color(node.errorRange.color)
  314. });
  315. }
  316. if (node.firstRange) {
  317. res.config.ranges.push({
  318. indicator: node.firstRange.zeroValueIndicator,
  319. color: color(node.firstRange.color)
  320. });
  321. }
  322. if (node.secondRange) {
  323. res.config.ranges.push({
  324. indicator: node.secondRange.firstValueIndicator,
  325. color: color(node.secondRange.color)
  326. });
  327. }
  328. if (node.thirdRange) {
  329. res.config.ranges.push({
  330. indicator: node.thirdRange.secondValueIndicator,
  331. color: color(node.thirdRange.color)
  332. });
  333. }
  334. if (node.fourthRange) {
  335. res.config.ranges.push({
  336. indicator: node.fourthRange.thirdValueIndicator,
  337. color: color(node.fourthRange.color)
  338. });
  339. }
  340. if (node.fifthRange) {
  341. res.config.ranges.push({
  342. indicator: node.fifthRange.fourthValueIndicator,
  343. color: color(node.fifthRange.color)
  344. });
  345. }
  346. if (res.config.ranges.length) delete node.ranges;
  347.  
  348. if (res.config.ranges.length < 6)
  349. res.config.ranges = [
  350. {
  351. "indicator": "Error",
  352. "color": "#BF0000"
  353. },
  354. {
  355. "indicator": "OFF",
  356. "color": "#F4D502"
  357. },
  358. {
  359. "indicator": "Running 2",
  360. "color": "#36CA38"
  361. },
  362. {
  363. "indicator": "/",
  364. "color": "#FF0000"
  365. },
  366. {
  367. "indicator": "Fault",
  368. "color": "#7F7F7F"
  369. },
  370. {
  371. "indicator": "Alarm",
  372. "color": "#FF4347"
  373. }
  374. ];
  375. } else if (node.firstRange) {
  376. res.config.ranges = [];
  377. if (node.firstRange) {
  378. res.config.ranges.push({
  379. from: node.firstRange.from,
  380. to: node.firstRange.to,
  381. color: color(node.firstRange.color)
  382. });
  383. }
  384. if (node.secondRange) {
  385. res.config.ranges.push({
  386. from: node.secondRange.from,
  387. to: node.secondRange.to,
  388. color: color(node.secondRange.color)
  389. });
  390. }
  391. if (node.thirdRange) {
  392. res.config.ranges.push({
  393. from: node.thirdRange.from,
  394. to: node.thirdRange.to,
  395. color: color(node.thirdRange.color)
  396. });
  397. }
  398. }
  399.  
  400. delete node.errorRange;
  401. delete node.firstRange;
  402. delete node.secondRange;
  403. delete node.thirdRange;
  404. delete node.fourthRange;
  405. delete node.fifthRange;
  406.  
  407. if (Array.isArray(node.ranges)) {
  408. for (let item of node.ranges) {
  409. for (let key in item) {
  410. item[key] = color(item[key]);
  411. }
  412. }
  413. }
  414.  
  415. for (let key in node) {
  416. if (res[key]) continue;
  417. if (["tenantId", "plantId", "systemId", "sensorId"].indexOf(key) > -1) {
  418. res.binding = res.binding || {};
  419. res.binding[key] = node[key];
  420.  
  421. if (key === "sensorId")
  422. allSensors[node[key]] = true;
  423.  
  424. } else if (["animation", "isTemplate", "textDisplay"].indexOf(key) > -1) {
  425. // res.temp[key] = node[key];
  426. continue;
  427. } else if (["chartHeight", "img", "size", "name", "description", "fontSize"].indexOf(key) > -1) {
  428. res.layout[key] = color(node[key]);
  429. } else if (["years", "months", "days", "hours", "minutes"].indexOf(key) > -1 && res["component"] === "time") {
  430. // res.temp[key] = node[key];
  431. continue;
  432. } else if (["years", "months", "days", "hours", "minutes"].indexOf(key) > -1 && res["component"] === "onoff") {
  433. // res.remove[key] = node[key];
  434. } else if (["scale"].indexOf(key) > -1) {
  435. // res.remove[key] = node[key];
  436. continue;
  437. } else if (["sensors"].indexOf(key) > -1) {
  438.  
  439. res.config[key] = color(node[key]);
  440.  
  441. res.binding["sensorIds"] = res.binding["sensorIds"] || [];
  442. res.binding["sensorId"] = undefined;
  443.  
  444. for (let t of res.config[key]) {
  445. res.binding["sensorIds"].push(t["sensorId"]);
  446.  
  447. allSensors[t["sensorId"]] = true;
  448.  
  449. }
  450. } else {
  451. let ren = key;
  452. let map = {
  453. "timestamp": "span"
  454. };
  455. ren = map[ren] || ren;
  456. res.config[ren] = color(node[key]);
  457. }
  458. }
  459. if (_.isEmpty(res.config)) delete res.config;
  460.  
  461. return res;
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement