-
Notifications
You must be signed in to change notification settings - Fork 12.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alerting: Adds support for timezones in mute timings #68813
Changes from 5 commits
b429a2f
d5e326a
bf16393
9c5285f
ecb3438
d81aeb9
a981705
4c83583
52f37e7
b028ee7
37864b5
64480ba
b55d937
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { css } from '@emotion/css'; | ||
import React, { useMemo } from 'react'; | ||
import React, { useState } from 'react'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
|
||
import { GrafanaTheme2, NavModelItem } from '@grafana/data'; | ||
import { Alert, Button, Field, FieldSet, Input, LinkButton, useStyles2 } from '@grafana/ui'; | ||
import { Alert, Button, Field, FieldSet, Input, LinkButton, LoadingPlaceholder, useStyles2 } from '@grafana/ui'; | ||
import { | ||
AlertmanagerConfig, | ||
AlertManagerCortexConfig, | ||
|
@@ -30,47 +30,49 @@ interface Props { | |
muteTiming?: MuteTimeInterval; | ||
showError?: boolean; | ||
provenance?: string; | ||
loading?: boolean; | ||
} | ||
|
||
const useDefaultValues = (muteTiming?: MuteTimeInterval): MuteTimingFields => { | ||
return useMemo(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Diff here looks a bit weird but removed the unnecessary |
||
const defaultValues = { | ||
name: '', | ||
time_intervals: [defaultTimeInterval], | ||
}; | ||
const defaultValues = { | ||
name: '', | ||
time_intervals: [defaultTimeInterval], | ||
}; | ||
|
||
if (!muteTiming) { | ||
return defaultValues; | ||
} | ||
|
||
const intervals = muteTiming.time_intervals.map((interval) => ({ | ||
times: interval.times ?? defaultTimeInterval.times, | ||
weekdays: interval?.weekdays?.join(', ') ?? defaultTimeInterval.weekdays, | ||
days_of_month: interval?.days_of_month?.join(', ') ?? defaultTimeInterval.days_of_month, | ||
months: interval?.months?.join(', ') ?? defaultTimeInterval.months, | ||
years: interval?.years?.join(', ') ?? defaultTimeInterval.years, | ||
})); | ||
|
||
return { | ||
name: muteTiming.name, | ||
time_intervals: intervals, | ||
}; | ||
}, [muteTiming]); | ||
if (!muteTiming) { | ||
return defaultValues; | ||
} | ||
|
||
const intervals = muteTiming.time_intervals.map((interval) => ({ | ||
times: interval.times ?? defaultTimeInterval.times, | ||
weekdays: interval.weekdays?.join(', ') ?? defaultTimeInterval.weekdays, | ||
days_of_month: interval.days_of_month?.join(', ') ?? defaultTimeInterval.days_of_month, | ||
months: interval.months?.join(', ') ?? defaultTimeInterval.months, | ||
years: interval.years?.join(', ') ?? defaultTimeInterval.years, | ||
location: interval.location ?? defaultTimeInterval.location, | ||
})); | ||
|
||
return { | ||
name: muteTiming.name, | ||
time_intervals: intervals, | ||
}; | ||
}; | ||
|
||
const defaultPageNav: Partial<NavModelItem> = { | ||
icon: 'sitemap', | ||
}; | ||
|
||
const MuteTimingForm = ({ muteTiming, showError, provenance }: Props) => { | ||
const MuteTimingForm = ({ muteTiming, showError, loading, provenance }: Props) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const dispatch = useDispatch(); | ||
const alertManagers = useAlertManagersByPermission('notification'); | ||
const [alertManagerSourceName, setAlertManagerSourceName] = useAlertManagerSourceName(alertManagers); | ||
const styles = useStyles2(getStyles); | ||
|
||
const [updating, setUpdating] = useState(false); | ||
|
||
const defaultAmCortexConfig = { alertmanager_config: {}, template_files: {} }; | ||
const amConfigs = useUnifiedAlertingSelector((state) => state.amConfigs); | ||
const { result = defaultAmCortexConfig, loading } = | ||
const { result = defaultAmCortexConfig } = | ||
(alertManagerSourceName && amConfigs[alertManagerSourceName]) || initialAsyncRequestState; | ||
|
||
const config: AlertmanagerConfig = result?.alertmanager_config ?? {}; | ||
|
@@ -96,15 +98,22 @@ const MuteTimingForm = ({ muteTiming, showError, provenance }: Props) => { | |
}, | ||
}; | ||
|
||
dispatch( | ||
const saveAction = dispatch( | ||
updateAlertManagerConfigAction({ | ||
newConfig, | ||
oldConfig: result, | ||
alertManagerSourceName: alertManagerSourceName!, | ||
successMessage: 'Mute timing saved', | ||
redirectPath: '/alerting/routes/', | ||
redirectSearch: 'tab=mute_timings', | ||
}) | ||
); | ||
|
||
setUpdating(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're disabling the form when the AM is still updating with changes – previously we immediately redirected and hoped it would have saved the changes. |
||
|
||
saveAction.unwrap().finally(() => { | ||
setUpdating(false); | ||
}); | ||
}; | ||
|
||
return ( | ||
|
@@ -123,11 +132,12 @@ const MuteTimingForm = ({ muteTiming, showError, provenance }: Props) => { | |
dataSources={alertManagers} | ||
/> | ||
{provenance && <ProvisioningAlert resource={ProvisionedResource.MuteTiming} />} | ||
{result && !loading && ( | ||
{loading && <LoadingPlaceholder text="Loading mute timing" />} | ||
{showError && <Alert title="No matching mute timing found" />} | ||
{result && !loading && !showError && ( | ||
<FormProvider {...formApi}> | ||
<form onSubmit={formApi.handleSubmit(onSubmit)} data-testid="mute-timing-form"> | ||
{showError && <Alert title="No matching mute timing found" />} | ||
<FieldSet label={'Create mute timing'} disabled={Boolean(provenance)}> | ||
<FieldSet label={'Create mute timing'} disabled={Boolean(provenance) || updating}> | ||
<Field | ||
required | ||
label="Name" | ||
|
@@ -143,21 +153,23 @@ const MuteTimingForm = ({ muteTiming, showError, provenance }: Props) => { | |
const existingMuteTiming = config?.mute_time_intervals?.find(({ name }) => value === name); | ||
return existingMuteTiming ? `Mute timing already exists for "${value}"` : true; | ||
} | ||
return value.length > 0 || 'Name is required'; | ||
return; | ||
}, | ||
})} | ||
className={styles.input} | ||
data-testid={'mute-timing-name'} | ||
/> | ||
</Field> | ||
<MuteTimingTimeInterval /> | ||
<Button type="submit" className={styles.submitButton}> | ||
<Button type="submit" className={styles.submitButton} disabled={updating}> | ||
Save mute timing | ||
</Button> | ||
<LinkButton | ||
type="button" | ||
variant="secondary" | ||
href={makeAMLink('/alerting/routes/', alertManagerSourceName)} | ||
fill="outline" | ||
href={makeAMLink('/alerting/routes/', alertManagerSourceName, { tab: 'mute_timings' })} | ||
disabled={updating} | ||
> | ||
Cancel | ||
</LinkButton> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this needs some comment about what timezone syntax is acceptable?