Skip to content

Commit bc6c3a6

Browse files
shahrokniabelyakin
authored andcommitted
[FEATURE] Table: embed variables into datalink (#521)
Signed-off-by: Mahmoud Shahrokni <seyedmahmoud.shahrokni@amadeus.com>
1 parent 9bafbc6 commit bc6c3a6

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

table/src/components/TablePanel.test.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
PluginLoader,
2121
PluginModuleResource,
2222
PluginRegistry,
23+
VariableStateMap,
2324
} from '@perses-dev/plugin-system';
2425
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
2526
import * as packagejson from '../../package.json';
@@ -31,6 +32,15 @@ import {
3132
} from '../test/mock-query-results';
3233
import { TablePanel } from './TablePanel';
3334

35+
/* mock all variables */
36+
jest.mock('@perses-dev/plugin-system', () => ({
37+
...jest.requireActual('@perses-dev/plugin-system'),
38+
useAllVariableValues: (): VariableStateMap => ({
39+
myproject: { loading: false, value: 'my_project' },
40+
__range: { loading: false, value: '1h' },
41+
}),
42+
}));
43+
3444
const TEST_TIMEOUT = 15000; // Github Actions is slow
3545
const TEST_TIME_SERIES_TABLE_PROPS: Omit<TimeSeriesTableProps, 'queryResults'> = {
3646
contentDimensions: {
@@ -81,7 +91,16 @@ describe('TablePanel', () => {
8191
async () => {
8292
renderPanel(MOCK_TIME_SERIES_DATA_SINGLEVALUE, {
8393
columnSettings: [
84-
{ name: 'value', header: 'Value', headerDescription: 'Timeseries Value' },
94+
{
95+
name: 'value',
96+
header: 'Value',
97+
headerDescription: 'Timeseries Value',
98+
dataLink: {
99+
url: 'https://fakeurl.com/$myproject/$__range',
100+
title: 'data link',
101+
openNewTab: true,
102+
},
103+
},
85104
{ name: 'device', width: 200 },
86105
{ name: 'env', hide: true },
87106
{ name: 'fstype', enableSorting: true },
@@ -95,6 +114,8 @@ describe('TablePanel', () => {
95114
expect(valueHeaderCell).toBeInTheDocument();
96115
expect(await within(valueHeaderCell).findByLabelText('Timeseries Value')).toBeInTheDocument();
97116
expect(await screen.findByRole('columnheader', { name: /Value/i })).toBeInTheDocument();
117+
// TODO: Add this line of test after the @perses-dev\components is updated
118+
// expect(await screen.getByRole('link', { name: /https:\/\/fakeurl\.com\/\$myproject\/\$__range/ }));
98119

99120
const fstypeHeaderCell = await screen.findByRole('columnheader', { name: 'fstype' });
100121
expect(fstypeHeaderCell).toBeInTheDocument();

table/src/components/TablePanel.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
import { PanelData, PanelProps } from '@perses-dev/plugin-system';
14+
import {
15+
PanelData,
16+
PanelProps,
17+
replaceVariablesInString,
18+
useAllVariableValues,
19+
VariableStateMap,
20+
} from '@perses-dev/plugin-system';
1521
import { Table, TableCellConfigs, TableColumnConfig } from '@perses-dev/components';
1622
import { ReactElement, useEffect, useMemo, useState } from 'react';
1723
import { formatValue, Labels, QueryDataType, TimeSeries, TimeSeriesData, transformData } from '@perses-dev/core';
@@ -157,22 +163,30 @@ function ColumnFilterDropdown({
157163
* If column is hidden, return undefined.
158164
* If column do not have a definition, return a default column config.
159165
*/
160-
function generateColumnConfig(name: string, columnSettings: ColumnSettings[]): TableColumnConfig<unknown> | undefined {
166+
function generateColumnConfig(
167+
name: string,
168+
columnSettings: ColumnSettings[],
169+
allVariables: VariableStateMap
170+
): TableColumnConfig<unknown> | undefined {
161171
for (const column of columnSettings) {
162172
if (column.name === name) {
163173
if (column.hide) {
164174
return undefined;
165175
}
166176

167177
const { name, header, headerDescription, enableSorting, width, align, dataLink } = column;
178+
const modifiedDataLink = dataLink
179+
? { ...dataLink, url: replaceVariablesInString(dataLink.url, allVariables) }
180+
: undefined;
181+
168182
return {
169183
accessorKey: name,
170184
header: header ?? name,
171185
headerDescription,
172186
enableSorting,
173187
width,
174188
align,
175-
dataLink,
189+
dataLink: modifiedDataLink,
176190
...generateCellContentConfig(column),
177191
};
178192
}
@@ -195,6 +209,7 @@ export type TableProps = PanelProps<TableOptions, TimeSeriesData>;
195209

196210
export function TablePanel({ contentDimensions, spec, queryResults }: TableProps): ReactElement | null {
197211
const theme = useTheme();
212+
const allVariables = useAllVariableValues();
198213

199214
// TODO: handle other query types
200215
const queryMode = getTablePanelQueryOptions(spec).mode;
@@ -273,7 +288,7 @@ export function TablePanel({ contentDimensions, spec, queryResults }: TableProps
273288
for (const columnSetting of spec.columnSettings ?? []) {
274289
if (customizedColumns.has(columnSetting.name)) continue; // Skip duplicates
275290

276-
const columnConfig = generateColumnConfig(columnSetting.name, spec.columnSettings ?? []);
291+
const columnConfig = generateColumnConfig(columnSetting.name, spec.columnSettings ?? [], allVariables);
277292
if (columnConfig !== undefined) {
278293
columns.push(columnConfig);
279294
customizedColumns.add(columnSetting.name);
@@ -284,7 +299,7 @@ export function TablePanel({ contentDimensions, spec, queryResults }: TableProps
284299
if (!spec.defaultColumnHidden) {
285300
for (const key of keys) {
286301
if (!customizedColumns.has(key)) {
287-
const columnConfig = generateColumnConfig(key, spec.columnSettings ?? []);
302+
const columnConfig = generateColumnConfig(key, spec.columnSettings ?? [], allVariables);
288303
if (columnConfig !== undefined) {
289304
columns.push(columnConfig);
290305
}
@@ -293,7 +308,7 @@ export function TablePanel({ contentDimensions, spec, queryResults }: TableProps
293308
}
294309

295310
return columns;
296-
}, [keys, spec.columnSettings, spec.defaultColumnHidden]);
311+
}, [keys, spec.columnSettings, spec.defaultColumnHidden, allVariables]);
297312

298313
// Generate cell settings that will be used by the table to render cells (text color, background color, ...)
299314
const cellConfigs: TableCellConfigs = useMemo(() => {

0 commit comments

Comments
 (0)