diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index bc71693b8..f78dab9ed 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -402,6 +402,25 @@ form.name_reserved = The username '%s' is reserved. form.name_pattern_not_allowed = The pattern '%s' is not allowed in a username. form.name_chars_not_allowed = User name '%s' contains invalid characters. +static.sheetname=User Analysis +static.id=ID +static.name=User Name +static.codemergecount=PR Count +static.issuecount=Issue Count +static.commentcount=Comment Count +static.focusrepocount=Focus Repo Count +static.starrepocount=Repo Star Count +static.logincount=Login Count +static.watchedcount=Watched Count +static.commitcodesize=Commit Code Line +static.solveissuecount=Solve Issue Count +static.encyclopediascount=Encyclopedias Count +static.createrepocount=Create Repo Count +static.openiindex=OpenI Index +static.registdate=Regist Date +static.countdate=Count Date + + [settings] profile = Profile account = Account diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index 6702a0434..0ecf1c1bf 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -405,6 +405,23 @@ form.name_reserved='%s' 用户名被保留。 form.name_pattern_not_allowed=用户名中不允许使用 "%s"。 form.name_chars_not_allowed=用户名 '%s' 包含无效字符。 +static.sheetname=用户分析 +static.id=ID +static.name=用户名 +static.codemergecount=PR数 +static.issuecount=提出任务数 +static.commentcount=评论数 +static.focusrepocount=关注项目数 +static.starrepocount=点赞项目数 +static.logincount=登录次数 +static.watchedcount=关注者数 +static.commitcodesize=commit代码行数 +static.solveissuecount=已解决任务数 +static.encyclopediascount=百科页面贡献次数 +static.createrepocount=创建项目数 +static.openiindex=OpenI指数 +static.registdate=用户注册时间 +static.countdate=系统统计时间 [settings] profile=个人信息 account=账号 diff --git a/routers/repo/user_data_analysis.go b/routers/repo/user_data_analysis.go index 2421b0660..2fa94f626 100755 --- a/routers/repo/user_data_analysis.go +++ b/routers/repo/user_data_analysis.go @@ -3,6 +3,7 @@ package repo import ( "fmt" "net/http" + "strings" "time" "code.gitea.io/gitea/models" @@ -10,6 +11,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "github.com/xuri/excelize/v2" ) func QueryUserStaticData(ctx *context.Context) { @@ -35,6 +37,7 @@ func QueryUserStaticDataPage(ctx *context.Context) { pageSize = setting.UI.IssuePagingNum } userName := ctx.Query("userName") + IsReturnFile := ctx.QueryBool("IsReturnFile") log.Info("startDate=" + startDate + " endDate=" + endDate + " userName=" + userName + " page=" + fmt.Sprint(page)) startTime, _ := time.Parse("2006-01-02", startDate) @@ -55,7 +58,71 @@ func QueryUserStaticDataPage(ctx *context.Context) { re, count := models.QueryUserStaticDataPage(pageOpts) mapInterface["data"] = re mapInterface["count"] = count - ctx.JSON(http.StatusOK, mapInterface) + if IsReturnFile { + //writer exec file. + xlsx := excelize.NewFile() + sheetName := ctx.Tr("static.sheetname") + index := xlsx.NewSheet(sheetName) + dataHeader := map[string]string{ + "A1": ctx.Tr("user.static.id"), + "B1": ctx.Tr("user.static.name"), + "C1": ctx.Tr("user.static.codemergecount"), + "D1": ctx.Tr("user.static.issuecount"), + "E1": ctx.Tr("user.static.commentcount"), + "F1": ctx.Tr("user.static.focusrepocount"), + "G1": ctx.Tr("user.static.starrepocount"), + "H1": ctx.Tr("user.static.logincount"), + "I1": ctx.Tr("user.static.watchedcount"), + "J1": ctx.Tr("user.static.commitcodesize"), + "K1": ctx.Tr("user.static.solveissuecount"), + "L1": ctx.Tr("user.static.encyclopediascount"), + "M1": ctx.Tr("user.static.createrepocount"), + "N1": ctx.Tr("user.static.openiindex"), + "O1": ctx.Tr("user.static.registdate"), + "P1": ctx.Tr("user.static.countdate"), + } + for k, v := range dataHeader { + //设置单元格的值 + xlsx.SetCellValue(sheetName, k, v) + } + + for i, userRecord := range re { + rows := fmt.Sprint(i + 2) + + xlsx.SetCellValue(sheetName, "A"+rows, userRecord.ID) + xlsx.SetCellValue(sheetName, "B"+rows, userRecord.Name) + xlsx.SetCellValue(sheetName, "C"+rows, userRecord.CodeMergeCount) + xlsx.SetCellValue(sheetName, "D"+rows, userRecord.IssueCount) + xlsx.SetCellValue(sheetName, "E"+rows, userRecord.CommentCount) + xlsx.SetCellValue(sheetName, "F"+rows, userRecord.FocusRepoCount) + xlsx.SetCellValue(sheetName, "G"+rows, userRecord.StarRepoCount) + xlsx.SetCellValue(sheetName, "H"+rows, userRecord.LoginCount) + xlsx.SetCellValue(sheetName, "I"+rows, userRecord.WatchedCount) + xlsx.SetCellValue(sheetName, "J"+rows, userRecord.CommitCodeSize) + xlsx.SetCellValue(sheetName, "K"+rows, userRecord.SolveIssueCount) + xlsx.SetCellValue(sheetName, "L"+rows, userRecord.EncyclopediasCount) + xlsx.SetCellValue(sheetName, "M"+rows, userRecord.CreateRepoCount) + xlsx.SetCellValue(sheetName, "N"+rows, userRecord.OpenIIndex) + xlsx.SetCellValue(sheetName, "O"+rows, userRecord.RegistDate.Format("2006-01-02")) + xlsx.SetCellValue(sheetName, "P"+rows, time.Unix(userRecord.CountDate, 0).Format("2006-01-02")) + } + + //设置默认打开的表单 + xlsx.SetActiveSheet(index) + filename := sheetName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx" + if len(userName) > 0 { + filename = sheetName + "_" + userName + "_" + strings.ReplaceAll(startDate, "-", "") + "_" + strings.ReplaceAll(endDate, "-", "") + ".xlsx" + } + + ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+filename) + ctx.Resp.Header().Set("Content-Type", "application/octet-stream") + if _, err := xlsx.WriteTo(ctx.Resp); err != nil { + log.Info("writer exel error." + err.Error()) + } + + } else { + ctx.JSON(http.StatusOK, mapInterface) + } } func TimingCountDataByDateAndReCount(date string, isReCount bool) {