mirror of
https://github.com/Unleash/unleash.git
synced 2024-10-28 19:06:12 +01:00
feat: add connected instances table (#6263)
This PR adds a first iteration of the connected instances table on a new connected instances tab of the application page (hidden behind a flag). As a first version, it only uses data that we currently return for each application (so no "connected through" or "last synchronized"). It also uses the existing version of the application data and just filters it for the "production" environment right now. Adding query parameters (and potentially a new URL?) to the applications page (to save state and fetch data) will be done in a follow-up. This should lay the groundwork for adding a new API too. <img width="1485" alt="image" src="https://github.com/Unleash/unleash/assets/17786332/4fb68456-d0f5-4f82-9246-5333a273df9c">
This commit is contained in:
parent
5f781b4c8f
commit
0e9102fb22
@ -16,6 +16,7 @@ import { ConditionallyRender } from 'component/common/ConditionallyRender/Condit
|
||||
import { UPDATE_APPLICATION } from 'component/providers/AccessProvider/permissions';
|
||||
import { ApplicationView } from '../ApplicationView/ApplicationView';
|
||||
import { ApplicationUpdate } from '../ApplicationUpdate/ApplicationUpdate';
|
||||
import { ConnectedInstances } from '../ConnectedInstances/ConnectedInstances';
|
||||
import { Dialogue } from 'component/common/Dialogue/Dialogue';
|
||||
import { PageContent } from 'component/common/PageContent/PageContent';
|
||||
import { PageHeader } from 'component/common/PageHeader/PageHeader';
|
||||
@ -30,8 +31,10 @@ import { formatDateYMD } from 'utils/formatDate';
|
||||
import { formatUnknownError } from 'utils/formatUnknownError';
|
||||
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
|
||||
import { TabPanel } from 'component/common/TabNav/TabPanel/TabPanel';
|
||||
import { useUiFlag } from 'hooks/useUiFlag';
|
||||
|
||||
export const ApplicationEdit = () => {
|
||||
const showAdvancedApplicationMetrics = useUiFlag('sdkReporting');
|
||||
const navigate = useNavigate();
|
||||
const name = useRequiredPathParam('name');
|
||||
const { application, loading } = useApplication(name);
|
||||
@ -84,6 +87,13 @@ export const ApplicationEdit = () => {
|
||||
},
|
||||
];
|
||||
|
||||
if (showAdvancedApplicationMetrics) {
|
||||
tabData.push({
|
||||
label: 'Connected instances',
|
||||
component: <ConnectedInstances />,
|
||||
});
|
||||
}
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div>
|
||||
|
@ -0,0 +1,53 @@
|
||||
import { useMemo } from 'react';
|
||||
import useApplication from 'hooks/api/getters/useApplication/useApplication';
|
||||
import { formatDateYMDHMS } from 'utils/formatDate';
|
||||
import { useRequiredPathParam } from 'hooks/useRequiredPathParam';
|
||||
import { useConnectedInstancesTable } from './useConnectedInstancesTable';
|
||||
import { ConnectedInstancesTable } from './ConnectedInstancesTable';
|
||||
|
||||
export const ConnectedInstances = () => {
|
||||
const name = useRequiredPathParam('name');
|
||||
const { application } = useApplication(name);
|
||||
|
||||
const tableData = useMemo(() => {
|
||||
return (
|
||||
application.instances
|
||||
// @ts-expect-error: the type definition here is incomplete. It
|
||||
// should be updated as part of this project.
|
||||
.filter((instance) => instance.environment === 'production')
|
||||
.map(({ instanceId, sdkVersion, clientIp, lastSeen }) => {
|
||||
return {
|
||||
instanceId,
|
||||
ip: clientIp,
|
||||
sdkVersion,
|
||||
lastSeen: formatDateYMDHMS(lastSeen),
|
||||
};
|
||||
})
|
||||
);
|
||||
}, [application]);
|
||||
|
||||
const {
|
||||
getTableProps,
|
||||
getTableBodyProps,
|
||||
headerGroups,
|
||||
rows,
|
||||
prepareRow,
|
||||
state: { globalFilter },
|
||||
setHiddenColumns,
|
||||
columns,
|
||||
} = useConnectedInstancesTable(tableData);
|
||||
|
||||
return (
|
||||
<ConnectedInstancesTable
|
||||
loading={false}
|
||||
headerGroups={headerGroups}
|
||||
setHiddenColumns={setHiddenColumns}
|
||||
prepareRow={prepareRow}
|
||||
getTableBodyProps={getTableBodyProps}
|
||||
getTableProps={getTableProps}
|
||||
rows={rows}
|
||||
columns={columns}
|
||||
globalFilter={globalFilter}
|
||||
/>
|
||||
);
|
||||
};
|
@ -0,0 +1,109 @@
|
||||
import {
|
||||
Row,
|
||||
TablePropGetter,
|
||||
TableProps,
|
||||
TableBodyPropGetter,
|
||||
TableBodyProps,
|
||||
HeaderGroup,
|
||||
} from 'react-table';
|
||||
import {
|
||||
SortableTableHeader,
|
||||
TableCell,
|
||||
TablePlaceholder,
|
||||
} from 'component/common/Table';
|
||||
import {
|
||||
Box,
|
||||
styled,
|
||||
Table,
|
||||
TableBody,
|
||||
TableRow,
|
||||
useMediaQuery,
|
||||
} from '@mui/material';
|
||||
import theme from 'themes/theme';
|
||||
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
||||
|
||||
import { useConditionallyHiddenColumns } from 'hooks/useConditionallyHiddenColumns';
|
||||
|
||||
const hiddenColumnsSmall = ['ip', 'sdkVersion'];
|
||||
const hiddenColumnsCompact = ['ip', 'sdkVersion', 'lastSeen'];
|
||||
|
||||
type ConnectedInstancesTableProps = {
|
||||
compact?: boolean;
|
||||
loading: boolean;
|
||||
setHiddenColumns: (param: any) => void;
|
||||
columns: any[];
|
||||
rows: Row<object>[];
|
||||
prepareRow: (row: Row<object>) => void;
|
||||
getTableProps: (
|
||||
propGetter?: TablePropGetter<object> | undefined,
|
||||
) => TableProps;
|
||||
getTableBodyProps: (
|
||||
propGetter?: TableBodyPropGetter<object> | undefined,
|
||||
) => TableBodyProps;
|
||||
headerGroups: HeaderGroup<object>[];
|
||||
globalFilter: any;
|
||||
};
|
||||
export const ConnectedInstancesTable = ({
|
||||
compact = false,
|
||||
setHiddenColumns,
|
||||
columns,
|
||||
loading,
|
||||
rows,
|
||||
getTableProps,
|
||||
getTableBodyProps,
|
||||
headerGroups,
|
||||
globalFilter,
|
||||
prepareRow,
|
||||
}: ConnectedInstancesTableProps) => {
|
||||
const isSmallScreen = useMediaQuery(theme.breakpoints.down('md'));
|
||||
|
||||
useConditionallyHiddenColumns(
|
||||
[
|
||||
{
|
||||
condition: isSmallScreen,
|
||||
columns: hiddenColumnsSmall,
|
||||
},
|
||||
{
|
||||
condition: compact,
|
||||
columns: hiddenColumnsCompact,
|
||||
},
|
||||
],
|
||||
setHiddenColumns,
|
||||
columns,
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Box sx={{ overflowX: 'auto' }}>
|
||||
<Table {...getTableProps()}>
|
||||
<SortableTableHeader headerGroups={headerGroups as any} />
|
||||
<TableBody {...getTableBodyProps()}>
|
||||
{rows.map((row) => {
|
||||
prepareRow(row);
|
||||
return (
|
||||
<TableRow hover {...row.getRowProps()}>
|
||||
{row.cells.map((cell) => (
|
||||
<TableCell {...cell.getCellProps()}>
|
||||
{cell.render('Cell')}
|
||||
</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Box>
|
||||
<ConditionallyRender
|
||||
condition={rows.length === 0 && !loading}
|
||||
show={
|
||||
<TablePlaceholder>
|
||||
<p>
|
||||
There's no data for any connected instances to
|
||||
display. Have you configured your clients correctly?
|
||||
</p>
|
||||
</TablePlaceholder>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
};
|
@ -0,0 +1,97 @@
|
||||
import { useMemo } from 'react';
|
||||
import { IApiToken } from 'hooks/api/getters/useApiTokens/useApiTokens';
|
||||
import { DateCell } from 'component/common/Table/cells/DateCell/DateCell';
|
||||
import { HighlightCell } from 'component/common/Table/cells/HighlightCell/HighlightCell';
|
||||
import { IconCell } from 'component/common/Table/cells/IconCell/IconCell';
|
||||
import { TimeAgoCell } from 'component/common/Table/cells/TimeAgoCell/TimeAgoCell';
|
||||
import { useTable, useGlobalFilter, useSortBy } from 'react-table';
|
||||
import { sortTypes } from 'utils/sortTypes';
|
||||
import { ProjectsList } from 'component/admin/apiToken/ProjectsList/ProjectsList';
|
||||
import { Key } from '@mui/icons-material';
|
||||
|
||||
type ConnectedInstancesTableData = {
|
||||
instanceId: string;
|
||||
ip: string;
|
||||
sdkVersion: string;
|
||||
lastSeen: string;
|
||||
};
|
||||
|
||||
export const useConnectedInstancesTable = (
|
||||
instanceData: ConnectedInstancesTableData[],
|
||||
) => {
|
||||
const initialState = useMemo(
|
||||
() => ({ sortBy: [{ id: 'instanceId' }] }),
|
||||
[],
|
||||
);
|
||||
|
||||
const COLUMNS = useMemo(() => {
|
||||
return [
|
||||
{
|
||||
Header: 'Instances',
|
||||
accessor: 'instanceId',
|
||||
Cell: HighlightCell,
|
||||
styles: {
|
||||
width: '45%',
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: 'SDK',
|
||||
accessor: 'sdkVersion',
|
||||
Cell: HighlightCell,
|
||||
styles: {
|
||||
width: '20%',
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: 'Last seen',
|
||||
accessor: 'lastSeen',
|
||||
Cell: HighlightCell,
|
||||
styles: {
|
||||
width: '20%',
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: 'IP',
|
||||
accessor: 'ip',
|
||||
Cell: HighlightCell,
|
||||
styles: {
|
||||
width: '15%',
|
||||
},
|
||||
},
|
||||
];
|
||||
}, []);
|
||||
|
||||
const {
|
||||
getTableProps,
|
||||
getTableBodyProps,
|
||||
headerGroups,
|
||||
rows,
|
||||
prepareRow,
|
||||
state,
|
||||
setGlobalFilter,
|
||||
setHiddenColumns,
|
||||
} = useTable(
|
||||
{
|
||||
columns: COLUMNS as any,
|
||||
data: instanceData as any,
|
||||
initialState,
|
||||
sortTypes,
|
||||
autoResetHiddenColumns: false,
|
||||
disableSortRemove: true,
|
||||
},
|
||||
useGlobalFilter,
|
||||
useSortBy,
|
||||
);
|
||||
|
||||
return {
|
||||
getTableProps,
|
||||
getTableBodyProps,
|
||||
headerGroups,
|
||||
rows,
|
||||
prepareRow,
|
||||
state,
|
||||
setGlobalFilter,
|
||||
setHiddenColumns,
|
||||
columns: COLUMNS,
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user