Skip to content

Commit

Permalink
fixing annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Apr 15, 2024
1 parent 5841060 commit d69f13a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/state_management/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export const selectFramePublicAPI = createSelector(
activeData,
dataViews,
...context,
absDateRange: context.dateRange,
};
}
);
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/lens/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit d69f13a

Please sign in to comment.