Skip to content

Commit

Permalink
[ML] New Platform server shim: update recognize modules routes to use…
Browse files Browse the repository at this point in the history
… new platform router (#57206)

* [ML] modules with NP router

* [ML] use extendedRouteInitializationDeps

* [ML] add apidoc annotations

* [ML] address PR comments

* [ML] convert data_recognizer test to jest

* [ML] optional indexPatternName
  • Loading branch information
darnautov authored Feb 11, 2020
1 parent 733f602 commit b133357
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 309 deletions.
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/ml/common/types/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export interface MlJob {
};
create_time: number;
custom_settings: object;
data_counts: object;
data_counts: {
earliest_record_timestamp: number;
latest_record_timestamp: number;
};
data_description: {
time_field: string;
time_format: string;
Expand Down
39 changes: 26 additions & 13 deletions x-pack/legacy/plugins/ml/common/types/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ export interface ModuleJob {
config: Omit<Job, 'job_id'>;
}

export interface ModuleDataFeed {
id: string;
config: Omit<Datafeed, 'datafeed_id'>;
}

export interface KibanaObjectConfig extends SavedObjectAttributes {
description: string;
title: string;
version: number;
kibanaSavedObjectMeta?: {
searchSourceJSON: string;
};
}

export interface KibanaObject {
id: string;
title: string;
config: KibanaObjectConfig;
exists?: boolean;
}

export interface KibanaObjects {
Expand All @@ -39,14 +48,18 @@ export interface Module {
defaultIndexPattern: string;
query: any;
jobs: ModuleJob[];
datafeeds: Datafeed[];
datafeeds: ModuleDataFeed[];
kibana: KibanaObjects;
}

export interface KibanaObjectResponse {
exists?: boolean;
success?: boolean;
export interface ResultItem {
id: string;
success?: boolean;
}

export interface KibanaObjectResponse extends ResultItem {
exists?: boolean;
error?: any;
}

export interface SetupError {
Expand All @@ -58,27 +71,27 @@ export interface SetupError {
statusCode: number;
}

export interface DatafeedResponse {
id: string;
success: boolean;
export interface DatafeedResponse extends ResultItem {
started: boolean;
error?: SetupError;
}

export interface JobResponse {
id: string;
success: boolean;
export interface JobResponse extends ResultItem {
error?: SetupError;
}

export interface DataRecognizerConfigResponse {
datafeeds: DatafeedResponse[];
jobs: JobResponse[];
kibana: {
search: KibanaObjectResponse;
visualization: KibanaObjectResponse;
dashboard: KibanaObjectResponse;
search: KibanaObjectResponse[];
visualization: KibanaObjectResponse[];
dashboard: KibanaObjectResponse[];
};
}

export type GeneralOverride = any;

export type JobOverride = Partial<Job>;

export type DatafeedOverride = Partial<Datafeed>;
7 changes: 7 additions & 0 deletions x-pack/legacy/plugins/ml/common/util/job_utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ export function mlFunctionToESAggregation(functionName: string): string | null;
export function isModelPlotEnabled(job: Job, detectorIndex: number, entityFields: any[]): boolean;

export function getSafeAggregationName(fieldName: string, index: number): string;

export function getLatestDataOrBucketTimestamp(
latestDataTimestamp: number,
latestBucketTimestamp: number
): number;

export function prefixDatafeedId(datafeedId: string, prefix: string): string;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export interface Datafeed {
chunking_config?: ChunkingConfig;
frequency?: string;
indices: IndexPatternTitle[];
/**
* The datafeed can contain indexes and indices
*/
indexes?: IndexPatternTitle[];
job_id?: JobId;
query: object;
query_delay?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { RequestHandlerContext } from 'kibana/server';
import { Module } from '../../../common/types/modules';
import { DataRecognizer } from '../data_recognizer';

describe('ML - data recognizer', () => {
const dr = new DataRecognizer({});
const dr = new DataRecognizer(({
ml: {
mlClient: {
callAsCurrentUser: jest.fn(),
},
},
core: {
savedObjects: {
client: {
find: jest.fn(),
bulkCreate: jest.fn(),
},
},
},
} as unknown) as RequestHandlerContext);

const moduleIds = [
'apache_ecs',
Expand All @@ -34,20 +49,20 @@ describe('ML - data recognizer', () => {
it('listModules - check all module IDs', async () => {
const modules = await dr.listModules();
const ids = modules.map(m => m.id);
expect(ids.join()).to.equal(moduleIds.join());
expect(ids.join()).toEqual(moduleIds.join());
});

it('getModule - load a single module', async () => {
const module = await dr.getModule(moduleIds[0]);
expect(module.id).to.equal(moduleIds[0]);
expect(module.id).toEqual(moduleIds[0]);
});

describe('jobOverrides', () => {
it('should apply job overrides correctly', () => {
// arrange
const prefix = 'pre-';
const testJobId = 'test-job';
const moduleConfig = {
const moduleConfig = ({
jobs: [
{
id: `${prefix}${testJobId}`,
Expand All @@ -64,7 +79,7 @@ describe('ML - data recognizer', () => {
},
},
],
};
} as unknown) as Module;
const jobOverrides = [
{
analysis_limits: {
Expand All @@ -80,7 +95,7 @@ describe('ML - data recognizer', () => {
// act
dr.applyJobConfigOverrides(moduleConfig, jobOverrides, prefix);
// assert
expect(moduleConfig.jobs).to.eql([
expect(moduleConfig.jobs).toEqual([
{
config: {
analysis_config: {
Expand Down
Loading

0 comments on commit b133357

Please sign in to comment.