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

fix bug by keeping track of interval within the explorer #1266

Merged
merged 13 commits into from
Nov 9, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const TraceConfig = (props: TraceConfigProps) => {
useEffect(() => {
setLoading(true);
const timeFilterDSL = filtersToDsl([], '', startTime, endTime);
const latencyTrendStartTime = dateMath.parse(endTime)?.subtract(24, 'hours').toISOString()!;
const latencyTrendStartTime = dateMath.parse(endTime, { roundUp: true })?.subtract(24, 'hours').toISOString()!;
const latencyTrendDSL = filtersToDsl(filters, query, latencyTrendStartTime, endTime);
handleDashboardRequest(
http,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ export const Explorer = ({
const [browserTabFocus, setBrowserTabFocus] = useState(true);
const [liveTimestamp, setLiveTimestamp] = useState(DATE_PICKER_FORMAT);
const [triggerAvailability, setTriggerAvailability] = useState(false);

const selectedIntervalRef = useRef<{
text: string;
value: string;
}>();
const [viewLogPatterns, setViewLogPatterns] = useState(false);
const [spanValue, setSpanValue] = useState(false);
const [subType, setSubType] = useState('visualization');
Expand All @@ -203,13 +208,11 @@ export const Explorer = ({
liveTailTabIdRef.current = liveTailTabId;
liveTailNameRef.current = liveTailName;

let minInterval = 'y';
const findAutoInterval = (start: string = '', end: string = '') => {
if (start?.length === 0 || end?.length === 0 || start === end)
return ['d', [...TIME_INTERVAL_OPTIONS]];
const momentStart = dateMath.parse(start)!;
const momentEnd = dateMath.parse(end)!;
const momentEnd = dateMath.parse(end, { roundUp: true })!;
const diffSeconds = momentEnd.unix() - momentStart.unix();
let minInterval = 'y'

// less than 1 second
if (diffSeconds <= 1) minInterval = 'ms';
Expand All @@ -230,6 +233,7 @@ export const Explorer = ({
{ text: 'Auto', value: 'auto_' + minInterval },
...TIME_INTERVAL_OPTIONS,
]);
selectedIntervalRef.current = ({ text: 'Auto', value: 'auto_' + minInterval })
};

useEffect(() => {
Expand Down Expand Up @@ -436,18 +440,20 @@ export const Explorer = ({
);
}
} else {
findAutoInterval(startTime, endTime);
if (!selectedIntervalRef.current || selectedIntervalRef.current.text === 'Auto') {
findAutoInterval(startingTime, endingTime);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between startingTime and startTime?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may need someone more familiar with this component/code (@mengweieric ?) - but from what I can tell this explorer is rendered by both Application analytics and event analytics - for event analytics startTime is undefined, resulting in auto interval not getting picked up. In either case startingTime should be already set above based on the query so I think(?) this is a more general implementation

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes confirmed. Event analytics previously does not use any passed in start/starting time, I think later there's live tail introduced with starting/ending time for differentiating existing start/end time just for reusing the exiting data fetching

}
if (isLiveTailOnRef.current) {
getLiveTail(undefined, getErrorHandler('Error fetching events'));
} else {
getEvents(undefined, getErrorHandler('Error fetching events'));
}
getCountVisualizations(minInterval);
getCountVisualizations(selectedIntervalRef.current!.value.replace(/^auto_/, ''));

// to fetch patterns data on current query
if (!finalQuery.match(PATTERNS_REGEX)) {
getPatterns(minInterval);
}
getPatterns(selectedIntervalRef.current!.value.replace(/^auto_/, ''));
}
}

// for comparing usage if for the same tab, user changed index from one to another
Expand Down Expand Up @@ -740,11 +746,14 @@ export const Explorer = ({
<TimechartHeader
dateFormat={'MMM D, YYYY @ HH:mm:ss.SSS'}
options={timeIntervalOptions}
onChangeInterval={(intrv) => {
onChangeInterval={(selectedIntrv) => {
const intervalOptionsIndex = timeIntervalOptions.findIndex(item => item.value === selectedIntrv)
const intrv = selectedIntrv.replace(/^auto_/, '')
getCountVisualizations(intrv);
selectedIntervalRef.current = timeIntervalOptions[intervalOptionsIndex]
getPatterns(intrv, getErrorHandler('Error fetching patterns'));
}}
stateInterval="auto"
stateInterval={selectedIntervalRef.current?.value}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down Expand Up @@ -1352,7 +1361,7 @@ export const Explorer = ({
setIsLiveTailOn(true);
setToast('Live tail On', 'success');
setIsLiveTailPopoverOpen(false);
setLiveTimestamp(dateMath.parse(endingTime)?.utc().format(DATE_PICKER_FORMAT) || '');
setLiveTimestamp(dateMath.parse(endingTime, { roundUp: true })?.utc().format(DATE_PICKER_FORMAT) || '');
setLiveHits(0);
await sleep(2000);
const curLiveTailname = liveTailNameRef.current;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports[`Time chart header component Renders Time chart header component 1`] = `
},
]
}
stateInterval="auto"
stateInterval="w"
>
<I18nProvider>
<IntlProvider
Expand Down Expand Up @@ -203,7 +203,7 @@ exports[`Time chart header component Renders Time chart header component 1`] = `
},
]
}
value="m"
value="w"
>
<EuiFormControlLayout
compressed={true}
Expand Down Expand Up @@ -231,7 +231,7 @@ exports[`Time chart header component Renders Time chart header component 1`] = `
id="dscResultsIntervalSelector"
onChange={[Function]}
onMouseUp={[Function]}
value="m"
value="w"
>
<option
key="0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Time chart header component', () => {
dateFormat={'MMM D, YYYY @ HH:mm:ss.SSS'}
onChangeInterval={onChangeInterval}
options={TIME_INTERVAL_OPTIONS}
stateInterval="auto"
stateInterval="w"
/>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState, useEffect, useCallback } from 'react';
import React from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiToolTip, EuiText, EuiSelect } from '@elastic/eui';
import { I18nProvider } from '@osd/i18n/react';
import { i18n } from '@osd/i18n';
import moment from 'moment';

export interface TimechartHeaderProps {
/**
Expand All @@ -32,18 +31,17 @@ export interface TimechartHeaderProps {
/**
* selected interval
*/
stateInterval: string;
stateInterval?: string | undefined;
}

export function TimechartHeader({
options,
onChangeInterval
onChangeInterval,
stateInterval,
}: TimechartHeaderProps) {
const [interval, setInterval] = useState(options[0].value);

const handleIntervalChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
setInterval(e.target.value);
onChangeInterval(e.target.value.length > 2 ? e.target.value.slice(5) : e.target.value);
onChangeInterval(e.target.value);
};

return (
Expand All @@ -68,7 +66,7 @@ export function TimechartHeader({
id="dscResultsIntervalSelector"
data-test-subj="eventAnalytics__EventIntervalSelect"
options={options}
value={interval}
value={stateInterval || options[0].value}
onChange={handleIntervalChange}
append={undefined}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ export {
populateDataGrid,
isValidTraceId,
formatError,
findAutoInterval,
getDefaultVisConfig
} from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -279,32 +279,6 @@ export const formatError = (name: string, message: string, details: string) => {
};
};

export const findAutoInterval = (start: string = '', end: string = '') => {
let minInterval = 'y';
if (start?.length === 0 || end?.length === 0 || start === end)
return ['d', [...TIME_INTERVAL_OPTIONS]];
const momentStart = dateMath.parse(start)!;
const momentEnd = dateMath.parse(end)!;
const diffSeconds = momentEnd.unix() - momentStart.unix();

// less than 1 second
if (diffSeconds <= 1) minInterval = 'ms';
// less than 2 minutes
else if (diffSeconds <= 60 * 2) minInterval = 's';
// less than 2 hours
else if (diffSeconds <= 3600 * 2) minInterval = 'm';
// less than 2 days
else if (diffSeconds <= 86400 * 2) minInterval = 'h';
// less than 1 month
else if (diffSeconds <= 86400 * 31) minInterval = 'd';
// less than 3 months
else if (diffSeconds <= 86400 * 93) minInterval = 'w';
// less than 1 year
else if (diffSeconds <= 86400 * 366) minInterval = 'M';

return [minInterval, [{ text: 'Auto', value: 'auto_' + minInterval }, ...TIME_INTERVAL_OPTIONS]];
};

export const hexToRgb = (
hex: string = '#3CA1C7',
opacity: number = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export function calculateTicks(min: number, max: number, numTicks = 5): number[]
export const minFixedInterval = (startTime: string, endTime: string) => {
if (startTime?.length === 0 || endTime?.length === 0 || startTime === endTime) return '1d';
const momentStart = dateMath.parse(startTime)!;
const momentEnd = dateMath.parse(endTime)!;
const momentEnd = dateMath.parse(endTime, { roundUp: true })!;
const diffSeconds = momentEnd.unix() - momentStart.unix();

if (diffSeconds <= 1) return '1ms'; // less than 1 second
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function DashboardContent(props: DashboardProps) {
setLoading(true);
const DSL = filtersToDsl(filters, query, startTime, endTime, page, appConfigs);
const timeFilterDSL = filtersToDsl([], '', startTime, endTime, page, appConfigs);
const latencyTrendStartTime = dateMath.parse(endTime)?.subtract(24, 'hours').toISOString()!;
const latencyTrendStartTime = dateMath.parse(endTime, { roundUp: true })?.subtract(24, 'hours').toISOString()!;
const latencyTrendDSL = filtersToDsl(
filters,
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const handleServiceMapRequest = async (
const startTime = DSL.custom?.timeFilter?.[0]?.range?.startTime;
if (startTime) {
const gte = dateMath.parse(startTime.gte)!;
const lte = dateMath.parse(startTime.lte)!;
const lte = dateMath.parse(startTime.lte, { roundUp: true })!;
minutesInDateRange = lte.diff(gte, 'minutes', true);
}

Expand Down