Monorepo for Tangled tangled.org

appview,knotserver: include language 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 <jeynesbrook@gmail.com>

authored by brookjeynes.dev and committed by anirudh.fi 033cb439 b245d4d7

Changed files
+77 -5
appview
knotserver
types
+1 -1
appview/pages/pages.go
··· 446 446 Raw bool 447 447 EmailToDidOrHandle map[string]string 448 448 VerifiedCommits commitverify.VerifiedCommits 449 - Languages *types.RepoLanguageResponse 449 + Languages []types.RepoLanguageDetails 450 450 Pipelines map[string]db.Pipeline 451 451 types.RepoIndexResponse 452 452 }
+64 -2
appview/repo/index.go
··· 6 6 "log" 7 7 "net/http" 8 8 "slices" 9 + "sort" 9 10 "strings" 10 11 11 12 "tangled.sh/tangled.sh/core/appview/commitverify" ··· 18 19 "tangled.sh/tangled.sh/core/types" 19 20 20 21 "github.com/go-chi/chi/v5" 22 + "github.com/go-enry/go-enry/v2" 21 23 ) 22 24 23 25 func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { ··· 121 123 } 122 124 } 123 125 124 - repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref) 126 + languageInfo, err := getLanguageInfo(repoInfo, rp, f, user, signedClient, ref) 125 127 if err != nil { 126 128 log.Printf("failed to compute language percentages: %s", err) 127 129 // non-fatal ··· 148 150 BranchesTrunc: branchesTrunc, 149 151 EmailToDidOrHandle: emailToDidOrHandle(rp, emailToDidMap), 150 152 VerifiedCommits: vc, 151 - Languages: repoLanguages, 153 + Languages: languageInfo, 152 154 Pipelines: pipelines, 153 155 }) 154 156 return 157 + } 158 + 159 + func getLanguageInfo( 160 + repoInfo repoinfo.RepoInfo, 161 + rp *Repo, 162 + f *reporesolver.ResolvedRepo, 163 + user *oauth.User, 164 + signedClient *knotclient.SignedClient, 165 + ref string, 166 + ) ([]types.RepoLanguageDetails, error) { 167 + repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref) 168 + if err != nil { 169 + return []types.RepoLanguageDetails{}, err 170 + } 171 + if repoLanguages == nil { 172 + repoLanguages = &types.RepoLanguageResponse{Languages: make(map[string]int)} 173 + } 174 + 175 + totalLanguageFileCount := 0 176 + for _, fileCount := range repoLanguages.Languages { 177 + totalLanguageFileCount += fileCount 178 + } 179 + 180 + var languageStats []types.RepoLanguageDetails 181 + var otherLanguageStat *types.RepoLanguageDetails 182 + var otherPercentage float32 = 0 183 + 184 + for fileType, fileCount := range repoLanguages.Languages { 185 + percentage := (float32(fileCount) / float32(totalLanguageFileCount)) * 100 186 + 187 + if percentage <= 0.1 { 188 + otherPercentage += percentage 189 + continue 190 + } 191 + 192 + // Exclude languages 193 + if fileType == "Text" { 194 + otherPercentage += percentage 195 + continue 196 + } 197 + 198 + color := enry.GetColor(fileType) 199 + 200 + if fileType == "Other" { 201 + otherLanguageStat = &types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color} 202 + } else { 203 + languageStats = append(languageStats, types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color}) 204 + } 205 + } 206 + 207 + sort.Slice(languageStats, func(i, j int) bool { 208 + return languageStats[i].Percentage > languageStats[j].Percentage 209 + }) 210 + 211 + if otherLanguageStat != nil { 212 + otherLanguageStat.Percentage += otherPercentage 213 + languageStats = append(languageStats, *otherLanguageStat) 214 + } 215 + 216 + return languageStats, nil 155 217 } 156 218 157 219 func getForkInfo(
+1
appview/repo/repo.go
··· 35 35 36 36 securejoin "github.com/cyphar/filepath-securejoin" 37 37 "github.com/go-chi/chi/v5" 38 + "github.com/go-enry/go-enry/v2" 38 39 "github.com/go-git/go-git/v5/plumbing" 39 40 "github.com/posthog/posthog-go" 40 41
+4 -1
knotserver/routes.go
··· 784 784 content, _ := gr.FileContentN(absPath, 1024) 785 785 if !safe { 786 786 lang = enry.GetLanguage(absPath, content) 787 + if len(lang) == 0 { 788 + lang = "Other" 789 + } 787 790 } else { 788 791 lang, _ = enry.GetLanguageByContent(absPath, content) 789 792 if len(lang) == 0 { 790 - return 793 + lang = "Other" 791 794 } 792 795 } 793 796 }
+7 -1
types/repo.go
··· 109 109 Status ForkStatus `json:"status"` 110 110 } 111 111 112 + type RepoLanguageDetails struct { 113 + Name string 114 + Percentage float32 115 + Color string 116 + } 117 + 112 118 type RepoLanguageResponse struct { 113 - // Language: Percentage 119 + // Language: File count 114 120 Languages map[string]int `json:"languages"` 115 121 }