Skip to content

Commit

Permalink
[ML] Ensure chart interval is not smaller than bucket span (#44895) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jgowdyelastic committed Sep 7, 2019
1 parent 85dc21d commit 4d92b49
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/ml/common/util/parse_interval.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

interface Duration {
asSeconds(): number;
asMilliseconds(): number;
}
export function parseInterval(interval: string): Duration;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mlJobService } from '../../../../services/job_service';
import { JobRunner, ProgressSubscriber } from '../job_runner';
import { JOB_TYPE, CREATED_BY_LABEL } from './util/constants';
import { isSparseDataJob } from './util/general';
import { parseInterval } from '../../../../../common/util/parse_interval';

export class JobCreator {
protected _type: JOB_TYPE = JOB_TYPE.SINGLE_METRIC;
Expand All @@ -25,6 +26,7 @@ export class JobCreator {
protected _datafeed_config: Datafeed;
protected _detectors: Detector[];
protected _influencers: string[];
protected _bucketSpanMs: number = 0;
protected _useDedicatedIndex: boolean = false;
protected _start: number = 0;
protected _end: number = 0;
Expand Down Expand Up @@ -113,12 +115,22 @@ export class JobCreator {

public set bucketSpan(bucketSpan: BucketSpan) {
this._job_config.analysis_config.bucket_span = bucketSpan;
this._setBucketSpanMs(bucketSpan);
}

public get bucketSpan(): BucketSpan {
return this._job_config.analysis_config.bucket_span;
}

protected _setBucketSpanMs(bucketSpan: BucketSpan) {
const bs = parseInterval(bucketSpan);
this._bucketSpanMs = bs === null ? 0 : bs.asMilliseconds();
}

public get bucketSpanMs(): number {
return this._bucketSpanMs;
}

public addInfluencer(influencer: string) {
if (this._influencers.includes(influencer) === false) {
this._influencers.push(influencer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { SavedSearch } from 'src/legacy/core_plugins/kibana/public/discover/types';
import { IndexPattern } from 'ui/index_patterns';
import { parseInterval } from 'ui/utils/parse_interval';
import { parseInterval } from '../../../../../common/util/parse_interval';
import { JobCreator } from './job_creator';
import { Field, Aggregation, AggFieldPair } from '../../../../../common/types/fields';
import { Job, Datafeed, Detector, BucketSpan } from './configs';
Expand Down Expand Up @@ -43,6 +43,7 @@ export class SingleMetricJobCreator extends JobCreator {

public set bucketSpan(bucketSpan: BucketSpan) {
this._job_config.analysis_config.bucket_span = bucketSpan;
this._setBucketSpanMs(bucketSpan);
this._createDatafeedAggregations();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ export function getChartSettings(jobCreator: JobCreatorType, chartInterval: MlTi
cs.intervalMs = interval.getInterval().asMilliseconds();
}

if (cs.intervalMs < jobCreator.bucketSpanMs) {
// don't allow the chart interval to be smaller than the bucket span
cs.intervalMs = jobCreator.bucketSpanMs;
}

if (isMultiMetricJobCreator(jobCreator) || isPopulationJobCreator(jobCreator)) {
if (jobCreator.aggFieldPairs.length > 2 && isMultiMetricJobCreator(jobCreator)) {
cs.cols = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const MultiMetricDetectors: FC<Props> = ({ setIsValid }) => {
const [loadingData, setLoadingData] = useState(false);
const [start, setStart] = useState(jobCreator.start);
const [end, setEnd] = useState(jobCreator.end);
const [bucketSpanMs, setBucketSpanMs] = useState(jobCreator.bucketSpanMs);
const [chartSettings, setChartSettings] = useState(defaultChartSettings);
const [splitField, setSplitField] = useState(jobCreator.splitField);
const [fieldValues, setFieldValues] = useState<string[]>([]);
Expand Down Expand Up @@ -94,6 +95,12 @@ export const MultiMetricDetectors: FC<Props> = ({ setIsValid }) => {
setEnd(jobCreator.end);
loadCharts();
}

if (jobCreator.bucketSpanMs !== bucketSpanMs) {
setBucketSpanMs(jobCreator.bucketSpanMs);
loadCharts();
}

setSplitField(jobCreator.splitField);
}, [jobCreatorUpdated]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const PopulationDetectors: FC<Props> = ({ setIsValid }) => {
const [loadingData, setLoadingData] = useState(false);
const [start, setStart] = useState(jobCreator.start);
const [end, setEnd] = useState(jobCreator.end);
const [bucketSpanMs, setBucketSpanMs] = useState(jobCreator.bucketSpanMs);
const [chartSettings, setChartSettings] = useState(defaultChartSettings);
const [splitField, setSplitField] = useState(jobCreator.splitField);
const [fieldValuesPerDetector, setFieldValuesPerDetector] = useState<DetectorFieldValues>({});
Expand Down Expand Up @@ -112,6 +113,12 @@ export const PopulationDetectors: FC<Props> = ({ setIsValid }) => {
setEnd(jobCreator.end);
loadCharts();
}

if (jobCreator.bucketSpanMs !== bucketSpanMs) {
setBucketSpanMs(jobCreator.bucketSpanMs);
loadCharts();
}

setSplitField(jobCreator.splitField);

// update by fields and their by fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const SingleMetricDetectors: FC<Props> = ({ setIsValid }) => {
const [loadingData, setLoadingData] = useState(false);
const [start, setStart] = useState(jobCreator.start);
const [end, setEnd] = useState(jobCreator.end);
const [bucketSpanMs, setBucketSpanMs] = useState(jobCreator.bucketSpanMs);

function detectorChangeHandler(selectedOptionsIn: DropDownLabel[]) {
setSelectedOptions(selectedOptionsIn);
Expand Down Expand Up @@ -75,6 +76,11 @@ export const SingleMetricDetectors: FC<Props> = ({ setIsValid }) => {
setEnd(jobCreator.end);
loadChart();
}

if (jobCreator.bucketSpanMs !== bucketSpanMs) {
setBucketSpanMs(jobCreator.bucketSpanMs);
loadChart();
}
}, [jobCreatorUpdated]);

async function loadChart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export const Page: FC<PageProps> = ({ existingJobsAndGroups, jobType }) => {
if (isSingleMetricJobCreator(jobCreator) === true) {
jobCreator.modelPlot = true;
}

if (kibanaContext.currentSavedSearch.id === undefined) {
// Jobs created from saved searches cannot be cloned in the wizard as the
// ML job config holds no reference to the saved search ID.
jobCreator.createdBy = null;
}
}

const chartInterval = new MlTimeBuckets();
Expand Down

0 comments on commit 4d92b49

Please sign in to comment.