Skip to content

Commit

Permalink
chore(react-components): support multiple queries in useTimeSeriesDat…
Browse files Browse the repository at this point in the history
…a hook (#632)
  • Loading branch information
diehbria authored Mar 2, 2023
1 parent 794b4a4 commit e346eff
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/react-components/src/components/dial/dial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Dial = ({
}) => {
const { dataStreams } = useTimeSeriesData({
viewport: passedInViewport,
query,
queries: [query],
settings: { fetchMostRecentBeforeEnd: true },
styles,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/src/components/kpi/kpi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Kpi = ({
}) => {
const { dataStreams } = useTimeSeriesData({
viewport: passedInViewport,
query,
queries: [query],
settings: { fetchMostRecentBeforeEnd: true },
styles,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react-components/src/components/status/status.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Status = ({
}) => {
const { dataStreams } = useTimeSeriesData({
viewport: passedInViewport,
query,
queries: [query],
settings: { fetchMostRecentBeforeEnd: true },
styles,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { renderHook } from '@testing-library/react-hooks';
import { useTimeSeriesData } from './useTimeSeriesData';

import { DataStream, TimeQuery, TimeSeriesData, TimeSeriesDataRequest, Viewport } from '@iot-app-kit/core';
import { useTimeSeriesData } from './useTimeSeriesData';

const noop = () => {};

const queryCreator = (
timeSeriesData: TimeSeriesData[],
overrides?: { updateViewport?: (viewport: Viewport) => void; unsubscribe?: () => void }
): TimeQuery<TimeSeriesData[], TimeSeriesDataRequest> => {
const { updateViewport = noop, unsubscribe = noop } = overrides || {};

return {
build: () => ({
subscribe: ({ next }) => {
Expand All @@ -21,12 +22,12 @@ const queryCreator = (
};

it('returns no time series data when query returns no time series data', () => {
const query = queryCreator([]);
const queries = [queryCreator([])];
const {
result: { current: timeSeriesData },
} = renderHook(() =>
useTimeSeriesData({
query,
queries,
viewport: { duration: '5m' },
})
);
Expand All @@ -36,14 +37,14 @@ it('returns no time series data when query returns no time series data', () => {

it('provides time series data returned from query', () => {
const QUERY_RESPONSE: TimeSeriesData[] = [{ dataStreams: [], viewport: { duration: '5m' }, annotations: { y: [] } }];
const query = queryCreator(QUERY_RESPONSE);
const queries = [queryCreator(QUERY_RESPONSE)];
const viewport = { duration: '5m' };

const {
result: { current },
} = renderHook(() =>
useTimeSeriesData({
query,
queries,
viewport,
})
);
Expand All @@ -58,14 +59,14 @@ it('binds style settings color to the data stream color', () => {
viewport: { duration: '5m' },
annotations: {},
};
const query = queryCreator([TIME_SERIES_DATA]);
const queries = [queryCreator([TIME_SERIES_DATA])];
const color = 'red';

const {
result: { current: timeSeriesData },
} = renderHook(() =>
useTimeSeriesData({
query,
queries,
viewport: { duration: '5m' },
styles: { red: { color } },
})
Expand All @@ -82,14 +83,14 @@ it('combines multiple time series data results into a single time series data',
{ dataStreams: [DATA_STREAM_1], viewport: { duration: '5m' }, annotations: { y: [] } },
{ dataStreams: [DATA_STREAM_2], viewport: { duration: '5m' }, annotations: { y: [] } },
];
const query = queryCreator(QUERY_RESPONSE);
const queries = [queryCreator(QUERY_RESPONSE)];
const viewport = { duration: '5m' };

const {
result: { current: timeSeriesData },
} = renderHook(() =>
useTimeSeriesData({
query,
queries,
viewport,
})
);
Expand All @@ -99,6 +100,33 @@ it('combines multiple time series data results into a single time series data',
});
});

it('returns data streams from multiple queries', () => {
const DATA_STREAM_1: DataStream = { id: 'abc-1', data: [], resolution: 0, name: 'my-name' };
const DATA_STREAM_2: DataStream = { id: 'abc-2', data: [], resolution: 0, name: 'my-name-2' };

const QUERY_RESPONSE_1: TimeSeriesData[] = [
{ dataStreams: [DATA_STREAM_1], viewport: { duration: '5m' }, annotations: { y: [] } },
];
const QUERY_RESPONSE_2: TimeSeriesData[] = [
{ dataStreams: [DATA_STREAM_2], viewport: { duration: '5m' }, annotations: { y: [] } },
];

const queries = [queryCreator(QUERY_RESPONSE_1), queryCreator(QUERY_RESPONSE_2)];

const {
result: {
current: { dataStreams },
},
} = renderHook(() =>
useTimeSeriesData({
queries,
viewport: { duration: '5m' },
})
);

expect(dataStreams).toEqual([DATA_STREAM_1, DATA_STREAM_2]);
});

it('providers updated viewport to query', () => {
let viewport = { duration: '5m' };
const updateViewport = jest.fn();
Expand All @@ -109,12 +137,12 @@ it('providers updated viewport to query', () => {
annotations: {},
};

const query = queryCreator([TIME_SERIES_DATA], { updateViewport });
const queries = [queryCreator([TIME_SERIES_DATA], { updateViewport })];
const color = 'red';

const { rerender } = renderHook(() =>
useTimeSeriesData({
query,
queries,
viewport,
styles: { red: { color } },
})
Expand All @@ -135,7 +163,7 @@ it.skip('does not attempt to re-create the subscription when provided a new refe
result: { current: timeSeriesData },
} = renderHook(() =>
useTimeSeriesData({
query: queryCreator([]),
queries: [queryCreator([])],
viewport: { duration: '5m' },
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
TimeSeriesDataRequestSettings,
ProviderWithViewport,
StyleSettingsMap,
combineProviders,
} from '@iot-app-kit/core';
import { v4 as uuid } from 'uuid';
import { bindStylesToDataStreams } from '../utils/bindStylesToDataStreams';
import { combineTimeSeriesData } from '../utils/combineTimeSeriesData';

import { useViewport } from '../useViewport/useViewport';
import { useViewport } from '../useViewport';

const DEFAULT_SETTINGS: TimeSeriesDataRequestSettings = {
resolution: '0',
Expand All @@ -22,12 +23,12 @@ const DEFAULT_SETTINGS: TimeSeriesDataRequestSettings = {
const DEFAULT_VIEWPORT = { duration: '10m' };

export const useTimeSeriesData = ({
query,
queries,
viewport: passedInViewport,
settings = DEFAULT_SETTINGS,
styles,
}: {
query: TimeQuery<TimeSeriesData[], TimeSeriesDataRequest>;
queries: TimeQuery<TimeSeriesData[], TimeSeriesDataRequest>[];
viewport?: Viewport;
settings?: TimeSeriesDataRequestSettings;
styles?: StyleSettingsMap;
Expand All @@ -42,10 +43,14 @@ export const useTimeSeriesData = ({

useEffect(() => {
const id = uuid();
provider.current = query.build(id, {
viewport,
settings,
});
provider.current = combineProviders(
queries.map((query) =>
query.build(id, {
viewport,
settings,
})
)
);

provider.current.subscribe({
next: (timeSeriesDataCollection: TimeSeriesData[]) => {
Expand All @@ -72,7 +77,7 @@ export const useTimeSeriesData = ({
prevViewport.current = undefined;
});
};
}, [query]);
}, [queries]);

useEffect(() => {
if (prevViewport.current != null) {
Expand Down

0 comments on commit e346eff

Please sign in to comment.