From 5244246ac6a5967168451b488c2f8bddc6ac1eb4 Mon Sep 17 00:00:00 2001 From: BrookJeynes Date: Tue, 3 Jun 2025 00:27:20 +1000 Subject: [PATCH] appview/knotserver: repo: include langauge colour and calculate percentage This commit updates the types related to fetching the languages used within a repo to be more structured and include additional information such as the language colour. Additional logic to calculate the language percentage was added to the repo index. Signed-off-by: BrookJeynes --- appview/pages/pages.go | 2 +- appview/repo/repo.go | 65 ++++++++++++++++++++++++++++++++++++++++-- knotserver/routes.go | 5 +++- types/repo.go | 8 +++++- 4 files changed, 75 insertions(+), 5 deletions(-) diff --git a/appview/pages/pages.go b/appview/pages/pages.go index 5b342d9..9a71016 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -416,7 +416,7 @@ type RepoIndexParams struct { Raw bool EmailToDidOrHandle map[string]string VerifiedCommits commitverify.VerifiedCommits - Languages *types.RepoLanguageResponse + Languages []types.RepoLanguageDetails types.RepoIndexResponse } diff --git a/appview/repo/repo.go b/appview/repo/repo.go index e652bbe..be7113d 100644 --- a/appview/repo/repo.go +++ b/appview/repo/repo.go @@ -34,6 +34,7 @@ import ( securejoin "github.com/cyphar/filepath-securejoin" "github.com/go-chi/chi/v5" + "github.com/go-enry/go-enry/v2" "github.com/go-git/go-git/v5/plumbing" "github.com/posthog/posthog-go" @@ -174,7 +175,7 @@ func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { } } - repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref) + languageInfo, err := getLanguageInfo(repoInfo, rp, f, user, signedClient, ref) if err != nil { log.Printf("failed to compute language percentages: %s", err) // non-fatal @@ -191,11 +192,71 @@ func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { BranchesTrunc: branchesTrunc, EmailToDidOrHandle: emailToDidOrHandle(rp, emailToDidMap), VerifiedCommits: vc, - Languages: repoLanguages, + Languages: languageInfo, }) return } +func getLanguageInfo( + repoInfo repoinfo.RepoInfo, + rp *Repo, + f *reporesolver.ResolvedRepo, + user *oauth.User, + signedClient *knotclient.SignedClient, + ref string, +) ([]types.RepoLanguageDetails, error) { + repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref) + if err != nil { + return []types.RepoLanguageDetails{}, err + } + if repoLanguages == nil { + repoLanguages = &types.RepoLanguageResponse{Languages: make(map[string]int)} + } + + totalLanguageFileCount := 0 + for _, fileCount := range repoLanguages.Languages { + totalLanguageFileCount += fileCount + } + + var languageStats []types.RepoLanguageDetails + var otherLanguageStat *types.RepoLanguageDetails + var otherPercentage float32 = 0 + + for fileType, fileCount := range repoLanguages.Languages { + percentage := (float32(fileCount) / float32(totalLanguageFileCount)) * 100 + + if percentage <= 0.1 { + otherPercentage += percentage + continue + } + + // Exclude languages + if fileType == "Text" { + otherPercentage += percentage + continue + } + + color := enry.GetColor(fileType) + + if fileType == "Other" { + otherLanguageStat = &types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color} + } else { + languageStats = append(languageStats, types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color}) + } + } + + sort.Slice(languageStats, func(i, j int) bool { + return languageStats[i].Percentage > languageStats[j].Percentage + }) + + if otherLanguageStat != nil { + otherLanguageStat.Percentage += otherPercentage + languageStats = append(languageStats, *otherLanguageStat) + } + + return languageStats, nil +} + func getForkInfo( repoInfo repoinfo.RepoInfo, rp *Repo, diff --git a/knotserver/routes.go b/knotserver/routes.go index 7424c99..17797e8 100644 --- a/knotserver/routes.go +++ b/knotserver/routes.go @@ -775,10 +775,13 @@ func (h *Handle) RepoLanguages(w http.ResponseWriter, r *http.Request) { content, _ := gr.FileContentN(absPath, 1024) if !safe { lang = enry.GetLanguage(absPath, content) + if len(lang) == 0 { + lang = "Other" + } } else { lang, _ = enry.GetLanguageByContent(absPath, content) if len(lang) == 0 { - return + lang = "Other" } } } diff --git a/types/repo.go b/types/repo.go index 985da9e..d347ec7 100644 --- a/types/repo.go +++ b/types/repo.go @@ -109,7 +109,13 @@ type AncestorCheckResponse struct { Status ForkStatus `json:"status"` } +type RepoLanguageDetails struct { + Name string + Percentage float32 + Color string +} + type RepoLanguageResponse struct { - // Language: Percentage + // Language: File count Languages map[string]int `json:"languages"` } -- 2.43.0 From 6d9ba8c8596a5a864dab893f1ff7cf4efe8e03e6 Mon Sep 17 00:00:00 2001 From: BrookJeynes Date: Tue, 3 Jun 2025 00:33:03 +1000 Subject: [PATCH] appview: repo(index): add languages to repo index; show name and percentage on hover This commit adds languages with their associated colour just below the tabs on the repo index page. Hovering over the colours will display the language name and file percentage within the repo. Signed-off-by: BrookJeynes --- appview/pages/templates/layouts/repobase.html | 49 ++++++++++--------- appview/pages/templates/repo/index.html | 14 ++++++ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/appview/pages/templates/layouts/repobase.html b/appview/pages/templates/layouts/repobase.html index ef6f4b4..940df80 100644 --- a/appview/pages/templates/layouts/repobase.html +++ b/appview/pages/templates/layouts/repobase.html @@ -1,29 +1,32 @@ {{ define "title" }}{{ .RepoInfo.FullName }}{{ end }} {{ define "content" }} -
- {{ if .RepoInfo.Source }} -

-

- {{ i "git-fork" "w-3 h-3 mr-1"}} - forked from - {{ $sourceOwner := didOrHandle .RepoInfo.Source.Did .RepoInfo.SourceHandle }} - {{ $sourceOwner }}/{{ .RepoInfo.Source.Name }} -
-

- {{ end }} -
- +
+ {{ if .RepoInfo.Source }} +

+

+ {{ i "git-fork" "w-3 h-3 mr-1"}} + forked from + {{ $sourceOwner := didOrHandle .RepoInfo.Source.Did .RepoInfo.SourceHandle }} + {{ $sourceOwner }}/{{ .RepoInfo.Source.Name }} +
+

+ {{ end }} +
+ - {{ template "repo/fragments/repoActions" .RepoInfo }} -
- {{ template "repo/fragments/repoDescription" . }} -
-
+ {{ template "repo/fragments/repoActions" .RepoInfo }} +
+ {{ template "repo/fragments/repoDescription" . }} +
+ +
{{ block "repoContent" . }}{{ end }}
diff --git a/appview/pages/templates/repo/index.html b/appview/pages/templates/repo/index.html index a06e3f6..a64eacf 100644 --- a/appview/pages/templates/repo/index.html +++ b/appview/pages/templates/repo/index.html @@ -7,9 +7,23 @@ {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo) }} {{ end }} +{{ define "repoLanguages" }} +
+ {{ range $value := .Languages }} +
+ {{ end }} +
+{{ end }} {{ define "repoContent" }}
+ {{ if .Languages }} + {{ block "repoLanguages" . }}{{ end }} + {{ end }}
{{ block "branchSelector" . }}{{ end }}
-- 2.43.0