#4644 文件句柄没有及时关闭、用户代码行统计优化优化合入

Merged
chenyifan01 merged 1 commits from zouap into V20230808.patch 8 months ago
  1. +0
    -4
      models/repo_activity_custom.go
  2. +75
    -84
      models/user_business_analysis.go
  3. +3
    -1
      modules/git/blame.go
  4. +37
    -26
      routers/repo/ai_model_manage.go

+ 0
- 4
models/repo_activity_custom.go View File

@@ -238,12 +238,8 @@ func GetAllUserPublicRepoKPIStats(startTime time.Time, endTime time.Time) (map[s
CommitLines: 0,
}
}
if value.Email == "1250125907@qq.com" || value.Email == "peiyongyu-34@163.com" {
log.Info("repo path=" + repository.RepoPath())
}
authors[key].Commits += value.Commits
authors[key].CommitLines += value.CommitLines

}

}


+ 75
- 84
models/user_business_analysis.go View File

@@ -10,6 +10,7 @@ import (
"strings"
"time"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
@@ -345,8 +346,8 @@ func QueryUserStaticDataForUserDefine(opts *UserBusinessAnalysisQueryOptions, wi
log.Info("query commit code errr.")
} else {
log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
CommitCodeSizeMapJson, _ := json.Marshal(CommitCodeSizeMap)
log.Info("CommitCodeSizeMapJson=" + string(CommitCodeSizeMapJson))
//CommitCodeSizeMapJson, _ := json.Marshal(CommitCodeSizeMap)
//log.Info("CommitCodeSizeMapJson=" + string(CommitCodeSizeMapJson))
}
CommitDatasetSizeMap, CommitDatasetNumMap, _ := queryDatasetSize(start_unix, end_unix)
SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
@@ -366,7 +367,7 @@ func QueryUserStaticDataForUserDefine(opts *UserBusinessAnalysisQueryOptions, wi
defer statictisSess.Close()

cond := "type != 1 and is_active=true"
count, err := sess.Where(cond).Count(new(User))
count, _ := sess.Where(cond).Count(new(User))

ParaWeight := getParaWeight()
ResultList := make([]*UserBusinessAnalysis, 0)
@@ -377,10 +378,10 @@ func QueryUserStaticDataForUserDefine(opts *UserBusinessAnalysisQueryOptions, wi
userList := make([]*User, 0)
sess.Find(&userList)

for i, userRecord := range userList {
for _, userRecord := range userList {
var dateRecord UserBusinessAnalysis
dateRecord.ID = userRecord.ID
log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
//log.Info("i=" + fmt.Sprint(i) + " userName=" + userRecord.Name)
dateRecord.CountDate = CountDate.Unix()
dateRecord.DataDate = DataDate
dateRecord.Email = userRecord.Email
@@ -573,6 +574,37 @@ func refreshUserStaticTable(wikiCountMap map[string]int, tableName string, pageS

statictisSess := xStatistic.NewSession()
defer statictisSess.Close()

var CommitCodeSizeMap map[string]*git.UserKPIStats
var err error
var existCommitCodeSize map[int64]int
if tableName == "user_business_analysis_all" || tableName == "user_business_analysis_current_year" {

oneDayStartTime := pageEndTime.AddDate(0, 0, -1)
if oneDayStartTime.Format("2006-01-02") == pageStartTime.Format("2006-01-02") {
existCommitCodeSize = make(map[int64]int, 0)
} else {
existCommitCodeSize = queryCommitCodeSizeFromDb("public." + tableName)
}
log.Info("GetAllUserKPIStats oneDayStartTime=" + oneDayStartTime.Format("2006-01-02 15:04:05"))
log.Info("GetAllUserKPIStats pageEndTime=" + pageEndTime.Format("2006-01-02 15:04:05"))
log.Info("existCommitCodeSize len=" + fmt.Sprint(len(existCommitCodeSize)))
CommitCodeSizeMap, err = GetAllUserKPIStats(oneDayStartTime, pageEndTime)
if err != nil {
log.Info("query commit code errr.")
} else {
log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
}
} else {
existCommitCodeSize = make(map[int64]int, 0)
CommitCodeSizeMap, err = GetAllUserKPIStats(pageStartTime, pageEndTime)
if err != nil {
log.Info("query commit code errr.")
} else {
log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
}
}

log.Info("truncate all data from table: " + tableName)
statictisSess.Exec("TRUNCATE TABLE " + tableName)

@@ -593,15 +625,7 @@ func refreshUserStaticTable(wikiCountMap map[string]int, tableName string, pageS
FocusRepoCountMap := queryWatch(start_unix, end_unix)
StarRepoCountMap := queryStar(start_unix, end_unix)
WatchedCountMap, WatchOtherMap := queryFollow(start_unix, end_unix)
CommitCodeSizeMap, err := GetAllUserKPIStats(pageStartTime, pageEndTime)
if err != nil {
log.Info("query commit code errr.")
} else {
log.Info("query commit code size, len=" + fmt.Sprint(len(CommitCodeSizeMap)))
//CommitCodeSizeMapJson, _ := json.Marshal(CommitCodeSizeMap)
//log.Info("CommitCodeSizeMapJson=" + string(CommitCodeSizeMapJson))
}
//CommitCodeSizeMap := queryCommitCodeSize(StartTimeNextDay.Unix(), EndTimeNextDay.Unix())

CommitDatasetSizeMap, CommitDatasetNumMap, _ := queryDatasetSize(start_unix, end_unix)
SolveIssueCountMap := querySolveIssue(start_unix, end_unix)
CreateRepoCountMap, _, _ := queryUserCreateRepo(start_unix, end_unix)
@@ -659,11 +683,10 @@ func refreshUserStaticTable(wikiCountMap map[string]int, tableName string, pageS
dateRecordAll.StarRepoCount = getMapValue(dateRecordAll.ID, StarRepoCountMap)
dateRecordAll.WatchedCount = getMapValue(dateRecordAll.ID, WatchedCountMap)
if _, ok := CommitCodeSizeMap[dateRecordAll.Email]; !ok {
dateRecordAll.CommitCodeSize = 0
dateRecordAll.CommitCodeSize = getMapValue(dateRecordAll.ID, existCommitCodeSize)
} else {
dateRecordAll.CommitCodeSize = int(CommitCodeSizeMap[dateRecordAll.Email].CommitLines)
dateRecordAll.CommitCodeSize = int(CommitCodeSizeMap[dateRecordAll.Email].CommitLines) + getMapValue(dateRecordAll.ID, existCommitCodeSize)
}
//dateRecordAll.CommitCodeSize = getMapValue(dateRecordAll.ID, CommitCodeSizeMap)
dateRecordAll.CommitDatasetSize = getMapValue(dateRecordAll.ID, CommitDatasetSizeMap)
dateRecordAll.CommitDatasetNum = getMapValue(dateRecordAll.ID, CommitDatasetNumMap)
dateRecordAll.SolveIssueCount = getMapValue(dateRecordAll.ID, SolveIssueCountMap)
@@ -1787,6 +1810,41 @@ func queryMostActiveCommitAction(start_unix int64, end_unix int64) map[int64]map
return mostActiveMap
}

func queryCommitCodeSizeFromDb(tableName string) map[int64]int {
statictisSess := xStatistic.NewSession()
defer statictisSess.Close()
resultMap := make(map[int64]int)
count, err := statictisSess.Table(tableName).Count()
if err != nil {
log.Info("query " + tableName + " error. return." + err.Error())
return resultMap
}
var indexTotal int64
indexTotal = 0
for {
commit_code_sizeList, err := statictisSess.QueryInterface("select id,commit_code_size from " + tableName + " order by id asc limit " + fmt.Sprint(PAGE_SIZE) + " offset " + fmt.Sprint(indexTotal))
if err != nil {
log.Info("error:" + err.Error())
continue
}
log.Info("query " + tableName + " size=" + fmt.Sprint(len(commit_code_sizeList)))
for _, record := range commit_code_sizeList {
userId := convertInterfaceToInt64(record["id"])
commit_code_size := convertInterfaceToInt64(record["commit_code_size"])
if _, ok := resultMap[userId]; !ok {
resultMap[userId] = int(commit_code_size)
} else {
resultMap[userId] += int(commit_code_size)
}
}
indexTotal += PAGE_SIZE
if indexTotal >= count {
break
}
}
return resultMap
}

func queryCommitAction(start_unix int64, end_unix int64, actionType int64) map[int64]int {
sess := x.NewSession()
defer sess.Close()
@@ -2130,39 +2188,6 @@ func queryRecommedImage(start_unix int64, end_unix int64) map[int64]int {
return userIdImageMap
}

func queryAllImage() (map[int64]int64, map[int64]int64) {
sess := x.NewSession()
defer sess.Close()
imageUserIdMap := make(map[int64]int64)
userIdDImageMap := make(map[int64]int64)
count, err := sess.Count(new(Image))
if err != nil {
log.Info("query image error. return.")
return imageUserIdMap, userIdDImageMap
}
var indexTotal int64
indexTotal = 0
for {
sess.Select("id,uid").Table(new(Image)).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
imageList := make([]*Image, 0)
sess.Find(&imageList)
log.Info("query imageList size=" + fmt.Sprint(len(imageList)))
for _, imageRecord := range imageList {
imageUserIdMap[imageRecord.ID] = imageRecord.UID
if _, ok := userIdDImageMap[imageRecord.UID]; !ok {
userIdDImageMap[imageRecord.UID] = 1
} else {
userIdDImageMap[imageRecord.UID] += 1
}
}
indexTotal += PAGE_SIZE
if indexTotal >= count {
break
}
}
return imageUserIdMap, userIdDImageMap
}

func queryDatasetStars(start_unix int64, end_unix int64) (map[int64]int, map[int64]int) {
sess := x.NewSession()
defer sess.Close()
@@ -2425,40 +2450,6 @@ func queryLoginCount(start_unix int64, end_unix int64) map[int64]int {
return resultMap
}

func queryCommitCodeSize(start_unix int64, end_unix int64) map[int64]int {
statictisSess := xStatistic.NewSession()
defer statictisSess.Close()

resultMap := make(map[int64]int)
cond := "count_date>=" + fmt.Sprint(start_unix) + " and count_date<=" + fmt.Sprint(end_unix)
count, err := statictisSess.Where(cond).Count(new(UserBusinessAnalysis))
if err != nil {
log.Info("query commit code size error. return.")
return resultMap
}
var indexTotal int64
indexTotal = 0
for {
statictisSess.Select("id,commit_code_size").Table("user_business_analysis").Where(cond).OrderBy("id asc").Limit(PAGE_SIZE, int(indexTotal))
userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0)
statictisSess.Find(&userBusinessAnalysisList)
log.Info("query user login size=" + fmt.Sprint(len(userBusinessAnalysisList)))
for _, analysisRecord := range userBusinessAnalysisList {
if _, ok := resultMap[analysisRecord.ID]; !ok {
resultMap[analysisRecord.ID] = analysisRecord.CommitCodeSize
} else {
resultMap[analysisRecord.ID] += analysisRecord.CommitCodeSize
}
}
indexTotal += PAGE_SIZE
if indexTotal >= count {
break
}
}
log.Info("user commit code size=" + fmt.Sprint(len(resultMap)))
return resultMap
}

func queryUserModel(start_unix int64, end_unix int64) map[int64]int {
sess := x.NewSession()
defer sess.Close()


+ 3
- 1
modules/git/blame.go View File

@@ -66,7 +66,9 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {
}
} else if line[0] == '\t' {
code := line[1:]

if blamePart == nil {
continue
}
blamePart.Lines = append(blamePart.Lines, code)
}
}


+ 37
- 26
routers/repo/ai_model_manage.go View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
@@ -671,26 +672,31 @@ func MinioDownloadManyFile(path string, ctx *context.Context, returnFileName str
ctx.ServerError("download file failed:", err)
return
} else {
defer body.Close()
p := make([]byte, 1024)
var readErr error
var readCount int
// 读取对象内容
for {
readCount, readErr = body.Read(p)
if readCount > 0 {
fDest.Write(p[:readCount])
}
if readErr != nil {
break
}
}
bodyReader(body, fDest)
}
}
}

}

func bodyReader(body io.ReadCloser, fDest io.Writer) {
defer body.Close()
p := make([]byte, 1024)
var readErr error
var readCount int

for {
readCount, readErr = body.Read(p)
if readCount > 0 {
fDest.Write(p[:readCount])
}
if readErr != nil {
break
}
}
}

func downloadFromCloudBrainOne(path string, task *models.AiModelManage, ctx *context.Context, id string) {
allFile, err := storage.GetAllObjectByBucketAndPrefixMinio(setting.Attachment.Minio.Bucket, path)
if err == nil {
@@ -726,25 +732,30 @@ func ObsDownloadManyFile(path string, ctx *context.Context, returnFileName strin
ctx.ServerError("download file failed:", err)
return
} else {
defer body.Close()
p := make([]byte, 1024)
var readErr error
var readCount int
// 读取对象内容
for {
readCount, readErr = body.Read(p)
if readCount > 0 {
fDest.Write(p[:readCount])
}
if readErr != nil {
break
}
}
obsBodyReader(body, fDest)
}
}
}
}

func obsBodyReader(body io.ReadCloser, fDest io.Writer) {
defer body.Close()
p := make([]byte, 1024)
var readErr error
var readCount int

for {
readCount, readErr = body.Read(p)
if readCount > 0 {
fDest.Write(p[:readCount])
}
if readErr != nil {
break
}
}
}

func downloadFromCloudBrainTwo(path string, task *models.AiModelManage, ctx *context.Context, id string) {
allFile, err := storage.GetAllObjectByBucketAndPrefix(setting.Bucket, path)
if err == nil {


Loading…
Cancel
Save