Skip to content

Commit

Permalink
[ML] Fix type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jul 28, 2020
1 parent e17518c commit a985f07
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export default ({ getService }: FtrProviderContext) => {
const annotationId = body._id;

const fetchedAnnotation = await ml.api.getAnnotationById(annotationId);
expect(fetchedAnnotation.annotation).to.eql(annotationRequestBody.annotation);
expect(fetchedAnnotation.job_id).to.eql(annotationRequestBody.job_id);
expect(fetchedAnnotation.event).to.eql(annotationRequestBody.event);
expect(fetchedAnnotation.user).to.eql(annotationRequestBody.user);
expect(fetchedAnnotation.create_username).to.eql(USER.ML_POWERUSER);
expect(fetchedAnnotation).to.not.be(undefined);
expect(fetchedAnnotation?.annotation).to.eql(annotationRequestBody.annotation);
expect(fetchedAnnotation?.job_id).to.eql(annotationRequestBody.job_id);
expect(fetchedAnnotation?.event).to.eql(annotationRequestBody.event);
expect(fetchedAnnotation?.create_username).to.eql(USER.ML_POWERUSER);
});

it('should successfully create annotation for user without required permission', async () => {
Expand All @@ -88,11 +88,11 @@ export default ({ getService }: FtrProviderContext) => {

const annotationId = body._id;
const fetchedAnnotation = await ml.api.getAnnotationById(annotationId);
expect(fetchedAnnotation.annotation).to.eql(annotationRequestBody.annotation);
expect(fetchedAnnotation.job_id).to.eql(annotationRequestBody.job_id);
expect(fetchedAnnotation.event).to.eql(annotationRequestBody.event);
expect(fetchedAnnotation.user).to.eql(annotationRequestBody.user);
expect(fetchedAnnotation.create_username).to.eql(USER.ML_VIEWER);
expect(fetchedAnnotation).to.not.be(undefined);
expect(fetchedAnnotation?.annotation).to.eql(annotationRequestBody.annotation);
expect(fetchedAnnotation?.job_id).to.eql(annotationRequestBody.job_id);
expect(fetchedAnnotation?.event).to.eql(annotationRequestBody.event);
expect(fetchedAnnotation?.create_username).to.eql(USER.ML_VIEWER);
});

it('should not allow to create annotation for unauthorized user', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default ({ getService }: FtrProviderContext) => {
}));
const jobIds = testSetupJobConfigs.map((j) => j.job_id);

const createAnnotationRequestBody = (jobId) => {
const createAnnotationRequestBody = (jobId: string) => {
return {
timestamp: Date.now(),
end_timestamp: Date.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default ({ getService }: FtrProviderContext) => {
}));
const jobIds = testSetupJobConfigs.map((j) => j.job_id);

const createAnnotationRequestBody = (jobId) => {
const createAnnotationRequestBody = (jobId: string) => {
return {
timestamp: Date.now(),
end_timestamp: Date.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default ({ getService }: FtrProviderContext) => {
}));
const jobIds = testSetupJobConfigs.map((j) => j.job_id);

const createAnnotationRequestBody = (jobId) => {
const createAnnotationRequestBody = (jobId: string) => {
return {
timestamp: Date.now(),
end_timestamp: Date.now(),
Expand Down Expand Up @@ -103,12 +103,9 @@ export default ({ getService }: FtrProviderContext) => {
expect(body.result).to.eql('updated');

const updatedAnnotation = await ml.api.getAnnotationById(originalAnnotation._id);
expect(updatedAnnotation.annotation).to.eql(annotationUpdateRequestBody.annotation);
expect(updatedAnnotation.detector_index).to.eql(annotationUpdateRequestBody.detector_index);
expect(updatedAnnotation.event).to.eql(annotationUpdateRequestBody.event);
expect(updatedAnnotation.partition_field_value).to.eql(
annotationUpdateRequestBody.partition_field_value
);
expect(updatedAnnotation?.annotation).to.eql(annotationUpdateRequestBody.annotation);
expect(updatedAnnotation?.detector_index).to.eql(annotationUpdateRequestBody.detector_index);
expect(updatedAnnotation?.event).to.eql(annotationUpdateRequestBody.event);
});

it('should correctly update annotation for user with viewer permission', async () => {
Expand Down Expand Up @@ -137,12 +134,9 @@ export default ({ getService }: FtrProviderContext) => {
expect(body.result).to.eql('updated');

const updatedAnnotation = await ml.api.getAnnotationById(originalAnnotation._id);
expect(updatedAnnotation.annotation).to.eql(annotationUpdateRequestBody.annotation);
expect(updatedAnnotation.detector_index).to.eql(annotationUpdateRequestBody.detector_index);
expect(updatedAnnotation.event).to.eql(annotationUpdateRequestBody.event);
expect(updatedAnnotation.partition_field_value).to.eql(
originalAnnotation.partition_field_value
);
expect(updatedAnnotation?.annotation).to.eql(annotationUpdateRequestBody.annotation);
expect(updatedAnnotation?.detector_index).to.eql(annotationUpdateRequestBody.detector_index);
expect(updatedAnnotation?.event).to.eql(annotationUpdateRequestBody.event);
});

it('should not update annotation for unauthorized user', async () => {
Expand Down
25 changes: 18 additions & 7 deletions x-pack/test/functional/services/ml/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import expect from '@kbn/expect';
import { ProvidedType } from '@kbn/test/types/ftr';
import { IndexDocumentParams } from 'elasticsearch';
import { Calendar, CalendarEvent } from '../../../../plugins/ml/server/models/calendar/index';
import { Annotation } from '../../../../plugins/ml/common/types/annotations';
import { DataFrameAnalyticsConfig } from '../../../../plugins/ml/public/application/data_frame_analytics/common';
Expand All @@ -17,7 +18,16 @@ import {
ML_ANNOTATIONS_INDEX_ALIAS_READ,
ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
} from '../../../../plugins/ml/common/constants/index_patterns';
import { IndexParams } from '../../../../plugins/ml/server/models/annotation_service/annotation';

interface EsIndexResult {
_index: string;
_id: string;
_version: number;
result: string;
_shards: any;
_seq_no: number;
_primary_term: number;
}

export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
const es = getService('legacyEs');
Expand Down Expand Up @@ -644,7 +654,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
async getAnnotations(jobId: string) {
log.debug(`Fetching annotations for job '${jobId}'...`);

const results = await es.search({
const results = await es.search<Annotation>({
index: ML_ANNOTATIONS_INDEX_ALIAS_READ,
body: {
size: 1,
Expand All @@ -660,7 +670,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
return results.hits.hits;
},

async getAnnotationById(annotationId: string) {
async getAnnotationById(annotationId: string): Promise<Annotation | undefined> {
log.debug(`Fetching annotation '${annotationId}'...`);

const result = await es.search({
Expand All @@ -674,21 +684,22 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
},
},
});
// @ts-ignore due to outdated type for hits.total
if (result.hits.total.value === 1) {
return result?.hits?.hits[0]?._source;
return result?.hits?.hits[0]?._source as Annotation;
}
return undefined;
},

async indexAnnotation(annotationRequestBody: Annotation) {
log.debug(`Indexing annotation '${JSON.stringify(annotationRequestBody)}'...`);
const params: IndexParams = {
// @ts-ignore due to outdated type for IndexDocumentParams.type
const params: IndexDocumentParams<Annotation> = {
index: ML_ANNOTATIONS_INDEX_ALIAS_WRITE,
body: annotationRequestBody,
refresh: 'wait_for',
};
const results = await es.index(params);

const results: EsIndexResult = await es.index(params);
await this.waitForAnnotationToExist(results._id);
return results;
},
Expand Down

0 comments on commit a985f07

Please sign in to comment.