| @@ -12,7 +12,7 @@ import ( | |||||
| // Cloudbrain statistic info of all CloudbrainTasks | // Cloudbrain statistic info of all CloudbrainTasks | ||||
| type CloudbrainStatistic struct { | type CloudbrainStatistic struct { | ||||
| ID int64 `xorm:"pk autoincr" json:"-"` | ID int64 `xorm:"pk autoincr" json:"-"` | ||||
| Date string `xorm:"unique(s) NOT NULL" json:"date"` | |||||
| Date string `xorm:"NOT NULL DEFAULT 0" json:"date"` | |||||
| WhichHour int64 `xorm:"NOT NULL DEFAULT -1" json:"whichHour"` | WhichHour int64 `xorm:"NOT NULL DEFAULT -1" json:"whichHour"` | ||||
| NumDubugOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numDubugOne"` | NumDubugOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numDubugOne"` | ||||
| NumBenchmarkOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numBenchmarkOne"` | NumBenchmarkOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numBenchmarkOne"` | ||||
| @@ -151,6 +151,7 @@ func init() { | |||||
| new(UserBusinessAnalysisCurrentWeek), | new(UserBusinessAnalysisCurrentWeek), | ||||
| new(UserBusinessAnalysisYesterday), | new(UserBusinessAnalysisYesterday), | ||||
| new(UserLoginLog), | new(UserLoginLog), | ||||
| new(CloudbrainStatistic), | |||||
| ) | ) | ||||
| gonicNames := []string{"SSL", "UID"} | gonicNames := []string{"SSL", "UID"} | ||||
| @@ -556,6 +556,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Get("/query_user_all", operationReq, repo_ext.QueryUserStaticAll) | m.Get("/query_user_all", operationReq, repo_ext.QueryUserStaticAll) | ||||
| //cloudbrain board | //cloudbrain board | ||||
| m.Group("/cloudbrainboard", func() { | m.Group("/cloudbrainboard", func() { | ||||
| m.Get("/downloadtable", repo.ServeCloudbrainPeriodStatisticsFile) | |||||
| m.Group("/cloudbrain", func() { | m.Group("/cloudbrain", func() { | ||||
| m.Get("", repo.GetAllCloudbrainsPeriodStatistics) | m.Get("", repo.GetAllCloudbrainsPeriodStatistics) | ||||
| }) | }) | ||||
| @@ -2,10 +2,12 @@ package repo | |||||
| import ( | import ( | ||||
| "net/http" | "net/http" | ||||
| "net/url" | |||||
| "code.gitea.io/gitea/models" | "code.gitea.io/gitea/models" | ||||
| "code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
| "code.gitea.io/gitea/modules/log" | "code.gitea.io/gitea/modules/log" | ||||
| "github.com/360EntSecGroup-Skylar/excelize/v2" | |||||
| ) | ) | ||||
| type CloudbrainsPeriodData struct { | type CloudbrainsPeriodData struct { | ||||
| @@ -81,3 +83,75 @@ func GetAllCloudbrainsPeriodStatistics(ctx *context.Context) { | |||||
| ctx.JSON(http.StatusOK, cloudbrainsPeriodData) | ctx.JSON(http.StatusOK, cloudbrainsPeriodData) | ||||
| } | } | ||||
| func ServeCloudbrainPeriodStatisticsFile(ctx *context.Context) { | |||||
| recordBeginTime, err := getRecordBeginTime() | |||||
| if err != nil { | |||||
| log.Error("Can not get record begin time", err) | |||||
| ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err")) | |||||
| return | |||||
| } | |||||
| beginTime, endTime, err := getTimePeroid(ctx, recordBeginTime) | |||||
| if err != nil { | |||||
| log.Error("Parameter is wrong", err) | |||||
| ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong")) | |||||
| return | |||||
| } | |||||
| q := ctx.QueryTrim("q") | |||||
| page := ctx.QueryInt("page") | |||||
| if page <= 0 { | |||||
| page = 1 | |||||
| } | |||||
| pageSize := 1000 | |||||
| orderBy := getOrderBy(ctx) | |||||
| _, latestDate, err := models.GetRepoStatLastUpdatedTime() | |||||
| if err != nil { | |||||
| log.Error("Can not query the last updated time.", err) | |||||
| ctx.Error(http.StatusBadRequest, ctx.Tr("repo.last_update_time_error")) | |||||
| return | |||||
| } | |||||
| countSql := generateCountSql(beginTime, endTime, latestDate, q) | |||||
| total, err := models.CountRepoStatByRawSql(countSql) | |||||
| if err != nil { | |||||
| log.Error("Can not query total count.", err) | |||||
| ctx.Error(http.StatusBadRequest, ctx.Tr("repo.total_count_get_error")) | |||||
| return | |||||
| } | |||||
| var projectAnalysis = ctx.Tr("repo.repo_stat_inspect") | |||||
| fileName := getFileName(ctx, beginTime, endTime, projectAnalysis) | |||||
| totalPage := getTotalPage(total, pageSize) | |||||
| f := excelize.NewFile() | |||||
| index := f.NewSheet(projectAnalysis) | |||||
| f.DeleteSheet("Sheet1") | |||||
| for k, v := range allProjectsPeroidHeader(ctx) { | |||||
| f.SetCellValue(projectAnalysis, k, v) | |||||
| } | |||||
| var row = 2 | |||||
| for i := 0; i <= totalPage; i++ { | |||||
| pageRecords := models.GetRepoStatisticByRawSql(generateSqlByType(ctx, beginTime, endTime, latestDate, q, orderBy, i+1, pageSize)) | |||||
| for _, record := range pageRecords { | |||||
| for k, v := range allProjectsPeroidValues(row, record, ctx) { | |||||
| f.SetCellValue(projectAnalysis, k, v) | |||||
| } | |||||
| row++ | |||||
| } | |||||
| } | |||||
| f.SetActiveSheet(index) | |||||
| ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(fileName)) | |||||
| ctx.Resp.Header().Set("Content-Type", "application/octet-stream") | |||||
| f.WriteTo(ctx.Resp) | |||||
| } | |||||
| @@ -10,13 +10,13 @@ import ( | |||||
| ) | ) | ||||
| func CloudbrainStatisticAuto() { | func CloudbrainStatisticAuto() { | ||||
| yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02") | |||||
| yesterday := time.Now().AddDate(0, 0, 0).Format("2006-01-02") | |||||
| CloudbrainStatisticDaily(yesterday) | CloudbrainStatisticDaily(yesterday) | ||||
| } | } | ||||
| func CloudbrainStatisticDaily(date string) { | func CloudbrainStatisticDaily(date string) { | ||||
| log.Info("%s", date) | log.Info("%s", date) | ||||
| log.Info("begin Repo Statistic") | |||||
| log.Info("begin Cloudbrain Statistic") | |||||
| warnEmailMessage := "云脑任务统计信息入库失败,请尽快定位。" | warnEmailMessage := "云脑任务统计信息入库失败,请尽快定位。" | ||||
| if err := models.DeleteCloudbrainStatisticDaily(date); err != nil { | if err := models.DeleteCloudbrainStatisticDaily(date); err != nil { | ||||
| log.Error("DeleteCloudbrainStatisticDaily failed: %v", err.Error()) | log.Error("DeleteCloudbrainStatisticDaily failed: %v", err.Error()) | ||||
| @@ -990,6 +990,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| }, context.RepoRef()) | }, context.RepoRef()) | ||||
| m.Group("/cloudbrain", func() { | m.Group("/cloudbrain", func() { | ||||
| m.Get("/test", repo.CloudbrainStatisticAuto) | |||||
| m.Group("/:id", func() { | m.Group("/:id", func() { | ||||
| m.Get("", reqRepoCloudBrainReader, repo.CloudBrainShow) | m.Get("", reqRepoCloudBrainReader, repo.CloudBrainShow) | ||||
| m.Get("/debug", cloudbrain.AdminOrJobCreaterRight, repo.CloudBrainDebug) | m.Get("/debug", cloudbrain.AdminOrJobCreaterRight, repo.CloudBrainDebug) | ||||