Repository navigation
Expand file tree
/
Copy pathtileConfigs.ts
More file actions
303 lines (284 loc) · 7.93 KB
/
Copy pathtileConfigs.ts
File metadata and controls
303 lines (284 loc) · 7.93 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
export type TileHttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
export interface TextTileConfig {
title: string;
body: string;
}
export function defaultTextConfig(): TextTileConfig {
return {
title: "Text tile",
body: "Edit this tile to show static notes or labels.",
};
}
export interface ApiTileConfig {
title: string;
url: string;
method: TileHttpMethod;
headersText: string;
bodyText: string;
/** Seconds; 0 disables polling */
pollIntervalSec: number;
/** JSON Schema (draft-2020-12) for the parsed JSON body */
responseSchemaText: string;
/**
* Newline-separated JSON Pointers (RFC 6901), e.g. `/id` and `/title`.
* Lines starting with `#` are comments. Empty = show full JSON.
*/
displayPathsText: string;
}
export function defaultApiConfig(): ApiTileConfig {
return {
title: "API",
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
headersText: "{}",
bodyText: "",
pollIntervalSec: 0,
responseSchemaText: JSON.stringify(
{
type: "object",
required: ["id", "title"],
properties: {
id: { type: "number" },
title: { type: "string" },
body: { type: "string" },
userId: { type: "number" },
},
},
null,
2,
),
displayPathsText: "",
};
}
export interface ApiMarkdownTileConfig {
title: string;
url: string;
method: TileHttpMethod;
headersText: string;
bodyText: string;
pollIntervalSec: number;
responseSchemaText: string;
/**
* Markdown source rendered by Nunjucks, with legacy JSON Pointer placeholders `{{/path}}` (RFC 6901).
*/
markdownTemplate: string;
}
export function defaultApiMarkdownConfig(): ApiMarkdownTileConfig {
return {
title: "API + Markdown",
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
headersText: "{}",
bodyText: "",
pollIntervalSec: 0,
responseSchemaText: JSON.stringify(
{
type: "object",
required: ["id", "title"],
properties: {
id: { type: "number" },
title: { type: "string" },
body: { type: "string" },
userId: { type: "number" },
},
},
null,
2,
),
markdownTemplate:
'## {{ /title }}\n\nPost **#{{ id }}** (user {{ userId }})\n\n{{ body }}\n\n{% if json.tags %}Tags: {{ json.tags | join(", ") }}{% endif %}\n',
};
}
export interface MetricTileConfig {
/** Shown in tile chrome */
title: string;
url: string;
method: TileHttpMethod;
headersText: string;
bodyText: string;
pollIntervalSec: number;
/**
* JSON Pointer (RFC 6901) to a numeric field, e.g. `/id` or `/stats/count`.
* Empty: the JSON root must be a single number (e.g. `42` or `3.14`).
*/
valuePointer: string;
/** Static label shown above the large number in the tile body (optional) */
valueTitle: string;
}
export function defaultMetricConfig(): MetricTileConfig {
return {
title: "Metric",
url: "https://jsonplaceholder.typicode.com/posts/1",
method: "GET",
headersText: "{}",
bodyText: "",
pollIntervalSec: 0,
valuePointer: "/id",
valueTitle: "",
};
}
const PRIMARY_FILL = "rgba(0, 78, 118, 0.65)";
const PRIMARY_STROKE = "rgba(0, 78, 118, 1)";
const SECONDARY_FILL = "rgba(230, 118, 0, 0.55)";
const SECONDARY_STROKE = "rgba(230, 118, 0, 1)";
export const CHART_TILE_TYPES = [
"chartBar",
"chartLine",
"chartPie",
"chartDoughnut",
"chartRadar",
"chartPolarArea",
] as const;
export type ChartTileType = (typeof CHART_TILE_TYPES)[number];
export function isChartTileType(type: string): type is ChartTileType {
return (CHART_TILE_TYPES as readonly string[]).includes(type);
}
export type ChartTileKind =
| "bar"
| "line"
| "pie"
| "doughnut"
| "radar"
| "polarArea";
/** Matches dashboard tile `type` string → ChartTileKind for vue-chartjs. */
export const CHART_TILE_TYPE_TO_KIND: Record<ChartTileType, ChartTileKind> = {
chartBar: "bar",
chartLine: "line",
chartPie: "pie",
chartDoughnut: "doughnut",
chartRadar: "radar",
chartPolarArea: "polarArea",
};
export interface ChartTileConfig {
title: string;
chartKind: ChartTileKind;
/** Chart.js `data` — JSON string `{ labels, datasets }`. Used when `chartDataUrl` is empty. */
chartDataJson: string;
/** Chart.js `options` — JSON string (merged over responsive defaults). */
chartOptionsJson: string;
/**
* Non-empty: fetch chart data from this URL (JSON body must be `{ labels, datasets }`).
* Static `chartDataJson` is ignored for rendering while a URL is set.
*/
chartDataUrl: string;
method: TileHttpMethod;
headersText: string;
bodyText: string;
/** Seconds between refetches; 0 = only on load / Refresh. */
pollIntervalSec: number;
}
const DEFAULT_CHART_OPTIONS = {
responsive: true,
maintainAspectRatio: false,
};
function sampleData(kind: ChartTileKind): {
labels: string[];
datasets: Record<string, unknown>[];
} {
switch (kind) {
case "bar":
case "line":
return {
labels: ["Mon", "Tue", "Wed", "Thu", "Fri"],
datasets: [
{
label: "Series A",
data: [12, 19, 8, 15, 22],
backgroundColor: kind === "bar" ? PRIMARY_FILL : undefined,
borderColor: PRIMARY_STROKE,
borderWidth: kind === "line" ? 2 : 1,
fill: kind === "line" ? false : undefined,
tension: kind === "line" ? 0.35 : undefined,
},
{
label: "Series B",
data: [8, 15, 12, 10, 18],
backgroundColor: kind === "bar" ? SECONDARY_FILL : undefined,
borderColor: SECONDARY_STROKE,
borderWidth: kind === "line" ? 2 : 1,
fill: kind === "line" ? false : undefined,
tension: kind === "line" ? 0.35 : undefined,
},
],
};
case "pie":
case "doughnut":
return {
labels: ["North", "South", "East", "West"],
datasets: [
{
data: [35, 25, 22, 18],
backgroundColor: [
PRIMARY_FILL,
SECONDARY_FILL,
"rgba(0, 78, 118, 0.35)",
"rgba(230, 118, 0, 0.35)",
],
borderColor: PRIMARY_STROKE,
borderWidth: 1,
},
],
};
case "radar":
return {
labels: ["Speed", "Reliability", "Comfort", "Safety", "Efficiency"],
datasets: [
{
label: "Series A",
data: [65, 59, 82, 81, 56],
fill: true,
backgroundColor: "rgba(0, 78, 118, 0.22)",
borderColor: PRIMARY_STROKE,
borderWidth: 2,
},
{
label: "Series B",
data: [48, 72, 63, 70, 74],
fill: true,
backgroundColor: "rgba(230, 118, 0, 0.18)",
borderColor: SECONDARY_STROKE,
borderWidth: 2,
},
],
};
case "polarArea":
return {
labels: ["Alpha", "Beta", "Gamma", "Delta", "Epsilon"],
datasets: [
{
data: [18, 14, 12, 10, 8],
backgroundColor: [
PRIMARY_FILL,
SECONDARY_FILL,
"rgba(0, 78, 118, 0.4)",
"rgba(230, 118, 0, 0.4)",
"rgba(0, 78, 118, 0.22)",
],
},
],
};
default:
return { labels: [], datasets: [] };
}
}
const chartTitles: Record<ChartTileKind, string> = {
bar: "Bar chart",
line: "Line chart",
pie: "Pie chart",
doughnut: "Doughnut chart",
radar: "Radar chart",
polarArea: "Polar area chart",
};
export function defaultChartConfig(kind: ChartTileKind): ChartTileConfig {
return {
title: chartTitles[kind],
chartKind: kind,
chartDataJson: JSON.stringify(sampleData(kind), null, 2),
chartOptionsJson: JSON.stringify(DEFAULT_CHART_OPTIONS, null, 2),
chartDataUrl: "",
method: "GET",
headersText: "{}",
bodyText: "",
pollIntervalSec: 0,
};
}