From d69f13a2fd028b7d2016b86583b3ebd1e183e0b8 Mon Sep 17 00:00:00 2001 From: ppisljar Date: Thu, 11 Apr 2024 12:05:47 +0200 Subject: [PATCH] fixing annotations --- .../editor_frame_service/editor_frame/editor_frame.tsx | 5 +++++ x-pack/plugins/lens/public/state_management/selectors.ts | 1 + x-pack/plugins/lens/public/types.ts | 1 + x-pack/plugins/lens/public/utils.ts | 9 +++++++++ .../public/visualizations/xy/annotations/helpers.tsx | 6 +++--- .../annotations_config_panel/annotations_panel.tsx | 4 ++-- 6 files changed, 21 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx index 45b11b1c7724b5..e0736b149fcbb3 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx @@ -9,6 +9,7 @@ import React, { useCallback, useRef } from 'react'; import { CoreStart } from '@kbn/core/public'; import { ReactExpressionRendererType } from '@kbn/expressions-plugin/public'; import { type DragDropAction, DragDropIdentifier, RootDragDropProvider } from '@kbn/dom-drag-drop'; +import { getAbsoluteDateRange } from '../../utils'; import { trackUiCounterEvents } from '../../lens_ui_telemetry'; import { DatasourceMap, @@ -68,6 +69,10 @@ export function EditorFrame(props: EditorFrameProps) { selectFramePublicAPI(state, datasourceMap) ); + framePublicAPI.absDateRange = getAbsoluteDateRange( + props.plugins.data.query.timefilter.timefilter + ); + // Using a ref to prevent rerenders in the child components while keeping the latest state const getSuggestionForField = useRef<(field: DragDropIdentifier) => Suggestion | undefined>(); getSuggestionForField.current = (field: DragDropIdentifier) => { diff --git a/x-pack/plugins/lens/public/state_management/selectors.ts b/x-pack/plugins/lens/public/state_management/selectors.ts index 58c7d981dcb412..2187302ae02e42 100644 --- a/x-pack/plugins/lens/public/state_management/selectors.ts +++ b/x-pack/plugins/lens/public/state_management/selectors.ts @@ -237,6 +237,7 @@ export const selectFramePublicAPI = createSelector( activeData, dataViews, ...context, + absDateRange: context.dateRange, }; } ); diff --git a/x-pack/plugins/lens/public/types.ts b/x-pack/plugins/lens/public/types.ts index c1c7be77b9608b..e9afc7300e0629 100644 --- a/x-pack/plugins/lens/public/types.ts +++ b/x-pack/plugins/lens/public/types.ts @@ -938,6 +938,7 @@ export interface FramePublicAPI { filters: Filter[]; datasourceLayers: DatasourceLayers; dateRange: DateRange; + absDateRange: DateRange; /** * Data of the chart currently rendered in the preview. * This data might be not available (e.g. if the chart can't be rendered) or outdated and belonging to another chart. diff --git a/x-pack/plugins/lens/public/utils.ts b/x-pack/plugins/lens/public/utils.ts index bd1d146a1f1f51..f783f15475f68c 100644 --- a/x-pack/plugins/lens/public/utils.ts +++ b/x-pack/plugins/lens/public/utils.ts @@ -50,6 +50,15 @@ export const getResolvedDateRange = function (timefilter: TimefilterContract) { return { fromDate: from, toDate: to }; }; +export const getAbsoluteDateRange = function (timefilter: TimefilterContract) { + const { from, to } = timefilter.getTime(); + const { min, max } = timefilter.calculateBounds({ + from, + to, + }); + return { fromDate: min?.toISOString() || from, toDate: max?.toISOString() || to }; +}; + export function containsDynamicMath(dateMathString: string) { return dateMathString.includes('now'); } diff --git a/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx b/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx index 0c821775dff203..7914bf81a717a8 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/annotations/helpers.tsx @@ -45,10 +45,10 @@ export const defaultRangeAnnotationLabel = i18n.translate( export function getStaticDate(dataLayers: XYDataLayerConfig[], frame: FramePublicAPI) { const dataLayersId = dataLayers.map(({ layerId }) => layerId); - const { activeData, dateRange } = frame; + const { activeData, absDateRange } = frame; - const dateRangeMinValue = moment(dateRange.fromDate).valueOf(); - const dateRangeMaxValue = moment(dateRange.toDate).valueOf(); + const dateRangeMinValue = moment(absDateRange.fromDate).valueOf(); + const dateRangeMaxValue = moment(absDateRange.toDate).valueOf(); const fallbackValue = moment((dateRangeMinValue + dateRangeMaxValue) / 2).toISOString(); if ( diff --git a/x-pack/plugins/lens/public/visualizations/xy/xy_config_panel/annotations_config_panel/annotations_panel.tsx b/x-pack/plugins/lens/public/visualizations/xy/xy_config_panel/annotations_config_panel/annotations_panel.tsx index db4d43d01b9639..a53b38dc38e334 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/xy_config_panel/annotations_config_panel/annotations_panel.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/xy_config_panel/annotations_config_panel/annotations_panel.tsx @@ -111,12 +111,12 @@ export const AnnotationsPanel = ( const getEndTimestamp = ( datatableUtilities: DatatableUtilitiesService, startTime: string, - { activeData, dateRange }: FramePublicAPI, + { activeData, absDateRange }: FramePublicAPI, dataLayers: XYDataLayerConfig[] ) => { const startTimeNumber = moment(startTime).valueOf(); const dateRangeFraction = - (moment(dateRange.toDate).valueOf() - moment(dateRange.fromDate).valueOf()) * 0.1; + (moment(absDateRange.toDate).valueOf() - moment(absDateRange.fromDate).valueOf()) * 0.1; const fallbackValue = moment(startTimeNumber + dateRangeFraction).toISOString(); const dataLayersId = dataLayers.map(({ layerId }) => layerId); if (