diff --git a/src/lib/elements/forms/index.ts b/src/lib/elements/forms/index.ts index 1f729ba4b..57c937e60 100644 --- a/src/lib/elements/forms/index.ts +++ b/src/lib/elements/forms/index.ts @@ -27,3 +27,4 @@ export { default as InputSecret } from './inputSecret.svelte'; export { default as Helper } from './helper.svelte'; export { default as Label } from './label.svelte'; export { default as InputProjectId } from './inputProjectId.svelte'; +export { default as InputDate } from './inputDate.svelte'; diff --git a/src/lib/elements/forms/inputDate.svelte b/src/lib/elements/forms/inputDate.svelte new file mode 100644 index 000000000..a49aa3c4e --- /dev/null +++ b/src/lib/elements/forms/inputDate.svelte @@ -0,0 +1,88 @@ + + + + + {label} + + + + + {#if isNullable} + + + + + + {/if} + + {#if error} + {error} + {/if} + diff --git a/src/lib/layout/header.svelte b/src/lib/layout/header.svelte index 901d0cf8c..947721d65 100644 --- a/src/lib/layout/header.svelte +++ b/src/lib/layout/header.svelte @@ -25,7 +25,6 @@ import { Feedback } from '$lib/components/feedback'; import ChangeOrganizationTierCloud from '$routes/console/changeOrganizationTierCloud.svelte'; import { Pill } from '$lib/elements'; - import { readOnly } from '$lib/stores/billing'; import { showExcess } from '$routes/console/organization-[organization]/store'; let showDropdown = false; diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index aac332ad4..465b71441 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -449,10 +449,16 @@ export class Billing { } // TODO: add date range - async listUsage(organizationId: string): Promise { + async listUsage( + organizationId: string, + startDate: string = '', + endDate: string = '' + ): Promise { const path = `/organizations/${organizationId}/usage`; const params = { - organizationId + organizationId, + startDate, + endDate }; const uri = new URL(this.client.config.endpoint + path); return await this.client.call( diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index 17c9016c9..3ac5f60c6 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -68,7 +68,6 @@ export function getServiceLimit(serviceId: PlanServices): number { if (!isCloud) return 0; if (!serviceId) return 0; const plan = get(plansInfo).plans.find((p) => p.$id === get(organization)?.billingPlan); - console.log(plan); return plan[serviceId]; } diff --git a/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte b/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte index 3c102e93b..5162eee5e 100644 --- a/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte +++ b/src/routes/console/organization-[organization]/usage/[[period]]/+page.svelte @@ -6,18 +6,37 @@ import { wizard } from '$lib/stores/wizard.js'; import { organization } from '$lib/stores/organization'; import UsageRates from '$routes/console/wizard/cloudOrganization/usageRates.svelte'; + import { InputDate, InputSelect } from '$lib/elements/forms'; + import { page } from '$app/stores'; + import { goto } from '$app/navigation'; const tier = tierToPlan($organization?.billingPlan)?.name; export let data; let showRates = false; + let period = 'current'; - $: console.log(data.organizationUsage.users); + async function handlePeriodChange() { + if ( + period === 'current' && + !$page.url.pathname.includes('usage/current') && + $page.url.pathname !== `/console/organization-${$organization.$id}/usage` + ) { + await goto(`/console/organization-${$organization.$id}/usage/current`); + } + if (period === 'previous' && !$page.url.pathname.includes('usage/previous')) { + await goto(`/console/organization-${$organization.$id}/usage/previous`); + } + if (period === 'custom' && !$page.url.pathname.includes('usage/custom')) { + console.log('test'); + // await goto(`/console/organization-${$organization.$id}/usage/custom`); + } + } Usage - + {#if $organization.billingPlan === 'tier-2'} On the Scale plan, you'll be charged only for any usage that exceeds the thresholds @@ -36,10 +55,32 @@ >. {/if} - + + + + Usage period: - JANUARY 2021 + diff --git a/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts b/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts index 52cbd4460..ad93286ae 100644 --- a/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts +++ b/src/routes/console/organization-[organization]/usage/[[period]]/+page.ts @@ -1,8 +1,32 @@ +import type { Organization } from '$lib/stores/organization'; import { sdk } from '$lib/stores/sdk'; import type { PageLoad } from './$types'; -export const load: PageLoad = async ({ params }) => { - const usage = await sdk.forConsole.billing.listUsage(params.organization); +export const load: PageLoad = async ({ params, parent, url }) => { + const { period } = params; + const parentData = await parent(); + const org = parentData.organization as Organization; + const today = new Date().toISOString(); + let startDate: string; + let endDate: string; + + if (period === 'current') { + startDate = org.billingCurrentInvoiceDate; + endDate = today; + } else if (period === 'previous') { + const prevDate = new Date(org.billingCurrentInvoiceDate); + prevDate.setDate(prevDate.getDate() - 30); + startDate = prevDate.toISOString(); + endDate = org.billingCurrentInvoiceDate; + } else if (period === 'custom') { + startDate = url.searchParams.get('start') ?? org.billingCurrentInvoiceDate; + endDate = url.searchParams.get('end') ?? today; + } + const usage = await sdk.forConsole.billing.listUsage( + params.organization, + startDate ?? org.billingCurrentInvoiceDate, + endDate ?? today + ); console.log(usage); return { organizationUsage: usage
On the Scale plan, you'll be charged only for any usage that exceeds the thresholds @@ -36,10 +55,32 @@ >.
Usage period: