[ML] Adding get buckets endpoint wrapper (#57752)

* [ML] Adding results buckets endpoint wrapper

* removing unnecessary spread operator

* correcting timstamp format in api doc

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
James Gowdy 2020-02-17 15:06:42 +00:00 committed by GitHub
parent d21d33d0b6
commit c31c7ee4c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 23 deletions

View file

@ -450,6 +450,31 @@ export const elasticsearchJsPlugin = (Client, config, components) => {
method: 'POST',
});
ml.buckets = ca({
urls: [
{
fmt: '/_ml/anomaly_detectors/<%=jobId%>/results/buckets',
req: {
jobId: {
type: 'string',
},
},
},
{
fmt: '/_ml/anomaly_detectors/<%=jobId%>/results/buckets/<%=timestamp%>',
req: {
jobId: {
type: 'string',
},
timestamp: {
type: 'string',
},
},
},
],
method: 'POST',
});
ml.overallBuckets = ca({
url: {
fmt: '/_ml/anomaly_detectors/<%=jobId%>/results/overall_buckets',

View file

@ -36,7 +36,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
try {
const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobs');
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -67,7 +67,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobs', { jobId });
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -94,7 +94,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
try {
const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobStats');
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -125,7 +125,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
const { jobId } = request.params;
const results = await context.ml!.mlClient.callAsCurrentUser('ml.jobStats', { jobId });
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -160,7 +160,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
body: request.body,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -195,7 +195,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
body: request.body,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -228,7 +228,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
jobId,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -265,7 +265,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
}
const results = await context.ml!.mlClient.callAsCurrentUser('ml.closeJob', options);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -302,7 +302,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
}
const results = await context.ml!.mlClient.callAsCurrentUser('ml.deleteJob', options);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -332,7 +332,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
body: request.body,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -368,7 +368,57 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
duration,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
}
})
);
/**
* @apiGroup AnomalyDetectors
*
* @api {post} /api/ml/anomaly_detectors/:jobId/results/buckets Obtain bucket scores for the specified job ID
* @apiName GetOverallBuckets
* @apiDescription The get buckets API presents a chronological view of the records, grouped by bucket.
*
* @apiParam {String} jobId Job ID.
* @apiParam {String} timestamp.
*
* @apiSuccess {Number} count
* @apiSuccess {Object[]} buckets
*/
router.post(
{
path: '/api/ml/anomaly_detectors/{jobId}/results/buckets/{timestamp?}',
validate: {
params: schema.object({
jobId: schema.string(),
timestamp: schema.maybe(schema.string()),
}),
body: schema.object({
anomaly_score: schema.maybe(schema.number()),
desc: schema.maybe(schema.boolean()),
end: schema.maybe(schema.string()),
exclude_interim: schema.maybe(schema.boolean()),
expand: schema.maybe(schema.boolean()),
'page.from': schema.maybe(schema.number()),
'page.size': schema.maybe(schema.number()),
sort: schema.maybe(schema.string()),
start: schema.maybe(schema.string()),
}),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const results = await context.ml!.mlClient.callAsCurrentUser('ml.buckets', {
jobId: request.params.jobId,
timestamp: request.params.timestamp,
...request.body,
});
return response.ok({
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -413,7 +463,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
end: request.body.end,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -449,7 +499,7 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
};
const results = await context.ml!.mlClient.callAsCurrentUser('ml.categories', options);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));

View file

@ -40,7 +40,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
try {
const results = await context.ml!.mlClient.callAsCurrentUser('ml.getDataFrameAnalytics');
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -71,7 +71,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
analyticsId,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -97,7 +97,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
'ml.getDataFrameAnalyticsStats'
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -131,7 +131,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
}
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -170,7 +170,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
}
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -201,7 +201,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
}
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -241,7 +241,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
}
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -277,7 +277,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
}
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -310,7 +310,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
analyticsId,
});
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));
@ -353,7 +353,7 @@ export function dataFrameAnalyticsRoutes({ xpackMainPlugin, router }: RouteIniti
options
);
return response.ok({
body: { ...results },
body: results,
});
} catch (e) {
return response.customError(wrapError(e));