Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Fixes unnecessary too many buckets warning on anomaly chart embeddable #105043

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,6 @@ export class ExplorerChartDistribution extends React.Component {
const tickValuesStart = Math.max(config.selectedEarliest, config.plotEarliest);
// +1 ms to account for the ms that was subtracted for query aggregations.
const interval = config.selectedLatest - config.selectedEarliest + 1;
const tickValues = getTickValues(
tickValuesStart,
interval,
config.plotEarliest,
config.plotLatest
);

const xAxis = d3.svg
.axis()
Expand All @@ -286,10 +280,18 @@ export class ExplorerChartDistribution extends React.Component {
.tickPadding(10)
.tickFormat((d) => moment(d).format(xAxisTickFormat));

// With tooManyBuckets the chart would end up with no x-axis labels
// because the ticks are based on the span of the emphasis section,
// and the highlighted area spans the whole chart.
if (tooManyBuckets === false) {
// With tooManyBuckets, or when the chart is used as an embeddable,
// the chart would end up with no x-axis labels because the ticks are based on the span of the
// emphasis section, and the selected area spans the whole chart.
const useAutoTicks =
tooManyBuckets === true || interval >= config.plotLatest - config.plotEarliest;
if (useAutoTicks === false) {
const tickValues = getTickValues(
tickValuesStart,
interval,
config.plotEarliest,
config.plotLatest
);
xAxis.tickValues(tickValues);
} else {
xAxis.ticks(numTicksForDateFormat(vizWidth, xAxisTickFormat));
Expand Down Expand Up @@ -327,7 +329,7 @@ export class ExplorerChartDistribution extends React.Component {
});
}

if (tooManyBuckets === false) {
if (useAutoTicks === false) {
removeLabelOverlap(gAxis, tickValuesStart, interval, vizWidth);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('ExplorerChart', () => {
expect(+selectedInterval.getAttribute('height')).toBe(166);

const xAxisTicks = wrapper.getDOMNode().querySelector('.x').querySelectorAll('.tick');
expect([...xAxisTicks]).toHaveLength(0);
expect([...xAxisTicks]).toHaveLength(1);
const yAxisTicks = wrapper.getDOMNode().querySelector('.y').querySelectorAll('.tick');
expect([...yAxisTicks]).toHaveLength(5);
const emphasizedAxisLabel = wrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,6 @@ export class ExplorerChartSingleMetric extends React.Component {
const tickValuesStart = Math.max(config.selectedEarliest, config.plotEarliest);
// +1 ms to account for the ms that was subtracted for query aggregations.
const interval = config.selectedLatest - config.selectedEarliest + 1;
const tickValues = getTickValues(
tickValuesStart,
interval,
config.plotEarliest,
config.plotLatest
);

const xAxis = d3.svg
.axis()
Expand All @@ -212,10 +206,18 @@ export class ExplorerChartSingleMetric extends React.Component {
.tickPadding(10)
.tickFormat((d) => moment(d).format(xAxisTickFormat));

// With tooManyBuckets the chart would end up with no x-axis labels
// because the ticks are based on the span of the emphasis section,
// and the highlighted area spans the whole chart.
if (tooManyBuckets === false) {
// With tooManyBuckets, or when the chart is used as an embeddable,
// the chart would end up with no x-axis labels because the ticks are based on the span of the
// emphasis section, and the selected area spans the whole chart.
const useAutoTicks =
tooManyBuckets === true || interval >= config.plotLatest - config.plotEarliest;
if (useAutoTicks === false) {
const tickValues = getTickValues(
tickValuesStart,
interval,
config.plotEarliest,
config.plotLatest
);
xAxis.tickValues(tickValues);
} else {
xAxis.ticks(numTicksForDateFormat(vizWidth, xAxisTickFormat));
Expand Down Expand Up @@ -243,7 +245,7 @@ export class ExplorerChartSingleMetric extends React.Component {

axes.append('g').attr('class', 'y axis').call(yAxis);

if (tooManyBuckets === false) {
if (useAutoTicks === false) {
removeLabelOverlap(gAxis, tickValuesStart, interval, vizWidth);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('ExplorerChart', () => {
expect(+selectedInterval.getAttribute('height')).toBe(166);

const xAxisTicks = wrapper.getDOMNode().querySelector('.x').querySelectorAll('.tick');
expect([...xAxisTicks]).toHaveLength(0);
expect([...xAxisTicks]).toHaveLength(1);
const yAxisTicks = wrapper.getDOMNode().querySelector('.y').querySelectorAll('.tick');
expect([...yAxisTicks]).toHaveLength(10);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,20 @@ export class AnomalyExplorerChartsService {
chartRange.min = chartRange.min + maxBucketSpanMs;
}

// When used as an embeddable, selectedEarliestMs is the start date on the time picker,
// which may be earlier than the time of the first point plotted in the chart (as we plot
// the first full bucket with a start date no earlier than the start).
const selectedEarliestBucketCeil = boundsMin
? Math.ceil(Math.max(selectedEarliestMs, boundsMin) / maxBucketSpanMs) * maxBucketSpanMs
: Math.ceil(selectedEarliestMs / maxBucketSpanMs) * maxBucketSpanMs;

const selectedLatestBucketStart = boundsMax
? Math.floor(Math.min(selectedLatestMs, boundsMax) / maxBucketSpanMs) * maxBucketSpanMs
: Math.floor(selectedLatestMs / maxBucketSpanMs) * maxBucketSpanMs;

if (
(chartRange.min > selectedEarliestMs || chartRange.max < selectedLatestBucketStart) &&
chartRange.max - chartRange.min < selectedLatestBucketStart - selectedEarliestMs
(chartRange.min > selectedEarliestBucketCeil || chartRange.max < selectedLatestBucketStart) &&
chartRange.max - chartRange.min < selectedLatestBucketStart - selectedEarliestBucketCeil
) {
tooManyBuckets = true;
}
Expand Down