mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[ML] Fix notifications count
endpoint when no ML entity is present (#143361)
* fix count endpoint when no ML entity present * remove dummy generic
This commit is contained in:
parent
33ccea1022
commit
ce455e99b2
2 changed files with 66 additions and 40 deletions
|
@ -28,6 +28,14 @@ export class NotificationsService {
|
|||
private readonly mlSavedObjectService: MLSavedObjectService
|
||||
) {}
|
||||
|
||||
private getDefaultCountResponse() {
|
||||
return {
|
||||
error: 0,
|
||||
warning: 0,
|
||||
info: 0,
|
||||
} as NotificationsCountResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides entity IDs per type for the current space.
|
||||
* @private
|
||||
|
@ -217,7 +225,11 @@ export class NotificationsService {
|
|||
},
|
||||
});
|
||||
|
||||
const byLevel = responseBody.aggregations!
|
||||
if (!responseBody.aggregations) {
|
||||
return this.getDefaultCountResponse();
|
||||
}
|
||||
|
||||
const byLevel = responseBody.aggregations
|
||||
.by_level as estypes.AggregationsMultiBucketAggregateBase<estypes.AggregationsStringTermsBucketKeys>;
|
||||
|
||||
return Array.isArray(byLevel.buckets)
|
||||
|
@ -229,17 +241,14 @@ export class NotificationsService {
|
|||
})
|
||||
);
|
||||
|
||||
return res.reduce(
|
||||
(acc, curr) => {
|
||||
for (const levelKey in curr) {
|
||||
if (curr.hasOwnProperty(levelKey)) {
|
||||
acc[levelKey as MlNotificationMessageLevel] +=
|
||||
curr[levelKey as MlNotificationMessageLevel];
|
||||
}
|
||||
return res.reduce((acc, curr) => {
|
||||
for (const levelKey in curr) {
|
||||
if (curr.hasOwnProperty(levelKey)) {
|
||||
acc[levelKey as MlNotificationMessageLevel] +=
|
||||
curr[levelKey as MlNotificationMessageLevel];
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{ error: 0, warning: 0, info: 0 } as NotificationsCountResponse
|
||||
);
|
||||
}
|
||||
return acc;
|
||||
}, this.getDefaultCountResponse());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,40 +16,57 @@ export default ({ getService }: FtrProviderContext) => {
|
|||
const ml = getService('ml');
|
||||
|
||||
describe('GET notifications count', () => {
|
||||
before(async () => {
|
||||
await ml.api.initSavedObjects();
|
||||
await ml.testResources.setKibanaTimeZoneToUTC();
|
||||
describe('when no ML entities present', () => {
|
||||
it('return a default response', async () => {
|
||||
const { body, status } = await supertest
|
||||
.get(`/api/ml/notifications/count`)
|
||||
.query({ lastCheckedAt: moment().subtract(7, 'd').valueOf() })
|
||||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
|
||||
.set(COMMON_REQUEST_HEADERS);
|
||||
ml.api.assertResponseStatusCode(200, status, body);
|
||||
|
||||
const adJobConfig = ml.commonConfig.getADFqSingleMetricJobConfig('fq_job');
|
||||
await ml.api.createAnomalyDetectionJob(adJobConfig);
|
||||
|
||||
await ml.api.waitForJobNotificationsToIndex('fq_job');
|
||||
expect(body.info).to.eql(0);
|
||||
expect(body.warning).to.eql(0);
|
||||
expect(body.error).to.eql(0);
|
||||
});
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await ml.api.cleanMlIndices();
|
||||
await ml.testResources.cleanMLSavedObjects();
|
||||
});
|
||||
describe('when ML entities exist', () => {
|
||||
before(async () => {
|
||||
await ml.api.initSavedObjects();
|
||||
await ml.testResources.setKibanaTimeZoneToUTC();
|
||||
|
||||
it('return notifications count by level', async () => {
|
||||
const { body, status } = await supertest
|
||||
.get(`/api/ml/notifications/count`)
|
||||
.query({ lastCheckedAt: moment().subtract(7, 'd').valueOf() })
|
||||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
|
||||
.set(COMMON_REQUEST_HEADERS);
|
||||
ml.api.assertResponseStatusCode(200, status, body);
|
||||
const adJobConfig = ml.commonConfig.getADFqSingleMetricJobConfig('fq_job');
|
||||
await ml.api.createAnomalyDetectionJob(adJobConfig);
|
||||
|
||||
expect(body.info).to.eql(1);
|
||||
expect(body.warning).to.eql(0);
|
||||
expect(body.error).to.eql(0);
|
||||
});
|
||||
await ml.api.waitForJobNotificationsToIndex('fq_job');
|
||||
});
|
||||
|
||||
it('returns an error for unauthorized user', async () => {
|
||||
const { body, status } = await supertest
|
||||
.get(`/api/ml/notifications/count`)
|
||||
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
|
||||
.set(COMMON_REQUEST_HEADERS);
|
||||
ml.api.assertResponseStatusCode(403, status, body);
|
||||
after(async () => {
|
||||
await ml.api.cleanMlIndices();
|
||||
await ml.testResources.cleanMLSavedObjects();
|
||||
});
|
||||
|
||||
it('return notifications count by level', async () => {
|
||||
const { body, status } = await supertest
|
||||
.get(`/api/ml/notifications/count`)
|
||||
.query({ lastCheckedAt: moment().subtract(7, 'd').valueOf() })
|
||||
.auth(USER.ML_POWERUSER, ml.securityCommon.getPasswordForUser(USER.ML_POWERUSER))
|
||||
.set(COMMON_REQUEST_HEADERS);
|
||||
ml.api.assertResponseStatusCode(200, status, body);
|
||||
|
||||
expect(body.info).to.eql(1);
|
||||
expect(body.warning).to.eql(0);
|
||||
expect(body.error).to.eql(0);
|
||||
});
|
||||
|
||||
it('returns an error for unauthorized user', async () => {
|
||||
const { body, status } = await supertest
|
||||
.get(`/api/ml/notifications/count`)
|
||||
.auth(USER.ML_UNAUTHORIZED, ml.securityCommon.getPasswordForUser(USER.ML_UNAUTHORIZED))
|
||||
.set(COMMON_REQUEST_HEADERS);
|
||||
ml.api.assertResponseStatusCode(403, status, body);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue