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

feat: implement daemonSets list table for infra monitoring #6631

Draft
wants to merge 4 commits into
base: feat/infra-monitoring-k8s-nodes
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions frontend/src/api/infraMonitoring/getK8sDaemonSetsList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ApiBaseInstance } from 'api';
import { ErrorResponseHandler } from 'api/ErrorResponseHandler';
import { AxiosError } from 'axios';
import { ErrorResponse, SuccessResponse } from 'types/api';
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import { TagFilter } from 'types/api/queryBuilder/queryBuilderData';

export interface K8sDaemonSetsListPayload {
filters: TagFilter;
groupBy?: BaseAutocompleteData[];
offset?: number;
limit?: number;
orderBy?: {
columnName: string;
order: 'asc' | 'desc';
};
}

export interface K8sDaemonSetsData {
daemonSetName: string;
cpuUsage: number;
memoryUsage: number;
desiredPods: number;
availablePods: number;
cpuRequest: number;
memoryRequest: number;
cpuLimit: number;
memoryLimit: number;
restarts: number;
meta: {
k8s_daemonSet_name: string;
k8s_namespace_name: string;
};
}

export interface K8sDaemonSetsListResponse {
status: string;
data: {
type: string;
records: K8sDaemonSetsData[];
groups: null;
total: number;
sentAnyHostMetricsData: boolean;
isSendingK8SAgentMetrics: boolean;
};
}

export const getK8sDaemonSetsList = async (
props: K8sDaemonSetsListPayload,
signal?: AbortSignal,
headers?: Record<string, string>,
): Promise<SuccessResponse<K8sDaemonSetsListResponse> | ErrorResponse> => {
try {
const response = await ApiBaseInstance.post('/daemonsets/list', props, {
signal,
headers,
});

return {
statusCode: 200,
error: null,
message: 'Success',
payload: response.data,
params: props,
};
} catch (error) {
return ErrorResponseHandler(error as AxiosError);
}
};
64 changes: 64 additions & 0 deletions frontend/src/api/infraMonitoring/getK8sNodesList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ApiBaseInstance } from 'api';
import { ErrorResponseHandler } from 'api/ErrorResponseHandler';
import { AxiosError } from 'axios';
import { ErrorResponse, SuccessResponse } from 'types/api';
import { BaseAutocompleteData } from 'types/api/queryBuilder/queryAutocompleteResponse';
import { TagFilter } from 'types/api/queryBuilder/queryBuilderData';

export interface K8sNodesListPayload {
filters: TagFilter;
groupBy?: BaseAutocompleteData[];
offset?: number;
limit?: number;
orderBy?: {
columnName: string;
order: 'asc' | 'desc';
};
}

export interface K8sNodesData {
nodeUID: string;
nodeCPUUsage: number;
nodeCPUAllocatable: number;
nodeMemoryUsage: number;
nodeMemoryAllocatable: number;
meta: {
k8s_node_name: string;
k8s_node_uid: string;
};
}

export interface K8sNodesListResponse {
status: string;
data: {
type: string;
records: K8sNodesData[];
groups: null;
total: number;
sentAnyHostMetricsData: boolean;
isSendingK8SAgentMetrics: boolean;
};
}

export const getK8sNodesList = async (
props: K8sNodesListPayload,
signal?: AbortSignal,
headers?: Record<string, string>,
): Promise<SuccessResponse<K8sNodesListResponse> | ErrorResponse> => {
try {
const response = await ApiBaseInstance.post('/nodes/list', props, {
signal,
headers,
});

return {
statusCode: 200,
error: null,
message: 'Success',
payload: response.data,
params: props,
};
} catch (error) {
return ErrorResponseHandler(error as AxiosError);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import '../InfraMonitoringK8s.styles.scss';

import { LoadingOutlined } from '@ant-design/icons';
import {
Skeleton,
Spin,
Table,
TablePaginationConfig,
TableProps,
Typography,
} from 'antd';
import { SorterResult } from 'antd/es/table/interface';
import logEvent from 'api/common/logEvent';
import { K8sDaemonSetsListPayload } from 'api/infraMonitoring/getK8sDaemonSetsList';
import { useGetK8sDaemonSetsList } from 'hooks/infraMonitoring/useGetK8sDaemonSetsList';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers';
import { IBuilderQuery } from 'types/api/queryBuilder/queryBuilderData';
import { GlobalReducer } from 'types/reducer/globalTime';

import K8sHeader from '../K8sHeader';
import {
defaultAddedColumns,
formatDataForTable,
getK8sDaemonSetsListColumns,
getK8sDaemonSetsListQuery,
K8sDaemonSetsRowData,
} from './utils';

function K8sDaemonSetsList({
isFiltersVisible,
handleFilterVisibilityChange,
}: {
isFiltersVisible: boolean;
handleFilterVisibilityChange: () => void;
}): JSX.Element {
const { maxTime, minTime } = useSelector<AppState, GlobalReducer>(
(state) => state.globalTime,
);

const [currentPage, setCurrentPage] = useState(1);

const [filters, setFilters] = useState<IBuilderQuery['filters']>({
items: [],
op: 'and',
});

const [orderBy, setOrderBy] = useState<{
columnName: string;
order: 'asc' | 'desc';
} | null>(null);

// const [selectedDaemonSetUID, setselectedDaemonSetUID] = useState<string | null>(null);

const pageSize = 10;

const query = useMemo(() => {
const baseQuery = getK8sDaemonSetsListQuery();
return {
...baseQuery,
limit: pageSize,
offset: (currentPage - 1) * pageSize,
filters,
start: Math.floor(minTime / 1000000),
end: Math.floor(maxTime / 1000000),
orderBy,
};
}, [currentPage, filters, minTime, maxTime, orderBy]);

const { data, isFetching, isLoading, isError } = useGetK8sDaemonSetsList(
query as K8sDaemonSetsListPayload,
{
queryKey: ['hostList', query],
enabled: !!query,
},
);

const DaemonSetsData = useMemo(() => data?.payload?.data?.records || [], [
data,
]);
const totalCount = data?.payload?.data?.total || 0;

const formattedDaemonSetsData = useMemo(
() => formatDataForTable(DaemonSetsData),
[DaemonSetsData],
);

const columns = useMemo(() => getK8sDaemonSetsListColumns(), []);

const handleTableChange: TableProps<K8sDaemonSetsRowData>['onChange'] = useCallback(
(
pagination: TablePaginationConfig,
_filters: Record<string, (string | number | boolean)[] | null>,
sorter:
| SorterResult<K8sDaemonSetsRowData>
| SorterResult<K8sDaemonSetsRowData>[],
): void => {
if (pagination.current) {
setCurrentPage(pagination.current);
}

if ('field' in sorter && sorter.order) {
setOrderBy({
columnName: sorter.field as string,
order: sorter.order === 'ascend' ? 'asc' : 'desc',
});
} else {
setOrderBy(null);
}
},
[],
);

const handleFiltersChange = useCallback(
(value: IBuilderQuery['filters']): void => {
const isNewFilterAdded = value.items.length !== filters.items.length;
if (isNewFilterAdded) {
setFilters(value);
setCurrentPage(1);

logEvent('Infra Monitoring: K8s list filters applied', {
filters: value,
});
}
},
[filters],
);

useEffect(() => {
logEvent('Infra Monitoring: K8s list page visited', {});
}, []);

// const selectedDaemonSetData = useMemo(() => {
// if (!selectedDaemonSetUID) return null;
// return DaemonSetsData.find((daemonSet) => daemonSet.DaemonSetUID === selectedDaemonSetUID) || null;
// }, [selectedDaemonSetUID, DaemonSetsData]);

const handleRowClick = (record: K8sDaemonSetsRowData): void => {
// setselectedDaemonSetUID(record.DaemonSetUID);

logEvent('Infra Monitoring: K8s daemonSet list item clicked', {
daemonSetName: record.daemonSetName,
});
};

// const handleCloseDaemonSetDetail = (): void => {
// setselectedDaemonSetUID(null);
// };

const showsDaemonSetsTable =
!isError &&
!isLoading &&
!isFetching &&
!(formattedDaemonSetsData.length === 0 && filters.items.length > 0);

const showNoFilteredDaemonSetsMessage =
!isFetching &&
!isLoading &&
formattedDaemonSetsData.length === 0 &&
filters.items.length > 0;

return (
<div className="k8s-list">
<K8sHeader
isFiltersVisible={isFiltersVisible}
handleFilterVisibilityChange={handleFilterVisibilityChange}
defaultAddedColumns={defaultAddedColumns}
addedColumns={[]}
availableColumns={[]}
handleFiltersChange={handleFiltersChange}
onAddColumn={() => {}}
onRemoveColumn={() => {}}
/>
{isError && <Typography>{data?.error || 'Something went wrong'}</Typography>}

{showNoFilteredDaemonSetsMessage && (
<div className="no-filtered-hosts-message-daemonSet">
<div className="no-filtered-hosts-message-content">
<img
src="/Icons/emptyState.svg"
alt="thinking-emoji"
className="empty-state-svg"
/>

<Typography.Text className="no-filtered-hosts-message">
This query had no results. Edit your query and try again!
</Typography.Text>
</div>
</div>
)}

{(isFetching || isLoading) && (
<div className="k8s-list-loading-state">
<Skeleton.Input
className="k8s-list-loading-state-item"
size="large"
block
active
/>
<Skeleton.Input
className="k8s-list-loading-state-item"
size="large"
block
active
/>
<Skeleton.Input
className="k8s-list-loading-state-item"
size="large"
block
active
/>
</div>
)}

{showsDaemonSetsTable && (
<Table
className="k8s-list-table"
dataSource={isFetching || isLoading ? [] : formattedDaemonSetsData}
columns={columns}
pagination={{
current: currentPage,
pageSize,
total: totalCount,
showSizeChanger: false,
hideOnSinglePage: true,
}}
scroll={{ x: true }}
loading={{
spinning: isFetching || isLoading,
indicator: <Spin indicator={<LoadingOutlined size={14} spin />} />,
}}
tableLayout="fixed"
rowKey={(record): string => record.daemonSetName}
onChange={handleTableChange}
onRow={(record): { onClick: () => void; className: string } => ({
onClick: (): void => handleRowClick(record),
className: 'clickable-row',
})}
/>
)}
{/* TODO - Handle DaemonSet Details flow */}
</div>
);
}

export default K8sDaemonSetsList;
Loading
Loading