Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).

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

+77 -5
+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" ··· 19 18 "tangled.sh/tangled.sh/core/types" 20 19 21 20 "github.com/go-chi/chi/v5" 21 + "github.com/go-enry/go-enry/v2" 22 22 ) 23 23 24 24 func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { ··· 123 121 } 124 122 } 125 123 126 - repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref) 124 + languageInfo, err := getLanguageInfo(repoInfo, rp, f, user, signedClient, ref) 127 125 if err != nil { 128 126 log.Printf("failed to compute language percentages: %s", err) 129 127 // non-fatal ··· 150 148 BranchesTrunc: branchesTrunc, 151 149 EmailToDidOrHandle: emailToDidOrHandle(rp, emailToDidMap), 152 150 VerifiedCommits: vc, 153 - Languages: repoLanguages, 151 + Languages: languageInfo, 154 152 Pipelines: pipelines, 155 153 }) 156 154 return 155 + } 156 + 157 + func getLanguageInfo( 158 + repoInfo repoinfo.RepoInfo, 159 + rp *Repo, 160 + f *reporesolver.ResolvedRepo, 161 + user *oauth.User, 162 + signedClient *knotclient.SignedClient, 163 + ref string, 164 + ) ([]types.RepoLanguageDetails, error) { 165 + repoLanguages, err := signedClient.RepoLanguages(f.OwnerDid(), f.RepoName, ref) 166 + if err != nil { 167 + return []types.RepoLanguageDetails{}, err 168 + } 169 + if repoLanguages == nil { 170 + repoLanguages = &types.RepoLanguageResponse{Languages: make(map[string]int)} 171 + } 172 + 173 + totalLanguageFileCount := 0 174 + for _, fileCount := range repoLanguages.Languages { 175 + totalLanguageFileCount += fileCount 176 + } 177 + 178 + var languageStats []types.RepoLanguageDetails 179 + var otherLanguageStat *types.RepoLanguageDetails 180 + var otherPercentage float32 = 0 181 + 182 + for fileType, fileCount := range repoLanguages.Languages { 183 + percentage := (float32(fileCount) / float32(totalLanguageFileCount)) * 100 184 + 185 + if percentage <= 0.1 { 186 + otherPercentage += percentage 187 + continue 188 + } 189 + 190 + // Exclude languages 191 + if fileType == "Text" { 192 + otherPercentage += percentage 193 + continue 194 + } 195 + 196 + color := enry.GetColor(fileType) 197 + 198 + if fileType == "Other" { 199 + otherLanguageStat = &types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color} 200 + } else { 201 + languageStats = append(languageStats, types.RepoLanguageDetails{Name: fileType, Percentage: percentage, Color: color}) 202 + } 203 + } 204 + 205 + sort.Slice(languageStats, func(i, j int) bool { 206 + return languageStats[i].Percentage > languageStats[j].Percentage 207 + }) 208 + 209 + if otherLanguageStat != nil { 210 + otherLanguageStat.Percentage += otherPercentage 211 + languageStats = append(languageStats, *otherLanguageStat) 212 + } 213 + 214 + return languageStats, nil 157 215 } 158 216 159 217 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 }