Skip to content

Commit

Permalink
feat(pipelines): add mlassociations resource to pipelines
Browse files Browse the repository at this point in the history
feat(pipelines): add mlassociations resource to pipelines
  • Loading branch information
rosalie-liu authored Nov 4, 2019
1 parent 03c98af commit 5ffde1a
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/resources/MachineLearning/Models/ModelsInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {IdAndDisplayNameModel} from '../../BaseInterfaces';
import {AssociatedPipelineModel} from '../../Pipelines';
import {MLModelInfo} from '../ModelInformation/ModelInformationInterfaces';

export interface MLModel extends MLModelInfo {
export interface MLModel extends MLModelInfo, ModelAttributes {
orgId: string;
id: string;
engineId: string;
Expand Down Expand Up @@ -53,3 +54,9 @@ export interface CustomerError {
precision?: string;
troubleshoot?: string;
}

export interface ModelAttributes {
associatedPipelines?: AssociatedPipelineModel[];
parsedExportOffset?: string;
parsedExportPeriod?: string;
}
66 changes: 66 additions & 0 deletions src/resources/Pipelines/MLAssociations/MLAssociations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Resource from '../../Resource';
import Pipelines from '../Pipelines';
import {
AssociatedPipelinesData,
AssociationsListModel,
CreateAssociation,
EditAssociation,
ListAssociationsParams,
MLAssociationModel,
} from './MLAssociationsInterfaces';

export default class MLAssociations extends Resource {
static getBaseUrl = (pipelineId: string) => `/rest/search/v2/admin/pipelines/${pipelineId}/ml/model/associations`;

list(pipelineId: string, options?: ListAssociationsParams) {
return this.api.get<AssociationsListModel>(this.buildPath(MLAssociations.getBaseUrl(pipelineId), options));
}

associate(pipelineId: string, options: CreateAssociation) {
return this.api.post(
this.buildPath(MLAssociations.getBaseUrl(pipelineId), {organizationId: this.api.organizationId}),
options
);
}

getAssociation(pipelineId: string, associationId: string) {
return this.api.get<MLAssociationModel>(
this.buildPath(`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}`, {
organizationId: this.api.organizationId,
})
);
}

disassociate(pipelineId: string, modelId: string, associationId: string) {
return this.api.delete(
this.buildPath(`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}/${modelId}`, {
organizationId: this.api.organizationId,
})
);
}

updateAssociation(pipelineId: string, associationId: string, options?: EditAssociation) {
return this.api.put(
this.buildPath(`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}`, {
organizationId: this.api.organizationId,
}),
options
);
}

updatePosition(pipelineId: string, associationId: string, position: number) {
return this.api.put(
this.buildPath(`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}/position`, {
position,
organizationId: this.api.organizationId,
}),
{}
);
}

getAssociatedPipelines() {
return this.api.get<AssociatedPipelinesData[]>(
this.buildPath(`${Pipelines.baseUrl}/ml/model/associations`, {organizationId: this.api.organizationId})
);
}
}
58 changes: 58 additions & 0 deletions src/resources/Pipelines/MLAssociations/MLAssociationsInterfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export interface AssociationsListModel {
rules: MLAssociationModel[];
totalEntries: number;
totalPages: number;
}

export interface MLAssociationModel {
id: string;
position: number;
modelId: string;
modelStatus: string;
cacheMaximumAge: string;
condition?: string;
conditionDefinition?: string;
customQueryParameters?: {};
enableWordCompletion?: boolean;
exclusive?: boolean;
intelligentTermDetection?: boolean;
matchAdvancedExpression?: boolean;
matchBasicExpression?: boolean;
maxRecommendations?: number;
modelDisplayName?: string;
rankingModifier?: number;
}

export interface ListAssociationsParams {
perPage?: number;
page?: number;
}

export interface CreateAssociation extends EditAssociation {
modelId: string;
}

export interface EditAssociation {
cacheMaximumAge?: string;
condition?: string;
customQueryParameters?: {};
description?: string;
enableWordCompletion?: boolean;
exclusive?: boolean;
intelligentTermDetection?: boolean;
matchAdvancedExpression?: boolean;
matchBasicExpression?: boolean;
maxRecommendations?: number;
rankingModifier?: number;
}

export interface AssociatedPipelinesData {
modelId: string;
associations: AssociatedPipelineModel[];
}

export interface AssociatedPipelineModel {
associationId: string;
pipelineId: string;
pipelineName: string;
}
2 changes: 2 additions & 0 deletions src/resources/Pipelines/MLAssociations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './MLAssociations';
export * from './MLAssociationsInterfaces';
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import API from '../../../../APICore';
import Pipelines from '../../Pipelines';
import MLAssociations from '../MLAssociations';

jest.mock('../../../../APICore');

const APIMock: jest.Mock<API> = API as any;

describe('MLAssociations', () => {
let associations: MLAssociations;
const api = new APIMock() as jest.Mocked<API>;

beforeEach(() => {
jest.clearAllMocks();
associations = new MLAssociations(api);
});

describe('list', () => {
it('should make a GET call to the specific MLAssociations url', () => {
const pipelineId = 'diEnilEpip';

associations.list(pipelineId);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(MLAssociations.getBaseUrl(pipelineId));
});
});

describe('associate', () => {
it('should make a POST call to the specific MLAssociations url', () => {
const pipelineId = '-_-';
const options = {modelId: '0_0'};

associations.associate(pipelineId, options);
expect(api.post).toHaveBeenCalledTimes(1);
expect(api.post).toHaveBeenCalledWith(`${MLAssociations.getBaseUrl(pipelineId)}`, options);
});
});

describe('getAssociation', () => {
it('should make a GET call to the specific MLAssociations url', () => {
const pipelineId = '-W-';
const associationId = 'OWO';

associations.getAssociation(pipelineId, associationId);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}`);
});
});

describe('disassociate', () => {
it('should make a DELETE call to the specific MLAssociations url', () => {
const pipelineId = '123';
const modelId = '321';
const associationId = '000';

associations.disassociate(pipelineId, modelId, associationId);
expect(api.delete).toHaveBeenCalledTimes(1);
expect(api.delete).toHaveBeenCalledWith(
`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}/${modelId}`
);
});
});

describe('updateAssociation', () => {
it('should make a PUT call to the specific MLAssociations url', () => {
const pipelineId = '999';
const associationId = '111';
const options = {exclusive: true};

associations.updateAssociation(pipelineId, associationId, options);
expect(api.put).toHaveBeenCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}`, options);
});
});

describe('updatePosition', () => {
it('should make a PUT call to the specific MLAssociations url', () => {
const pipelineId = '222';
const associationId = '888';
const position = 6;

associations.updatePosition(pipelineId, associationId, position);
expect(api.put).toHaveBeenCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(
`${MLAssociations.getBaseUrl(pipelineId)}/${associationId}/position?position=${position}`,
{}
);
});
});

describe('getAssociatedPipelines', () => {
it('should make a GET call to the specific MLAssociations url', () => {
associations.getAssociatedPipelines();
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Pipelines.baseUrl}/ml/model/associations`);
});
});
});
22 changes: 22 additions & 0 deletions src/resources/Pipelines/Pipelines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import API from '../../APICore';
import Resource from '../Resource';
import MLAssociations from './MLAssociations/MLAssociations';
import {PipelineBackendVersion} from './PipelinesInterfaces';

export default class Pipelines extends Resource {
static baseUrl = '/rest/search/v2/admin/pipelines';

associations: MLAssociations;

constructor(protected api: API) {
super(api);

this.associations = new MLAssociations(api);
}

getBackendVersion() {
return this.api.get<PipelineBackendVersion>(
this.buildPath(`${Pipelines.baseUrl}/ml/version`, {organizationId: this.api.organizationId})
);
}
}
3 changes: 3 additions & 0 deletions src/resources/Pipelines/PipelinesInterfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PipelineBackendVersion {
version: '1' | '2';
}
3 changes: 3 additions & 0 deletions src/resources/Pipelines/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './Pipelines';
export * from './PipelinesInterfaces';
export * from './MLAssociations/';
24 changes: 24 additions & 0 deletions src/resources/Pipelines/tests/Pipelines.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import API from '../../../APICore';
import Pipelines from '../Pipelines';

jest.mock('../../../APICore');

const APIMock: jest.Mock<API> = API as any;

describe('Pipelines', () => {
let pipelines: Pipelines;
const api = new APIMock() as jest.Mocked<API>;

beforeEach(() => {
jest.clearAllMocks();
pipelines = new Pipelines(api);
});

describe('getBackendVersion', () => {
it('should make a GET call to the specific Pipelines url', () => {
pipelines.getBackendVersion();
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(`${Pipelines.baseUrl}/ml/version`);
});
});
});
3 changes: 3 additions & 0 deletions src/resources/PlatformResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Group from './Groups/Groups';
import Index from './Indexes/Indexes';
import MachineLearning from './MachineLearning/MachineLearning';
import Organization from './Organizations/Organization';
import Pipelines from './Pipelines/Pipelines';
import Resource from './Resource';
import SecurityCache from './SecurityCache/SecurityCache';

Expand All @@ -20,6 +21,7 @@ const resourcesMap: Array<{key: string; resource: typeof Resource}> = [
{key: 'securityCache', resource: SecurityCache},
{key: 'apiKey', resource: ApiKey},
{key: 'ml', resource: MachineLearning},
{key: 'pipeline', resource: Pipelines},
];

class PlatformResources {
Expand All @@ -34,6 +36,7 @@ class PlatformResources {
apiKey: ApiKey;
ml: MachineLearning;
securityCache: SecurityCache;
pipeline: Pipelines;

registerAll() {
resourcesMap.forEach(({key, resource}) => {
Expand Down
1 change: 1 addition & 0 deletions src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from './License';
export * from './Organizations';
export * from './ApiKeys';
export * from './MachineLearning';
export * from './Pipelines';
9 changes: 9 additions & 0 deletions src/resources/tests/PlatformResources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Cluster from '../Clusters/Cluster';
import Group from '../Groups/Groups';
import MachineLearning from '../MachineLearning/MachineLearning';
import Organization from '../Organizations/Organization';
import Pipelines from '../Pipelines/Pipelines';
import PlatformResources from '../PlatformResources';
import SecurityCache from '../SecurityCache/SecurityCache';

Expand Down Expand Up @@ -73,5 +74,13 @@ describe('PlatformResources', () => {
expect(platformResources.ml).toBeDefined();
expect(platformResources.ml).toBeInstanceOf(MachineLearning);
});

it('should register the pipeline resource on the platform instance', () => {
const platformResources = new PlatformResources();
platformResources.registerAll();

expect(platformResources.pipeline).toBeDefined();
expect(platformResources.pipeline).toBeInstanceOf(Pipelines);
});
});
});

0 comments on commit 5ffde1a

Please sign in to comment.