feat: add language statistics to repo page #208

Changed files
+119 -28
appview
pages
templates
layouts
repo
repo
knotserver
types
+1 -1
appview/pages/pages.go
···
Raw bool
EmailToDidOrHandle map[string]string
VerifiedCommits commitverify.VerifiedCommits
-
Languages *types.RepoLanguageResponse
+
Languages []types.RepoLanguageDetails
types.RepoIndexResponse
}
+63 -2
appview/repo/repo.go
···
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"
···
}
}
-
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
···
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,
+4 -1
knotserver/routes.go
···
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"
}
}
}
+7 -1
types/repo.go
···
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"`
}
+33 -23
appview/pages/templates/layouts/repobase.html
···
{{ define "title" }}{{ .RepoInfo.FullName }}{{ end }}
{{ define "content" }}
-
<section id="repo-header" class="mb-4 py-2 px-6 dark:text-white">
-
{{ if .RepoInfo.Source }}
-
<p class="text-sm">
-
<div class="flex items-center">
-
{{ i "git-fork" "w-3 h-3 mr-1"}}
-
forked from
-
{{ $sourceOwner := didOrHandle .RepoInfo.Source.Did .RepoInfo.SourceHandle }}
-
<a class="ml-1 underline" href="/{{ $sourceOwner }}/{{ .RepoInfo.Source.Name }}">{{ $sourceOwner }}/{{ .RepoInfo.Source.Name }}</a>
-
</div>
-
</p>
-
{{ end }}
-
<div class="text-lg flex items-center justify-between">
-
<div>
-
<a href="/{{ .RepoInfo.OwnerWithAt }}">{{ .RepoInfo.OwnerWithAt }}</a>
-
<span class="select-none">/</span>
-
<a href="/{{ .RepoInfo.FullName }}" class="font-bold">{{ .RepoInfo.Name }}</a>
-
</div>
+
<section id="repo-header" class="mb-4 py-2 px-6 dark:text-white">
+
{{ if .RepoInfo.Source }}
+
<p class="text-sm">
+
<div class="flex items-center">
+
{{ i "git-fork" "w-3 h-3 mr-1"}}
+
forked from
+
{{ $sourceOwner := didOrHandle .RepoInfo.Source.Did .RepoInfo.SourceHandle }}
+
<a class="ml-1 underline" href="/{{ $sourceOwner }}/{{ .RepoInfo.Source.Name }}">{{ $sourceOwner }}/{{ .RepoInfo.Source.Name }}</a>
+
</div>
+
</p>
+
{{ end }}
+
<div class="text-lg flex items-center justify-between">
+
<div>
+
<a href="/{{ .RepoInfo.OwnerWithAt }}">{{ .RepoInfo.OwnerWithAt }}</a>
+
<span class="select-none">/</span>
+
<a href="/{{ .RepoInfo.FullName }}" class="font-bold">{{ .RepoInfo.Name }}</a>
+
</div>
-
{{ template "repo/fragments/repoActions" .RepoInfo }}
-
</div>
-
{{ template "repo/fragments/repoDescription" . }}
-
</section>
-
<section class="min-h-screen flex flex-col drop-shadow-sm">
+
{{ template "repo/fragments/repoActions" .RepoInfo }}
+
</div>
+
{{ template "repo/fragments/repoDescription" . }}
+
</section>
+
+
<section
+
class="min-h-screen w-full flex flex-col drop-shadow-sm"
+
>
<nav class="w-full pl-4 overflow-auto">
<div class="flex z-60">
{{ $activeTabStyles := "-mb-px bg-white dark:bg-gray-800" }}
···
{{ end }}
</div>
</nav>
+
{{ if .Languages }}
+
<div
+
class="bg-white dark:bg-gray-800 rounded-t overflow-hidden relative w-full drop-shadow-sm"
+
>
+
{{ block "repoLanguages" . }}{{ end }}
+
</div>
+
{{ end }}
<section
-
class="bg-white dark:bg-gray-800 p-6 rounded relative w-full mx-auto drop-shadow-sm dark:text-white"
+
class="bg-white dark:bg-gray-800 p-6 {{ if .Languages }}rounded-b{{ else }}rounded{{ end}} relative w-full drop-shadow-sm dark:text-white"
>
{{ block "repoContent" . }}{{ end }}
</section>
+11
appview/pages/templates/repo/index.html
···
{{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo) }}
{{ end }}
+
{{ define "repoLanguages" }}
+
<div class="flex gap-[3px]">
+
{{ range $value := .Languages }}
+
<div
+
title="{{ $value.Name }} {{ printf "%.1f" $value.Percentage }}%"
+
class="h-2"
+
style="background-color: {{ $value.Color }}; width: {{ $value.Percentage }}%"
+
></div>
+
{{ end }}
+
</div>
+
{{ end }}
{{ define "repoContent" }}
<main>