[ML] Set zoom to start of forecast on opening in Single Metric Viewer (#29503)

This commit is contained in:
Pete Harverson 2019-01-29 15:10:52 +00:00 committed by GitHub
parent 1ce293e26d
commit f5638bdce3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -573,10 +573,12 @@ module.controller('MlTimeSeriesExplorerController', function (
// Store forecast ID in the appState.
$scope.appState.mlTimeSeriesExplorer.forecastId = forecastId;
// Set the zoom to show backwards from the end of the forecast range.
// Set the zoom to centre on the start of the forecast range, depending
// on the time range of the forecast and data.
const earliestDataDate = _.first($scope.contextChartData).date;
const zoomLatestMs = resp.latest;
const zoomEarliestMs = Math.max(earliestDataDate.getTime(), zoomLatestMs - $scope.autoZoomDuration);
const zoomLatestMs = Math.min(earliest + ($scope.autoZoomDuration / 2), latest.valueOf());
const zoomEarliestMs = Math.max(zoomLatestMs - $scope.autoZoomDuration, earliestDataDate.getTime());
const zoomState = {
from: moment(zoomEarliestMs).toISOString(),
to: moment(zoomLatestMs).toISOString()
@ -588,9 +590,11 @@ module.controller('MlTimeSeriesExplorerController', function (
// Ensure the forecast data will be shown if hidden previously.
$scope.showForecast = true;
if (earliest.isBefore(bounds.min) || latest.isAfter(bounds.max)) {
const earliestMs = Math.min(earliest.valueOf(), bounds.min.valueOf());
const latestMs = Math.max(latest.valueOf(), bounds.max.valueOf());
timefilter.setTime({
from: moment(earliestMs).toISOString(),
to: moment(latestMs).toISOString()
@ -677,6 +681,7 @@ module.controller('MlTimeSeriesExplorerController', function (
}
const defaultRange = calculateDefaultFocusRange();
if ((selection.from.getTime() !== defaultRange[0].getTime() || selection.to.getTime() !== defaultRange[1].getTime()) &&
(isNaN(Date.parse(selection.from)) === false && isNaN(Date.parse(selection.to)) === false)) {
const zoomState = { from: selection.from.toISOString(), to: selection.to.toISOString() };
@ -905,18 +910,34 @@ module.controller('MlTimeSeriesExplorerController', function (
}
function calculateDefaultFocusRange() {
// Returns the range that shows the most recent data at bucket span granularity.
$scope.autoZoomDuration = getAutoZoomDuration();
const isForecastData = $scope.contextForecastData !== undefined && $scope.contextForecastData.length > 0;
const combinedData = $scope.contextForecastData === undefined ?
const combinedData = (isForecastData === false) ?
$scope.contextChartData : $scope.contextChartData.concat($scope.contextForecastData);
const earliestDataDate = _.first(combinedData).date;
const latestDataDate = _.last(combinedData).date;
const latestMsToLoad = latestDataDate.getTime() + $scope.contextAggregationInterval.asMilliseconds();
const earliestMsToLoad = Math.max(earliestDataDate.getTime(), latestMsToLoad - $scope.autoZoomDuration);
return [new Date(earliestMsToLoad), new Date(latestMsToLoad)];
let rangeEarliestMs;
let rangeLatestMs;
if (isForecastData === true) {
// Return a range centred on the start of the forecast range, depending
// on the time range of the forecast and data.
const earliestForecastDataDate = _.first($scope.contextForecastData).date;
const latestForecastDataDate = _.last($scope.contextForecastData).date;
rangeLatestMs = Math.min(earliestForecastDataDate.getTime() + ($scope.autoZoomDuration / 2), latestForecastDataDate.getTime());
rangeEarliestMs = Math.max(rangeLatestMs - $scope.autoZoomDuration, earliestDataDate.getTime());
} else {
// Returns the range that shows the most recent data at bucket span granularity.
rangeLatestMs = latestDataDate.getTime() + $scope.contextAggregationInterval.asMilliseconds();
rangeEarliestMs = Math.max(earliestDataDate.getTime(), rangeLatestMs - $scope.autoZoomDuration);
}
return [new Date(rangeEarliestMs), new Date(rangeLatestMs)];
}
function calculateAggregationInterval(bounds, bucketsTarget) {