Skip to content

Commit

Permalink
fix no alerts + graph to get greyed out
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Aug 1, 2022
1 parent a2b98d4 commit 1c96337
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export async function fetchRuleAlertsAggByTimeRange({
(
acc: AlertChartData[],
dayAlerts: {
key_as_string: string;
key: number;
doc_count: number;
alertStatus: {
buckets: Array<{
Expand All @@ -226,8 +226,8 @@ export async function fetchRuleAlertsAggByTimeRange({
) => {
// We are adding this to each day to construct the 30 days bars (background bar) when there is no data for a given day or to show the delta today alerts/total alerts.
const totalDayAlerts = {
date: dayAlerts.key_as_string,
count: totalAlerts,
date: dayAlerts.key,
count: totalAlerts === 0 ? 1 : totalAlerts,
status: 'total',
};

Expand All @@ -238,14 +238,14 @@ export async function fetchRuleAlertsAggByTimeRange({
dayAlerts.alertStatus.buckets.forEach((alert) => {
countOfRecoveredActiveAlerts += alert.doc_count;
localAlertChartData.push({
date: dayAlerts.key_as_string,
date: dayAlerts.key,
count: alert.doc_count,
status: alert.key,
});
});
if (totalAlerts - countOfRecoveredActiveAlerts > 0) {
localAlertChartData.push({
date: dayAlerts.key_as_string,
date: dayAlerts.key,
count: totalAlerts - countOfRecoveredActiveAlerts,
status: 'total',
});
Expand All @@ -261,7 +261,11 @@ export async function fetchRuleAlertsAggByTimeRange({
return {
active,
recovered,
alertsChartData,
alertsChartData: [
...alertsChartData.filter((acd) => acd.status === 'active'),
...alertsChartData.filter((acd) => acd.status === 'recovered'),
...alertsChartData.filter((acd) => acd.status === 'total'),
],
};
} catch (error) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AlertChartData } from './types';

export const formatChartAlertData = (
data: AlertChartData[]
): Array<{ x: string; y: number; g: string }> =>
): Array<{ x: number; y: number; g: string }> =>
data.map((alert) => ({
x: alert.date,
y: alert.count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
EUI_SPARKLINE_THEME_PARTIAL,
} from '@elastic/eui/dist/eui_charts_theme';
import { useUiSetting } from '@kbn/kibana-react-plugin/public';
import moment from 'moment';
import { useLoadRuleAlertsAggs } from '../../../../hooks/use_load_rule_alerts_aggregations';
import { useLoadRuleTypes } from '../../../../hooks/use_load_rule_types';
import { formatChartAlertData, getColorSeries } from '.';
Expand All @@ -44,6 +45,7 @@ const G_ACCESSORS = ['g'];
export const RuleAlertsSummary = ({ rule, filteredRuleTypes }: RuleAlertsSummaryProps) => {
const [features, setFeatures] = useState<string>('');
const isDarkMode = useUiSetting<boolean>('theme:darkMode');
const dateFormat = useUiSetting<string>('dateFormat');
const theme = useMemo(
() => [
EUI_SPARKLINE_THEME_PARTIAL,
Expand All @@ -64,6 +66,15 @@ export const RuleAlertsSummary = ({ rule, filteredRuleTypes }: RuleAlertsSummary
features,
});
const chartData = useMemo(() => formatChartAlertData(alertsChartData), [alertsChartData]);
const tooltipSettings = useMemo(
() => ({
type: TooltipType.VerticalCursor,
headerFormatter: ({ value }: { value: number }) => {
return <>{moment(value).format(dateFormat)}</>;
},
}),
[dateFormat]
);

useEffect(() => {
const matchedRuleType = ruleTypes.find((type) => type.id === rule.ruleTypeId);
Expand Down Expand Up @@ -184,7 +195,7 @@ export const RuleAlertsSummary = ({ rule, filteredRuleTypes }: RuleAlertsSummary
</EuiFlexGroup>
<EuiSpacer size="m" />
<Chart size={{ height: 50 }}>
<Settings tooltip={TooltipType.VerticalCursor} theme={theme} />
<Settings tooltip={tooltipSettings} theme={theme} />
<BarSeries
id="bars"
xScaleType={ScaleType.Time}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface RuleAlertsSummaryProps {
export interface AlertChartData {
status: 'active' | 'recovered' | 'total';
count: number;
date: string;
date: number;
}

export interface AlertsChartProps {
Expand Down

0 comments on commit 1c96337

Please sign in to comment.