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.
216 lines
5.5 KiB
216 lines
5.5 KiB
5 years ago
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package repo
|
||
5 years ago
|
|
||
|
import (
|
||
|
"math"
|
||
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
|
|
||
5 years ago
|
"github.com/go-enry/go-enry/v2"
|
||
5 years ago
|
)
|
||
|
|
||
|
// LanguageStat describes language statistics of a repository
|
||
|
type LanguageStat struct {
|
||
|
ID int64 `xorm:"pk autoincr"`
|
||
|
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
||
|
CommitID string
|
||
|
IsPrimary bool
|
||
4 years ago
|
Language string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"`
|
||
5 years ago
|
Percentage float32 `xorm:"-"`
|
||
|
Size int64 `xorm:"NOT NULL DEFAULT 0"`
|
||
5 years ago
|
Color string `xorm:"-"`
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX CREATED"`
|
||
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(LanguageStat))
|
||
|
}
|
||
|
|
||
5 years ago
|
// LanguageStatList defines a list of language statistics
|
||
|
type LanguageStatList []*LanguageStat
|
||
|
|
||
3 years ago
|
// LoadAttributes loads attributes
|
||
|
func (stats LanguageStatList) LoadAttributes() {
|
||
5 years ago
|
for i := range stats {
|
||
|
stats[i].Color = enry.GetColor(stats[i].Language)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func (stats LanguageStatList) getLanguagePercentages() map[string]float32 {
|
||
|
langPerc := make(map[string]float32)
|
||
|
var otherPerc float32 = 100
|
||
|
var total int64
|
||
5 years ago
|
|
||
5 years ago
|
for _, stat := range stats {
|
||
|
total += stat.Size
|
||
|
}
|
||
|
if total > 0 {
|
||
|
for _, stat := range stats {
|
||
|
perc := float32(math.Round(float64(stat.Size)/float64(total)*1000) / 10)
|
||
|
if perc <= 0.1 {
|
||
|
continue
|
||
|
}
|
||
|
otherPerc -= perc
|
||
|
langPerc[stat.Language] = perc
|
||
|
}
|
||
|
otherPerc = float32(math.Round(float64(otherPerc)*10) / 10)
|
||
|
}
|
||
|
if otherPerc > 0 {
|
||
|
langPerc["other"] = otherPerc
|
||
|
}
|
||
|
return langPerc
|
||
|
}
|
||
|
|
||
3 years ago
|
func getLanguageStats(e db.Engine, repo *Repository) (LanguageStatList, error) {
|
||
5 years ago
|
stats := make(LanguageStatList, 0, 6)
|
||
5 years ago
|
if err := e.Where("`repo_id` = ?", repo.ID).Desc("`size`").Find(&stats); err != nil {
|
||
5 years ago
|
return nil, err
|
||
|
}
|
||
|
return stats, nil
|
||
|
}
|
||
|
|
||
|
// GetLanguageStats returns the language statistics for a repository
|
||
3 years ago
|
func GetLanguageStats(repo *Repository) (LanguageStatList, error) {
|
||
|
return getLanguageStats(db.GetEngine(db.DefaultContext), repo)
|
||
5 years ago
|
}
|
||
|
|
||
|
// GetTopLanguageStats returns the top language statistics for a repository
|
||
3 years ago
|
func GetTopLanguageStats(repo *Repository, limit int) (LanguageStatList, error) {
|
||
|
stats, err := getLanguageStats(db.GetEngine(db.DefaultContext), repo)
|
||
5 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
5 years ago
|
perc := stats.getLanguagePercentages()
|
||
5 years ago
|
topstats := make(LanguageStatList, 0, limit)
|
||
|
var other float32
|
||
|
for i := range stats {
|
||
5 years ago
|
if _, ok := perc[stats[i].Language]; !ok {
|
||
|
continue
|
||
|
}
|
||
5 years ago
|
if stats[i].Language == "other" || len(topstats) >= limit {
|
||
5 years ago
|
other += perc[stats[i].Language]
|
||
5 years ago
|
continue
|
||
|
}
|
||
5 years ago
|
stats[i].Percentage = perc[stats[i].Language]
|
||
5 years ago
|
topstats = append(topstats, stats[i])
|
||
|
}
|
||
|
if other > 0 {
|
||
|
topstats = append(topstats, &LanguageStat{
|
||
|
RepoID: repo.ID,
|
||
|
Language: "other",
|
||
|
Color: "#cccccc",
|
||
|
Percentage: float32(math.Round(float64(other)*10) / 10),
|
||
|
})
|
||
|
}
|
||
3 years ago
|
topstats.LoadAttributes()
|
||
5 years ago
|
return topstats, nil
|
||
|
}
|
||
|
|
||
|
// UpdateLanguageStats updates the language statistics for repository
|
||
3 years ago
|
func UpdateLanguageStats(repo *Repository, commitID string, stats map[string]int64) error {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
|
sess := db.GetEngine(ctx)
|
||
5 years ago
|
|
||
3 years ago
|
oldstats, err := getLanguageStats(sess, repo)
|
||
5 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
var topLang string
|
||
5 years ago
|
var s int64
|
||
|
for lang, size := range stats {
|
||
|
if size > s {
|
||
|
s = size
|
||
5 years ago
|
topLang = strings.ToLower(lang)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
for lang, size := range stats {
|
||
5 years ago
|
upd := false
|
||
|
llang := strings.ToLower(lang)
|
||
|
for _, s := range oldstats {
|
||
|
// Update already existing language
|
||
|
if strings.ToLower(s.Language) == llang {
|
||
|
s.CommitID = commitID
|
||
|
s.IsPrimary = llang == topLang
|
||
5 years ago
|
s.Size = size
|
||
|
if _, err := sess.ID(s.ID).Cols("`commit_id`", "`size`", "`is_primary`").Update(s); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
upd = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
// Insert new language
|
||
|
if !upd {
|
||
|
if _, err := sess.Insert(&LanguageStat{
|
||
5 years ago
|
RepoID: repo.ID,
|
||
|
CommitID: commitID,
|
||
|
IsPrimary: llang == topLang,
|
||
|
Language: lang,
|
||
|
Size: size,
|
||
5 years ago
|
}); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// Delete old languages
|
||
5 years ago
|
statsToDelete := make([]int64, 0, len(oldstats))
|
||
|
for _, s := range oldstats {
|
||
|
if s.CommitID != commitID {
|
||
|
statsToDelete = append(statsToDelete, s.ID)
|
||
|
}
|
||
|
}
|
||
|
if len(statsToDelete) > 0 {
|
||
|
if _, err := sess.In("`id`", statsToDelete).Delete(&LanguageStat{}); err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// Update indexer status
|
||
3 years ago
|
if err = updateIndexerStatus(sess, repo, RepoIndexerTypeStats, commitID); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
return committer.Commit()
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
// CopyLanguageStat Copy originalRepo language stat information to destRepo (use for forked repo)
|
||
|
func CopyLanguageStat(originalRepo, destRepo *Repository) error {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
|
sess := db.GetEngine(ctx)
|
||
|
|
||
5 years ago
|
RepoLang := make(LanguageStatList, 0, 6)
|
||
5 years ago
|
if err := sess.Where("`repo_id` = ?", originalRepo.ID).Desc("`size`").Find(&RepoLang); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
if len(RepoLang) > 0 {
|
||
|
for i := range RepoLang {
|
||
|
RepoLang[i].ID = 0
|
||
|
RepoLang[i].RepoID = destRepo.ID
|
||
|
RepoLang[i].CreatedUnix = timeutil.TimeStampNow()
|
||
|
}
|
||
4 years ago
|
// update destRepo's indexer status
|
||
5 years ago
|
tmpCommitID := RepoLang[0].CommitID
|
||
3 years ago
|
if err := updateIndexerStatus(sess, destRepo, RepoIndexerTypeStats, tmpCommitID); err != nil {
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
if _, err := sess.Insert(&RepoLang); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
3 years ago
|
return committer.Commit()
|
||
5 years ago
|
}
|