Browse Source

提交代码,修改用户运营统计相关Bug

Signed-off-by: zouap <zouap@pcl.ac.cn>
tags/v1.21.12.1
zouap 3 years ago
parent
commit
2531431009
3 changed files with 97 additions and 4 deletions
  1. +30
    -3
      models/custom_migrations.go
  2. +3
    -1
      models/models.go
  3. +64
    -0
      models/user_business_analysis.go

+ 30
- 3
models/custom_migrations.go View File

@@ -1,6 +1,8 @@
package models

import (
"fmt"

"code.gitea.io/gitea/modules/log"
"xorm.io/xorm"
)
@@ -10,11 +12,18 @@ 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{
{"Delete zuzhi user history data ", deleteNotDisplayUser},
}

func MigrateCustom(x *xorm.Engine) {

@@ -29,10 +38,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 +56,21 @@ func syncTopicStruct(x *xorm.Engine) error {
_, err := x.Exec(query)
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
}

+ 3
- 1
models/models.go View File

@@ -133,6 +133,8 @@ func init() {
new(FileChunk),
new(BlockChain),
new(RecommendOrg),
new(AiModelManage),
new(TrainjobConfigDetail),
)

tablesStatistic = append(tablesStatistic,
@@ -230,7 +232,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



+ 64
- 0
models/user_business_analysis.go View File

@@ -1,6 +1,7 @@
package models

import (
"encoding/json"
"fmt"
"time"

@@ -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 = int(OpenIIndexMap[dateRecord.ID] * 100)
}

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()


Loading…
Cancel
Save