Skip to content

Reduce contracts and deployments loading time #4233

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

Draft
wants to merge 18 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 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
128 changes: 100 additions & 28 deletions packages/playground/src/components/k8s_deployment_table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
<span>
This might happen because the node is down or it's not reachable
<span v-if="showEncryption">
or the deployment{{ count - items.length > 1 ? "s are" : " is" }} encrypted by another key
</span>.
or the deployment{{ count - items.length > 1 ? "s are" : " is" }} encrypted by another key </span>.
</span>
<v-tooltip location="top" text="Show failed deployments">
<template #activator="{ props: tooltipProps }">
Expand Down Expand Up @@ -166,9 +165,9 @@ import { onMounted, ref } from "vue";
import { getNodeHealthColor, NodeHealth } from "@/utils/get_nodes";

import { useProfileManager } from "../stores";
import { getGrid, updateGrid } from "../utils/grid";
import { getGrid } from "../utils/grid";
import { markAsFromAnotherClient } from "../utils/helpers";
import { type K8S, type LoadedDeployments, loadK8s, mergeLoadedDeployments } from "../utils/load_deployment";
import { loadK8s, mergeLoadedDeployments, getGridClient } from "../utils/load_deployment";
const profileManager = useProfileManager();
const showDialog = ref(false);
const showEncryption = ref(false);
Expand All @@ -188,35 +187,108 @@ const loading = ref(false);

onMounted(loadDeployments);
async function loadDeployments() {
const start = performance.now();
items.value = [];
loading.value = true;
const grid = await getGrid(profileManager.profile!, props.projectName);
const chunk1 = await loadK8s(grid!);
const chunk2 = await loadK8s(updateGrid(grid!, { projectName: props.projectName.toLowerCase() }));
let chunk3: LoadedDeployments<K8S> = { count: 0, items: [], failedDeployments: [] };

if (showAllDeployments.value) {
chunk3 = await loadK8s(updateGrid(grid!, { projectName: "" }));
chunk3.items = chunk3.items.map(i => {
return !i.projectName || i.projectName === "Kubernetes" ? markAsFromAnotherClient(i) : i;
try {
const grid = await getGrid(profileManager.profile!, props.projectName);
if (!grid) {
loading.value = false;
console.error("Failed to initialize grid connection");
return;
}
const shouldLoadAllDeployments = showAllDeployments.value;
const results = await Promise.allSettled([
loadK8s(grid),
loadK8s(await getGridClient(grid.clientOptions, props.projectName.toLowerCase())),
shouldLoadAllDeployments
? loadK8s(await getGridClient(grid.clientOptions, ""))
: Promise.resolve({ count: 0, items: [], failedDeployments: [] }),
]);
const chunk1 =
results[0].status === "fulfilled"
? results[0].value
: (() => {
console.error("Failed to load K8s deployments from default project:", results[0].reason);
return { count: 0, items: [], failedDeployments: [] };
})();
const chunk2 =
results[1].status === "fulfilled"
? results[1].value
: (() => {
console.error(`Failed to load K8s deployments from project "${props.projectName}":`, results[1].reason);
return { count: 0, items: [], failedDeployments: [] };
})();
const chunk3 =
results[2].status === "fulfilled"
? results[2].value
: (() => {
console.error("Failed to load K8s deployments from all projects:", results[2].reason);
return { count: 0, items: [], failedDeployments: [] };
})();
if (chunk3.items) {
chunk3.items = chunk3.items.map(i => {
return !i.projectName || i.projectName === "Kubernetes" ? markAsFromAnotherClient(i) : i;
});
}
const clusters = mergeLoadedDeployments(chunk1, chunk2, chunk3);
failedDeployments.value = clusters.failedDeployments;
count.value = clusters.count;
items.value = clusters.items.map(item => {
const master = item.masters[0];
const publicIP = master.publicIP?.ip;
return {
...item,
name: item.deploymentName,
ipv4: publicIP ? publicIP.split("/")?.[0] || publicIP : "-",
ipv6: master.publicIP?.ip6?.replace(/\/64$/, "") || "-",
planetary: master.planetary || "-",
workersLength: item.workers.length,
billing: undefined,
wireguard: undefined,
detailsLoading: false,
};
});

await Promise.allSettled(items.value.map(item => fetchClusterDetails(item)));
} catch (error) {
console.error("Error loading deployments:", error);
items.value = [];
count.value = 0;
failedDeployments.value = [];
} finally {
loading.value = false;
const end = performance.now();
console.log(`Time taken: ${(end - start) / 1000} seconds`);
}
}

async function fetchClusterDetails(item: any) {
if (item.detailsLoading || (item.billing !== undefined && item.wireguard !== undefined)) return;
item.detailsLoading = true;
try {
const grid = await getGrid(profileManager.profile!, item.projectName || props.projectName);
if (!grid) {
item.detailsLoading = false;
return;
}

const clusters = mergeLoadedDeployments(chunk1, chunk2, chunk3);
failedDeployments.value = clusters.failedDeployments;

count.value = clusters.count;
items.value = clusters.items.map((item: any) => {
item.name = item.deploymentName;
item.ipv4 = item.masters[0].publicIP?.ip?.split("/")?.[0] || item.masters[0].publicIP?.ip || "-";
item.ipv6 = item.masters[0].publicIP?.ip6.replace(/\/64$/, "") || "-";
item.planetary = item.masters[0].planetary || "-";
item.workersLength = item.workers.length;
item.billing = item.masters[0].billing;
item.created = item.masters[0].created;
return item;
});
loading.value = false;
const [consumption, wireguardConfig] = await Promise.allSettled([
grid.contracts.getConsumption({ id: item.masters[0].contractId }),
grid.networks.getWireGuardConfigs({
name: item.masters[0].interfaces[0].network,
ipRange: item.masters[0].interfaces[0].ip,
}),
]);

item.billing =
consumption.status === "fulfilled" && consumption.value ? consumption.value.amountBilled : "No Data Available";

item.wireguard =
wireguardConfig.status === "fulfilled" && wireguardConfig.value?.[0] ? wireguardConfig.value[0] : undefined;
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a catch block; to at least log the errors

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

item.detailsLoading = false;
}
}

defineExpose({ loadDeployments });
Expand Down
Loading
Loading