Skip to content

Commit

Permalink
Correct date pass-through on Notebook Visualizations. (#1327)
Browse files Browse the repository at this point in the history
* Correct date pass-through on Notebook Visualizations.

Also fix an scss error.

---------

Signed-off-by: Peter Fitzgibbons <peter.fitzgibbons@gmail.com>
(cherry picked from commit 8e18097)
  • Loading branch information
pjfitzgibbons committed Dec 22, 2023
1 parent 908a961 commit 780722a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import React from 'react';
import {
convertDateTime,
findMinInterval,
parsePromQLIntoKeywords,
preprocessMetricQuery,
Expand Down Expand Up @@ -61,6 +62,42 @@ describe('Query Utils', () => {
expect(minInterval).toEqual(span);
});
});
describe('convertDateTime', () => {
it('converts from absolute timestamp', () => {
const time = '2020-07-21T18:37:44.710Z';
const converted = convertDateTime(time);
expect(converted).toEqual('2020-07-21 18:37:44.710000');
});
it('formats to PPL standard format when default formatting', () => {
const time = '2020-07-21T18:37:44.710Z';
const converted = convertDateTime(time, true, true);
expect(converted).toEqual('2020-07-21 18:37:44.710000');
});
it('formats to specified format when provided', () => {
const time = '2020-07-21T18:37:44.710Z';
const converted = convertDateTime(time, true, 'YYYY-MMM-DD');
expect(converted).toMatch(/2020-jul-21/i);
});
describe('with moment reference notations', () => {
beforeEach(() => {
jest.useFakeTimers().setSystemTime(new Date('2020-02-02 12:01:00'));
});
afterEach(() => {
jest.useRealTimers();
});

it('converts named-reference, rounded', () => {
const time = 'now-1d/d';
const converted = convertDateTime(time, true);
expect(converted).toEqual('2020-02-01 00:00:00.000000');
});
it.skip('converts named-reference, rounded as end of interval', () => {
const time = 'now/d';
const converted = convertDateTime(time);
expect(converted).toEqual('2020-02-02 23:59:59.999999');
});
});
});
describe('Metric Query processors', () => {
const defaultQueryMetaData = {
catalogSourceName: 'my_catalog',
Expand Down
4 changes: 3 additions & 1 deletion public/components/common/query_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ export const convertDateTime = (
const epochTime = myDate.getTime() / 1000.0;
return Math.round(epochTime);
}
if (formatted) return returnTime?.utc()?.format(PPL_DATE_FORMAT);
if (formatted === true) return returnTime?.utc()?.format(PPL_DATE_FORMAT);
if (formatted) return returnTime?.utc()?.format(formatted);

return returnTime;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
dt {
background-color: transparentize(shade($euiColorPrimary, 20%), 0.9);
color: $euiTextColor;
padding: ($euiSizeXS / 2) $euiSizeXS;
padding: calc($euiSizeXS / 2) $euiSizeXS;
margin-right: $euiSizeXS;
word-break: normal;
border-radius: $euiBorderRadius;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ exports[`<ParaOutput /> spec renders visualization outputs 1`] = `
class="euiText euiText--small"
style="margin-left: 9px;"
>
2020-07-21 18:37:44.710000 - 2020-07-21 18:37:44.710000
2020-Jul-21 18:37:44 - 2020-Aug-20 18:37:44
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Adapter from 'enzyme-adapter-react-16';
import React from 'react';
import { sampleParsedParagraghs1 } from '../../helpers/__tests__/sampleDefaultNotebooks';
import { ParaOutput } from '../para_output';
import { uiSettingsService } from '../../../../../../common/utils/core_services';

describe('<ParaOutput /> spec', () => {
configure({ adapter: new Adapter() });
Expand Down Expand Up @@ -48,18 +49,21 @@ describe('<ParaOutput /> spec', () => {
it('renders visualization outputs', () => {
const para = sampleParsedParagraghs1[2];
para.isSelected = true;

uiSettingsService.get = jest.fn().mockReturnValue('YYYY-MMM-DD HH:mm:ss');
const setVisInput = jest.fn();
const utils = render(
<ParaOutput
key={para.uniqueId}
para={para}
visInput={{
timeRange: { from: '2020-07-21T18:37:44.710Z', to: '2020-08-20T18:37:44.710Z' },
timeRange: { from: '2020-JUL-21 18:37:44', to: '2020-AUG-20 18:37:44' },
}}
setVisInput={setVisInput}
DashboardContainerByValueRenderer={() => null}
/>
);
expect(utils.container.textContent).toMatch('2020-Jul-21 18:37:44 - 2020-Aug-20 18:37:44');
expect(utils.container.firstChild).toMatchSnapshot();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ const OutputBody = ({
*/
const dateFormat = uiSettingsService.get('dateFormat');
const from = convertDateTime(visInput?.timeRange?.from, true, false);
const to = convertDateTime(visInput?.timeRange?.from, false, false);
const displayFrom = convertDateTime(visInput?.timeRange?.from, true) || 'Invalid date';
const displayTo = convertDateTime(visInput?.timeRange?.from, false) || 'Invalid date';
const to = convertDateTime(visInput?.timeRange?.to, false, false);
const displayFrom =
convertDateTime(visInput?.timeRange?.from, true, dateFormat) || 'Invalid date';
const displayTo = convertDateTime(visInput?.timeRange?.to, false, dateFormat) || 'Invalid date';
if (typeOut !== undefined) {
switch (typeOut) {
case 'QUERY':
Expand Down

0 comments on commit 780722a

Please sign in to comment.