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

Use grid store instead of getGrid #2563

Merged
merged 9 commits into from
May 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const props = defineProps({
required: true,
},
grid: {
type: Object as PropType<Ref<GridClient | undefined>>,
type: Object as PropType<GridClient>,
required: true,
},
loading: {
Expand Down Expand Up @@ -232,7 +232,7 @@ async function showDetails(value: any) {
loadingContractId.value = contractId;

try {
const deployment = await props.grid.value?.zos.getDeployment({ contractId });
const deployment = await props.grid?.zos.getDeployment({ contractId });
return layout.value.openDialog(deployment, false, true);
} catch (e) {
failedContractId.value = contractId;
Expand All @@ -248,12 +248,12 @@ async function contractLockDetails(item: any) {
contractStateDialog.value = true;
loadingShowDetails.value = true;
selectedItem.value = item;
await props.grid.value?.contracts
await props.grid?.contracts
.contractLock({ id: item.contractId })
.then((data: ContractLock) => {
contractLocked.value = data;
})
.catch(err => {
.catch((err: any) => {
layout.value.setStatus(
"failed",
normalizeError(err, `Failed to fetch the contract ${item.contractId} lock details.`),
Expand All @@ -279,7 +279,7 @@ async function onDelete() {
deleting.value = true;

try {
await props.grid.value?.contracts.batchCancelContracts({
await props.grid?.contracts.batchCancelContracts({
ids: selectedContracts.value.map(c => c.contractId),
});
contracts.value = contracts.value.filter(c => !selectedContracts.value.includes(c));
Expand Down
10 changes: 5 additions & 5 deletions packages/playground/src/components/deployment_data_dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ const contract = computed(() => contracts.value?.[activeTab.value] ?? {});
const code = computed(() => JSON.stringify(props.data || {}, undefined, 2));
const html = computed(() => hljs.highlight(code.value, { language: "json" }).value);
const profileManager = useProfileManager();
const gridStore = useGrid();
const grid = gridStore.client as GridClient;

function copy() {
navigator.clipboard.writeText(code.value);
Expand Down Expand Up @@ -196,7 +198,6 @@ function getValue(key: string) {

async function getGrafanaUrl() {
isLoading.value = true;
const grid = await getGrid(profileManager.profile!);
if (grid) {
if (contract.value.type !== ContractType.NAME) {
const nodeId = await grid.capacity.getNodeIdFromContractId({
Expand All @@ -213,8 +214,6 @@ getGrafanaUrl();

async function getGPUInfo() {
loadingCard.value = true;

const grid = await getGrid(profileManager.profile!);
if (grid) {
const nodeId = await grid.capacity.getNodeIdFromContractId({
contractId: contract.value.contractId,
Expand Down Expand Up @@ -302,13 +301,14 @@ function getTooltipText(contract: any, index: number) {
</script>

<script lang="ts">
import type { GridClient } from "@threefold/grid_client";

import { useProfileManager } from "@/stores/profile_manager";
import { ContractType } from "@/utils/contracts";
import { createCustomToast, ToastType } from "@/utils/custom_toast";
import { GrafanaStatistics } from "@/utils/get_metrics_url";
import { getGrid } from "@/utils/grid";

import type { NodeInfo } from "../../../grid_client/dist/es6";
import { useGrid } from "../stores";
import type { Disk } from "../utils/deploy_vm";
import CopyReadonlyInput from "./copy_readonly_input.vue";
import { HighlightDark, HighlightLight } from "./highlight_themes";
Expand Down
10 changes: 7 additions & 3 deletions packages/playground/src/components/deposit_dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ import { manual } from "@/utils/manual";

import { useProfileManagerController } from "../components/profile_manager_controller.vue";
import QrcodeGenerator from "../components/qrcode_generator.vue";
import { useProfileManager } from "../stores";
import { useGrid, useProfileManager } from "../stores";
import { createCustomToast, ToastType } from "../utils/custom_toast";
import { getGrid } from "../utils/grid";
import CopyReadonlyInput from "./copy_readonly_input.vue";
const depositDialog = ref(false);
const emits = defineEmits(["close"]);
Expand All @@ -95,6 +94,8 @@ const loading = ref(false);
const dots = ref(".");
const interval = ref<number | null>(null);
const ProfileManagerController = useProfileManagerController();
const gridStore = useGrid();
const grid = gridStore.client as GridClient;

const apps = [
{
Expand Down Expand Up @@ -141,7 +142,7 @@ onMounted(async () => {
interval.value = window.setInterval(loadingDots, 500);
try {
loading.value = true;
const grid = await getGrid(profileManager.profile!);
updateGrid(grid, { projectName: "" });
const address = profileManager.profile?.address as string;
const receivedDeposit = await grid!.bridge.listenToMintCompleted({
address: address,
Expand Down Expand Up @@ -171,8 +172,11 @@ onBeforeUnmount(() => {
});
</script>
<script lang="ts">
import type { GridClient } from "@threefold/grid_client";
import { defineComponent } from "vue";

import { updateGrid } from "../utils/grid";

export default defineComponent({
name: "DepositDialog",
});
Expand Down
11 changes: 7 additions & 4 deletions packages/playground/src/components/funds_card.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@
</template>

<script lang="ts">
import type { GridClient } from "@threefold/grid_client";
import { ref } from "vue";

import { manual } from "@/utils/manual";

import { useProfileManagerController } from "../components/profile_manager_controller.vue";
import { useProfileManager } from "../stores";
import { useGrid } from "../stores";
import { createCustomToast, ToastType } from "../utils/custom_toast";
import { getGrid } from "../utils/grid";
import { updateGrid } from "../utils/grid";

export default {
name: "FundsCard",
setup() {
const loadingAddTFT = ref(false);
const profileManager = useProfileManager();
const ProfileManagerController = useProfileManagerController();
const gridStore = useGrid();
const grid = gridStore.client as GridClient;

const addTFT = async () => {
if (window.env.NETWORK !== "dev" && window.env.NETWORK !== "qa") {
window.open("https://gettft.com/gettft/", "_blank");
} else {
loadingAddTFT.value = true;
try {
const grid = await getGrid(profileManager.profile!);
updateGrid(grid, { projectName: "" });
await grid?.balance.getMoreFunds();
await ProfileManagerController.reloadBalance();
loadingAddTFT.value = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@
<script lang="ts" setup>
import { ref } from "vue";

import { useProfileManager } from "../stores";
import { useGrid } from "../stores";
import { addMachine, deleteMachine, loadVM } from "../utils/deploy_vm";
import { getGrid } from "../utils/grid";
import rootFs from "../utils/root_fs";

const props = defineProps<{ master: any; data: any[]; projectName: string }>();
const emits = defineEmits<{ (event: "close"): void; (event: "update:caprover", data: any): void }>();

const profileManager = useProfileManager();

const selectedWorkers = ref<any[]>([]);
const deleting = ref(false);
const deployedDialog = ref(false);
const gridStore = useGrid();
const grid = gridStore.client as GridClient;

const worker = ref(createWorker());

Expand All @@ -112,8 +111,8 @@ async function deploy(layout: any) {
layout.setStatus("deploy");

try {
updateGrid(grid, { projectName: props.projectName });
layout.value?.validateSSH();
const grid = await getGrid(profileManager.profile!, props.projectName);

await layout.validateBalance(grid);

Expand Down Expand Up @@ -158,7 +157,6 @@ async function deploy(layout: any) {

async function onDelete(cb: (workers: any[]) => void) {
deleting.value = true;
const grid = await getGrid(profileManager.profile!, props.projectName);
for (const worker of selectedWorkers.value) {
console.log(props.master.name, worker.name);

Expand All @@ -184,8 +182,11 @@ async function onDelete(cb: (workers: any[]) => void) {
</script>

<script lang="ts">
import type { GridClient } from "@threefold/grid_client";

import CaproverWorker, { createWorker } from "../components/caprover_worker.vue";
import ListTable from "../components/list_table.vue";
import { updateGrid } from "../utils/grid";
import { normalizeError } from "../utils/helpers";
import ManageWorkerDialog from "./manage_worker_dialog.vue";

Expand Down
19 changes: 9 additions & 10 deletions packages/playground/src/components/manage_gateway_dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,14 @@
</template>

<script lang="ts">
import type { GridClient } from "@threefold/grid_client";
import { onMounted, type PropType, ref } from "vue";

import { useProfileManager } from "../stores";
import { useGrid } from "../stores";
import { ProjectName } from "../types";
import type { SelectionDetails } from "../types/nodeSelector";
import { deployGatewayName, type GridGateway, loadDeploymentGateways } from "../utils/gateway";
import { getGrid } from "../utils/grid";
import { updateGrid } from "../utils/grid";
import { normalizeError } from "../utils/helpers";
import { generateName } from "../utils/strings";
import { isAvailableName } from "../utils/validators";
Expand All @@ -235,7 +236,6 @@ export default {
vm: { type: Array as PropType<any>, required: true },
},
setup(props) {
const profileManager = useProfileManager();
const layout = useLayout();
const gatewayTab = ref(0);

Expand All @@ -249,9 +249,12 @@ export default {

const ip = props.vm.interfaces[0].ip as string;
const networkName = props.vm.interfaces[0].network as string;
const gridStore = useGrid();
const grid = gridStore.client as unknown as GridClient;

onMounted(async () => {
const grid = await getGrid(profileManager.profile!);
updateGrid(grid, { projectName: "" });

oldPrefix.value =
(props.vm.projectName.toLowerCase().includes(ProjectName.Fullvm.toLowerCase()) ? "fvm" : "vm") +
grid!.config.twinId;
Expand All @@ -269,7 +272,8 @@ export default {
gateways.value = [];
gatewaysToDelete.value = [];
loadingGateways.value = true;
const grid = await getGrid(profileManager.profile!, props.vm.projectName);
updateGrid(grid, { projectName: props.vm.projectName });

const { gateways: gws, failedToList } = await loadDeploymentGateways(grid!);
gateways.value = gws;
failedToListGws.value = failedToList;
Expand All @@ -285,7 +289,6 @@ export default {

try {
const [x, y] = ip.split(".");
const grid = await getGrid(profileManager.profile!, props.vm.projectName);

const data = {
name: networkName,
Expand Down Expand Up @@ -318,7 +321,6 @@ export default {
const deleting = ref(false);
async function deleteSelectedGateways() {
deleting.value = true;
const grid = await getGrid(profileManager.profile!, props.vm.projectName);
const deletedGateways = new Set<GridGateway>();
for (const gw of gatewaysToDelete.value) {
await grid!.gateway
Expand All @@ -335,13 +337,10 @@ export default {
}

async function validateSubdomain() {
const grid = await getGrid(profileManager.profile!, props.vm.projectName);
return await isAvailableName(grid!, subdomain.value);
}

return {
profileManager,

oldPrefix,
prefix,
layout,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,24 @@
<script lang="ts" setup>
import { ref } from "vue";

import { useProfileManager } from "../stores";
import { useGrid } from "../stores";
import { deleteWorker, deployWorker, loadK8S } from "../utils/deploy_k8s";
import { getGrid } from "../utils/grid";

const props = defineProps<{ data: K8S }>();
const emits = defineEmits<{ (event: "close"): void; (event: "update:k8s", data: any): void }>();

const profileManager = useProfileManager();

const worker = ref(createWorker());
const selectedWorkers = ref<any[]>([]);
const deleting = ref(false);
const gridStore = useGrid();
const grid = gridStore.client as GridClient;

function calcDiskSize(disks: { size: number }[]) {
return disks.reduce((t, d) => t + d.size, 0) / 1024 ** 3;
}

async function deploy(layout: any) {
layout.setStatus("deploy");
const grid = await getGrid(profileManager.profile!, props.data.projectName);

deployWorker(grid!, {
...worker.value,
Expand All @@ -86,7 +84,6 @@ async function deploy(layout: any) {

async function onDelete(cb: (workers: any[]) => void) {
deleting.value = true;
const grid = await getGrid(profileManager.profile!, props.data.projectName);

for (const worker of selectedWorkers.value) {
try {
Expand All @@ -108,6 +105,8 @@ async function onDelete(cb: (workers: any[]) => void) {
</script>

<script lang="ts">
import type { GridClient } from "@threefold/grid_client";

import ListTable from "../components/list_table.vue";
import type { K8S } from "../utils/load_deployment";
import K8SWorker, { createWorker } from "./k8s_worker.vue";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
</VCard>
</template>
<script lang="ts">
import type { NodeInfo, NodeResources } from "@threefold/grid_client";
import type { GridClient, NodeInfo, NodeResources } from "@threefold/grid_client";
import { CertificationType, type GridNode } from "@threefold/gridproxy_client";
import { computed, onMounted, ref, watch } from "vue";
import { capitalize } from "vue";
Expand All @@ -243,9 +243,8 @@ import { getCountryCode } from "@/utils/get_nodes";
import { manual } from "@/utils/manual";
import toReadableDate from "@/utils/to_readable_data";

import { useProfileManager } from "../../stores";
import { useGrid, useProfileManager } from "../../stores";
import formatResourceSize from "../../utils/format_resource_size";
import { getGrid } from "../../utils/grid";
import { toGigaBytes } from "../../utils/helpers";
import ResourceDetails from "./node_details_internals/ResourceDetails.vue";

Expand All @@ -264,6 +263,8 @@ export default {
},
setup(props, ctx) {
const profileManager = useProfileManager();
const gridStore = useGrid();
const grid = gridStore.client as unknown as GridClient;
const node = ref(props.node);
const stakingDiscount = ref<number>();
const loadingStakingDiscount = ref<boolean>(false);
Expand Down Expand Up @@ -399,7 +400,6 @@ export default {

async function getStakingDiscount() {
try {
const grid = await getGrid(profileManager.profile!);
const total_resources = props.node?.total_resources;
const { cru, hru, mru, sru } = total_resources as NodeResources;
const price = await grid?.calculator.calculateWithMyBalance({
Expand Down
Loading
Loading