Skip to content

Commit

Permalink
[ML] Refactor allEventsExistInCalendar
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Jul 22, 2020
1 parent 65f473a commit 2a5eb37
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 41 deletions.
24 changes: 1 addition & 23 deletions x-pack/test/api_integration/apis/ml/calendars/get_calendars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,11 @@
*/

import expect from '@kbn/expect';

import { Calendar, CalendarEvent } from '../../../../../plugins/ml/server/models/calendar/index';
import { allEventsExistInCalendar } from './helpers';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { USER } from '../../../../functional/services/ml/security_common';
import { COMMON_REQUEST_HEADERS } from '../../../../functional/services/ml/common';

const allEventsExistInCalendar = (eventsToCheck: CalendarEvent[], calendar: Calendar): boolean => {
const updatedCalendarEvents = calendar.events as CalendarEvent[];
let allEventsAreUpdated = true;
for (const eventToCheck of eventsToCheck) {
// if at least one of the events that we need to check is not in the updated events
// no need to continue
if (
updatedCalendarEvents.findIndex(
(updatedEvent) =>
updatedEvent.description === eventToCheck.description &&
updatedEvent.start_time === eventToCheck.start_time &&
updatedEvent.end_time === eventToCheck.end_time
) < 0
) {
allEventsAreUpdated = false;
break;
}
}
return allEventsAreUpdated;
};

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext) => {
const supertest = getService('supertestWithoutAuth');
Expand Down
31 changes: 31 additions & 0 deletions x-pack/test/api_integration/apis/ml/calendars/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Calendar, CalendarEvent } from '../../../../../plugins/ml/server/models/calendar';

export const allEventsExistInCalendar = (
eventsToCheck: CalendarEvent[],
calendar: Calendar
): boolean => {
const updatedCalendarEvents = calendar.events as CalendarEvent[];
let allEventsAreUpdated = true;
for (const eventToCheck of eventsToCheck) {
// if at least one of the events that we need to check is not in the updated events
// no need to continue
if (
updatedCalendarEvents.findIndex(
(updatedEvent) =>
updatedEvent.description === eventToCheck.description &&
updatedEvent.start_time === eventToCheck.start_time &&
updatedEvent.end_time === eventToCheck.end_time
) < 0
) {
allEventsAreUpdated = false;
break;
}
}
return allEventsAreUpdated;
};
20 changes: 2 additions & 18 deletions x-pack/test/functional/services/ml/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FtrProviderContext } from '../../ftr_provider_context';
import { DATAFEED_STATE, JOB_STATE } from '../../../../plugins/ml/common/constants/states';
import { DATA_FRAME_TASK_STATE } from '../../../../plugins/ml/public/application/data_frame_analytics/pages/analytics_management/components/analytics_list/common';
import { Datafeed, Job } from '../../../../plugins/ml/common/types/anomaly_detection_jobs';
import { allEventsExistInCalendar } from '../../../api_integration/apis/ml/calendars/helpers';
export type MlApi = ProvidedType<typeof MachineLearningAPIProvider>;

export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
Expand Down Expand Up @@ -382,24 +383,7 @@ export function MachineLearningAPIProvider({ getService }: FtrProviderContext) {
// validate if calendar events have been updated with the requested events
const { body } = await this.getCalendarEvents(calendarId, 200);

const updatedCalendarEvents = body.events as CalendarEvent[];
let allEventsAreUpdated = true;
for (const eventToCheck of eventsToCheck) {
// if at least one of the events that we need to check is not in the updated events
// no need to continue
if (
updatedCalendarEvents.findIndex(
(updatedEvent) =>
updatedEvent.description === eventToCheck.description &&
updatedEvent.start_time === eventToCheck.start_time &&
updatedEvent.end_time === eventToCheck.end_time
) < 0
) {
allEventsAreUpdated = false;
break;
}
}
if (allEventsAreUpdated) {
if (allEventsExistInCalendar(eventsToCheck, body)) {
return true;
} else {
throw new Error(
Expand Down

0 comments on commit 2a5eb37

Please sign in to comment.