forked from parseablehq/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.tsx
388 lines (360 loc) · 11.9 KB
/
Settings.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { Box, Button, Divider, Group, Loader, Modal, NumberInput, px, Stack, TextInput } from '@mantine/core';
import classes from '../../styles/Management.module.css';
import { Text } from '@mantine/core';
import { useAppStore } from '@/layouts/MainLayout/providers/AppProvider';
import { useForm } from '@mantine/form';
import _ from 'lodash';
import { useCallback, useEffect, useState } from 'react';
import { useStreamStore } from '../../providers/StreamProvider';
import { IconCheck, IconX, IconReload } from '@tabler/icons-react';
import { sanitizeBytes, convertGibToBytes } from '@/utils/formatBytes';
import timeRangeUtils from '@/utils/timeRangeUtils';
import ErrorView from './ErrorView';
import RestrictedView from '@/components/Misc/RestrictedView';
import IconButton from '@/components/Button/IconButton';
const { formatDateWithTimezone } = timeRangeUtils;
const renderRefreshIcon = () => <IconReload size={px('1rem')} stroke={1.5} />;
const Header = () => {
return (
<Stack className={classes.headerContainer} style={{ minHeight: '3rem', maxHeight: '3rem' }}>
<Text className={classes.title}>Settings</Text>
</Stack>
);
};
const RetentionForm = (props: { updateRetentionConfig: ({ config }: { config: any }) => void }) => {
const [retention] = useStreamStore((store) => store.retention);
const form = useForm({
mode: 'controlled',
initialValues: {
duration: retention.duration,
description: retention.description,
action: 'delete',
},
validate: {
duration: (val) => (_.toInteger(val) <= 0 ? 'Must be a number greater than 0' : null),
},
validateInputOnChange: true,
validateInputOnBlur: true,
});
useEffect(() => {
form.setValues({
duration: retention.duration,
description: retention.description,
});
}, [retention]);
const onSubmit = useCallback(
({ reset }: { reset?: boolean }) => {
if (reset) {
props.updateRetentionConfig({ config: [] });
} else {
const { hasErrors } = form.validate();
if (hasErrors) return;
const parsedDuration = `${form.values.duration}d`;
props.updateRetentionConfig({
config: [{ ...form.values, duration: parsedDuration }],
});
}
},
[form.values],
);
return (
<Stack gap={12} flex={1}>
<Stack gap={8}>
<Stack gap={16} style={{ flexDirection: 'row', width: '100%' }}>
<NumberInput
withAsterisk
classNames={{ label: classes.fieldDescription }}
styles={{ label: { marginBottom: 4 } }}
label="Duration (In Days)"
placeholder="Duration in days"
key="duration"
{...form.getInputProps('duration')}
style={{ width: '50%' }}
/>
<TextInput
classNames={{ label: classes.fieldDescription, input: classes.inputField }}
styles={{ label: { marginBottom: 4 } }}
label="Action"
key="action"
disabled
{...form.getInputProps('action')}
style={{ width: '50%' }}
/>
</Stack>
<TextInput
classNames={{ label: classes.fieldDescription, input: classes.inputField }}
styles={{ label: { marginBottom: 4 } }}
label="Description"
key="description"
placeholder="Description"
{...form.getInputProps('description')}
/>
</Stack>
<Stack style={{ flexDirection: 'row', justifyContent: 'flex-end' }} mt="0.6rem">
<Button
className={classes.submitBtn}
onClick={() => onSubmit({ reset: true })}
variant="outline"
disabled={retention.duration === 0}>
Reset
</Button>
<Button className={classes.submitBtn} onClick={() => onSubmit({ reset: false })} disabled={!form.isDirty()}>
Submit
</Button>
</Stack>
</Stack>
);
};
function extractNumber(value: string | null) {
if (_.isEmpty(value) || value === null) return 0;
const regex = /^(\d+)/;
const match = value.match(regex);
return match ? parseFloat(match[0]) : 0;
}
const DeleteHotTierModal = (props: {
deleteHotTierInfo: ({ onSuccess }: { onSuccess: () => void }) => void;
isDeleting: boolean;
closeModal: () => void;
showDeleteModal: boolean;
}) => {
const [currentStream] = useAppStore((store) => store.currentStream);
const onDelete = useCallback(() => {
props.deleteHotTierInfo({ onSuccess: props.closeModal });
}, []);
return (
<Modal
opened={props.showDeleteModal}
onClose={props.closeModal}
size="auto"
centered
styles={{
body: { padding: '0 1rem 1rem 1rem', width: 400 },
header: { padding: '1rem', paddingBottom: '0.4rem' },
}}
title={<Text style={{ fontSize: '0.9rem', fontWeight: 600 }}>Delete Hot Tier</Text>}>
<Stack>
<Stack gap={8}>
<Text className={classes.deleteWarningText}>
Are you sure want to reset hot tier config and cached data for {currentStream} ?
</Text>
</Stack>
<Stack style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end' }}>
<Box>
<Button onClick={props.closeModal} variant="outline">
Cancel
</Button>
</Box>
<Box>
<Button loading={props.isDeleting} onClick={onDelete}>
Delete
</Button>
</Box>
</Stack>
</Stack>
</Modal>
);
};
const HotTierConfig = (props: {
updateHotTierInfo: ({ size }: { size: string }) => void;
refetchHotTierInfo: () => void;
deleteHotTierInfo: ({ onSuccess }: { onSuccess: () => void }) => void;
isDeleting: boolean;
isUpdating: boolean;
}) => {
const [hotTier] = useStreamStore((store) => store.hotTier);
const [info] = useStreamStore((store) => store.info);
const streamType = 'stream_type' in info && info.stream_type;
const size = _.get(hotTier, 'size', '');
const usedSize = _.get(hotTier, 'used_size', '');
const availableSize = _.get(hotTier, 'available_size', '');
const oldestEntry = _.get(hotTier, 'oldest_date_time_entry', '');
const humanizedSize = sanitizeBytes(size);
const humanizedUsedSize = sanitizeBytes(usedSize);
const humanizedAvailableSize = sanitizeBytes(availableSize);
const sanitizedSize = extractNumber(humanizedSize);
const [localSizeValue, setLocalSizeValue] = useState<number>(sanitizedSize);
const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false);
const [isDirty, setIsDirty] = useState<boolean>(false);
const onChangeHandler = useCallback((e: string | number) => {
setLocalSizeValue(_.toNumber(e));
}, []);
const onCancel = useCallback(() => {
setLocalSizeValue(sanitizedSize);
}, [sanitizedSize]);
useEffect(() => {
setIsDirty(sanitizedSize !== localSizeValue);
}, [localSizeValue]);
useEffect(() => {
setLocalSizeValue(sanitizedSize);
setIsDirty(sanitizedSize !== localSizeValue);
}, [hotTier]);
const onUpdate = useCallback(() => {
props.updateHotTierInfo({ size: `${convertGibToBytes(localSizeValue)}` });
}, [localSizeValue]);
const hotTierNotSet = _.isEmpty(size) || _.isEmpty(hotTier);
const closeDeleteModal = useCallback(() => {
return setShowDeleteModal(false);
}, []);
const openDeleteModal = useCallback(() => {
return setShowDeleteModal(true);
}, []);
if (hotTierNotSet && streamType === 'Internal') return null;
return (
<Stack className={classes.fieldsContainer} style={{ border: 'none', gap: 16 }}>
<DeleteHotTierModal
deleteHotTierInfo={props.deleteHotTierInfo}
isDeleting={props.isDeleting}
closeModal={closeDeleteModal}
showDeleteModal={showDeleteModal}
/>
<Stack style={{ flexDirection: 'row', justifyContent: 'space-between' }} gap={8}>
<Text className={classes.fieldTitle}>Hot Tier Storage Size</Text>
{!hotTierNotSet ? (
<Group style={{ justifyContent: 'end' }}>
<IconButton
size={38}
renderIcon={renderRefreshIcon}
onClick={props.refetchHotTierInfo}
tooltipLabel="Refresh now"
/>
</Group>
) : null}
</Stack>
<Stack style={{ flexDirection: 'row', height: '6.8rem' }}>
<Stack style={{ width: hotTierNotSet ? '100%' : '50%' }} gap={isDirty || hotTierNotSet ? 16 : 4}>
<Stack gap={12}>
{streamType === 'UserDefined' ? (
<NumberInput
w={hotTierNotSet ? '100%' : '50%'}
classNames={{ label: classes.fieldDescription }}
placeholder="Size in GiB"
key="size"
value={localSizeValue}
onChange={onChangeHandler}
min={0}
suffix=" GiB"
style={{ flex: 1 }}
/>
) : !hotTierNotSet ? (
<DisabledInput size={humanizedSize} />
) : null}
<Text
className={classes.fieldDescription}
ta="start"
style={{ ...(isDirty || hotTierNotSet ? { display: 'none' } : {}) }}>
{humanizedUsedSize} used | {humanizedAvailableSize} available
</Text>
</Stack>
<Stack
style={{
flexDirection: 'row',
justifyContent: 'flex-start',
...(!isDirty || hotTierNotSet ? { display: 'none' } : {}),
}}
gap={12}>
<Stack
className={classes.actionIconClose}
p="0.1rem"
onClick={onCancel}
style={{
...(props.isUpdating ? { display: 'none' } : {}),
}}>
<IconX stroke={1.4} size="1rem" />
</Stack>
<Stack
style={{
...(props.isUpdating ? { display: 'none' } : {}),
}}
className={classes.actionIconCheck}
p="0.1rem"
onClick={onUpdate}>
<IconCheck stroke={1.4} size="1.1rem" />
</Stack>
{props.isUpdating && <Loader size="sm" />}
</Stack>
{!hotTierNotSet && streamType === 'UserDefined' ? (
<Stack
style={{ alignItems: 'flex-start', paddingTop: '0.8rem', ...(hotTierNotSet ? { display: 'none' } : {}) }}>
<Box>
<Button variant="outline" onClick={openDeleteModal}>
Delete
</Button>
</Box>
</Stack>
) : null}
<Stack style={{ alignItems: 'flex-end', ...(!hotTierNotSet ? { display: 'none' } : {}) }}>
<Box>
<Button onClick={onUpdate} disabled={localSizeValue <= 0} loading={props.isUpdating}>
Submit
</Button>
</Box>
</Stack>
</Stack>
<Divider orientation="vertical" size={2} style={{ ...(hotTierNotSet ? { display: 'none' } : {}) }} />
<Stack
gap={4}
style={{ ...(hotTierNotSet ? { display: 'none' } : { display: 'flex', justifyContent: 'flex-start' }) }}>
<Text style={{ fontSize: '11.2px' }}>Oldest Record:</Text>
<Text className={classes.fieldDescription}>
{_.isEmpty(oldestEntry) ? 'No Entries Stored' : formatDateWithTimezone(oldestEntry)}
</Text>
</Stack>
</Stack>
</Stack>
);
};
const DisabledInput = (props: { size: string }) => {
return <TextInput classNames={{ label: classes.fieldDescription }} disabled value={props.size} style={{ flex: 1 }} />;
};
const Settings = (props: {
isLoading: boolean;
updateRetentionConfig: ({ config }: { config: any }) => void;
updateHotTierInfo: ({ size }: { size: string }) => void;
refetchHotTierInfo: () => void;
deleteHotTierInfo: ({ onSuccess }: { onSuccess: () => void }) => void;
isDeleting: boolean;
isUpdating: boolean;
isRetentionError: boolean;
hasSettingsAccess: boolean;
}) => {
return (
<Stack className={classes.sectionContainer} gap={0} w="100%">
<Header />
<Stack gap={0} h="100%" pr="0.65rem" pl="0.65rem">
{props.isLoading ? (
<Stack style={{ flex: 1, width: '100%', alignItems: 'centrer', justifyContent: 'center' }}>
<Stack style={{ alignItems: 'center' }}>
<Loader />
</Stack>
</Stack>
) : (
<>
{!props.hasSettingsAccess ? (
<RestrictedView />
) : (
<>
<HotTierConfig
refetchHotTierInfo={props.refetchHotTierInfo}
updateHotTierInfo={props.updateHotTierInfo}
deleteHotTierInfo={props.deleteHotTierInfo}
isDeleting={props.isDeleting}
isUpdating={props.isUpdating}
/>
<Divider />
<Stack className={classes.fieldsContainer} style={{ border: 'none', flex: 1, gap: 4 }}>
<Text className={classes.fieldTitle}>Retention</Text>
{!props.isRetentionError ? (
<RetentionForm updateRetentionConfig={props.updateRetentionConfig} />
) : (
<ErrorView />
)}
</Stack>
</>
)}
</>
)}
</Stack>
</Stack>
);
};
export default Settings;