From c60e6d2bc8a56efa3ad6f2bf326273e5351463f9 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 16 Jul 2024 14:25:44 +0900 Subject: [PATCH] Update to use new sizes instead of individual memory cpu specs --- src/lib/sdk/functionsSpec.ts | 32 +++++------ src/lib/stores/sdk.ts | 4 +- .../project-[project]/functions/+layout.ts | 6 +- .../settings/updateRuntime.svelte | 57 ++++++------------- .../project-[project]/functions/store.ts | 3 +- 5 files changed, 38 insertions(+), 64 deletions(-) diff --git a/src/lib/sdk/functionsSpec.ts b/src/lib/sdk/functionsSpec.ts index a50679099..3de4ce847 100644 --- a/src/lib/sdk/functionsSpec.ts +++ b/src/lib/sdk/functionsSpec.ts @@ -129,24 +129,27 @@ export type Func = { */ providerSilentMode: boolean; /** - * CPU limit + * Runtime Size */ - cpus: number; - /** - * Memory limit - */ - memory: number; + size: string; }; -export class SpecsFunctions { +export type Sizes = { + /** + * List of available runtime sizes + */ + sizes: string[]; +}; + +export class SizesFunctions { client: Client; constructor(client: Client) { this.client = client; } - async getSpecs(): Promise { - const path = '/functions/specs'; + async getSizes(): Promise { + const path = '/functions/sizes'; const uri = new URL(this.client.config.endpoint + path); return await this.client.call('GET', uri, { @@ -197,8 +200,7 @@ export class SpecsFunctions { providerBranch?: string, providerSilentMode?: boolean, providerRootDirectory?: string, - cpus?: number, - memory?: number + size?: string ): Promise { if (typeof functionId === 'undefined') { throw new AppwriteException('Missing required parameter: "functionId"'); @@ -271,12 +273,8 @@ export class SpecsFunctions { payload['providerRootDirectory'] = providerRootDirectory; } - if (typeof cpus !== 'undefined') { - payload['cpus'] = cpus; - } - - if (typeof memory !== 'undefined') { - payload['memory'] = memory; + if (typeof size !== 'undefined') { + payload['size'] = size; } const uri = new URL(this.client.config.endpoint + apiPath); diff --git a/src/lib/stores/sdk.ts b/src/lib/stores/sdk.ts index ec3ca5328..f040a732e 100644 --- a/src/lib/stores/sdk.ts +++ b/src/lib/stores/sdk.ts @@ -23,7 +23,7 @@ import { } from '@appwrite.io/console'; import { Billing } from '../sdk/billing'; import { Sources } from '$lib/sdk/sources'; -import { SpecsFunctions } from '$lib/sdk/functionsSpec'; +import { SizesFunctions } from '$lib/sdk/functionsSpec'; const endpoint = VARS.APPWRITE_ENDPOINT ?? `${globalThis?.location?.origin}/v1`; @@ -39,7 +39,7 @@ const sdkForProject = { avatars: new Avatars(clientProject), databases: new Databases(clientProject), functions: new Functions(clientProject), - specsFunctions: new SpecsFunctions(clientProject), + sizesFunctions: new SizesFunctions(clientProject), health: new Health(clientProject), locale: new Locale(clientProject), messaging: new Messaging(clientProject), diff --git a/src/routes/console/project-[project]/functions/+layout.ts b/src/routes/console/project-[project]/functions/+layout.ts index 5be87b9cf..40125c493 100644 --- a/src/routes/console/project-[project]/functions/+layout.ts +++ b/src/routes/console/project-[project]/functions/+layout.ts @@ -8,10 +8,10 @@ import type { LayoutLoad } from './$types'; export const load: LayoutLoad = async ({ depends }) => { depends(Dependencies.FUNCTION_INSTALLATIONS); - const [runtimesList, installations, specs] = await Promise.all([ + const [runtimesList, installations, sizes] = await Promise.all([ sdk.forProject.functions.listRuntimes(), sdk.forProject.vcs.listInstallations([Query.limit(100)]), - sdk.forProject.specsFunctions.getSpecs() + sdk.forProject.sizesFunctions.getSizes() ]); return { @@ -19,6 +19,6 @@ export const load: LayoutLoad = async ({ depends }) => { breadcrumbs: Breadcrumbs, runtimesList, installations, - specs + sizes }; }; diff --git a/src/routes/console/project-[project]/functions/function-[function]/settings/updateRuntime.svelte b/src/routes/console/project-[project]/functions/function-[function]/settings/updateRuntime.svelte index 714493913..64ff8d012 100644 --- a/src/routes/console/project-[project]/functions/function-[function]/settings/updateRuntime.svelte +++ b/src/routes/console/project-[project]/functions/function-[function]/settings/updateRuntime.svelte @@ -10,46 +10,32 @@ import { onMount } from 'svelte'; import { func } from '../store'; import InputSelect from '$lib/elements/forms/inputSelect.svelte'; - import { runtimesList, specs } from '../../store'; + import { runtimesList, sizes } from '../../store'; import { isValueOfStringEnum } from '$lib/helpers/types'; import { Runtime } from '@appwrite.io/console'; const functionId = $page.params.function; let runtime: string = null; - let memory: number = null; - let cpus: number = null; + let size: string = null; let options = []; - let cpuOptions = []; - let memoryOptions = []; + let sizeOptions = []; onMount(async () => { runtime ??= $func.runtime; - memory ??= $func.memory; - cpus ??= $func.cpus; + size ??= $func.size let runtimes = await $runtimesList; - let allowedSpecs = await $specs; + let allowedSizes = await $sizes; options = runtimes.runtimes.map((runtime) => ({ label: `${runtime.name} - ${runtime.version}`, value: runtime.$id })); - memoryOptions = allowedSpecs.memory.map((memory) => - memory > 1024 - ? { - label: `${memory / 1024} GB`, - value: memory - } - : { - label: `${memory} MB`, - value: memory - } - ); - - cpuOptions = allowedSpecs.cpus.map((cpu) => ({ - label: cpu === 1 ? `${cpu} CPU Cores` : `${cpu} CPU Core`, - value: cpu + console.log(allowedSizes); + sizeOptions = allowedSizes.map((size) => ({ + label: size, + value: size })); }); @@ -58,7 +44,7 @@ if (!isValueOfStringEnum(Runtime, runtime)) { throw new Error(`Invalid runtime: ${runtime}`); } - await sdk.forProject.specsFunctions.update( + await sdk.forProject.sizesFunctions.update( functionId, $func.name, runtime, @@ -75,8 +61,7 @@ $func.providerBranch || undefined, $func.providerSilentMode || undefined, $func.providerRootDirectory || undefined, - cpus, - memory + size ); await invalidate(Dependencies.FUNCTION); addNotification({ @@ -94,7 +79,7 @@ } $: isUpdateButtonEnabled = - runtime !== $func?.runtime || memory !== $func?.memory || cpus !== $func?.cpus; + runtime !== $func?.runtime || size !== $func?.size;
@@ -112,19 +97,11 @@ required hideRequired /> - diff --git a/src/routes/console/project-[project]/functions/store.ts b/src/routes/console/project-[project]/functions/store.ts index 396c5d584..a5532285f 100644 --- a/src/routes/console/project-[project]/functions/store.ts +++ b/src/routes/console/project-[project]/functions/store.ts @@ -1,14 +1,13 @@ import { page } from '$app/stores'; import { derived } from 'svelte/store'; import type { Models } from '@appwrite.io/console'; -import type { Specs } from '$lib/sdk/functionsSpec'; export const runtimesList = derived( page, async ($page) => (await $page.data.runtimesList) as Models.RuntimeList ); -export const specs = derived(page, async ($page) => (await $page.data.specs) as Specs); +export const sizes = derived(page, async ($page) => (await $page.data.sizes.sizes) as string[]); export const baseRuntimesList = derived(runtimesList, async ($runtimesList) => { const baseRuntimes = new Map();