You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

language_stats.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "context"
  7. "math"
  8. "strings"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "github.com/go-enry/go-enry/v2"
  12. )
  13. // LanguageStat describes language statistics of a repository
  14. type LanguageStat struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  17. CommitID string
  18. IsPrimary bool
  19. Language string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"`
  20. Percentage float32 `xorm:"-"`
  21. Size int64 `xorm:"NOT NULL DEFAULT 0"`
  22. Color string `xorm:"-"`
  23. CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
  24. }
  25. func init() {
  26. db.RegisterModel(new(LanguageStat))
  27. }
  28. // LanguageStatList defines a list of language statistics
  29. type LanguageStatList []*LanguageStat
  30. // LoadAttributes loads attributes
  31. func (stats LanguageStatList) LoadAttributes() {
  32. for i := range stats {
  33. stats[i].Color = enry.GetColor(stats[i].Language)
  34. }
  35. }
  36. func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
  37. langPerc := make(map[string]float32)
  38. var otherPerc float32 = 100
  39. var total int64
  40. for _, stat := range stats {
  41. total += stat.Size
  42. }
  43. if total > 0 {
  44. for _, stat := range stats {
  45. perc := float32(math.Round(float64(stat.Size)/float64(total)*1000) / 10)
  46. if perc <= 0.1 {
  47. continue
  48. }
  49. otherPerc -= perc
  50. langPerc[stat.Language] = perc
  51. }
  52. otherPerc = float32(math.Round(float64(otherPerc)*10) / 10)
  53. }
  54. if otherPerc > 0 {
  55. langPerc["other"] = otherPerc
  56. }
  57. return langPerc
  58. }
  59. // GetLanguageStats returns the language statistics for a repository
  60. func GetLanguageStats(ctx context.Context, repo *Repository) (LanguageStatList, error) {
  61. stats := make(LanguageStatList, 0, 6)
  62. if err := db.GetEngine(ctx).Where("`repo_id` = ?", repo.ID).Desc("`size`").Find(&stats); err != nil {
  63. return nil, err
  64. }
  65. return stats, nil
  66. }
  67. // GetTopLanguageStats returns the top language statistics for a repository
  68. func GetTopLanguageStats(repo *Repository, limit int) (LanguageStatList, error) {
  69. stats, err := GetLanguageStats(db.DefaultContext, repo)
  70. if err != nil {
  71. return nil, err
  72. }
  73. perc := stats.getLanguagePercentages()
  74. topstats := make(LanguageStatList, 0, limit)
  75. var other float32
  76. for i := range stats {
  77. if _, ok := perc[stats[i].Language]; !ok {
  78. continue
  79. }
  80. if stats[i].Language == "other" || len(topstats) >= limit {
  81. other += perc[stats[i].Language]
  82. continue
  83. }
  84. stats[i].Percentage = perc[stats[i].Language]
  85. topstats = append(topstats, stats[i])
  86. }
  87. if other > 0 {
  88. topstats = append(topstats, &LanguageStat{
  89. RepoID: repo.ID,
  90. Language: "other",
  91. Color: "#cccccc",
  92. Percentage: float32(math.Round(float64(other)*10) / 10),
  93. })
  94. }
  95. topstats.LoadAttributes()
  96. return topstats, nil
  97. }
  98. // UpdateLanguageStats updates the language statistics for repository
  99. func UpdateLanguageStats(repo *Repository, commitID string, stats map[string]int64) error {
  100. ctx, committer, err := db.TxContext()
  101. if err != nil {
  102. return err
  103. }
  104. defer committer.Close()
  105. sess := db.GetEngine(ctx)
  106. oldstats, err := GetLanguageStats(ctx, repo)
  107. if err != nil {
  108. return err
  109. }
  110. var topLang string
  111. var s int64
  112. for lang, size := range stats {
  113. if size > s {
  114. s = size
  115. topLang = strings.ToLower(lang)
  116. }
  117. }
  118. for lang, size := range stats {
  119. upd := false
  120. llang := strings.ToLower(lang)
  121. for _, s := range oldstats {
  122. // Update already existing language
  123. if strings.ToLower(s.Language) == llang {
  124. s.CommitID = commitID
  125. s.IsPrimary = llang == topLang
  126. s.Size = size
  127. if _, err := sess.ID(s.ID).Cols("`commit_id`", "`size`", "`is_primary`").Update(s); err != nil {
  128. return err
  129. }
  130. upd = true
  131. break
  132. }
  133. }
  134. // Insert new language
  135. if !upd {
  136. if err := db.Insert(ctx, &LanguageStat{
  137. RepoID: repo.ID,
  138. CommitID: commitID,
  139. IsPrimary: llang == topLang,
  140. Language: lang,
  141. Size: size,
  142. }); err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. // Delete old languages
  148. statsToDelete := make([]int64, 0, len(oldstats))
  149. for _, s := range oldstats {
  150. if s.CommitID != commitID {
  151. statsToDelete = append(statsToDelete, s.ID)
  152. }
  153. }
  154. if len(statsToDelete) > 0 {
  155. if _, err := sess.In("`id`", statsToDelete).Delete(&LanguageStat{}); err != nil {
  156. return err
  157. }
  158. }
  159. // Update indexer status
  160. if err = UpdateIndexerStatus(ctx, repo, RepoIndexerTypeStats, commitID); err != nil {
  161. return err
  162. }
  163. return committer.Commit()
  164. }
  165. // CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo)
  166. func CopyLanguageStat(originalRepo, destRepo *Repository) error {
  167. ctx, committer, err := db.TxContext()
  168. if err != nil {
  169. return err
  170. }
  171. defer committer.Close()
  172. RepoLang := make(LanguageStatList, 0, 6)
  173. if err := db.GetEngine(ctx).Where("`repo_id` = ?", originalRepo.ID).Desc("`size`").Find(&RepoLang); err != nil {
  174. return err
  175. }
  176. if len(RepoLang) > 0 {
  177. for i := range RepoLang {
  178. RepoLang[i].ID = 0
  179. RepoLang[i].RepoID = destRepo.ID
  180. RepoLang[i].CreatedUnix = timeutil.TimeStampNow()
  181. }
  182. // update destRepo's indexer status
  183. tmpCommitID := RepoLang[0].CommitID
  184. if err := UpdateIndexerStatus(ctx, destRepo, RepoIndexerTypeStats, tmpCommitID); err != nil {
  185. return err
  186. }
  187. if err := db.Insert(ctx, &RepoLang); err != nil {
  188. return err
  189. }
  190. }
  191. return committer.Commit()
  192. }