#4854 fix-4798

Merged
ychao_1983 merged 4 commits from fix-4798 into V20231102 6 months ago
  1. +0
    -2
      modules/setting/setting.go
  2. +5
    -71
      services/cloudbrain/statistic.go

+ 0
- 2
modules/setting/setting.go View File

@@ -663,7 +663,6 @@ var (
Enabled bool
Cron string
RunAtStart bool
AllDayRest bool
}{}

C2NetInfos *C2NetSqInfos
@@ -1917,7 +1916,6 @@ func getCardStatistic() {
CardStatistic.Enabled = sec.Key("ENABLED").MustBool(false)
CardStatistic.Cron = sec.Key("CRON").MustString("@daily")
CardStatistic.RunAtStart = sec.Key("RUN_AT_START").MustBool(false)
CardStatistic.AllDayRest = sec.Key("ALL_DAY_RESET").MustBool(false)
}

func GetGrampusConfig() {


+ 5
- 71
services/cloudbrain/statistic.go View File

@@ -1,7 +1,6 @@
package cloudbrain

import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

"time"
@@ -20,50 +19,8 @@ func CardStatistic() {
}

func daysAllStatistic() {
if models.HasTotalTypeXPUInfoStatisticRecord() && !setting.CardStatistic.AllDayRest {
beginTime := models.GetTotalTypeLatestUpdateUnix()
if beginTime == 0 {
daysStatistic(0, models.TypeAllDays)
} else {
statisticMap, err := getStatisticInfoMap(0, models.TypeAllDays, true, beginTime)
if err != nil {
return
}
recordsInDB, err := models.FindTotalTypeXPUInfoStatisticRecords()
if err != nil {
log.Error("can not get xpu statistic records", err)
return
}

xpuInfos, err := models.GetXPUInfos()
if err != nil {
log.Error("can not get XPU base info", err)
return
}

var recordsMap = make(map[string]models.XPUInfoStatistic, len(recordsInDB))
for _, record := range recordsInDB {
for _, xpuInfo := range xpuInfos {
if record.InfoID == xpuInfo.ID {
recordsMap[xpuInfo.CardType] = record
}
}
}

for k, v := range statisticMap {
if record, ok := recordsMap[k]; ok {
v.TaskCount += record.TaskCount
v.UserCount += record.UserCount
v.UsedDuration += record.UsedDuration
}

}

updateOrInsertStatisticRecord(statisticMap)
}
} else {
daysStatistic(0, models.TypeAllDays)
}
daysStatistic(0, models.TypeAllDays)
}

func sevenDayStatistic() {
@@ -79,7 +36,7 @@ func thirtyDayStatistic() {
}

func daysStatistic(days int, statisticType int) {
xpuCardStatisticMap, err := getStatisticInfoMap(days, statisticType, false)
xpuCardStatisticMap, err := getStatisticInfoMap(days, statisticType)
if err != nil {
return
}
@@ -98,7 +55,7 @@ func updateOrInsertStatisticRecord(xpuCardStatisticMap map[string]*models.XPUInf
}
}

func getStatisticInfoMap(days int, statisticType int, lookBefore bool, beginTime ...int64) (map[string]*models.XPUInfoStatistic, error) {
func getStatisticInfoMap(days int, statisticType int, beginTime ...int64) (map[string]*models.XPUInfoStatistic, error) {
endTimeUnix := time.Now().Unix()
beginTimeUnix := time.Now().AddDate(0, 0, -days).Unix()
if days == 0 {
@@ -174,31 +131,8 @@ func getStatisticInfoMap(days int, statisticType int, lookBefore bool, beginTime
v.UsedDuration += calculateCardDuration(task.Cloudbrain)
}
if _, found := xpuCardStatisticUserMap[task.Spec.AccCardType][task.UserID]; !found {
if lookBefore {
_, count, err := models.CloudbrainAll(&models.CloudbrainsOptions{
UserID: task.UserID,
ListOptions: models.ListOptions{
Page: 1,
PageSize: 1,
},
NeedRepoInfo: false,
BeginTimeUnix: 1,
EndTimeUnix: beginTimeUnix,
Type: -1,
AccCardsNum: -1,
AccCardType: task.Spec.AccCardType,
})
if err != nil {
log.Error("Get job failed:", err)
return nil, err
}
if count == 0 {
v.UserCount += 1
}

} else {
v.UserCount += 1
}

v.UserCount += 1
xpuCardStatisticUserMap[task.Spec.AccCardType][task.UserID] = struct{}{}
}
}


Loading…
Cancel
Save