[Data] Fixed Date histogram bounds calculation doesn't update "now" (#135899)

* Fixed the problem with date histogram bounds calculation.

* Update src/plugins/data/server/search/aggs/aggs_service.ts

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>

Co-authored-by: Anton Dosov <dosantappdev@gmail.com>
This commit is contained in:
Yaroslav Kuznietsov 2022-07-08 18:26:55 +03:00 committed by GitHub
parent 9860e25a88
commit fc2c3ec10f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 18 deletions

View file

@ -28,6 +28,7 @@ export * from './search';
export type {
RefreshInterval,
TimeRangeBounds,
TimeRange,
GetConfigFn,
SavedQuery,
SavedQueryAttributes,

View file

@ -9,8 +9,7 @@
import { ExpressionsServiceSetup } from '@kbn/expressions-plugin/common';
import type { DataView } from '@kbn/data-views-plugin/common';
import type { FieldFormatsStartCommon } from '@kbn/field-formats-plugin/common';
import { CalculateBoundsOptions } from '../../query';
import { UI_SETTINGS, AggTypesDependencies, calculateBounds } from '../..';
import { UI_SETTINGS, AggTypesDependencies } from '../..';
import { GetConfigFn } from '../../types';
import {
AggConfigs,
@ -42,7 +41,7 @@ export interface AggsCommonStartDependencies {
getIndexPattern(id: string): Promise<DataView>;
getConfig: GetConfigFn;
fieldFormats: FieldFormatsStartCommon;
calculateBoundsOptions: CalculateBoundsOptions;
calculateBounds: AggTypesDependencies['calculateBounds'];
}
/**
@ -75,13 +74,13 @@ export class AggsCommonService {
public start({
getConfig,
fieldFormats,
calculateBoundsOptions,
calculateBounds,
}: AggsCommonStartDependencies): AggsCommonStart {
const aggTypesStart = this.aggTypesRegistry.start({
getConfig,
getFieldFormatsStart: () => fieldFormats,
aggExecutionContext: this.aggExecutionContext,
calculateBounds: (timeRange) => calculateBounds(timeRange, calculateBoundsOptions),
calculateBounds,
});
return {

View file

@ -137,7 +137,6 @@ export class DataPublicPlugin
fieldFormats,
indexPatterns: dataViews,
screenshotMode,
nowProvider: this.nowProvider,
});
setSearchService(search);

View file

@ -34,11 +34,11 @@ describe('AggsService - public', () => {
setupDeps = {
registerFunction: expressionsPluginMock.createSetupContract().registerFunction,
uiSettings,
nowProvider: createNowProviderMock(),
};
startDeps = {
fieldFormats: fieldFormatsServiceMock.createStartContract(),
indexPatterns: dataPluginMock.createStartContract().indexPatterns,
nowProvider: createNowProviderMock(),
};
});

View file

@ -17,6 +17,7 @@ import {
AggsCommonStartDependencies,
AggsCommonService,
} from '../../../common/search/aggs';
import { calculateBounds, TimeRange } from '../../../common';
import type { AggsSetup, AggsStart } from './types';
import type { NowProviderInternalContract } from '../../now_provider';
@ -49,13 +50,13 @@ export function createGetConfig(
export interface AggsSetupDependencies {
uiSettings: IUiSettingsClient;
registerFunction: ExpressionsServiceSetup['registerFunction'];
nowProvider: NowProviderInternalContract;
}
/** @internal */
export interface AggsStartDependencies {
fieldFormats: FieldFormatsStart;
indexPatterns: DataViewsContract;
nowProvider: NowProviderInternalContract;
}
/**
@ -69,8 +70,17 @@ export class AggsService {
});
private getConfig?: AggsCommonStartDependencies['getConfig'];
private subscriptions: Subscription[] = [];
private nowProvider!: NowProviderInternalContract;
public setup({ registerFunction, uiSettings }: AggsSetupDependencies): AggsSetup {
/**
* NowGetter uses window.location, so we must have a separate implementation
* of calculateBounds on the client and the server.
*/
private calculateBounds = (timeRange: TimeRange) =>
calculateBounds(timeRange, { forceNow: this.nowProvider.get() });
public setup({ registerFunction, uiSettings, nowProvider }: AggsSetupDependencies): AggsSetup {
this.nowProvider = nowProvider;
this.getConfig = createGetConfig(uiSettings, aggsRequiredUiSettings, this.subscriptions);
return this.aggsCommonService.setup({
@ -78,14 +88,12 @@ export class AggsService {
});
}
public start({ indexPatterns, fieldFormats, nowProvider }: AggsStartDependencies): AggsStart {
public start({ indexPatterns, fieldFormats }: AggsStartDependencies): AggsStart {
const { calculateAutoTimeExpression, types, createAggConfigs } = this.aggsCommonService.start({
getConfig: this.getConfig!,
getIndexPattern: indexPatterns.get,
calculateBounds: this.calculateBounds,
fieldFormats,
calculateBoundsOptions: {
forceNow: nowProvider.get(),
},
});
return {

View file

@ -89,7 +89,6 @@ export interface SearchServiceStartDependencies {
fieldFormats: FieldFormatsStart;
indexPatterns: DataViewsContract;
screenshotMode: ScreenshotModePluginStart;
nowProvider: NowProviderInternalContract;
}
export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
@ -191,6 +190,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
const aggs = this.aggsService.setup({
uiSettings,
registerFunction: expressions.registerFunction,
nowProvider,
});
if (this.initializerContext.config.get().search.aggs.shardDelay.enabled) {
@ -223,7 +223,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
public start(
{ http, theme, uiSettings, chrome, application }: CoreStart,
{ fieldFormats, indexPatterns, screenshotMode, nowProvider }: SearchServiceStartDependencies
{ fieldFormats, indexPatterns, screenshotMode }: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options = {}) => {
return this.searchInterceptor.search(request, options);
@ -232,7 +232,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
const loadingCount$ = new BehaviorSubject(0);
http.addLoadingCountSource(loadingCount$);
const aggs = this.aggsService.start({ fieldFormats, indexPatterns, nowProvider });
const aggs = this.aggsService.start({ fieldFormats, indexPatterns });
const searchSourceDependencies: SearchSourceDependencies = {
aggs,

View file

@ -17,7 +17,12 @@ import type {
import { ExpressionsServiceSetup } from '@kbn/expressions-plugin/common';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/server';
import { DataViewsServerPluginStart } from '@kbn/data-views-plugin/server';
import { AggsCommonService, aggsRequiredUiSettings } from '../../../common';
import {
calculateBounds,
AggsCommonService,
aggsRequiredUiSettings,
TimeRange,
} from '../../../common';
import { AggsSetup, AggsStart } from './types';
/** @internal */
@ -48,6 +53,12 @@ async function getConfigFn(uiSettingsClient: IUiSettingsClient) {
export class AggsService {
private readonly aggsCommonService = new AggsCommonService({ shouldDetectTimeZone: false });
/**
* getForceNow uses window.location on the client, so we must have a
* separate implementation of calculateBounds on the server.
*/
private calculateBounds = (timeRange: TimeRange) => calculateBounds(timeRange);
public setup({ registerFunction }: AggsSetupDependencies): AggsSetup {
return this.aggsCommonService.setup({
registerFunction,
@ -65,10 +76,10 @@ export class AggsService {
this.aggsCommonService.start({
getConfig: await getConfigFn(uiSettingsClient),
fieldFormats: await fieldFormats.fieldFormatServiceFactory(uiSettingsClient),
calculateBoundsOptions: {},
getIndexPattern: (
await indexPatterns.dataViewsServiceFactory(savedObjectsClient, elasticsearchClient)
).get,
calculateBounds: this.calculateBounds,
});
return {