Skip to content

Commit

Permalink
Merge pull request #1432 from appwrite/fix-100-projects-usage
Browse files Browse the repository at this point in the history
Ensure we fetch all projects for usage breakdown
  • Loading branch information
stnguyen90 authored Oct 24, 2024
2 parents 89d87e4 + e6418e7 commit 8b94623
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sdk } from '$lib/stores/sdk';
import { Query } from '@appwrite.io/console';
import { Query, type Models } from '@appwrite.io/console';
import type { PageLoad } from './$types';
import type { Organization } from '$lib/stores/organization';
import type { Invoice } from '$lib/sdk/billing';
Expand Down Expand Up @@ -48,22 +48,33 @@ export const load: PageLoad = async ({ params, parent }) => {
sdk.forConsole.teams.listMemberships(params.organization)
]);

const queries: string[] = [];

const projectNames: { [key: string]: Models.Project } = {};
if (usage?.projects?.length > 0) {
queries.push(
Query.equal(
'$id',
usage.projects.map((p) => p.projectId)
)
);
}
// in batches of 100 (the max number of values in a query)
const requests = [];
const chunk = 100;
for (let i = 0; i < usage.projects.length; i += chunk) {
const queries = [
Query.limit(chunk),
Query.equal(
'$id',
usage.projects.slice(i, i + chunk).map((p) => p.projectId)
)
];
requests.push(sdk.forConsole.projects.list(queries));
}

const projectNames = await sdk.forConsole.projects.list(queries);
const responses = await Promise.all(requests);
for (const response of responses) {
for (const project of response.projects) {
projectNames[project.$id] = project;
}
}
}

return {
organizationUsage: usage,
projectNames: projectNames.projects,
projectNames,
invoices,
currentInvoice,
organizationMembers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
export let projects: OrganizationUsage['projects'];
export let metric: Metric;
function getProjectName(projectId: string): string {
return data.projectNames.find((project) => project.$id === projectId)?.name;
}
function getProjectUsageLink(projectId: string): string {
return `${base}/project-${projectId}/settings/usage`;
}
Expand Down Expand Up @@ -69,7 +65,7 @@
{#each groupByProject(metric).sort((a, b) => b.usage - a.usage) as project}
<TableRow>
<TableCellText title="Project" style="padding-left: 0;">
{getProjectName(project.projectId)}
{data.projectNames[project.projectId]?.name ?? 'Unknown'}
</TableCellText>
<TableCellText title="Usage">{format(project.usage)}</TableCellText>
{#if $canSeeProjects}
Expand Down

0 comments on commit 8b94623

Please sign in to comment.