From b6f6d57b18e9cc23c6c9e9faa54bece8650b6bff Mon Sep 17 00:00:00 2001 From: zouap Date: Wed, 24 Nov 2021 10:38:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=BF=AE=E6=94=B9=E8=A1=A8?= =?UTF-8?q?=E7=BB=93=E6=A3=8D=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E7=BB=A7=E7=BB=AD=E4=BF=AE=E6=94=B9=E5=88=86=E9=A1=B5?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zouap --- models/custom_migrations.go | 9 ---- models/user_business_analysis.go | 70 ++++++++++++++++++++++++++++++ routers/repo/user_data_analysis.go | 11 +++-- 3 files changed, 77 insertions(+), 13 deletions(-) diff --git a/models/custom_migrations.go b/models/custom_migrations.go index fe40fdf13..d0158530b 100644 --- a/models/custom_migrations.go +++ b/models/custom_migrations.go @@ -22,7 +22,6 @@ var customMigrations = []CustomMigration{ } var customMigrationsStatic = []CustomMigrationStatic{ - {"Alter user static table field type ", alterUserStaticTable}, {"Delete organization user history data ", deleteNotDisplayUser}, {"update issue_fixed_rate to 1 if num_issues is 0 ", updateIssueFixedRate}, } @@ -59,14 +58,6 @@ func syncTopicStruct(x *xorm.Engine) error { 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" diff --git a/models/user_business_analysis.go b/models/user_business_analysis.go index e5f92c07b..08be81241 100644 --- a/models/user_business_analysis.go +++ b/models/user_business_analysis.go @@ -153,6 +153,76 @@ func getLastCountDate() int64 { return pageStartTime.Unix() } +func QueryUserStaticDataAll(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysis, int64) { + log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll)) + + statictisSess := xStatistic.NewSession() + defer statictisSess.Close() + + resultMap := make(map[int64]*UserBusinessAnalysis) + + var newAndCond = builder.NewCond() + if len(opts.UserName) > 0 { + newAndCond = newAndCond.And( + builder.Like{"name", opts.UserName}, + ) + } + if !opts.IsAll { + newAndCond = newAndCond.And( + builder.Gte{"count_date": opts.StartTime}, + ) + newAndCond = newAndCond.And( + builder.Lte{"count_date": opts.EndTime}, + ) + } + + allCount, err := statictisSess.Where(newAndCond).Count(new(UserBusinessAnalysis)) + if err != nil { + log.Info("query error." + err.Error()) + return nil, 0 + } + log.Info("query return total:" + fmt.Sprint(allCount)) + pageSize := 200 + totalPage := int(allCount) / pageSize + + for i := 0; i <= int(totalPage); i++ { + userBusinessAnalysisList := make([]*UserBusinessAnalysis, 0) + if err := statictisSess.Table("user_business_analysis").Where(newAndCond).OrderBy("count_date desc").Limit(pageSize, i*pageSize). + Find(&userBusinessAnalysisList); err != nil { + return nil, 0 + } + log.Info("query " + fmt.Sprint(i+1) + " result size=" + fmt.Sprint(len(userBusinessAnalysisList))) + for _, userRecord := range userBusinessAnalysisList { + if _, ok := resultMap[userRecord.ID]; !ok { + resultMap[userRecord.ID] = userRecord + } else { + resultMap[userRecord.ID].CodeMergeCount += userRecord.CodeMergeCount + resultMap[userRecord.ID].CommitCount += userRecord.CommitCount + resultMap[userRecord.ID].IssueCount += userRecord.IssueCount + resultMap[userRecord.ID].CommentCount += userRecord.CommentCount + resultMap[userRecord.ID].FocusRepoCount += userRecord.FocusRepoCount + resultMap[userRecord.ID].StarRepoCount += userRecord.StarRepoCount + resultMap[userRecord.ID].WatchedCount += userRecord.WatchedCount + resultMap[userRecord.ID].CommitCodeSize += userRecord.CommitCodeSize + resultMap[userRecord.ID].CommitDatasetSize += userRecord.CommitDatasetSize + resultMap[userRecord.ID].CommitModelCount += userRecord.CommitModelCount + resultMap[userRecord.ID].SolveIssueCount += userRecord.SolveIssueCount + resultMap[userRecord.ID].EncyclopediasCount += userRecord.EncyclopediasCount + resultMap[userRecord.ID].CreateRepoCount += userRecord.CreateRepoCount + resultMap[userRecord.ID].LoginCount += userRecord.LoginCount + } + } + } + + userBusinessAnalysisReturnList := UserBusinessAnalysisList{} + for _, v := range resultMap { + userBusinessAnalysisReturnList = append(userBusinessAnalysisReturnList, v) + } + sort.Sort(userBusinessAnalysisReturnList) + log.Info("return size=" + fmt.Sprint(len(userBusinessAnalysisReturnList))) + return userBusinessAnalysisReturnList, allCount +} + func QueryUserStaticDataPage(opts *UserBusinessAnalysisQueryOptions) ([]*UserBusinessAnalysis, int64) { log.Info("query startTime =" + fmt.Sprint(opts.StartTime) + " endTime=" + fmt.Sprint(opts.EndTime) + " isAll=" + fmt.Sprint(opts.IsAll)) diff --git a/routers/repo/user_data_analysis.go b/routers/repo/user_data_analysis.go index 8d7f72ce0..5a6b3d724 100755 --- a/routers/repo/user_data_analysis.go +++ b/routers/repo/user_data_analysis.go @@ -76,11 +76,10 @@ func QueryUserStaticDataPage(ctx *context.Context) { EndTime: endTime.Unix(), IsAll: isAll, } - mapInterface := make(map[string]interface{}) - re, count := models.QueryUserStaticDataPage(pageOpts) - mapInterface["data"] = re - mapInterface["count"] = count + if IsReturnFile { + re, count := models.QueryUserStaticDataAll(pageOpts) + log.Info("return count=" + fmt.Sprint(count)) //writer exec file. xlsx := excelize.NewFile() sheetName := ctx.Tr("user.static.sheetname") @@ -163,6 +162,10 @@ func QueryUserStaticDataPage(ctx *context.Context) { } } else { + mapInterface := make(map[string]interface{}) + re, count := models.QueryUserStaticDataPage(pageOpts) + mapInterface["data"] = re + mapInterface["count"] = count ctx.JSON(http.StatusOK, mapInterface) } }