Skip to content

Commit

Permalink
Merge pull request #1527 from appwrite/fix-broken-org-usage-tab-for-n…
Browse files Browse the repository at this point in the history
…on-default-plans

Fix missing limits data for non-standard tiers
  • Loading branch information
ernstmul authored Nov 20, 2024
2 parents 7f4f4ae + e935fe0 commit cd5c23d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/lib/stores/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ export type PlanServices =
| 'usersAddon'
| 'webhooks';

export function getServiceLimit(serviceId: PlanServices, tier: Tier = null): number {
export function getServiceLimit(serviceId: PlanServices, tier: Tier = null, plan?: Plan): number {
if (!isCloud) return 0;
if (!serviceId) return 0;
const info = get(plansInfo);
if (!info) return 0;
const plan = info.get(tier ?? get(organization)?.billingPlan);
plan ??= info.get(tier ?? get(organization)?.billingPlan);
return plan?.[serviceId];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
getServiceLimit,
showUsageRatesModal,
tierToPlan,
type Tier,
upgradeURL
} from '$lib/stores/billing';
import { organization } from '$lib/stores/organization';
Expand All @@ -21,23 +21,10 @@
export let data;
const tier = data?.currentInvoice?.plan ?? $organization?.billingPlan;
const plan = tierToPlan(tier).name;
// let invoice = null;
// async function handlePeriodChange() {
// const target = invoice
// ? `/console/organization-${$organization.$id}/usage/${invoice}`
// : `/console/organization-${$organization.$id}/usage`;
// if ($page.url.pathname !== target) {
// await goto(target);
// }
// }
// const cycles = data.invoices.invoices.map((invoice) => ({
// label: toLocaleDate(invoice.from),
// value: invoice.$id
// }));
const tier = data?.plan
? (data.plan.$id as Tier)
: (data?.currentInvoice?.plan ?? $organization?.billingPlan);
const plan = data?.plan ?? undefined;
$: project = (data.organizationUsage as OrganizationUsage).projects;
</script>
Expand Down Expand Up @@ -78,29 +65,11 @@
</p>
{:else if $organization.billingPlan === BillingPlan.FREE}
<p class="text">
If you exceed the limits of the {plan} plan, services for your organization's projects
If you exceed the limits of the Free plan, services for your organization's projects
may be disrupted.
<a href={$upgradeURL} class="link">Upgrade for greater capacity</a>.
</p>
{/if}

<!--<div class="u-flex u-gap-8 u-cross-center">
<p class="text">Usage period:</p>
<InputSelect
wrapperTag="div"
id="period"
label="Usage period"
showLabel={false}
bind:value={invoice}
on:change={handlePeriodChange}
options={[
{
label: 'Current billing cycle',
value: null
},
...cycles
]} />
</div>-->
</div>

<CardGrid>
Expand All @@ -115,7 +84,7 @@
{#if data.organizationUsage.bandwidth}
{@const current = total(data.organizationUsage.bandwidth)}
{@const currentHumanized = humanFileSize(current)}
{@const max = getServiceLimit('bandwidth', tier)}
{@const max = getServiceLimit('bandwidth', tier, plan)}
<ProgressBarBig
currentUnit={currentHumanized.unit}
currentValue={currentHumanized.value}
Expand Down Expand Up @@ -178,7 +147,7 @@
<svelte:fragment slot="aside">
{#if data.organizationUsage.users}
{@const current = data.organizationUsage.usersTotal}
{@const max = getServiceLimit('users', tier)}
{@const max = getServiceLimit('users', tier, plan)}
<ProgressBarBig
currentUnit="Users"
currentValue={formatNum(current)}
Expand Down Expand Up @@ -233,7 +202,7 @@
<svelte:fragment slot="aside">
{#if data.organizationUsage.executionsTotal}
{@const current = data.organizationUsage.executionsTotal}
{@const max = getServiceLimit('executions', tier)}
{@const max = getServiceLimit('executions', tier, plan)}
<ProgressBarBig
currentUnit="Executions"
currentValue={formatNum(current)}
Expand Down Expand Up @@ -290,7 +259,7 @@
{#if data.organizationUsage.storageTotal}
{@const current = data.organizationUsage.storageTotal}
{@const currentHumanized = humanFileSize(current)}
{@const max = getServiceLimit('storage', tier)}
{@const max = getServiceLimit('storage', tier, plan)}
{@const progressBarStorageDate = [
{
size: bytesToSize(data.organizationUsage.filesStorageTotal, 'GB'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sdk } from '$lib/stores/sdk';
import { Query, type Models } from '@appwrite.io/console';
import type { PageLoad } from './$types';
import type { Organization } from '$lib/stores/organization';
import { type Organization } from '$lib/stores/organization';
import type { Invoice } from '$lib/sdk/billing';

export const load: PageLoad = async ({ params, parent }) => {
Expand Down Expand Up @@ -41,11 +41,11 @@ export const load: PageLoad = async ({ params, parent }) => {
startDate = currentInvoice.from;
endDate = currentInvoice.to;
}

const [invoices, usage, organizationMembers] = await Promise.all([
const [invoices, usage, organizationMembers, plan] = await Promise.all([
sdk.forConsole.billing.listInvoices(org.$id, [Query.orderDesc('from')]),
sdk.forConsole.billing.listUsage(params.organization, startDate, endDate),
sdk.forConsole.teams.listMemberships(params.organization)
sdk.forConsole.teams.listMemberships(params.organization),
sdk.forConsole.billing.getPlan(org.$id)
]);

const projectNames: { [key: string]: Models.Project } = {};
Expand Down Expand Up @@ -77,6 +77,7 @@ export const load: PageLoad = async ({ params, parent }) => {
projectNames,
invoices,
currentInvoice,
organizationMembers
organizationMembers,
plan
};
};

0 comments on commit cd5c23d

Please sign in to comment.