Skip to content

Commit 5b842db

Browse files
fix broken anomaly detector monitor defining method (#1371)
Signed-off-by: Jackie <jkhanjob@gmail.com>
1 parent ce12951 commit 5b842db

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

public/pages/CreateMonitor/containers/AnomalyDetectors/AnomalyDetectors.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,38 @@ class AnomalyDetectors extends React.Component {
2020
this.state = {
2121
detectorOptions: [],
2222
isLoading: false,
23+
lastFetchedDataSourceId: undefined,
2324
};
2425
this.searchDetectors = this.searchDetectors.bind(this);
2526
}
2627
async componentDidMount() {
2728
await this.searchDetectors();
2829
}
2930

31+
async componentDidUpdate(prevProps) {
32+
if (prevProps.landingDataSourceId !== this.props.landingDataSourceId) {
33+
await this.searchDetectors();
34+
}
35+
}
36+
3037
async searchDetectors() {
3138
const httpClient = getClient();
3239
try {
33-
const dataSourceQuery = getDataSourceQueryObj();
40+
// Prefer the landing dataSourceId when available.
41+
// Fallback to the global helper
42+
const dataSourceQuery = this.props.landingDataSourceId
43+
? { query: { dataSourceId: this.props.landingDataSourceId } }
44+
: getDataSourceQueryObj();
45+
46+
// Avoid refetching if we're already fetched for this dataSourceId
47+
const currentDataSourceId = dataSourceQuery?.query?.dataSourceId;
48+
if (
49+
this.state.lastFetchedDataSourceId === currentDataSourceId &&
50+
this.state.detectorOptions.length
51+
) {
52+
return;
53+
}
54+
3455
const response = await httpClient.post('../api/alerting/detectors/_search', {
3556
query: dataSourceQuery?.query,
3657
});
@@ -44,7 +65,7 @@ class AnomalyDetectors extends React.Component {
4465
interval: detector.detectionInterval,
4566
resultIndex: detector.resultIndex,
4667
}));
47-
this.setState({ detectorOptions });
68+
this.setState({ detectorOptions, lastFetchedDataSourceId: currentDataSourceId });
4869
} else {
4970
// TODO: 'response.ok' is 'false' when there is no anomaly-detection config index in the cluster, and notification should not be shown to new Anomaly-Detection users
5071
// backendErrorNotification(notifications, 'get', 'detectors', response.resp);
@@ -129,6 +150,7 @@ class AnomalyDetectors extends React.Component {
129150
AnomalyDetectors.propTypes = {
130151
values: PropTypes.object.isRequired,
131152
renderEmptyMessage: PropTypes.func.isRequired,
153+
landingDataSourceId: PropTypes.string,
132154
};
133155

134156
export default AnomalyDetectors;

public/pages/CreateMonitor/containers/AnomalyDetectors/__tests__/AnomalyDetector.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ describe('AnomalyDetectors', () => {
4747
expect(wrapper).toMatchSnapshot();
4848
});
4949

50+
test('refetches detectors when landingDataSourceId changes', async () => {
51+
httpClient.post.mockResolvedValue({ ok: true, detectors: [] });
52+
const TestHarness = ({ landingDataSourceId }) => (
53+
<CoreContext.Provider value={{ http: httpClientMock }}>
54+
<Formik initialValues={FORMIK_INITIAL_VALUES}>
55+
{({ values }) => (
56+
<AnomalyDetectors
57+
values={values}
58+
renderEmptyMessage={renderEmptyMessage}
59+
landingDataSourceId={landingDataSourceId}
60+
/>
61+
)}
62+
</Formik>
63+
</CoreContext.Provider>
64+
);
65+
66+
const wrapper = mount(<TestHarness landingDataSourceId={undefined} />);
67+
68+
await runAllPromises();
69+
wrapper.update();
70+
71+
// Change the data source id and ensure we re-fetch with the new query param.
72+
wrapper.setProps({ landingDataSourceId: 'demo' });
73+
74+
await runAllPromises();
75+
wrapper.update();
76+
77+
const lastCallArgs = httpClient.post.mock.calls[httpClient.post.mock.calls.length - 1];
78+
expect(lastCallArgs[0]).toEqual('../api/alerting/detectors/_search');
79+
expect(lastCallArgs[1]).toMatchObject({ query: { dataSourceId: 'demo' } });
80+
});
81+
5082
test('should be able to select the detector', async () => {
5183
httpClientMock.post.mockResolvedValueOnce({
5284
ok: true,

public/pages/CreateMonitor/containers/CreateMonitor/CreateMonitor.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ export default class CreateMonitor extends Component {
253253
/>
254254
) : null
255255
}
256+
landingDataSourceId={this.props.landingDataSourceId}
256257
/>
257258

258259
{values.preventVisualEditor ? null : (

public/pages/CreateMonitor/containers/MonitorDetails/MonitorDetails.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ import AnomalyDetectors from '../AnomalyDetectors/AnomalyDetectors';
1414
import { MONITOR_TYPE } from '../../../../utils/constants';
1515
import Schedule from '../../components/Schedule';
1616

17-
const renderAnomalyDetector = ({ httpClient, values, detectorId, flyoutMode }) => ({
17+
const renderAnomalyDetector = ({
18+
httpClient,
19+
values,
20+
detectorId,
21+
flyoutMode,
22+
landingDataSourceId,
23+
}) => ({
1824
actions: [],
1925
content: (
2026
<React.Fragment>
@@ -24,6 +30,7 @@ const renderAnomalyDetector = ({ httpClient, values, detectorId, flyoutMode }) =
2430
renderEmptyMessage={renderEmptyMessage}
2531
detectorId={detectorId}
2632
flyoutMode={flyoutMode}
33+
landingDataSourceId={landingDataSourceId}
2734
/>
2835
</React.Fragment>
2936
),
@@ -51,9 +58,11 @@ const MonitorDetails = ({
5158
detectorId,
5259
flyoutMode,
5360
renderNewToggle,
61+
landingDataSourceId,
5462
}) => {
5563
const anomalyDetectorContent =
56-
isAd && renderAnomalyDetector({ httpClient, values, detectorId, flyoutMode });
64+
isAd &&
65+
renderAnomalyDetector({ httpClient, values, detectorId, flyoutMode, landingDataSourceId });
5766
const displayMonitorDefinitionCards = values.monitor_type !== MONITOR_TYPE.CLUSTER_METRICS;
5867
const Container = useMemo(
5968
() => (flyoutMode ? ({ children }) => <>{children}</> : ContentPanel),

server/services/AnomalyDetectorService.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import { get } from 'lodash';
66
import { mapKeysDeep, toCamel } from './utils/helpers';
77
import { anomalyResultMapper } from './utils/adHelpers';
88
import { MDSEnabledClientService } from './MDSEnabledClientService';
9+
import { DEFAULT_HEADERS } from './utils/constants';
910

1011
const MAX_DETECTOR_COUNT = 1000;
1112
export default class DestinationsService extends MDSEnabledClientService {
1213
getDetector = async (context, req, res) => {
1314
const { detectorId } = req.params;
1415
const client = this.getClientBasedOnDataSource(context, req);
1516
try {
16-
const resp = await client('alertingAD.getDetector', { detectorId });
17+
const resp = await client('alertingAD.getDetector', { detectorId, headers: DEFAULT_HEADERS });
1718
const {
1819
anomaly_detector,
1920
_seq_no: seqNo,
@@ -49,6 +50,7 @@ export default class DestinationsService extends MDSEnabledClientService {
4950
try {
5051
const resp = await client('alertingAD.searchDetectors', {
5152
body: searchRequest,
53+
headers: DEFAULT_HEADERS,
5254
});
5355

5456
const totalDetectors = resp.hits.total.value;
@@ -93,6 +95,7 @@ export default class DestinationsService extends MDSEnabledClientService {
9395
const previewResponse = await client('alertingAD.previewDetector', {
9496
detectorId,
9597
body: requestBody,
98+
headers: DEFAULT_HEADERS,
9699
});
97100
const transformedKeys = mapKeysDeep(previewResponse, toCamel);
98101
return res.ok({
@@ -133,9 +136,11 @@ export default class DestinationsService extends MDSEnabledClientService {
133136
};
134137
const detectorResponse = await client('alertingAD.getDetector', {
135138
detectorId,
139+
headers: DEFAULT_HEADERS,
136140
});
137141
const anomaliesResponse = await client('alertingAD.searchResults', {
138142
body: requestBody,
143+
headers: DEFAULT_HEADERS,
139144
});
140145
const transformedKeys = get(anomaliesResponse, 'hits.hits', []).map((result) =>
141146
mapKeysDeep(result._source, toCamel)

0 commit comments

Comments
 (0)