Skip to content

Commit

Permalink
feat(ml): add model configuration resource (#64)
Browse files Browse the repository at this point in the history
* feat(ml): add model configuration resource

* feat(modelconfiguration): apply review

* feat(modelconfig): minor fix
  • Loading branch information
rosalie-liu authored Jan 17, 2020
1 parent c07b1b5 commit 98a14f9
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/resources/Enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,12 @@ export enum ModelStatus {
OFFLINE = 'OFFLINE',
IN_CREATION = 'IN_CREATION',
}

export enum ModelConfigFileType {
ADVANCED_CONFIGURATION = 'ADVANCED_CONFIGURATION',
STOPWORDS = 'STOPWORDS',
BLACKLISTS = 'BLACKLISTS',
ID_MAPPING = 'ID_MAPPING',
FACET_ID_MAPPING = 'FACET_ID_MAPPING',
DEFAULT_QUERIES = 'DEFAULT_QUERIES',
}
3 changes: 3 additions & 0 deletions src/resources/MachineLearning/MachineLearning.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import API from '../../APICore';
import Resource from '../Resource';
import {MLModelCreated, RegistrationModel} from './MachineLearningInterfaces';
import ModelConfiguration from './ModelConfiguration/ModelConfiguration';
import ModelInformation from './ModelInformation/ModelInformation';
import Models from './Models/Models';

Expand All @@ -9,12 +10,14 @@ export default class MachineLearning extends Resource {

models: Models;
modelInfo: ModelInformation;
modelConfig: ModelConfiguration;

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

this.models = new Models(api);
this.modelInfo = new ModelInformation(api);
this.modelConfig = new ModelConfiguration(api);
}

register(registration: RegistrationModel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import API from '../../../APICore';
import {ModelConfigFileType} from '../../Enums';
import Resource from '../../Resource';
import {
AdvancedRegistrationConfigFile,
AdvancedRegistrationConfigFileCreationResponse,
ModelConfigurationUpdateArgs,
} from './ModelConfigurationInterfaces';

export default class ModelConfiguration extends Resource {
static getBaseUrl = (modelId: string, modelConfigFileType: ModelConfigFileType) =>
`/rest/organizations/${API.orgPlaceholder}/machinelearning/models/${modelId}/configs/${modelConfigFileType}`;

get(modelId: string, modelConfigFileType: ModelConfigFileType, languageCode?: string) {
return this.api.get<AdvancedRegistrationConfigFile>(
this.buildPath(ModelConfiguration.getBaseUrl(modelId, modelConfigFileType), {languageCode})
);
}

update(
modelId: string,
modelConfigFileType: ModelConfigFileType,
{modelConfigFileContents, languageCode}: ModelConfigurationUpdateArgs
) {
return this.api.put<AdvancedRegistrationConfigFileCreationResponse>(
this.buildPath(ModelConfiguration.getBaseUrl(modelId, modelConfigFileType), {languageCode}),
modelConfigFileContents
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface AdvancedRegistrationConfigFile {
fileContents?: string;
lastModified?: string;
modelId?: RegistrationId;
orgId?: Record<string, string>;
}

export interface RegistrationId {
modelId?: string;
}

export interface AdvancedRegistrationConfigFileCreationResponse {
filePath?: Record<string, string>;
modelId?: RegistrationId;
}

export interface ModelConfigurationUpdateArgs {
modelConfigFileContents: string;
languageCode?: string;
}
2 changes: 2 additions & 0 deletions src/resources/MachineLearning/ModelConfiguration/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ModelConfiguration';
export * from './ModelConfigurationInterfaces';
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import API from '../../../../APICore';
import {ModelConfigFileType} from '../../../Enums';
import ModelConfiguration from '../ModelConfiguration';

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

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

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

beforeEach(() => {
jest.clearAllMocks();
modelConfig = new ModelConfiguration(api);
});

describe('get', () => {
it('should make a GET call to the specific ModelConfiguration url', () => {
const modelId = '💀Papyrus';
const modelConfigFileType = ModelConfigFileType.ADVANCED_CONFIGURATION;

modelConfig.get(modelId, modelConfigFileType);
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(ModelConfiguration.getBaseUrl(modelId, modelConfigFileType));
});
});

describe('update', () => {
it('should make a PUT call to the specific ModelConfiguration url', () => {
const modelId = '💀Sans';
const modelConfigFileType = ModelConfigFileType.FACET_ID_MAPPING;
const modelConfigFileContents = '🦴🦴🦴';

modelConfig.update(modelId, modelConfigFileType, {modelConfigFileContents});
expect(api.put).toBeCalledTimes(1);
expect(api.put).toHaveBeenCalledWith(
ModelConfiguration.getBaseUrl(modelId, modelConfigFileType),
modelConfigFileContents
);
});
});
});
1 change: 1 addition & 0 deletions src/resources/MachineLearning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './MachineLearning';
export * from './MachineLearningInterfaces';
export * from './Models/';
export * from './ModelInformation/';
export * from './ModelConfiguration/';

0 comments on commit 98a14f9

Please sign in to comment.