From 70b16e58619eb21ca64210dd54965d1f35d0c56a Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Thu, 2 May 2024 23:49:01 +0200 Subject: [PATCH] Decouple stats from tree endpoint response --- httpd-client/index.ts | 2 ++ httpd-client/lib/project.ts | 40 ++++++++++++++++++------- httpd-client/lib/project/commit.ts | 9 +----- httpd-client/tests/project.test.ts | 4 +++ src/views/projects/History.svelte | 11 +++---- src/views/projects/Source.svelte | 10 ++++++- src/views/projects/Source/Header.svelte | 11 +++++-- src/views/projects/router.ts | 14 ++++++--- 8 files changed, 71 insertions(+), 30 deletions(-) diff --git a/httpd-client/index.ts b/httpd-client/index.ts index 51fc3bce5..f2328f436 100644 --- a/httpd-client/index.ts +++ b/httpd-client/index.ts @@ -6,6 +6,7 @@ import type { ProjectListQuery, Remote, Tree, + TreeStats, } from "./lib/project.js"; import type { SuccessResponse, @@ -82,6 +83,7 @@ export type { Review, Revision, Scope, + TreeStats, Tree, Verdict, }; diff --git a/httpd-client/lib/project.ts b/httpd-client/lib/project.ts index d11706d30..9ce00f767 100644 --- a/httpd-client/lib/project.ts +++ b/httpd-client/lib/project.ts @@ -102,11 +102,13 @@ const treeEntrySchema = object({ export type TreeEntry = z.infer; -export interface TreeStats { - commits: number; - branches: number; - contributors: number; -} +const treeStatsSchema = object({ + commits: number(), + branches: number(), + contributors: number(), +}); + +export type TreeStats = z.infer; export type Tree = z.infer; @@ -115,11 +117,6 @@ const treeSchema = object({ lastCommit: commitHeaderSchema, name: string(), path: string(), - stats: object({ - commits: number(), - branches: number(), - contributors: number(), - }), }); export type Remote = z.infer; @@ -153,6 +150,7 @@ export class Client { // persisted and new instances are frequently created. static #cache = { tree: new LRUCache({ max: 300 }), + stats: new LRUCache({ max: 300 }), blob: new LRUCache({ max: 500 }), }; @@ -275,6 +273,28 @@ export class Client { return tree; } + public async getTreeStatsBySha( + id: string, + sha: string, + options?: RequestOptions, + ): Promise { + const cacheKey = `tree:stats:${sha}`; + const cachedTree = Client.#cache.stats.get(cacheKey); + if (cachedTree) { + return cachedTree; + } + const tree = await this.#fetcher.fetchOk( + { + method: "GET", + path: `projects/${id}/stats/tree/${sha}`, + options, + }, + treeStatsSchema, + ); + Client.#cache.stats.set(cacheKey, tree); + return tree; + } + public async getAllRemotes( id: string, options?: RequestOptions, diff --git a/httpd-client/lib/project/commit.ts b/httpd-client/lib/project/commit.ts index 125a26c40..f4726bd68 100644 --- a/httpd-client/lib/project/commit.ts +++ b/httpd-client/lib/project/commit.ts @@ -236,11 +236,4 @@ const commitSchema = object({ type Commits = z.infer; -const commitsSchema = object({ - commits: array(commitHeaderSchema), - stats: object({ - commits: number(), - branches: number(), - contributors: number(), - }), -}); +const commitsSchema = array(commitHeaderSchema); diff --git a/httpd-client/tests/project.test.ts b/httpd-client/tests/project.test.ts index c8479cb08..406b614f5 100644 --- a/httpd-client/tests/project.test.ts +++ b/httpd-client/tests/project.test.ts @@ -53,6 +53,10 @@ describe("project", () => { await api.project.getTree(sourceBrowsingRid, aliceMainHead); }); + test("#getTreeStats(id, sha)", async () => { + await api.project.getTreeStatsBySha(sourceBrowsingRid, aliceMainHead); + }); + test("#getTree(id, sha, path)", async () => { await api.project.getTree(sourceBrowsingRid, aliceMainHead, "src"); }); diff --git a/src/views/projects/History.svelte b/src/views/projects/History.svelte index 9e77964f7..8f39fbc61 100644 --- a/src/views/projects/History.svelte +++ b/src/views/projects/History.svelte @@ -5,6 +5,7 @@ Project, Remote, Tree, + TreeStats, } from "@httpd-client"; import type { Route } from "@app/lib/router"; @@ -29,8 +30,8 @@ export let peers: Remote[]; export let project: Project; export let revision: string | undefined; - export let totalCommitCount: number; export let tree: Tree; + export let stats: TreeStats; export let seeding: boolean; const api = new HttpdClient(baseUrl); @@ -54,8 +55,7 @@ page, perPage: COMMITS_PER_PAGE, }); - allCommitHeaders = response.commits; - totalCommitCount = response.stats.commits; + allCommitHeaders = response; } catch (e) { error = e; } @@ -117,6 +117,7 @@ branches={branchesWithRoute} {revision} {tree} + {stats} filesLinkActive={false} historyLinkActive={true} /> @@ -135,11 +136,11 @@ {/each} - {#if loading || allCommitHeaders.length < totalCommitCount} + {#if loading || allCommitHeaders.length < stats.commits}
{#if loading} - {:else if allCommitHeaders.length < totalCommitCount} + {:else if allCommitHeaders.length < stats.commits} {/if}
diff --git a/src/views/projects/Source.svelte b/src/views/projects/Source.svelte index 33a580129..26b026cbb 100644 --- a/src/views/projects/Source.svelte +++ b/src/views/projects/Source.svelte @@ -1,5 +1,11 @@