diff --git a/models/custom_migrations.go b/models/custom_migrations.go index 0b9a0d552..ec0e5f75e 100644 --- a/models/custom_migrations.go +++ b/models/custom_migrations.go @@ -1,6 +1,8 @@ package models import ( + "fmt" + "code.gitea.io/gitea/modules/log" "xorm.io/xorm" ) @@ -10,11 +12,19 @@ type CustomMigration struct { Migrate func(*xorm.Engine) error } +type CustomMigrationStatic struct { + Description string + Migrate func(*xorm.Engine, *xorm.Engine) error +} + var customMigrations = []CustomMigration{ {"Custom v1 Topic struct change to support chinese", syncTopicStruct}, } -var customMigrationsStatic = []CustomMigration{} +var customMigrationsStatic = []CustomMigrationStatic{ + {"Alter user static table field type ", alterUserStaticTable}, + {"Delete zuzhi user history data ", deleteNotDisplayUser}, +} func MigrateCustom(x *xorm.Engine) { @@ -29,10 +39,10 @@ func MigrateCustom(x *xorm.Engine) { } -func MigrateCustomStatic(x *xorm.Engine) { +func MigrateCustomStatic(x *xorm.Engine, static *xorm.Engine) { for _, m := range customMigrationsStatic { log.Info("Migration: %s", m.Description) - if err := m.Migrate(x); err != nil { + if err := m.Migrate(x, static); err != nil { log.Error("Migration: %v", err) @@ -47,3 +57,29 @@ func syncTopicStruct(x *xorm.Engine) error { _, err := x.Exec(query) return err } + +func alterUserStaticTable(x *xorm.Engine, static *xorm.Engine) error { + alterSql := "alter table public.user_business_analysis alter column open_i_index type double precision" + + _, err := static.Exec(alterSql) + return err + +} + +func deleteNotDisplayUser(x *xorm.Engine, static *xorm.Engine) error { + + querySQL := "select id,name from public.user where type=1" + rows, err := x.Query(querySQL) + if err != nil { + log.Info("select db failed,err:", err) + return err + } + + for i, userRow := range rows { + log.Info("delete zuzi user, i=" + fmt.Sprint(i) + " userName=" + string(userRow["name"])) + deleteSql := "delete from user_business_analysis where id=" + string(userRow["id"]) + " and name='" + string(userRow["name"]) + "'" + static.Exec(deleteSql) + } + + return nil +} diff --git a/models/models.go b/models/models.go index 86a091bca..4d39e2f14 100755 --- a/models/models.go +++ b/models/models.go @@ -230,7 +230,7 @@ func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err e if err = newEngine(ctx, migrateFunc, xStatistic, tablesStatistic, setting.DatabaseStatistic); err != nil { return fmt.Errorf("newEngine statistic failed: %v", err) } - MigrateCustomStatic(xStatistic) + MigrateCustomStatic(x, xStatistic) HasEngine = true diff --git a/models/user_business_analysis.go b/models/user_business_analysis.go index a4a155ba7..f30631ae1 100644 --- a/models/user_business_analysis.go +++ b/models/user_business_analysis.go @@ -1,6 +1,7 @@ package models import ( + "encoding/json" "fmt" "time" @@ -64,7 +65,7 @@ type UserBusinessAnalysis struct { LoginCount int `xorm:"NOT NULL DEFAULT 0"` //openi index - OpenIIndex int `xorm:"NOT NULL DEFAULT 0"` + OpenIIndex float64 `xorm:"NOT NULL DEFAULT 0"` //user Email string `xorm:"NOT NULL"` @@ -267,6 +268,7 @@ func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime ti SolveIssueCountMap := querySolveIssue(start_unix, end_unix) CreateRepoCountMap := queryUserCreateRepo(start_unix, end_unix) LoginCountMap := queryLoginCount(start_unix, end_unix) + OpenIIndexMap := queryUserRepoOpenIIndex(start_unix, end_unix) statictisSess := xStatistic.NewSession() defer statictisSess.Close() @@ -361,6 +363,12 @@ func CounDataByDate(wikiCountMap map[string]int, startTime time.Time, endTime ti dateRecord.LoginCount = LoginCountMap[dateRecord.ID] } + if _, ok := OpenIIndexMap[dateRecord.ID]; !ok { + dateRecord.OpenIIndex = 0 + } else { + dateRecord.OpenIIndex = OpenIIndexMap[dateRecord.ID] + } + dateRecord.CommitModelCount = 0 statictisSess.Insert(&dateRecord) @@ -545,6 +553,62 @@ func queryUserCreateRepo(start_unix int64, end_unix int64) map[int64]int { return resultMap } +func queryUserRepoOpenIIndex(start_unix int64, end_unix int64) map[int64]float64 { + statictisSess := xStatistic.NewSession() + defer statictisSess.Close() + statictisSess.Select("repo_id,radar_total").Table("repo_statistic").Where("created_unix>=" + fmt.Sprint(start_unix) + " and created_unix<=" + fmt.Sprint(end_unix)) + repoStatisticList := make([]*RepoStatistic, 0) + statictisSess.Find(&repoStatisticList) + repoOpenIIndexMap := make(map[int64]float64) + log.Info("query repo_statistic size=" + fmt.Sprint(len(repoStatisticList))) + for _, repoRecord := range repoStatisticList { + if _, ok := repoOpenIIndexMap[repoRecord.RepoID]; !ok { + repoOpenIIndexMap[repoRecord.RepoID] = repoRecord.RadarTotal + } + } + + sess := x.NewSession() + defer sess.Close() + sess.Select("id,owner_id,name").Table("repository").Where("is_fork=false") + repoList := make([]*Repository, 0) + sess.Find(&repoList) + + userMap := make(map[int64]float64) + + log.Info("query Repository size=" + fmt.Sprint(len(repoList))) + for _, repoRecord := range repoList { + if _, ok := userMap[repoRecord.OwnerID]; !ok { + if _, ok := repoOpenIIndexMap[repoRecord.ID]; ok { + userMap[repoRecord.OwnerID] = repoOpenIIndexMap[repoRecord.ID] + } + } + } + + //query collaboration + sess.Select("repo_id,user_id,mode").Table("collaboration") + collaborationList := make([]*Collaboration, 0) + sess.Find(&collaborationList) + + log.Info("query collaborationList size=" + fmt.Sprint(len(collaborationList))) + + for _, collaborationRecord := range collaborationList { + if _, ok := userMap[collaborationRecord.UserID]; !ok { + if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok { + userMap[collaborationRecord.UserID] = repoOpenIIndexMap[collaborationRecord.RepoID] + } + } else { + if _, ok := repoOpenIIndexMap[collaborationRecord.RepoID]; ok { + userMap[collaborationRecord.UserID] += repoOpenIIndexMap[collaborationRecord.RepoID] + } + } + } + + userMapJson, _ := json.Marshal(userMap) + log.Info("userMapJson=" + string(userMapJson)) + + return userMap +} + func queryLoginCount(start_unix int64, end_unix int64) map[int64]int { statictisSess := xStatistic.NewSession() defer statictisSess.Close()