/
icon.ts
125 lines (108 loc) · 3.5 KB
/
icon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ? ==================================== (c) TagoIO ====================================
// * What is this file?
// Migration of old ICON to new ICON
// ? ====================================================================================
import { WidgetInfo } from "../Account/dashboards.types";
import convertFormula from "./common/convertFormula";
const layoutMatrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
export function convert(oldWidget: any): WidgetInfo {
const oldDisplay = oldWidget.display || {};
const newStructure: any = {
dashboard: oldWidget.dashboard,
display: {
header_buttons: oldDisplay.header_buttons || [],
help: oldDisplay.help || "",
show_units: !!oldDisplay?.show_unit,
show_values: !oldDisplay?.hide_values,
show_variables: !oldDisplay?.hide_variables,
variables: [],
},
id: oldWidget.id,
label: oldWidget.label,
realtime: null,
type: "icon",
};
const variables = [];
if (Array.isArray(oldWidget.data)) {
newStructure.data = oldWidget.data; // transfers the .data property
for (const item of oldWidget.data) {
if (item.is_hide) {
// is_hide items are not visible in the columns, so we cannot
// put them in the variables array
continue;
}
for (const variable of item.variables) {
const key = `${item.origin}${variable}`;
const { show_thousand, decimals } = oldDisplay.vars_format?.[key] || {};
const numberFormat =
show_thousand || decimals
? {
decimals,
show_thousand,
}
: null;
const alias = oldDisplay.vars_labels?.[key];
const formula = convertFormula(oldDisplay.vars_formula?.[key]);
const iconConditions = oldDisplay?.conditions?.[key] || [];
const colorConditions = iconConditions.map((e: any) => ({
color: e?.color,
condition: e?.condition,
}));
const layout = oldDisplay?.layout?.[key] || null;
/**
* If none column or row has been found, it should takes the 0 position
* If two variables has the same position doesn't matter
*/
const row = layout?.row || 0;
const column = layout?.column || 0;
const position = layout ? layoutMatrix[row][column] : 10;
variables.push({
// Not sent to backend, position tracking for sorting the variables
_position: position,
origin: item.origin,
variable,
...(alias ? { alias } : {}),
...(numberFormat ? { number_format: numberFormat } : {}),
...(formula ? { formula } : {}),
...(iconConditions ? { icon_conditions: iconConditions } : {}),
...(colorConditions ? { color_conditions: colorConditions } : {}),
});
}
}
}
/**
* Sort by the position of the icon
*/
variables.sort((a, b) => {
if (a._position > b._position) {
return 1;
}
if (a._position < b._position) {
return -1;
}
return 0;
});
/**
* Remove the _position property
*/
newStructure.display.variables = variables.map((e) => {
delete e._position;
return e;
});
return newStructure;
}
export function isOldStructure(widget: any) {
const isOld = !!(
widget?.display?.vars_labels ||
widget?.display?.vars_format ||
widget?.display?.vars_formula ||
widget?.display?.numberformat ||
widget?.display?.hide_values ||
widget?.display?.hide_variables
);
return isOld;
}