diff options
author | Lauris BH <lauris@nix.lv> | 2020-05-30 10:46:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-30 10:46:15 +0300 |
commit | ea4c139cd2f7e5174627a40aa8a9973fabf508ff (patch) | |
tree | bcdcfc9cb15c56799af03bacbae3d25790539a02 /modules | |
parent | 4395c607ed79985602a99dda251f090fbd2f5cf9 (diff) | |
download | gitea-ea4c139cd2f7e5174627a40aa8a9973fabf508ff.tar.gz gitea-ea4c139cd2f7e5174627a40aa8a9973fabf508ff.zip |
Change language statistics to save size instead of percentage (#11681)
* Change language statistics to save size instead of percentage in database
Co-Authored-By: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com>
* Do not exclude if only language
* Fix edge cases with special langauges
Co-authored-by: Cirno the Strongest <1447794+CirnoT@users.noreply.github.com>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo_language_stats.go | 25 | ||||
-rw-r--r-- | modules/indexer/stats/indexer_test.go | 3 |
2 files changed, 9 insertions, 19 deletions
diff --git a/modules/git/repo_language_stats.go b/modules/git/repo_language_stats.go index 8ff8fa20c1..d623d6f57d 100644 --- a/modules/git/repo_language_stats.go +++ b/modules/git/repo_language_stats.go @@ -8,7 +8,6 @@ import ( "bytes" "io" "io/ioutil" - "math" "code.gitea.io/gitea/modules/analyze" @@ -21,7 +20,7 @@ import ( const fileSizeLimit int64 = 16 * 1024 * 1024 // GetLanguageStats calculates language stats for git repository at specified commit -func (repo *Repository) GetLanguageStats(commitID string) (map[string]float32, error) { +func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, error) { r, err := git.PlainOpen(repo.Path) if err != nil { return nil, err @@ -43,7 +42,6 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]float32, e } sizes := make(map[string]int64) - var total int64 err = tree.Files().ForEach(func(f *object.File) error { if enry.IsVendor(f.Name) || enry.IsDotFile(f.Name) || enry.IsDocumentation(f.Name) || enry.IsConfiguration(f.Name) { @@ -60,11 +58,10 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]float32, e language := analyze.GetCodeLanguage(f.Name, content) if language == enry.OtherLanguage || language == "" { - return nil + language = "other" } sizes[language] += f.Size - total += f.Size return nil }) @@ -72,21 +69,11 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]float32, e return nil, err } - stats := make(map[string]float32) - var otherPerc float32 = 100 - for language, size := range sizes { - perc := float32(math.Round(float64(size)/float64(total)*1000) / 10) - if perc <= 0.1 { - continue - } - otherPerc -= perc - stats[language] = perc + if len(sizes) == 0 { + sizes["other"] = 0 } - otherPerc = float32(math.Round(float64(otherPerc)*10) / 10) - if otherPerc > 0 { - stats["other"] = otherPerc - } - return stats, nil + + return sizes, nil } func readFile(f *object.File, limit int64) ([]byte, error) { diff --git a/modules/indexer/stats/indexer_test.go b/modules/indexer/stats/indexer_test.go index 29d0f6dbe4..b60c6d9bb4 100644 --- a/modules/indexer/stats/indexer_test.go +++ b/modules/indexer/stats/indexer_test.go @@ -34,6 +34,9 @@ func TestRepoStatsIndex(t *testing.T) { repo, err := models.GetRepositoryByID(1) assert.NoError(t, err) + status, err := repo.GetIndexerStatus(models.RepoIndexerTypeStats) + assert.NoError(t, err) + assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.CommitSha) langs, err := repo.GetTopLanguageStats(5) assert.NoError(t, err) assert.Len(t, langs, 1) |