Skip to content

Commit

Permalink
add schema definitions for route params
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarezmelissa87 committed Feb 12, 2020
1 parent a92fed4 commit a1a88ca
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 40 deletions.
66 changes: 66 additions & 0 deletions x-pack/legacy/plugins/ml/server/new_platform/job_service_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { schema } from '@kbn/config-schema';

const analyzerSchema = {
tokenizer: schema.string(),
filter: schema.arrayOf(
schema.object({
type: schema.string(),
stopwords: schema.arrayOf(schema.maybe(schema.string())),
})
),
};

export const catFieldExamplesSchema = {
indexPatternTitle: schema.string(),
query: schema.any(),
size: schema.number(),
field: schema.string(),
timeField: schema.string(),
start: schema.number(),
end: schema.number(),
analyzer: schema.object(analyzerSchema),
};

export const chartSchema = {
indexPatternTitle: schema.string(),
timefield: schema.string(),
start: schema.number(),
end: schema.number(),
intervalMs: schema.number(),
query: schema.any(),
aggFieldNamePairs: schema.arrayOf(schema.any()),
splitFieldName: schema.maybe(schema.nullable(schema.string())),
splitFieldValue: schema.maybe(schema.nullable(schema.string())),
};

export const datafeedIdsSchema = { datafeedIds: schema.arrayOf(schema.maybe(schema.string())) };

export const forceStartDatafeedSchema = {
datafeedIds: schema.arrayOf(schema.maybe(schema.string())),
start: schema.maybe(schema.number()),
end: schema.maybe(schema.number()),
};

export const jobIdsSchema = {
jobIds: schema.oneOf([schema.string(), schema.arrayOf(schema.maybe(schema.string()))]),
};
export const jobsWithTimerangeSchema = { dateFormatTz: schema.maybe(schema.string()) };

export const lookBackProgressSchema = {
jobId: schema.string(),
start: schema.maybe(schema.number()),
end: schema.maybe(schema.number()),
};

export const topCategoriesSchema = { jobId: schema.string(), count: schema.number() };

export const updateGroupsSchema = {
job_id: schema.string(),
groups: schema.arrayOf(schema.maybe(schema.string())),
};
96 changes: 56 additions & 40 deletions x-pack/legacy/plugins/ml/server/routes/job_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { schema } from '@kbn/config-schema';
import { licensePreRoutingFactory } from '../new_platform/licence_check_pre_routing_factory';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../new_platform/plugin';
// import {
// anomaliesTableDataSchema,
// categoryDefinitionSchema,
// categoryExamplesSchema,
// maxAnomalyScoreSchema,
// partitionFieldValuesSchema,
// } from '../new_platform/job_service_schema';
import {
catFieldExamplesSchema,
chartSchema,
datafeedIdsSchema,
forceStartDatafeedSchema,
jobIdsSchema,
jobsWithTimerangeSchema,
lookBackProgressSchema,
topCategoriesSchema,
updateGroupsSchema,
} from '../new_platform/job_service_schema';

// @ts-ignore
import { jobServiceProvider } from '../models/job_service';
Expand All @@ -35,12 +39,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/force_start_datafeeds',
validate: {
body: schema.any(),
body: schema.object(forceStartDatafeedSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { forceStartDatafeeds } = jobServiceProvider(context);
const { forceStartDatafeeds } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { datafeedIds, start, end } = request.body;
const resp = await forceStartDatafeeds(datafeedIds, start, end);

Expand All @@ -64,12 +68,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/stop_datafeeds',
validate: {
body: schema.any(),
body: schema.object(datafeedIdsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { stopDatafeeds } = jobServiceProvider(context);
const { stopDatafeeds } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { datafeedIds } = request.body;
const resp = await stopDatafeeds(datafeedIds);

Expand All @@ -93,12 +97,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/delete_jobs',
validate: {
body: schema.any(),
body: schema.object(jobIdsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { deleteJobs } = jobServiceProvider(context);
const { deleteJobs } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobIds } = request.body;
const resp = await deleteJobs(jobIds);

Expand All @@ -122,12 +126,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/close_jobs',
validate: {
body: schema.any(),
body: schema.object(jobIdsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { closeJobs } = jobServiceProvider(context);
const { closeJobs } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobIds } = request.body;
const resp = await closeJobs(jobIds);

Expand All @@ -151,12 +155,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/jobs_summary',
validate: {
body: schema.any(),
body: schema.object(jobIdsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { jobsSummary } = jobServiceProvider(context);
const { jobsSummary } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobIds } = request.body;
const resp = await jobsSummary(jobIds);

Expand All @@ -180,12 +184,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/jobs_with_timerange',
validate: {
body: schema.any(),
body: schema.object(jobsWithTimerangeSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { jobsWithTimerange } = jobServiceProvider(context);
const { jobsWithTimerange } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { dateFormatTz } = request.body;
const resp = await jobsWithTimerange(dateFormatTz);

Expand All @@ -209,12 +213,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/jobs',
validate: {
body: schema.any(),
body: schema.object(jobIdsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { createFullJobsList } = jobServiceProvider(context);
const { createFullJobsList } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobIds } = request.body;
const resp = await createFullJobsList(jobIds);

Expand All @@ -241,7 +245,7 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { getAllGroups } = jobServiceProvider(context);
const { getAllGroups } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const resp = await getAllGroups();

return response.ok({
Expand All @@ -264,12 +268,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/update_groups',
validate: {
body: schema.any(),
body: schema.object(updateGroupsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { updateGroups } = jobServiceProvider(context);
const { updateGroups } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobs } = request.body;
const resp = await updateGroups(jobs);

Expand All @@ -296,7 +300,7 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { deletingJobTasks } = jobServiceProvider(context);
const { deletingJobTasks } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const resp = await deletingJobTasks();

return response.ok({
Expand All @@ -319,12 +323,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/jobs_exist',
validate: {
body: schema.any(),
body: schema.object(jobIdsSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { jobsExist } = jobServiceProvider(context);
const { jobsExist } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobIds } = request.body;
const resp = await jobsExist(jobIds);

Expand All @@ -347,13 +351,15 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
router.get(
{
path: '/api/ml/jobs/new_job_caps/{indexPattern}',
validate: false,
validate: {
params: schema.object({ indexPattern: schema.string() }),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { indexPattern } = request.params;
const isRollup = request.query.rollup === 'true';
const { newJobCaps } = jobServiceProvider(context, request);
const { newJobCaps } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser, request);
const resp = await newJobCaps(indexPattern, isRollup);

return response.ok({
Expand All @@ -376,7 +382,7 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/new_job_line_chart',
validate: {
body: schema.any(),
body: schema.object(chartSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
Expand All @@ -392,7 +398,11 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
splitFieldName,
splitFieldValue,
} = request.body;
const { newJobLineChart } = jobServiceProvider(context, request);

const { newJobLineChart } = jobServiceProvider(
context.ml!.mlClient.callAsCurrentUser,
request
);
const resp = await newJobLineChart(
indexPatternTitle,
timeField,
Expand Down Expand Up @@ -425,7 +435,7 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/new_job_population_chart',
validate: {
body: schema.any(),
body: schema.object(chartSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
Expand All @@ -440,7 +450,11 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
aggFieldNamePairs,
splitFieldName,
} = request.body;
const { newJobPopulationChart } = jobServiceProvider(context, request);

const { newJobPopulationChart } = jobServiceProvider(
context.ml!.mlClient.callAsCurrentUser,
request
);
const resp = await newJobPopulationChart(
indexPatternTitle,
timeField,
Expand Down Expand Up @@ -475,7 +489,7 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { getAllJobAndGroupIds } = jobServiceProvider(context);
const { getAllJobAndGroupIds } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const resp = await getAllJobAndGroupIds();

return response.ok({
Expand All @@ -498,12 +512,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/look_back_progress',
validate: {
body: schema.any(),
body: schema.object(lookBackProgressSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { getLookBackProgress } = jobServiceProvider(context);
const { getLookBackProgress } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobId, start, end } = request.body;
const resp = await getLookBackProgress(jobId, start, end);

Expand All @@ -527,12 +541,14 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/categorization_field_examples',
validate: {
body: schema.any(),
body: schema.object(catFieldExamplesSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { validateCategoryExamples } = jobServiceProvider(context);
const { validateCategoryExamples } = jobServiceProvider(
context.ml!.mlClient.callAsCurrentUser
);
const {
indexPatternTitle,
timeField,
Expand Down Expand Up @@ -575,12 +591,12 @@ export function jobServiceRoutes({ xpackMainPlugin, router }: RouteInitializatio
{
path: '/api/ml/jobs/top_categories',
validate: {
body: schema.any(),
body: schema.object(topCategoriesSchema),
},
},
licensePreRoutingFactory(xpackMainPlugin, async (context, request, response) => {
try {
const { topCategories } = jobServiceProvider(context);
const { topCategories } = jobServiceProvider(context.ml!.mlClient.callAsCurrentUser);
const { jobId, count } = request.body;
const resp = await topCategories(jobId, count);

Expand Down

0 comments on commit a1a88ca

Please sign in to comment.