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

cluster status issue fix #291

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions gql-queries-generator/doc/queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,19 @@ query consoleGetCluster($name: String!) {
}
}

query consoleListClusterStatus($pagination: CursorPaginationIn) {
infra_listBYOKClusters(pagination: $pagination) {
edges {
node {
lastOnlineAt
metadata {
name
}
}
}
}
}

query consoleGetKubeConfig($name: String!) {
infra_getCluster(name: $name) {
adminKubeconfig {
Expand Down
33 changes: 20 additions & 13 deletions src/apps/console/hooks/use-cluster-status-v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
import { useSocketWatch } from '~/root/lib/client/helpers/socket/useWatch';
import useDebounce from '~/root/lib/client/hooks/use-debounce';
import { useConsoleApi } from '../server/gql/api-provider';
import { IByocClusters } from '../server/gql/queries/byok-cluster-queries';
import { ExtractNodeType, parseNodes } from '../server/r-utils/common';
import { IClustersStatus } from '../server/gql/queries/cluster-queries';

type IClusterMap = { [key: string]: ExtractNodeType<IByocClusters> };
type IClusterMap = { [key: string]: ExtractNodeType<IClustersStatus> };

const ClusterStatusContext = createContext<{
clusters: IClusterMap;
Expand All @@ -36,14 +36,15 @@ const ClusterStatusProvider = ({ children }: { children: ReactNode }) => {

const listCluster = useCallback(async () => {
try {
const cl = await api.listAllClusters();
const parsed = parseNodes(cl.data).reduce(
(acc, c) => {
acc[c.metadata.name] = c;
return acc;
const cl = await api.listClusterStatus({
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the code to improve readability and reduce complexity.

While the changes introduce necessary functionality, we can improve readability and reduce complexity:

  1. Refactor listCluster using async/await and reduce nesting:
const listCluster = useCallback(async () => {
  try {
    const { data } = await api.listClusterStatus({
      pagination: { first: 5 },
    });
    const parsed = parseNodes(data).reduce((acc, c) => {
      acc[c.metadata.name] = c;
      return acc;
    }, {} as IClusterMap);
    setClusters(parsed);
    return parsed;
  } catch (err) {
    console.error(err);
    return false;
  }
}, [api]);
  1. Split the useEffect hook for better separation of concerns:
useEffect(() => {
  const intervalId = setInterval(listCluster, 30 * 1000);
  return () => clearInterval(intervalId);
}, [listCluster]);

useEffect(() => {
  const onlineEvent = () => {
    setTimeout(listCluster, 3000);
  };
  window.addEventListener('online', onlineEvent);
  return () => window.removeEventListener('online', onlineEvent);
}, [listCluster]);
  1. Consider removing the pagination limit of 5 in the API call, or add a comment explaining why it's necessary:
const { data } = await api.listClusterStatus({
  // pagination: { first: 5 }, // Removed unless there's a specific reason for this limit
});

These changes maintain the added functionality while improving code readability and reducing complexity.

pagination: {
first: 5,
},
{} as { [key: string]: ExtractNodeType<IByocClusters> },
);
});
const parsed = parseNodes(cl.data).reduce((acc, c) => {
acc[c.metadata.name] = c;
return acc;
}, {} as IClusterMap);
setClusters(parsed);
return clusters;
} catch (err) {
Expand All @@ -56,8 +57,18 @@ const ClusterStatusProvider = ({ children }: { children: ReactNode }) => {
const interval = setInterval(() => {
listCluster();
}, 30 * 1000);

const onlineEvent = () => {
setTimeout(() => {
listCluster();
}, 3000);
};

window.addEventListener('online', onlineEvent);

return () => {
clearInterval(interval);
window.removeEventListener('online', onlineEvent);
};
}, []);

Expand All @@ -73,10 +84,6 @@ const ClusterStatusProvider = ({ children }: { children: ReactNode }) => {
setUpdate((p) => !p);
}, topic);

useEffect(() => {
console.log('helre', topic);
}, [topic]);

return (
<ClusterStatusContext.Provider
value={useMemo(
Expand Down
45 changes: 35 additions & 10 deletions src/apps/console/server/gql/queries/cluster-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ import {
ConsoleListDnsHostsQueryVariables,
ConsoleListAllClustersQuery,
ConsoleListAllClustersQueryVariables,
ConsoleListClusterStatusQuery,
ConsoleListClusterStatusQueryVariables,
} from '~/root/src/generated/gql/server';

export type ICluster = NN<ConsoleGetClusterQuery['infra_getCluster']>;
export type IClusters = NN<ConsoleListClustersQuery['infra_listClusters']>;
export type IClustersStatus = NN<
ConsoleListClusterStatusQuery['infra_listClusters']
>;

export type IDnsHosts = NN<ConsoleListDnsHostsQuery>['infra_listClusters'];

Expand Down Expand Up @@ -50,7 +55,7 @@ export const clusterQueries = (executor: IExecutor) => ({
return data.infra_listClusters;
},
vars(_: ConsoleListDnsHostsQueryVariables) {},
}
},
),

createCluster: executor(
Expand All @@ -65,7 +70,7 @@ export const clusterQueries = (executor: IExecutor) => ({
transformer: (data: ConsoleCreateClusterMutation) =>
data.infra_createCluster,
vars(_: ConsoleCreateClusterMutationVariables) {},
}
},
),
deleteCluster: executor(
gql`
Expand All @@ -77,7 +82,7 @@ export const clusterQueries = (executor: IExecutor) => ({
transformer: (data: ConsoleDeleteClusterMutation) =>
data.infra_deleteCluster,
vars(_: ConsoleDeleteClusterMutationVariables) {},
}
},
),
clustersCount: executor(
gql`
Expand All @@ -90,7 +95,7 @@ export const clusterQueries = (executor: IExecutor) => ({
{
transformer: (data: ConsoleClustersCountQuery) => data.infra_listClusters,
vars(_: ConsoleClustersCountQueryVariables) {},
}
},
),

listAllClusters: executor(
Expand Down Expand Up @@ -271,9 +276,8 @@ export const clusterQueries = (executor: IExecutor) => ({
{
transformer: (data: ConsoleListAllClustersQuery) => data.byok_clusters,
vars(_: ConsoleListAllClustersQueryVariables) {},
}
},
),

listClusters: executor(
gql`
query Infra_listClusterss(
Expand Down Expand Up @@ -396,7 +400,7 @@ export const clusterQueries = (executor: IExecutor) => ({
{
transformer: (data: ConsoleListClustersQuery) => data.infra_listClusters,
vars(_: ConsoleListClustersQueryVariables) {},
}
},
),
getCluster: executor(
gql`
Expand Down Expand Up @@ -502,7 +506,28 @@ export const clusterQueries = (executor: IExecutor) => ({
{
transformer: (data: ConsoleGetClusterQuery) => data.infra_getCluster,
vars(_: ConsoleGetClusterQueryVariables) {},
}
},
),
listClusterStatus: executor(
gql`
query listCluster($pagination: CursorPaginationIn) {
infra_listBYOKClusters(pagination: $pagination) {
edges {
node {
lastOnlineAt
metadata {
name
}
}
}
}
}
`,
{
transformer: (data: ConsoleListClusterStatusQuery) =>
data.infra_listBYOKClusters,
vars(_: ConsoleListClusterStatusQueryVariables) {},
},
),
getKubeConfig: executor(
gql`
Expand All @@ -518,7 +543,7 @@ export const clusterQueries = (executor: IExecutor) => ({
{
transformer: (data: ConsoleGetKubeConfigQuery) => data.infra_getCluster,
vars(_: ConsoleGetClusterQueryVariables) {},
}
},
),
updateCluster: executor(
gql`
Expand All @@ -532,6 +557,6 @@ export const clusterQueries = (executor: IExecutor) => ({
transformer: (data: ConsoleUpdateClusterMutation) =>
data.infra_updateCluster,
vars(_: ConsoleUpdateClusterMutationVariables) {},
}
},
),
});
10 changes: 10 additions & 0 deletions src/generated/gql/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,16 @@ export type ConsoleGetClusterQuery = {
};
};

export type ConsoleListClusterStatusQueryVariables = Exact<{
pagination?: InputMaybe<CursorPaginationIn>;
}>;

export type ConsoleListClusterStatusQuery = {
infra_listBYOKClusters?: {
edges: Array<{ node: { lastOnlineAt?: any; metadata: { name: string } } }>;
};
};

export type ConsoleGetKubeConfigQueryVariables = Exact<{
name: Scalars['String']['input'];
}>;
Expand Down
Loading