Skip to content

Commit 995d265

Browse files
committed
Fix edge cases with special langauges
1 parent d76f7a1 commit 995d265

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

models/repo_language_stats.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,38 @@ func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
5555
langPerc := make(map[string]float32)
5656
var otherPerc float32 = 100
5757
var total int64
58+
// Check that repository has at least one non-special language
59+
var skipSpecial bool
5860
for _, stat := range stats {
59-
// Exclude specific languages from percentage calculation
60-
if _, ok := specialLanguages[stat.Language]; ok && len(stats) > 1 {
61-
continue
61+
if _, ok := specialLanguages[stat.Language]; !ok {
62+
skipSpecial = true
63+
break
6264
}
63-
total += stat.Size
6465
}
6566
for _, stat := range stats {
6667
// Exclude specific languages from percentage calculation
67-
if _, ok := specialLanguages[stat.Language]; ok && len(stats) > 1 {
68+
if _, ok := specialLanguages[stat.Language]; ok && skipSpecial {
6869
continue
6970
}
70-
perc := float32(math.Round(float64(stat.Size)/float64(total)*1000) / 10)
71-
if perc <= 0.1 {
72-
continue
71+
total += stat.Size
72+
}
73+
if total > 0 {
74+
for _, stat := range stats {
75+
// Exclude specific languages from percentage calculation
76+
if _, ok := specialLanguages[stat.Language]; ok && skipSpecial {
77+
continue
78+
}
79+
perc := float32(math.Round(float64(stat.Size)/float64(total)*1000) / 10)
80+
if perc <= 0.1 {
81+
continue
82+
}
83+
otherPerc -= perc
84+
langPerc[stat.Language] = perc
7385
}
74-
otherPerc -= perc
75-
langPerc[stat.Language] = perc
86+
otherPerc = float32(math.Round(float64(otherPerc)*10) / 10)
87+
} else {
88+
otherPerc = 100
7689
}
77-
otherPerc = float32(math.Round(float64(otherPerc)*10) / 10)
7890
if otherPerc > 0 {
7991
langPerc["other"] = otherPerc
8092
}

modules/git/repo_language_stats.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
6969
return nil, err
7070
}
7171

72+
if len(sizes) == 0 {
73+
sizes["other"] = 0
74+
}
75+
7276
return sizes, nil
7377
}
7478

modules/indexer/stats/indexer_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ func TestRepoStatsIndex(t *testing.T) {
3434

3535
repo, err := models.GetRepositoryByID(1)
3636
assert.NoError(t, err)
37+
status, err := repo.GetIndexerStatus(models.RepoIndexerTypeStats)
38+
assert.NoError(t, err)
39+
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.CommitSha)
3740
langs, err := repo.GetTopLanguageStats(5)
3841
assert.NoError(t, err)
3942
assert.Len(t, langs, 1)

0 commit comments

Comments
 (0)