Skip to content

Commit

Permalink
[ML] Adding get records endpoint wrapper (#58500)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic authored Feb 26, 2020
1 parent 0e0f114 commit 71254de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
12 changes: 12 additions & 0 deletions x-pack/legacy/plugins/ml/server/client/elasticsearch_ml.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,18 @@ export const elasticsearchJsPlugin = (Client, config, components) => {
method: 'POST',
});

ml.records = ca({
url: {
fmt: '/_ml/anomaly_detectors/<%=jobId%>/results/records',
req: {
jobId: {
type: 'string',
},
},
},
method: 'POST',
});

ml.buckets = ca({
urls: [
{
Expand Down
48 changes: 47 additions & 1 deletion x-pack/legacy/plugins/ml/server/routes/anomaly_detectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,57 @@ export function jobRoutes({ xpackMainPlugin, router }: RouteInitialization) {
})
);

/**
* @apiGroup AnomalyDetectors
*
* @api {post} /api/ml/anomaly_detectors/:jobId/results/records Retrieves anomaly records for a job.
* @apiName GetRecords
* @apiDescription Retrieves anomaly records for a job.
*
* @apiParam {String} jobId Job ID.
*
* @apiSuccess {Number} count
* @apiSuccess {Object[]} records
*/
router.post(
{
path: '/api/ml/anomaly_detectors/{jobId}/results/records',
validate: {
params: schema.object({
jobId: schema.string(),
}),
body: schema.object({
desc: schema.maybe(schema.boolean()),
end: schema.maybe(schema.string()),
exclude_interim: schema.maybe(schema.boolean()),
'page.from': schema.maybe(schema.number()),
'page.size': schema.maybe(schema.number()),
record_score: 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.records', {
jobId: request.params.jobId,
...request.body,
});
return response.ok({
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
* @apiName GetBuckets
* @apiDescription The get buckets API presents a chronological view of the records, grouped by bucket.
*
* @apiParam {String} jobId Job ID.
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/ml/server/routes/apidoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"DeleteAnomalyDetectorsJob",
"ValidateAnomalyDetector",
"ForecastAnomalyDetector",
"GetRecords",
"GetBuckets",
"GetOverallBuckets",
"GetCategories",
"FileDataVisualizer",
Expand Down

0 comments on commit 71254de

Please sign in to comment.