1
+ //-----------Import External Modules-----------
1
2
import React , { useEffect } from "react" ;
2
3
import { useState } from "react" ;
3
4
import { TextField , Typography , MenuItem , Select , Drawer , IconButton , Grid , Button , Stack } from "@mui/material"
@@ -9,20 +10,25 @@ import {
9
10
gql ,
10
11
} from "@apollo/client" ;
11
12
13
+ //-----------Import Internal Modules-----------
14
+ import { getUnixRange , getDuration , validateDuration } from "../utils/utilityFunctions.js" ;
15
+
12
16
const client = new ApolloClient ( {
13
17
uri : "http://localhost:5001/graphql" ,
14
18
cache : new InMemoryCache ( ) ,
15
19
} ) ;
16
20
17
21
export const SettingsSidebar = ( { showSettings, setShowSettings, metricsState, setMetricsState } ) => {
22
+ const [ invalidPrometheusMessage , setInvalidPrometheusMessage ] = useState ( null ) ;
23
+ const [ invalidKsqlDBMessage , setInvalidKsqlDBMessage ] = useState ( null ) ;
24
+ const [ invalidDuration , setInvalidDuration ] = useState ( false ) ;
25
+ const [ showSubmissionConfirmation , setShowSubmissionConfirmation ] = useState ( false ) ;
18
26
const [ localMetricsState , setLocalMetricsState ] = useState ( {
19
27
prometheusURL : metricsState . prometheusURL ,
20
28
ksqlDBURL : metricsState . ksqlDBURL ,
21
29
duration : metricsState . duration ,
22
30
refreshRate : metricsState . refreshRate
23
31
} ) ;
24
- const [ invalidPrometheusMessage , setInvalidPrometheusMessage ] = useState ( null ) ;
25
- const [ showSubmissionConfirmation , setShowSubmissionConfirmation ] = useState ( false ) ;
26
32
27
33
const handleLocalMetrics = ( event ) => {
28
34
switch ( event . target . name ) {
@@ -80,15 +86,59 @@ export const SettingsSidebar = ({ showSettings, setShowSettings, metricsState, s
80
86
event . preventDefault ( ) ;
81
87
82
88
// Verify Prometheus URL
83
- client . query ( {
84
- query : gql `
85
- query validatePrometheusURL{
86
- isValidPrometheusURL(prometheusURL: "${ event . target [ 1 ] . value } ")
87
- }
88
- `
89
- } )
90
- . then ( res => {
91
- // update state
89
+ try {
90
+ // Validate Prometheus URL points to live server
91
+ const { data : { isValidPrometheusURL : { isValid : prometheusValid , error : prometheusError } } } = await client . query ( {
92
+ query : gql `
93
+ query validatePrometheusURL{
94
+ isValidPrometheusURL(prometheusURL: "${ event . target [ 1 ] . value } ") {
95
+ isValid,
96
+ error
97
+ }
98
+ }
99
+ `
100
+ } ) ;
101
+
102
+ if ( prometheusError ) {
103
+ setInvalidPrometheusMessage ( prometheusError ) ;
104
+ return ;
105
+ } else if ( prometheusValid ) {
106
+ setInvalidPrometheusMessage ( null ) ;
107
+ }
108
+
109
+ // Validate ksqlDB URL points to live server (if provided)
110
+ if ( localMetricsState . ksqlDBURL ) {
111
+ const { data : { isValidKsqlDBURL : { isValid : ksqlDBValid , error : ksqlDBError } } } = await client . query ( {
112
+ query : gql `
113
+ query isValidKsqlDBURL{
114
+ isValidKsqlDBURL(ksqlDBURL: "${ event . target [ 3 ] . value } ") {
115
+ isValid,
116
+ error
117
+ }
118
+ }
119
+ `
120
+ } ) ;
121
+
122
+ if ( ksqlDBError ) {
123
+ setInvalidKsqlDBMessage ( ksqlDBError ) ;
124
+ return ;
125
+ } else if ( ksqlDBValid ) {
126
+ setInvalidKsqlDBMessage ( null ) ;
127
+ }
128
+ } else {
129
+ setInvalidKsqlDBMessage ( null ) ;
130
+ }
131
+
132
+ // Validate Prometheus accepts user's duration input
133
+ const duration = getDuration ( localMetricsState . duration . days , localMetricsState . duration . hours , localMetricsState . duration . minutes ) ;
134
+ if ( ! validateDuration ( duration , localMetricsState . refreshRate ) ) {
135
+ setInvalidDuration ( true ) ;
136
+ return ;
137
+ } else {
138
+ setInvalidDuration ( false ) ;
139
+ }
140
+
141
+ // Update state with user's input values
92
142
setMetricsState ( {
93
143
prometheusURL : localMetricsState . prometheusURL ,
94
144
ksqlDBURL : localMetricsState . ksqlDBURL ,
@@ -99,13 +149,11 @@ export const SettingsSidebar = ({ showSettings, setShowSettings, metricsState, s
99
149
} ,
100
150
refreshRate : localMetricsState . refreshRate
101
151
} ) ;
102
- setInvalidPrometheusMessage ( null ) ;
103
152
setShowSubmissionConfirmation ( true ) ;
104
153
setTimeout ( ( ) => setShowSubmissionConfirmation ( false ) , 3000 ) ;
105
- } )
106
- . catch ( error => {
107
- setInvalidPrometheusMessage ( error . message ) ;
108
- } ) ;
154
+ } catch ( error ) {
155
+ console . log ( error ) ;
156
+ }
109
157
}
110
158
111
159
return (
@@ -144,18 +192,106 @@ export const SettingsSidebar = ({ showSettings, setShowSettings, metricsState, s
144
192
< hr className = "w-full invisible mb-2 mt-2" > </ hr >
145
193
< Typography variant = "h6" sx = { { color : "#333" } } > ksqlDB Connection</ Typography >
146
194
< hr className = "w-full mb-3 mt-1" > </ hr >
147
- < TextField
148
- fullWidth
149
- variant = "outlined"
150
- label = "URL"
151
- name = "ksqldb-url"
152
- onChange = { handleLocalMetrics }
153
- value = { localMetricsState . ksqlDBURL }
154
- />
195
+ { invalidKsqlDBMessage ? (
196
+ < >
197
+ < TextField
198
+ error
199
+ fullWidth
200
+ variant = "outlined"
201
+ label = "URL"
202
+ name = "ksqldb-url"
203
+ onChange = { handleLocalMetrics }
204
+ value = { localMetricsState . ksqlDBURL }
205
+ />
206
+ < Typography variant = "h8" sx = { { color : "red" } } > { invalidKsqlDBMessage } </ Typography >
207
+ </ >
208
+ ) : (
209
+ < TextField
210
+ fullWidth
211
+ variant = "outlined"
212
+ label = "URL"
213
+ name = "ksqldb-url"
214
+ onChange = { handleLocalMetrics }
215
+ value = { localMetricsState . ksqlDBURL }
216
+ />
217
+ ) }
155
218
< hr className = "w-full invisible mb-2 mt-2" > </ hr >
156
219
< Typography variant = "h6" sx = { { color : "#333" } } > Duration</ Typography >
157
220
< hr className = "w-full mb-3 mt-1" > </ hr >
158
- < Grid spacing = { 2 } container justifyContent = "flex-start" alignItems = "center" >
221
+ { invalidDuration ? (
222
+ < >
223
+ < Grid spacing = { 2 } container justifyContent = "flex-start" alignItems = "center" >
224
+ < Grid item xs = { 4 } >
225
+ < TextField
226
+ error
227
+ variant = "outlined"
228
+ label = "Days"
229
+ name = "duration-days"
230
+ onChange = { handleLocalMetrics }
231
+ value = { localMetricsState . duration . days }
232
+ type = "number"
233
+ />
234
+ </ Grid >
235
+ < Grid item xs = { 4 } >
236
+ < TextField
237
+ error
238
+ variant = "outlined"
239
+ label = "Hours"
240
+ name = "duration-hours"
241
+ onChange = { handleLocalMetrics }
242
+ value = { localMetricsState . duration . hours }
243
+ type = "number"
244
+ />
245
+ </ Grid >
246
+ < Grid item xs = { 4 } >
247
+ < TextField
248
+ error
249
+ variant = "outlined"
250
+ label = "Minutes"
251
+ name = "duration-minutes"
252
+ onChange = { handleLocalMetrics }
253
+ value = { localMetricsState . duration . minutes }
254
+ type = "number"
255
+ />
256
+ </ Grid >
257
+ </ Grid >
258
+ < Typography variant = "h8" sx = { { color : "red" } } > Duration must not include more than 11,000 data points</ Typography >
259
+ </ >
260
+ ) : (
261
+ < Grid spacing = { 2 } container justifyContent = "flex-start" alignItems = "center" >
262
+ < Grid item xs = { 4 } >
263
+ < TextField
264
+ variant = "outlined"
265
+ label = "Days"
266
+ name = "duration-days"
267
+ onChange = { handleLocalMetrics }
268
+ value = { localMetricsState . duration . days }
269
+ type = "number"
270
+ />
271
+ </ Grid >
272
+ < Grid item xs = { 4 } >
273
+ < TextField
274
+ variant = "outlined"
275
+ label = "Hours"
276
+ name = "duration-hours"
277
+ onChange = { handleLocalMetrics }
278
+ value = { localMetricsState . duration . hours }
279
+ type = "number"
280
+ />
281
+ </ Grid >
282
+ < Grid item xs = { 4 } >
283
+ < TextField
284
+ variant = "outlined"
285
+ label = "Minutes"
286
+ name = "duration-minutes"
287
+ onChange = { handleLocalMetrics }
288
+ value = { localMetricsState . duration . minutes }
289
+ type = "number"
290
+ />
291
+ </ Grid >
292
+ </ Grid >
293
+ ) }
294
+ { /* <Grid spacing={2} container justifyContent="flex-start" alignItems="center">
159
295
<Grid item xs={4}>
160
296
<TextField
161
297
variant="outlined"
@@ -164,7 +300,6 @@ export const SettingsSidebar = ({ showSettings, setShowSettings, metricsState, s
164
300
onChange={handleLocalMetrics}
165
301
value={localMetricsState.duration.days}
166
302
type="number"
167
-
168
303
/>
169
304
</Grid>
170
305
<Grid item xs={4}>
@@ -187,7 +322,7 @@ export const SettingsSidebar = ({ showSettings, setShowSettings, metricsState, s
187
322
type="number"
188
323
/>
189
324
</Grid>
190
- </ Grid >
325
+ </Grid> */ }
191
326
< hr className = "w-full invisible mb-2 mt-2" > </ hr >
192
327
< Typography variant = "h6" > Refresh Rate</ Typography >
193
328
< hr className = "w-full mb-3 mt-1" > </ hr >
0 commit comments