Skip to content

Commit

Permalink
[ML] use pre-defined job types
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Mar 18, 2020
1 parent ceb8dea commit 0ec7263
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 26 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/ml/common/types/anomaly_detection_jobs/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface ModelPlotConfig {

// TODO, finish this when it's needed
export interface CustomRule {
actions: any;
scope: object;
conditions: object;
actions: string[];
scope?: object;
conditions: any[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
*/

import numeral from '@elastic/numeral';
import { TypeOf } from '@kbn/config-schema';
import { APICaller } from 'kibana/server';
import { analysisConfigSchema } from '../../routes/schemas/anomaly_detectors_schema';
import { AnalysisConfig } from '../../../common/types/anomaly_detection_jobs';
import { fieldsServiceProvider } from '../fields_service';

type AnalysisConfig = TypeOf<typeof analysisConfigSchema>;

interface ModelMemoryEstimationResult {
/**
* Result model memory limit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

import { i18n } from '@kbn/i18n';
import { CombinedJob } from '../../../common/types/anomaly_detection_jobs';
import { AnomalyDetectionJob } from '../../routes/schemas/anomaly_detectors_schema';

export function validateJobObject(job: AnomalyDetectionJob | CombinedJob | null) {
export function validateJobObject(job: CombinedJob | null) {
if (job === null || typeof job !== 'object') {
throw new Error(
i18n.translate('xpack.ml.models.jobValidation.validateJobObject.jobIsNotObjectErrorMessage', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { APICaller } from 'kibana/server';
import { AnomalyDetectionJob } from '../../routes/schemas/anomaly_detectors_schema';
import { CombinedJob, Detector } from '../../../common/types/anomaly_detection_jobs';
import { ModelMemoryEstimate } from '../calculate_model_memory_limit/calculate_model_memory_limit';
import { validateModelMemoryLimit } from './validate_model_memory_limit';

Expand Down Expand Up @@ -98,10 +98,7 @@ describe('ML - validateModelMemoryLimit', () => {
return Promise.resolve(response);
}) as APICaller;

function getJobConfig(
influencers: string[] = [],
detectors: AnomalyDetectionJob['analysis_config']['detectors'] = []
) {
function getJobConfig(influencers: string[] = [], detectors: Detector[] = []) {
return ({
analysis_config: { detectors, influencers },
data_description: { time_field: '@timestamp' },
Expand All @@ -111,13 +108,11 @@ describe('ML - validateModelMemoryLimit', () => {
analysis_limits: {
model_memory_limit: '20mb',
},
} as unknown) as AnomalyDetectionJob;
} as unknown) as CombinedJob;
}

// create a specified number of mock detectors
function createDetectors(
numberOfDetectors: number
): AnomalyDetectionJob['analysis_config']['detectors'] {
function createDetectors(numberOfDetectors: number): Detector[] {
const dtrs = [];
for (let i = 0; i < numberOfDetectors; i++) {
dtrs.push({
Expand All @@ -126,7 +121,7 @@ describe('ML - validateModelMemoryLimit', () => {
partition_field_name: 'instance',
});
}
return dtrs as AnomalyDetectionJob['analysis_config']['detectors'];
return dtrs as Detector[];
}

it('Called with no duration or split and mml within limit', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numeral from '@elastic/numeral';
import { APICaller } from 'kibana/server';
import { AnomalyDetectionJob } from '../../routes/schemas/anomaly_detectors_schema';
import { CombinedJob } from '../../../common/types/anomaly_detection_jobs';
import { validateJobObject } from './validate_job_object';
import { calculateModelMemoryLimitProvider } from '../calculate_model_memory_limit';
import { ALLOWED_DATA_UNITS } from '../../../common/constants/validation';
Expand All @@ -16,7 +16,7 @@ const MODEL_MEMORY_LIMIT_MINIMUM_BYTES = 1048576;

export async function validateModelMemoryLimit(
callWithRequest: APICaller,
job: AnomalyDetectionJob,
job: CombinedJob,
duration?: { start?: number; end?: number }
) {
validateJobObject(job);
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/ml/server/routes/job_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Boom from 'boom';
import { RequestHandlerContext } from 'kibana/server';
import { schema, TypeOf } from '@kbn/config-schema';
import { AnalysisConfig } from '../../common/types/anomaly_detection_jobs';
import { wrapError } from '../client/error_wrapper';
import { RouteInitialization } from '../types';
import {
Expand All @@ -32,7 +33,7 @@ export function jobValidationRoutes({ router, mlLicense }: RouteInitialization,
const { analysisConfig, indexPattern, query, timeFieldName, earliestMs, latestMs } = payload;

return calculateModelMemoryLimitProvider(context.ml!.mlClient.callAsCurrentUser)(
analysisConfig,
analysisConfig as AnalysisConfig,
indexPattern,
query,
timeFieldName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

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

const customRulesSchema = schema.maybe(
schema.arrayOf(
Expand Down Expand Up @@ -107,6 +107,3 @@ export const anomalyDetectionJobSchema = {
results_retention_days: schema.maybe(schema.number()),
state: schema.maybe(schema.string()),
};

const anomalyDetectionJobSchemaObject = schema.object(anomalyDetectionJobSchema);
export type AnomalyDetectionJob = TypeOf<typeof anomalyDetectionJobSchemaObject>;

0 comments on commit 0ec7263

Please sign in to comment.