-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Dashboard.tsx
256 lines (235 loc) · 9.88 KB
/
Dashboard.tsx
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
import { Alert, AlertActionLink, AlertVariant, Grid, GridItem, Spinner, Title } from '@patternfly/react-core';
import { QueryObserverResult, useQuery } from 'react-query';
import React, { memo, useContext, useState } from 'react';
import { InView } from 'react-intersection-observer';
import {
ClustersContext,
IClusterContext,
IDashboard,
IPluginDefaults,
IPluginTimes,
IPluginsContext,
IRow,
PluginPanel,
PluginsContext,
} from '@kobsio/plugin-core';
import { interpolate, rowHeight, toGridSpans } from '../../utils/dashboard';
import DashboardToolbar from './DashboardToolbar';
import { IVariableValues } from '../../utils/interfaces';
interface IDashboardProps {
activeKey: string;
eventKey: string;
defaults: IPluginDefaults;
dashboard: IDashboard;
forceDefaultSpan: boolean;
setDetails?: (details: React.ReactNode) => void;
}
// The Dashboard component renders the rows and panels for a single dashboard, by including a plugin via the PluginPanel
// component.
// The component is also used to set the initial value for the times and for fetching all defined variable values. To
// allow a user to change the time or select another variable value the component includes a toolbar component.
const Dashboard: React.FunctionComponent<IDashboardProps> = ({
activeKey,
eventKey,
defaults,
dashboard,
forceDefaultSpan,
setDetails,
}: IDashboardProps) => {
const clustersContext = useContext<IClusterContext>(ClustersContext);
const pluginsContext = useContext<IPluginsContext>(PluginsContext);
// times is the state for the users selected time. The default value will always be the last 15 minutes. This is
// required for some plugins (e.g. the Prometheus plugin), which making use of a time range.
const [times, setTimes] = useState<IPluginTimes>({
time: 'last15Minutes',
timeEnd: Math.floor(Date.now() / 1000),
timeStart: Math.floor(Date.now() / 1000) - 900,
});
// variables is the state for the dashboard variables. The initial state is undefined or when the user provided some
// variables we convert them to the IVariableValues interface which contains the currently selected value and all
// possible values.
const [variables, setVariables] = useState<IVariableValues[] | undefined>(
dashboard.variables?.map((variable) => {
return {
...variable,
value: '',
values: [],
};
}),
);
// Everytime the variables state is changed we run the following query to refetch the variable values. This also means
// that all child components should used the retunred data array instead of the formerly defined variables state,
// because it contains the current values and selected value for a variable. The variables state is mainly there to
// trigger this function everytime a new variable value is selected.
// Currently we support "core" variable, which can be used to select a cluster or plugin and we are supporting the
// Prometheus plugin. For the Prometheus plugin the user must specify the name of the Prometheus instance via the name
// parameter in the options. When the user changes the variables, we keep the old variable values, so that we not have
// to rerender all the panels in the dashboard.
const { isError, error, data, refetch } = useQuery<IVariableValues[] | null, Error>(
['dashboard/variables', dashboard, variables, times, activeKey],
async () => {
if (activeKey !== eventKey) {
return null;
}
if (!variables) {
return [];
}
try {
const tmpVariables = [...variables];
for (let i = 0; i < tmpVariables.length; i++) {
if (tmpVariables[i].plugin.name === 'core') {
if (tmpVariables[i].plugin.options.type) {
if (tmpVariables[i].plugin.options.type === 'clusters') {
tmpVariables[i].values = clustersContext.clusters;
tmpVariables[i].value = tmpVariables[i].value || tmpVariables[i].values[0];
} else if (tmpVariables[i].plugin.options.type === 'plugins') {
tmpVariables[i].values = pluginsContext.plugins.map((plugin) => plugin.name);
tmpVariables[i].value = tmpVariables[i].value || tmpVariables[i].values[0];
} else if (tmpVariables[i].plugin.options.type === 'static') {
if (tmpVariables[i].plugin.options.items && Array.isArray(tmpVariables[i].plugin.options.items)) {
tmpVariables[i].values = tmpVariables[i].plugin.options.items;
tmpVariables[i].value = tmpVariables[i].value || tmpVariables[i].plugin.options.items[0];
}
}
}
} else {
const pluginDetails = pluginsContext.getPluginDetails(tmpVariables[i].plugin.name);
if (pluginDetails?.type === 'prometheus') {
const response = await fetch(`/api/plugins/prometheus/${tmpVariables[i].plugin.name}/variable`, {
body: JSON.stringify({
label: tmpVariables[i].plugin.options.label,
query: interpolate(tmpVariables[i].plugin.options.query, tmpVariables, times),
timeEnd: times.timeEnd,
timeStart: times.timeStart,
type: tmpVariables[i].plugin.options.type,
}),
method: 'post',
});
const json = await response.json();
if (response.status >= 200 && response.status < 300) {
if (json && Array.isArray(json) && json.length > 0) {
if (json && json.length > 1 && tmpVariables[i].plugin.options.allowAll) {
json.unshift(json.join('|'));
}
tmpVariables[i].values = json ? json : [''];
tmpVariables[i].value =
json && json.includes(tmpVariables[i].value) ? tmpVariables[i].value : json ? json[0] : '';
} else {
tmpVariables[i].values = [''];
tmpVariables[i].value = '';
}
} else {
if (json.error) {
throw new Error(json.error);
} else {
throw new Error('An unknown error occured');
}
}
}
}
}
return tmpVariables;
} catch (err) {
throw err;
}
},
{ keepPreviousData: true },
);
// We do not use the dashboard.rows array directly to render the dashboard. Instead we are replacing all the variables
// in the dashboard first with users selected values. For that we have to convert the array to a string first so that
// we can replace the variables in the string and then we have to convert it back to an array,
const rows: IRow[] = JSON.parse(interpolate(JSON.stringify(dashboard.rows), data ? data : [], times));
if (isError) {
return (
<Alert
variant={AlertVariant.danger}
title="Variables were not fetched"
actionLinks={
<React.Fragment>
<AlertActionLink onClick={(): Promise<QueryObserverResult<IVariableValues[] | null, Error>> => refetch()}>
Retry
</AlertActionLink>
</React.Fragment>
}
>
<p>{error?.message}</p>
</Alert>
);
}
if (!data) {
return (
<div className="pf-u-text-align-center">
<Spinner />
</div>
);
}
return (
<React.Fragment>
<DashboardToolbar variables={data} setVariables={setVariables} times={times} setTimes={setTimes} />
<p> </p>
<Grid hasGutter={true}>
{rows.map((row, rowIndex) => (
<React.Fragment key={rowIndex}>
{row.title ? (
<Title headingLevel="h6" size="lg">
{row.title}
</Title>
) : null}
{row.panels.map((panel, panelIndex) => (
<GridItem
key={panelIndex}
span={toGridSpans(12, forceDefaultSpan, panel.colSpan)}
rowSpan={toGridSpans(1, forceDefaultSpan, panel.rowSpan)}
>
<InView>
{({ inView, ref }): React.ReactNode => (
<div ref={ref}>
{inView ? (
<div
style={
row.size !== undefined && row.size === -1
? undefined
: { height: rowHeight(row.size, panel.rowSpan), overflow: 'scroll' }
}
>
<PluginPanel
defaults={defaults}
times={times}
title={panel.title}
description={panel.description}
name={panel.plugin.name}
options={panel.plugin.options}
setDetails={setDetails}
/>
</div>
) : (
<div
style={
row.size !== undefined && row.size === -1
? undefined
: { height: rowHeight(row.size, panel.rowSpan), overflow: 'scroll' }
}
></div>
)}
</div>
)}
</InView>
</GridItem>
))}
</React.Fragment>
))}
</Grid>
</React.Fragment>
);
};
export default memo(Dashboard, (prevProps, nextProps) => {
if (
prevProps.activeKey === nextProps.activeKey &&
JSON.stringify(prevProps.defaults) === JSON.stringify(nextProps.defaults) &&
JSON.stringify(prevProps.dashboard) === JSON.stringify(nextProps.dashboard) &&
prevProps.forceDefaultSpan === nextProps.forceDefaultSpan
) {
return true;
}
return false;
});