Browse Source

update

tags/v1.22.6.1^2
liuzx 4 years ago
parent
commit
851262f18a
2 changed files with 81 additions and 74 deletions
  1. +45
    -2
      models/cloudbrain_static.go
  2. +36
    -72
      routers/api/v1/repo/cloudbrain_dashboard.go

+ 45
- 2
models/cloudbrain_static.go View File

@@ -1,5 +1,48 @@
package models

func CountBrainStatByRawSql(sql string) (int64, error) {
return xStatistic.SQL(sql).Count()
import (
"strconv"
"time"
)

func GenerateDebugOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
countSql := "SELECT count(*) FROM " +
"public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "DEBUG"

return xStatistic.SQL(countSql).Count()
}

func GenerateBenchmarkOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
countSql := "SELECT count(*) FROM " +
"SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "BENCHMARK" +
" and type=" + "0"
return xStatistic.SQL(countSql).Count()
}
func GenerateDebugTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
countSql := "SELECT count(*) FROM " +
"SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "DEBUG" +
" and type=" + "2"
return xStatistic.SQL(countSql).Count()
}
func GenerateTrainTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
countSql := "SELECT count(*) FROM " +
"SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "TRAIN" +
" and type=" + "1"
return xStatistic.SQL(countSql).Count()
}
func GenerateInferenceTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
countSql := "SELECT count(*) FROM " +
"SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "INFERENCE" +
" and type=" + "1"
return xStatistic.SQL(countSql).Count()
}

+ 36
- 72
routers/api/v1/repo/cloudbrain_dashboard.go View File

@@ -2,14 +2,20 @@ package repo

import (
"net/http"
"strconv"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
)

type CloudbrainsPeriodData struct {
DebugOneCount int64 `json:"debugOneCount"`
BenchmarkOneCount int64 `json:"benchmarkOneCount"`
DebugTwoCount int64 `json:"debugTwoCount"`
TrainTwoCount int64 `json:"trainTwoCount"`
InferenceTwoCount int64 `json:"inferenceTwoCount"`
}

func GetAllCloudbrainsPeriodStatistics(ctx *context.Context) {

recordBeginTime, err := getRecordBeginTime()
@@ -24,7 +30,6 @@ func GetAllCloudbrainsPeriodStatistics(ctx *context.Context) {
ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong"))
return
}
q := ctx.QueryTrim("q")
page := ctx.QueryInt("page")
if page <= 0 {
page = 1
@@ -33,87 +38,46 @@ func GetAllCloudbrainsPeriodStatistics(ctx *context.Context) {
if pageSize <= 0 {
pageSize = DEFAULT_PAGE_SIZE
}
orderBy := getOrderBy(ctx)

latestUpdatedTime, latestDate, err := models.GetRepoStatLastUpdatedTime()
debugOneCount, err := models.GenerateDebugOneCount(beginTime, endTime)
if err != nil {
log.Error("Can not query the last updated time.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("repo.last_update_time_error"))
log.Error("Can not query debugOneCount.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("debugOneCount_get_error"))
return
}

DebugOneCountSql := generateDebugOneCountSql(beginTime, endTime)
DebugOneTotal, err := models.CountBrainStatByRawSql(beginTime, endTime)
benchmarkOneCount, err := models.GenerateBenchmarkOneCount(beginTime, endTime)
if err != nil {
log.Error("Can not query total count.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("repo.total_count_get_error"))
log.Error("Can not query benchmarkCount.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("benchmarkOneCount_get_error"))
return
}
DebugTwoCountSql := generateDebugTwoCountSql(beginTime, endTime)
DebugOneTotal, err := models.CountBrainStatByRawSql(DebugTwoCountSql)
debugTwoCount, err := models.GenerateDebugTwoCount(beginTime, endTime)
if err != nil {
log.Error("Can not query total count.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("repo.total_count_get_error"))
log.Error("Can not query debugTwoCount.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("debugTwoCount_get_error"))
return
}

sql := generateSqlByType(ctx, beginTime, endTime, latestDate, q, orderBy, page, pageSize)

projectsPeriodData := ProjectsPeriodData{
RecordBeginTime: recordBeginTime.Format(DATE_FORMAT),
PageSize: pageSize,
TotalPage: getTotalPage(DebugOneTotal, pageSize),
TotalCount: DebugOneTotal,
LastUpdatedTime: latestUpdatedTime,
PageRecords: models.GetRepoStatisticByRawSql(sql),
trainTwoCount, err := models.GenerateTrainTwoCount(beginTime, endTime)
if err != nil {
log.Error("Can not query DebugOneTotal count.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("total_count_get_error"))
return
}
inferenceTwoCount, err := models.GenerateInferenceTwoCount(beginTime, endTime)
if err != nil {
log.Error("Can not query inferenceTwoCount.", err)
ctx.Error(http.StatusBadRequest, ctx.Tr("inferenceTwoCount_get_error"))
return
}

ctx.JSON(http.StatusOK, projectsPeriodData)
cloudbrainsPeriodData := CloudbrainsPeriodData{
DebugOneCount: debugOneCount,
BenchmarkOneCount: benchmarkOneCount,
DebugTwoCount: debugTwoCount,
TrainTwoCount: trainTwoCount,
InferenceTwoCount: inferenceTwoCount,
}

}
ctx.JSON(http.StatusOK, cloudbrainsPeriodData)

func generateDebugOneCountSql(beginTime time.Time, endTime time.Time) string {
countSql := "SELECT count(*) FROM " +
"(SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "DEBUG" +
" and type=" + "0" +
" group by job_id) "
return countSql
}
func generateBenchmarkOneCountSql(beginTime time.Time, endTime time.Time) string {
countSql := "SELECT count(*) FROM " +
"(SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "BENCHMARK" +
" and type=" + "0" +
" group by job_id) "
return countSql
}
func generateDebugTwoCountSql(beginTime time.Time, endTime time.Time) string {
countSql := "SELECT count(*) FROM " +
"(SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "DEBUG" +
" and type=" + "2" +
" group by job_id) "
return countSql
}
func generateTrainTwoCountSql(beginTime time.Time, endTime time.Time) string {
countSql := "SELECT count(*) FROM " +
"(SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "TRAIN" +
" and type=" + "1" +
" group by job_id) "
return countSql
}
func generateInferenceTwoCountSql(beginTime time.Time, endTime time.Time) string {
countSql := "SELECT count(*) FROM " +
"(SELECT job_id FROM cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
" and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
" and job_type=" + "INFERENCE" +
" and type=" + "1" +
" group by job_id) "
return countSql
}

Loading…
Cancel
Save