Skip to content

Commit ef67a77

Browse files
committed
[ML] fix calendars saving
1 parent 998f9a9 commit ef67a77

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export interface Calendar {
8+
calendar_id: string;
9+
description: string;
10+
events: any[];
11+
job_ids: string[];
12+
}
13+
14+
export interface UpdateCalendar extends Calendar {
15+
calendarId: string;
16+
}

x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/configs/job.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { UrlConfig } from '../../../../../../../common/types/custom_urls';
88
import { CREATED_BY_LABEL } from '../util/constants';
9+
import { Calendar } from '../../../../../../../common/types/calendars';
910

1011
export type JobId = string;
1112
export type BucketSpan = string;
@@ -24,7 +25,7 @@ export interface Job {
2425
data_description: DataDescription;
2526
description: string;
2627
groups: string[];
27-
calendars?: string[];
28+
calendars?: Calendar[];
2829
model_plot_config?: ModelPlotConfig;
2930
model_snapshot_retention_days?: number;
3031
renormalization_window_days?: number;

x-pack/legacy/plugins/ml/public/application/jobs/new_job/common/job_creator/job_creator.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { JobRunner, ProgressSubscriber } from '../job_runner';
1818
import { JOB_TYPE, CREATED_BY_LABEL, SHARED_RESULTS_INDEX_NAME } from './util/constants';
1919
import { isSparseDataJob } from './util/general';
2020
import { parseInterval } from '../../../../../../common/util/parse_interval';
21+
import { ml } from '../../../../services/ml_api_service';
22+
import { Calendar } from '../../../../../../common/types/calendars';
2123

2224
export class JobCreator {
2325
protected _type: JOB_TYPE = JOB_TYPE.SINGLE_METRIC;
@@ -189,11 +191,11 @@ export class JobCreator {
189191
this._job_config.groups = groups;
190192
}
191193

192-
public get calendars(): string[] {
194+
public get calendars(): Calendar[] {
193195
return this._job_config.calendars || [];
194196
}
195197

196-
public set calendars(calendars: string[]) {
198+
public set calendars(calendars: Calendar[]) {
197199
this._job_config.calendars = calendars;
198200
}
199201

@@ -441,7 +443,19 @@ export class JobCreator {
441443

442444
public async createJob(): Promise<object> {
443445
try {
444-
const { success, resp } = await mlJobService.saveNewJob(this._job_config);
446+
const { calendars, ...jobConfig } = this._job_config;
447+
const { success, resp } = await mlJobService.saveNewJob(jobConfig);
448+
449+
if (calendars && calendars.length > 0) {
450+
for (const calendar of calendars) {
451+
await ml.updateCalendar({
452+
...calendar,
453+
calendarId: calendar.calendar_id,
454+
job_ids: [this.jobId],
455+
});
456+
}
457+
}
458+
445459
if (success === true) {
446460
return resp;
447461
} else {

x-pack/legacy/plugins/ml/public/application/jobs/new_job/pages/components/job_details_step/components/additional_section/components/calendars/calendars_selection.tsx

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,52 @@
55
*/
66

77
import React, { FC, useState, useContext, useEffect } from 'react';
8-
import { EuiComboBox, EuiComboBoxOptionProps } from '@elastic/eui';
8+
import { EuiComboBox, EuiComboBoxOptionProps, EuiComboBoxProps } from '@elastic/eui';
99
import { JobCreatorContext } from '../../../../../job_creator_context';
1010
import { Description } from './description';
1111
import { ml } from '../../../../../../../../../services/ml_api_service';
12+
import { Calendar } from '../../../../../../../../../../../common/types/calendars';
1213

1314
export const CalendarsSelection: FC = () => {
1415
const { jobCreator, jobCreatorUpdate } = useContext(JobCreatorContext);
15-
const [selectedCalendars, setSelectedCalendars] = useState(jobCreator.calendars);
16-
const [selectedOptions, setSelectedOptions] = useState<EuiComboBoxOptionProps[]>([]);
17-
const [options, setOptions] = useState<EuiComboBoxOptionProps[]>([]);
16+
const [selectedCalendars, setSelectedCalendars] = useState<Calendar[]>(jobCreator.calendars);
17+
const [selectedOptions, setSelectedOptions] = useState<Array<EuiComboBoxOptionProps<Calendar>>>(
18+
[]
19+
);
20+
const [options, setOptions] = useState<Array<EuiComboBoxOptionProps<Calendar>>>([]);
1821
const [isLoading, setIsLoading] = useState(false);
1922

2023
async function loadCalendars() {
2124
setIsLoading(true);
2225
const calendars = await ml.calendars();
23-
setOptions(calendars.map(c => ({ label: c.calendar_id })));
24-
setSelectedOptions(selectedCalendars.map(c => ({ label: c })));
26+
setOptions(calendars.map(c => ({ label: c.calendar_id, value: c })));
27+
setSelectedOptions(selectedCalendars.map(c => ({ label: c.calendar_id, value: c })));
2528
setIsLoading(false);
2629
}
2730

2831
useEffect(() => {
2932
loadCalendars();
3033
}, []);
3134

32-
function onChange(optionsIn: EuiComboBoxOptionProps[]) {
33-
setSelectedOptions(optionsIn);
34-
setSelectedCalendars(optionsIn.map(o => o.label));
35-
}
36-
3735
useEffect(() => {
3836
jobCreator.calendars = selectedCalendars;
3937
jobCreatorUpdate();
4038
}, [selectedCalendars.join()]);
4139

40+
const comboBoxProps: EuiComboBoxProps<Calendar> = {
41+
async: true,
42+
options,
43+
selectedOptions,
44+
isLoading,
45+
onChange: optionsIn => {
46+
setSelectedOptions(optionsIn);
47+
setSelectedCalendars(optionsIn.map(o => o.value as Calendar));
48+
},
49+
};
50+
4251
return (
4352
<Description>
44-
<EuiComboBox
45-
async
46-
options={options}
47-
selectedOptions={selectedOptions}
48-
isLoading={isLoading}
49-
onChange={onChange}
50-
/>
53+
<EuiComboBox {...comboBoxProps} />
5154
</Description>
5255
);
5356
};

x-pack/legacy/plugins/ml/public/application/services/ml_api_service/index.d.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DataFrameAnalyticsStats } from '../../data_frame_analytics/pages/analyt
1515
import { JobMessage } from '../../../../common/types/audit_message';
1616
import { DataFrameAnalyticsConfig } from '../../data_frame_analytics/common/analytics';
1717
import { DeepPartial } from '../../../../common/types/common';
18+
import { Calendar, UpdateCalendar } from '../../../../common/types/calendars';
1819

1920
// TODO This is not a complete representation of all methods of `ml.*`.
2021
// It just satisfies needs for other parts of the code area which use
@@ -98,14 +99,8 @@ declare interface Ml {
9899
setupDataRecognizerConfig(obj: object): Promise<any>;
99100
getTimeFieldRange(obj: object): Promise<GetTimeFieldRangeResponse>;
100101
calculateModelMemoryLimit(obj: object): Promise<{ modelMemoryLimit: string }>;
101-
calendars(): Promise<
102-
Array<{
103-
calendar_id: string;
104-
description: string;
105-
events: any[];
106-
job_ids: string[];
107-
}>
108-
>;
102+
calendars(): Promise<Calendar[]>;
103+
updateCalendar(obj: UpdateCalendar): Promise<any>;
109104

110105
getVisualizerFieldStats(obj: object): Promise<any>;
111106
getVisualizerOverallStats(obj: object): Promise<any>;

0 commit comments

Comments
 (0)