Skip to content

Commit 5a91247

Browse files
committed
feat(ou-923): fix links in incidents table
1 parent bf09741 commit 5a91247

File tree

3 files changed

+33
-64
lines changed

3 files changed

+33
-64
lines changed

web/src/components/Incidents/IncidentsDetailsRowTable.tsx

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,59 +14,20 @@ import KebabDropdown from '../kebab-dropdown';
1414
import { useTranslation } from 'react-i18next';
1515
import {
1616
getAlertUrl,
17-
getLegacyObserveState,
1817
getNewSilenceAlertUrl,
1918
getRuleUrl,
2019
usePerspective,
2120
} from '../hooks/usePerspective';
2221
import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons';
2322
import './incidents-styles.css';
2423
import { SeverityBadge } from '../alerting/AlertUtils';
25-
import { useAlertsPoller } from '../hooks/useAlertsPoller';
26-
import { useSelector } from 'react-redux';
27-
import isEqual from 'lodash/isEqual';
28-
import { MonitoringState } from 'src/reducers/observe';
29-
30-
function useDeepCompareMemoize(value) {
31-
const ref = React.useRef();
32-
33-
if (!isEqual(value, ref.current)) {
34-
ref.current = value;
35-
}
36-
37-
return ref.current;
38-
}
3924

4025
const IncidentsDetailsRowTable = ({ alerts }) => {
4126
const history = useHistory();
4227
const [namespace] = useActiveNamespace();
43-
useAlertsPoller();
44-
const { perspective, alertsKey } = usePerspective();
45-
const [alertsWithMatchedData, setAlertsWithMatchedData] = React.useState([]);
28+
const { perspective } = usePerspective();
4629
const { t } = useTranslation(process.env.I18N_NAMESPACE);
4730

48-
const alertsWithLabels = useSelector((state: MonitoringState) =>
49-
getLegacyObserveState(perspective, state)?.get(alertsKey),
50-
);
51-
52-
function findMatchingAlertsWithId(alertsArray, rulesArray) {
53-
return alertsArray.map((alert) => {
54-
if (!Array.isArray(rulesArray?.data)) {
55-
return alert;
56-
}
57-
const match = rulesArray.data.find((rule) => alert.alertname === rule.labels.alertname);
58-
if (match) {
59-
return { ...alert, rule: match };
60-
}
61-
return alert;
62-
});
63-
}
64-
const memoizedAlerts = useDeepCompareMemoize(alerts);
65-
66-
React.useEffect(() => {
67-
setAlertsWithMatchedData(findMatchingAlertsWithId(memoizedAlerts, alertsWithLabels));
68-
}, [memoizedAlerts, alertsWithLabels]);
69-
7031
return (
7132
<Table borders={false} variant="compact">
7233
<Thead>
@@ -80,31 +41,26 @@ const IncidentsDetailsRowTable = ({ alerts }) => {
8041
</Tr>
8142
</Thead>
8243
<Tbody>
83-
{!alertsWithMatchedData ? (
44+
{!alerts ? (
8445
<Bullseye>
8546
<Spinner aria-label="incidents-chart-spinner" />
8647
</Bullseye>
8748
) : (
88-
alertsWithMatchedData?.map((alertDetails, rowIndex) => {
49+
alerts?.map((alertDetails, rowIndex) => {
8950
return (
9051
<Tr key={rowIndex}>
9152
<Td dataLabel="expanded-details-alertname">
9253
<ResourceIcon kind={AlertResource.kind} />
9354
<Link
9455
to={
95-
alertDetails?.rule
56+
alertDetails.rule.state === 'firing'
9657
? getAlertUrl(
9758
perspective,
9859
alertDetails,
99-
alertDetails?.rule?.rule.id,
100-
namespace,
60+
alertDetails?.rule?.id,
61+
alertDetails?.labels?.namespace || namespace,
10162
)
102-
: '#'
103-
}
104-
style={
105-
!alertDetails?.rule || alertDetails.resolved
106-
? { pointerEvents: 'none', color: 'inherit', textDecoration: 'inherit' }
107-
: {}
63+
: getRuleUrl(perspective, alertDetails?.rule, namespace)
10864
}
10965
>
11066
{alertDetails.alertname}
@@ -162,7 +118,7 @@ const IncidentsDetailsRowTable = ({ alerts }) => {
162118
isDisabled={alertDetails?.alertstate === 'resolved' ? true : false}
163119
>
164120
<Link
165-
to={getRuleUrl(perspective, alertDetails?.rule?.rule)}
121+
to={getRuleUrl(perspective, alertDetails?.rule)}
166122
style={{ color: 'inherit', textDecoration: 'inherit' }}
167123
>
168124
{t('View alerting rule')}

web/src/components/Incidents/IncidentsPage.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
setIncidentsActiveFilters,
5050
} from '../../actions/observe';
5151
import { useLocation } from 'react-router-dom';
52-
import { usePerspective } from '../hooks/usePerspective';
52+
import { getLegacyObserveState, usePerspective } from '../hooks/usePerspective';
5353
import { changeDaysFilter } from './utils';
5454
import { parsePrometheusDuration } from '../console/console-shared/src/datetime/prometheus';
5555
import withFallback from '../console/console-shared/error/fallbacks/withFallback';
@@ -58,13 +58,16 @@ import AlertsChart from './AlertsChart/AlertsChart';
5858
import { usePatternFlyTheme } from '../hooks/usePatternflyTheme';
5959
import { MonitoringState } from 'src/reducers/observe';
6060
import { Incident } from './model';
61+
import { useAlertsPoller } from '../hooks/useAlertsPoller';
62+
import { Rule } from '@openshift-console/dynamic-plugin-sdk';
6163

6264
const IncidentsPage = () => {
6365
const { t } = useTranslation(process.env.I18N_NAMESPACE);
6466
const dispatch = useDispatch();
6567
const location = useLocation();
6668
const urlParams = React.useMemo(() => parseUrlParams(location.search), [location.search]);
67-
const { perspective } = usePerspective();
69+
const { perspective, rulesKey } = usePerspective();
70+
useAlertsPoller();
6871
const { theme } = usePatternFlyTheme();
6972
// loading states
7073
const [incidentsAreLoading, setIncidentsAreLoading] = React.useState(true);
@@ -206,13 +209,19 @@ const IncidentsPage = () => {
206209
})();
207210
}, [incidentForAlertProcessing]);
208211

212+
const alertingRulesData: Rule[] = useSelector((state: MonitoringState) =>
213+
getLegacyObserveState(perspective, state)?.get(rulesKey),
214+
);
215+
209216
React.useEffect(() => {
210-
dispatch(
211-
setAlertsTableData({
212-
alertsTableData: groupAlertsForTable(alertsData),
213-
}),
214-
);
215-
}, [alertsData]);
217+
if (alertingRulesData && alertsData) {
218+
dispatch(
219+
setAlertsTableData({
220+
alertsTableData: groupAlertsForTable(alertsData, alertingRulesData),
221+
}),
222+
);
223+
}
224+
}, [alertsData, alertingRulesData]);
216225

217226
React.useEffect(() => {
218227
(async () => {

web/src/components/Incidents/processAlerts.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,17 @@ export function processAlerts(
188188
.filter((alert) => alert !== null);
189189
}
190190

191-
export const groupAlertsForTable = (alerts: Array<Alert>): Array<Alert> => {
191+
export const groupAlertsForTable = (alerts: Array<Alert>, alertingRulesData): Array<Alert> => {
192192
// group alerts by the component and coun
193193
const groupedAlerts: Array<Alert> = alerts.reduce((acc, alert) => {
194-
const { component, alertstate, severity, layer } = alert;
194+
const { component, alertstate, severity, layer, alertname } = alert;
195195
const existingGroup = acc.find((group) => group.component === component);
196+
let rule;
197+
if (alertingRulesData) {
198+
rule = alertingRulesData.find((rule) => alertname === rule.name);
199+
}
196200
if (existingGroup) {
197-
existingGroup.alertsExpandedRowData.push(alert);
201+
existingGroup.alertsExpandedRowData.push({ ...alert, rule });
198202
if (severity === 'warning') existingGroup.warning += 1;
199203
else if (severity === 'info') existingGroup.info += 1;
200204
else if (severity === 'critical') existingGroup.critical += 1;
@@ -206,7 +210,7 @@ export const groupAlertsForTable = (alerts: Array<Alert>): Array<Alert> => {
206210
warning: severity === 'warning' ? 1 : 0,
207211
info: severity === 'info' ? 1 : 0,
208212
critical: severity === 'critical' ? 1 : 0,
209-
alertsExpandedRowData: [alert],
213+
alertsExpandedRowData: [{ ...alert, rule }],
210214
});
211215
}
212216

0 commit comments

Comments
 (0)