You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloudbrain_dashboard.go 72 kB

3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/timeutil"
  13. "code.gitea.io/gitea/routers/repo"
  14. cloudbrainService "code.gitea.io/gitea/services/cloudbrain"
  15. "code.gitea.io/gitea/services/cloudbrain/resource"
  16. "github.com/360EntSecGroup-Skylar/excelize/v2"
  17. )
  18. type DateCloudbrainNum struct {
  19. Date string `json:"date"`
  20. CloudOneJobTypeRes map[string]int `json:"cloudOneJobTypeRes"`
  21. CloudTwoJobTypeRes map[string]int `json:"cloudTwoJobTypeRes"`
  22. IntelligentNetJobTypeRes map[string]int `json:"intelligentNetJobTypeRes"`
  23. CDCenterJobTypeRes map[string]int `json:"cDCenterJobTypeRes"`
  24. CloudBrainPeriodNum map[int]int `json:"cloudBrainPeriodNum"`
  25. CloudBrainComputeResource map[string]int `json:"cloudBrainComputeResource"`
  26. }
  27. type DateCloudbrainInfo struct {
  28. Date string `json:"date"`
  29. CloudBrainPeriodNum map[int]int `json:"cloudBrainPeriodNum"`
  30. CloudBrainComputeResource map[string]int `json:"cloudBrainComputeResource"`
  31. }
  32. type CloudbrainsStatusAnalysis struct {
  33. JobWaitingCount int64 `json:"jobWaitingCount"`
  34. JobRunningCount int64 `json:"jobRunningCount"`
  35. JobStoppedCount int64 `json:"jobStoppedCount"`
  36. JobCompletedCount int64 `json:"jobCompletedCount"`
  37. JobFailedCount int64 `json:"jobFailedCount"`
  38. JobKilledCount int64 `json:"jobKilledCount"`
  39. JobInitCount int64 `json:"jobInitCount"`
  40. }
  41. func GetAllCloudbrainsOverview(ctx *context.Context) {
  42. recordCloudbrain, err := models.GetRecordBeginTime()
  43. if err != nil {
  44. log.Error("Can not get recordCloudbrain", err)
  45. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  46. return
  47. }
  48. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  49. now := time.Now()
  50. beginTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  51. endTime := now
  52. todayCreatorCount, err := models.GetTodayCreatorCount(beginTime, endTime)
  53. if err != nil {
  54. log.Error("Can not query todayCreatorCount.", err)
  55. return
  56. }
  57. cloudbrainTypeCount, err := models.GetCloudbrainTypeCount()
  58. if err != nil {
  59. log.Error("Can not query cloudbrainTypeCount.", err)
  60. return
  61. }
  62. todayCloudbrainCount, err := models.GetTodayCloudbrainCount(beginTime, endTime)
  63. if err != nil {
  64. log.Error("Can not query todayCloudbrainCount.", err)
  65. return
  66. }
  67. todayRunningCount, err := models.GetTodayRunningCount(beginTime, endTime)
  68. if err != nil {
  69. log.Error("Can not query todayRunningCount.", err)
  70. return
  71. }
  72. todayWaitingCount, err := models.GetTodayWaitingCount(beginTime, endTime)
  73. if err != nil {
  74. log.Error("Can not query todayWaitingCount.", err)
  75. return
  76. }
  77. todayCompletedCount := todayCloudbrainCount - todayRunningCount - todayWaitingCount
  78. creatorCount, err := models.GetCreatorCount()
  79. if err != nil {
  80. log.Error("Can not query creatorCount.", err)
  81. return
  82. }
  83. ctx.JSON(http.StatusOK, map[string]interface{}{
  84. "recordBeginTime": recordBeginTime,
  85. "updateTime": now.Unix(),
  86. "todayCreatorCount": todayCreatorCount,
  87. "creatorCount": creatorCount,
  88. "todayRunningCount": todayRunningCount,
  89. "todayCompletedCount": todayCompletedCount,
  90. "todayWaitingCount": todayWaitingCount,
  91. "todayNewJobCount": todayCloudbrainCount,
  92. "cloudbrainTypeCount": cloudbrainTypeCount,
  93. })
  94. }
  95. func GetOverviewDuration(ctx *context.Context) {
  96. recordCloudbrain, err := models.GetRecordBeginTime()
  97. if err != nil {
  98. log.Error("Can not get recordCloudbrain", err)
  99. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  100. return
  101. }
  102. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  103. now := time.Now()
  104. endTime := now
  105. var workServerNumber int64
  106. var cardNum int64
  107. durationAllSum := int64(0)
  108. cardDuSum := int64(0)
  109. cloudBrainOneCardDuSum := int64(0)
  110. cloudBrainTwoCardDuSum := int64(0)
  111. c2NetCardDuSum := int64(0)
  112. cDNetCardDuSum := int64(0)
  113. cloudBrainOneDuration := int64(0)
  114. cloudBrainTwoDuration := int64(0)
  115. c2NetDuration := int64(0)
  116. cDCenterDuration := int64(0)
  117. cloudbrains, _, err := models.CloudbrainAllKanBan(&models.CloudbrainsOptions{
  118. Type: models.TypeCloudBrainAll,
  119. BeginTimeUnix: int64(recordBeginTime),
  120. EndTimeUnix: endTime.Unix(),
  121. })
  122. if err != nil {
  123. ctx.ServerError("Get cloudbrains failed:", err)
  124. return
  125. }
  126. models.LoadSpecs4CloudbrainInfo(cloudbrains)
  127. for _, cloudbrain := range cloudbrains {
  128. cloudbrain = cloudbrainService.UpdateCloudbrainAiCenter(cloudbrain)
  129. if cloudbrain.Cloudbrain.Spec != nil {
  130. cardNum = int64(cloudbrain.Cloudbrain.Spec.AccCardsNum)
  131. } else {
  132. cardNum = 1
  133. }
  134. if cloudbrain.Cloudbrain.WorkServerNumber >= 1 {
  135. workServerNumber = int64(cloudbrain.Cloudbrain.WorkServerNumber)
  136. } else {
  137. workServerNumber = 1
  138. }
  139. duration := models.ConvertStrToDuration(cloudbrain.TrainJobDuration)
  140. CardDuration := workServerNumber * int64(cardNum) * duration
  141. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne {
  142. cloudBrainOneDuration += duration
  143. cloudBrainOneCardDuSum += CardDuration
  144. } else if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo {
  145. cloudBrainTwoDuration += duration
  146. cloudBrainTwoCardDuSum += CardDuration
  147. } else if cloudbrain.Cloudbrain.Type == models.TypeC2Net {
  148. c2NetDuration += duration
  149. c2NetCardDuSum += CardDuration
  150. } else if cloudbrain.Cloudbrain.Type == models.TypeCDCenter {
  151. cDCenterDuration += duration
  152. cDNetCardDuSum += CardDuration
  153. }
  154. durationAllSum += duration
  155. cardDuSum += CardDuration
  156. }
  157. ctx.JSON(http.StatusOK, map[string]interface{}{
  158. "cloudBrainOneCardDuSum": cloudBrainOneCardDuSum,
  159. "cloudBrainTwoCardDuSum": cloudBrainTwoCardDuSum,
  160. "c2NetCardDuSum": c2NetCardDuSum,
  161. "cDNetCardDuSum": cDNetCardDuSum,
  162. "cardDuSum": cardDuSum,
  163. "cloudBrainOneDuration": cloudBrainOneDuration,
  164. "cloudBrainTwoDuration": cloudBrainTwoDuration,
  165. "c2NetDuration": c2NetDuration,
  166. "cDCenterDuration": cDCenterDuration,
  167. "durationSum": durationAllSum,
  168. })
  169. }
  170. func GetCloudbrainCardDuration(task models.Cloudbrain) string {
  171. cardNum := int(0)
  172. spec, err := resource.GetCloudbrainSpec(task.ID)
  173. if err != nil {
  174. log.Info("error:" + err.Error())
  175. return ""
  176. }
  177. if spec != nil {
  178. cardNum = spec.AccCardsNum
  179. } else {
  180. cardNum = 1
  181. }
  182. var workServerNumber int64
  183. if task.WorkServerNumber >= 1 {
  184. workServerNumber = int64(task.WorkServerNumber)
  185. } else {
  186. workServerNumber = 1
  187. }
  188. cardDuration := models.ConvertDurationToStr(workServerNumber * int64(cardNum) * task.Duration)
  189. return cardDuration
  190. }
  191. func GetAllCloudbrainsTrend(ctx *context.Context) {
  192. queryType := ctx.QueryTrim("type")
  193. now := time.Now()
  194. beginTimeStr := ctx.QueryTrim("beginTime")
  195. endTimeStr := ctx.QueryTrim("endTime")
  196. var beginTime time.Time
  197. var endTime time.Time
  198. dateCloudbrainNum := make([]DateCloudbrainNum, 0)
  199. var err error
  200. if queryType != "" {
  201. if queryType == "all" {
  202. recordCloudbrain, err := models.GetRecordBeginTime()
  203. if err != nil {
  204. log.Error("Can not get recordCloudbrain", err)
  205. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  206. return
  207. }
  208. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  209. beginTime = brainRecordBeginTime
  210. endTime = now
  211. dateCloudbrainNum, err = getMonthCloudbrainNum(beginTime, endTime)
  212. if err != nil {
  213. log.Error("Can not query getMonthCloudbrainNum.", err)
  214. ctx.Error(http.StatusBadRequest, ctx.Tr("getMonthCloudbrainNum_get_error"))
  215. return
  216. }
  217. } else if queryType == "today" {
  218. beginTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  219. // endTimeTemp = now
  220. endTime = beginTime.AddDate(0, 0, 1)
  221. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  222. if err != nil {
  223. log.Error("Can not query getDayCloudbrainNum.", err)
  224. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  225. return
  226. }
  227. } else if queryType == "yesterday" {
  228. beginTime = now.AddDate(0, 0, -1)
  229. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  230. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  231. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  232. if err != nil {
  233. log.Error("Can not query getDayCloudbrainNum.", err)
  234. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  235. return
  236. }
  237. } else if queryType == "last_7day" {
  238. beginTime = now.AddDate(0, 0, -6)
  239. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  240. endTime = now
  241. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  242. if err != nil {
  243. log.Error("Can not query getDayCloudbrainNum.", err)
  244. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  245. return
  246. }
  247. } else if queryType == "last_30day" {
  248. beginTime = now.AddDate(0, 0, -29)
  249. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  250. endTime = now
  251. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  252. if err != nil {
  253. log.Error("Can not query getDayCloudbrainNum.", err)
  254. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  255. return
  256. }
  257. } else if queryType == "current_month" {
  258. endTime = now
  259. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  260. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  261. if err != nil {
  262. log.Error("Can not query getDayCloudbrainNum.", err)
  263. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  264. return
  265. }
  266. } else if queryType == "current_year" {
  267. endTime = now
  268. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  269. // endTimeTemp = beginTime.AddDate(0, 1, 0)
  270. dateCloudbrainNum, err = getMonthCloudbrainNum(beginTime, endTime)
  271. if err != nil {
  272. log.Error("Can not query getMonthCloudbrainNum.", err)
  273. ctx.Error(http.StatusBadRequest, ctx.Tr("getMonthCloudbrainNum_get_error"))
  274. return
  275. }
  276. } else if queryType == "last_month" {
  277. lastMonthTime := now.AddDate(0, -1, 0)
  278. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  279. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  280. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  281. if err != nil {
  282. log.Error("Can not query getDayCloudbrainNum.", err)
  283. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  284. return
  285. }
  286. }
  287. } else {
  288. if beginTimeStr == "" || endTimeStr == "" {
  289. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  290. recordCloudbrain, err := models.GetRecordBeginTime()
  291. if err != nil {
  292. log.Error("Can not get recordCloudbrain", err)
  293. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  294. return
  295. }
  296. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  297. beginTime = brainRecordBeginTime
  298. endTime = now
  299. dateCloudbrainNum, err = getMonthCloudbrainNum(beginTime, endTime)
  300. if err != nil {
  301. log.Error("Can not query getMonthCloudbrainNum.", err)
  302. ctx.Error(http.StatusBadRequest, ctx.Tr("getMonthCloudbrainNum_get_error"))
  303. return
  304. }
  305. } else {
  306. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  307. if err != nil {
  308. log.Error("Can not ParseInLocation.", err)
  309. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  310. return
  311. }
  312. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  313. if err != nil {
  314. log.Error("Can not ParseInLocation.", err)
  315. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  316. return
  317. }
  318. if endTime.After(time.Now()) {
  319. endTime = time.Now()
  320. }
  321. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  322. if err != nil {
  323. log.Error("Can not query getDayCloudbrainNum.", err)
  324. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  325. return
  326. }
  327. }
  328. }
  329. ctx.JSON(http.StatusOK, map[string]interface{}{
  330. "timeCloudbrainNum": dateCloudbrainNum,
  331. })
  332. }
  333. func GetAllCloudbrainsTrendDetail(ctx *context.Context) {
  334. queryType := ctx.QueryTrim("type")
  335. now := time.Now()
  336. beginTimeStr := ctx.QueryTrim("beginTime")
  337. endTimeStr := ctx.QueryTrim("endTime")
  338. var beginTime time.Time
  339. var endTime time.Time
  340. var endTimeTemp time.Time
  341. dayCloudbrainInfo := make([]DateCloudbrainInfo, 0)
  342. var err error
  343. var count int
  344. if queryType != "" {
  345. if queryType == "all" {
  346. recordCloudbrain, err := models.GetRecordBeginTime()
  347. if err != nil {
  348. log.Error("Can not get recordCloudbrain", err)
  349. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  350. return
  351. }
  352. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  353. beginTime = brainRecordBeginTime
  354. endTime = now
  355. dayCloudbrainInfo, count, err = getMonthCloudbrainInfo(beginTime, endTime)
  356. if err != nil {
  357. log.Error("Can not query getDayCloudbrainInfo.", err)
  358. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  359. return
  360. }
  361. } else if queryType == "today" {
  362. beginTime = now.AddDate(0, 0, 0)
  363. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  364. endTime = now
  365. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  366. if err != nil {
  367. log.Error("Can not query getDayCloudbrainInfo.", err)
  368. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  369. return
  370. }
  371. } else if queryType == "yesterday" {
  372. beginTime = now.AddDate(0, 0, -1)
  373. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  374. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  375. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  376. if err != nil {
  377. log.Error("Can not query getDayCloudbrainInfo.", err)
  378. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  379. return
  380. }
  381. } else if queryType == "last_7day" {
  382. beginTime = now.AddDate(0, 0, -6)
  383. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  384. endTime = now
  385. endTimeTemp = time.Date(endTimeTemp.Year(), endTimeTemp.Month(), endTimeTemp.Day(), 0, 0, 0, 0, now.Location())
  386. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  387. if err != nil {
  388. log.Error("Can not query getDayCloudbrainInfo.", err)
  389. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  390. return
  391. }
  392. } else if queryType == "last_30day" {
  393. beginTime = now.AddDate(0, 0, -29)
  394. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  395. endTime = now
  396. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  397. if err != nil {
  398. log.Error("Can not query getDayCloudbrainInfo.", err)
  399. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  400. return
  401. }
  402. } else if queryType == "current_month" {
  403. endTime = now
  404. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  405. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  406. if err != nil {
  407. log.Error("Can not query getDayCloudbrainInfo.", err)
  408. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  409. return
  410. }
  411. } else if queryType == "current_year" {
  412. endTime = now
  413. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  414. dayCloudbrainInfo, count, err = getMonthCloudbrainInfo(beginTime, endTime)
  415. if err != nil {
  416. log.Error("Can not query getDayCloudbrainInfo.", err)
  417. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  418. return
  419. }
  420. } else if queryType == "last_month" {
  421. lastMonthTime := now.AddDate(0, -1, 0)
  422. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  423. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  424. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  425. if err != nil {
  426. log.Error("Can not query getDayCloudbrainInfo.", err)
  427. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  428. return
  429. }
  430. }
  431. } else {
  432. if beginTimeStr == "" || endTimeStr == "" {
  433. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  434. recordCloudbrain, err := models.GetRecordBeginTime()
  435. if err != nil {
  436. log.Error("Can not get recordCloudbrain", err)
  437. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  438. return
  439. }
  440. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  441. beginTime = brainRecordBeginTime
  442. endTime = now
  443. dayCloudbrainInfo, count, err = getMonthCloudbrainInfo(beginTime, endTime)
  444. if err != nil {
  445. log.Error("Can not query getDayCloudbrainInfo.", err)
  446. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  447. return
  448. }
  449. } else {
  450. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  451. if err != nil {
  452. log.Error("Can not ParseInLocation.", err)
  453. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  454. return
  455. }
  456. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  457. if err != nil {
  458. log.Error("Can not ParseInLocation.", err)
  459. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  460. return
  461. }
  462. if endTime.After(time.Now()) {
  463. endTime = time.Now()
  464. }
  465. endTimeTemp = beginTime.AddDate(0, 0, 1)
  466. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  467. if err != nil {
  468. log.Error("Can not query getDayCloudbrainInfo.", err)
  469. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  470. return
  471. }
  472. }
  473. }
  474. page := ctx.QueryInt("page")
  475. if page <= 0 {
  476. page = 1
  477. }
  478. pagesize := ctx.QueryInt("pagesize")
  479. if pagesize <= 0 {
  480. pagesize = 5
  481. }
  482. pageDateCloudbrainInfo := getPageDateCloudbrainInfo(dayCloudbrainInfo, page, pagesize)
  483. ctx.JSON(http.StatusOK, map[string]interface{}{
  484. "totalCount": count,
  485. "timeCloudbrainInfo": pageDateCloudbrainInfo,
  486. })
  487. }
  488. func getPageDateCloudbrainInfo(dateCloudbrainInfo []DateCloudbrainInfo, page int, pagesize int) []DateCloudbrainInfo {
  489. begin := (page - 1) * pagesize
  490. end := (page) * pagesize
  491. if begin > len(dateCloudbrainInfo)-1 {
  492. return nil
  493. }
  494. if end > len(dateCloudbrainInfo)-1 {
  495. return dateCloudbrainInfo[begin:]
  496. } else {
  497. return dateCloudbrainInfo[begin:end]
  498. }
  499. }
  500. func getPageDateCloudbrainDuration(dateUsageStatistic []models.DateUsageStatistic, page int, pagesize int) []models.DateUsageStatistic {
  501. begin := (page - 1) * pagesize
  502. end := (page) * pagesize
  503. if begin > len(dateUsageStatistic)-1 {
  504. return nil
  505. }
  506. if end > len(dateUsageStatistic)-1 {
  507. return dateUsageStatistic[begin:]
  508. } else {
  509. return dateUsageStatistic[begin:end]
  510. }
  511. }
  512. func GetAllCloudbrainsPeriodDistribution(ctx *context.Context) {
  513. queryType := ctx.QueryTrim("type")
  514. beginTimeStr := ctx.QueryTrim("beginTime")
  515. endTimeStr := ctx.QueryTrim("endTime")
  516. recordCloudbrain, err := models.GetRecordBeginTime()
  517. if err != nil {
  518. log.Error("Can not get record begin time", err)
  519. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  520. return
  521. }
  522. recordBeginTime := time.Unix(int64(recordCloudbrain[0].Cloudbrain.CreatedUnix), 0)
  523. beginTime, endTime, err := getCloudbrainTimePeroid(ctx, recordBeginTime)
  524. if err != nil {
  525. log.Error("getCloudbrainTimePeroid error:", err)
  526. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong"))
  527. return
  528. }
  529. cloudOneJobTypeRes := make(map[string]int)
  530. cloudTwoJobTypeRes := make(map[string]int)
  531. intelligentNetJobTypeRes := make(map[string]int)
  532. cDCenterJobTypeRes := make(map[string]int)
  533. cloudBrainPeriodNum := make(map[int]int)
  534. cloudBrainComputeResource := make(map[string]int)
  535. beginTimeTemp := beginTime.Unix()
  536. if queryType == "all" || (queryType == "" && (beginTimeStr == "" || endTimeStr == "")) {
  537. beginTimeTemp = int64(recordCloudbrain[0].Cloudbrain.CreatedUnix)
  538. }
  539. page := 1
  540. pagesize := 10000
  541. count := pagesize
  542. //Each time a maximum of 10000 pieces of data are detected to the memory, batch processing
  543. for count == pagesize && count != 0 {
  544. cloudbrains, _, err := models.CloudbrainAllStatic(&models.CloudbrainsOptions{
  545. ListOptions: models.ListOptions{
  546. Page: page,
  547. PageSize: pagesize,
  548. },
  549. Type: models.TypeCloudBrainAll,
  550. BeginTimeUnix: beginTimeTemp,
  551. EndTimeUnix: endTime.Unix(),
  552. })
  553. if err != nil {
  554. ctx.ServerError("Get cloudbrains failed:", err)
  555. return
  556. }
  557. for _, cloudbrain := range cloudbrains {
  558. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne {
  559. if _, ok := cloudOneJobTypeRes[cloudbrain.JobType]; !ok {
  560. cloudOneJobTypeRes[cloudbrain.JobType] = 1
  561. } else {
  562. cloudOneJobTypeRes[cloudbrain.JobType] += 1
  563. }
  564. }
  565. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo {
  566. if _, ok := cloudTwoJobTypeRes[cloudbrain.JobType]; !ok {
  567. cloudTwoJobTypeRes[cloudbrain.JobType] = 1
  568. } else {
  569. cloudTwoJobTypeRes[cloudbrain.JobType] += 1
  570. }
  571. }
  572. if cloudbrain.Cloudbrain.Type == models.TypeC2Net {
  573. if _, ok := intelligentNetJobTypeRes[cloudbrain.JobType]; !ok {
  574. intelligentNetJobTypeRes[cloudbrain.JobType] = 1
  575. } else {
  576. intelligentNetJobTypeRes[cloudbrain.JobType] += 1
  577. }
  578. }
  579. if cloudbrain.Cloudbrain.Type == models.TypeCDCenter {
  580. if _, ok := cDCenterJobTypeRes[cloudbrain.JobType]; !ok {
  581. cDCenterJobTypeRes[cloudbrain.JobType] = 1
  582. } else {
  583. cDCenterJobTypeRes[cloudbrain.JobType] += 1
  584. }
  585. }
  586. if _, ok := cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type]; !ok {
  587. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] = 1
  588. } else {
  589. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] += 1
  590. }
  591. if _, ok := cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource]; !ok {
  592. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] = 1
  593. } else {
  594. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] += 1
  595. }
  596. }
  597. count = len(cloudbrains)
  598. page += 1
  599. }
  600. jobTypeList := []string{string(models.JobTypeDebug), string(models.JobTypeTrain), string(models.JobTypeInference), string(models.JobTypeBenchmark),
  601. string(models.JobTypeModelSafety), string(models.JobTypeBrainScore), string(models.JobTypeSnn4imagenet)}
  602. for _, v := range jobTypeList {
  603. if _, ok := cloudOneJobTypeRes[v]; !ok {
  604. cloudOneJobTypeRes[v] = 0
  605. }
  606. if _, ok := cloudTwoJobTypeRes[v]; !ok {
  607. cloudTwoJobTypeRes[v] = 0
  608. }
  609. if _, ok := intelligentNetJobTypeRes[v]; !ok {
  610. intelligentNetJobTypeRes[v] = 0
  611. }
  612. if _, ok := cDCenterJobTypeRes[v]; !ok {
  613. cDCenterJobTypeRes[v] = 0
  614. }
  615. }
  616. cloudBrainTypeList := []int{0, 1, 2, 3}
  617. for _, v := range cloudBrainTypeList {
  618. if _, ok := cloudBrainPeriodNum[v]; !ok {
  619. cloudBrainPeriodNum[v] = 0
  620. }
  621. }
  622. ComputeResourceList := []string{"CPU/GPU", "NPU"}
  623. for _, v := range ComputeResourceList {
  624. if _, ok := cloudBrainComputeResource[v]; !ok {
  625. cloudBrainComputeResource[v] = 0
  626. }
  627. }
  628. cloudOneJobTypeRes["EVALUATION"] = cloudBrainPeriodNum[0] - cloudOneJobTypeRes[string(models.JobTypeTrain)] - cloudOneJobTypeRes[string(models.JobTypeInference)] - cloudOneJobTypeRes[string(models.JobTypeDebug)]
  629. cloudTwoJobTypeRes["EVALUATION"] = cloudBrainPeriodNum[1] - cloudTwoJobTypeRes[string(models.JobTypeTrain)] - cloudTwoJobTypeRes[string(models.JobTypeInference)] - cloudTwoJobTypeRes[string(models.JobTypeDebug)]
  630. intelligentNetJobTypeRes["EVALUATION"] = cloudBrainPeriodNum[2] - intelligentNetJobTypeRes[string(models.JobTypeTrain)] - intelligentNetJobTypeRes[string(models.JobTypeInference)] - intelligentNetJobTypeRes[string(models.JobTypeDebug)]
  631. cDCenterJobTypeRes["EVALUATION"] = cloudBrainPeriodNum[3] - cDCenterJobTypeRes[string(models.JobTypeTrain)] - cDCenterJobTypeRes[string(models.JobTypeInference)] - cDCenterJobTypeRes[string(models.JobTypeDebug)]
  632. ctx.JSON(http.StatusOK, map[string]interface{}{
  633. "cloudOneJobTypeRes": cloudOneJobTypeRes,
  634. "cloudTwoJobTypeRes": cloudTwoJobTypeRes,
  635. "intelligentNetJobTypeRes": intelligentNetJobTypeRes,
  636. "cDCenterJobTypeRes": cDCenterJobTypeRes,
  637. "cloudBrainPeriodNum": cloudBrainPeriodNum,
  638. "cloudBrainComputeResource": cloudBrainComputeResource,
  639. })
  640. }
  641. func GetCloudbrainsStatusAnalysis(ctx *context.Context) {
  642. cloudbrainStatusCount, err := models.GetCloudbrainStatusCount()
  643. log.Info("cloudbrainStatusCount:", cloudbrainStatusCount)
  644. if err != nil {
  645. log.Error("Can not query cloudbrainStatusCount.", err)
  646. return
  647. }
  648. ctx.JSON(http.StatusOK, map[string]interface{}{
  649. "cloudbrainStatusCount": cloudbrainStatusCount,
  650. })
  651. }
  652. func GetCloudbrainsDetailData(ctx *context.Context) {
  653. recordCloudbrain, err := models.GetRecordBeginTime()
  654. if err != nil {
  655. log.Error("Can not get recordCloudbrain", err)
  656. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  657. return
  658. }
  659. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  660. endTime := time.Now()
  661. listType := ctx.Query("listType")
  662. jobType := ctx.Query("jobType")
  663. jobStatus := ctx.Query("jobStatus")
  664. cloudBrainType := ctx.QueryInt("Type")
  665. aiCenter := ctx.Query("aiCenter")
  666. needDeleteInfo := ctx.Query("needDeleteInfo")
  667. if cloudBrainType == models.TypeCloudBrainOne && aiCenter == models.AICenterOfCloudBrainOne {
  668. aiCenter = ""
  669. }
  670. if cloudBrainType == models.TypeCloudBrainTwo && aiCenter == models.AICenterOfCloudBrainTwo {
  671. aiCenter = ""
  672. }
  673. if cloudBrainType == models.TypeCDCenter && aiCenter == models.AICenterOfChengdu {
  674. aiCenter = ""
  675. }
  676. if cloudBrainType == models.TypeCloudBrainAll {
  677. if aiCenter == models.AICenterOfCloudBrainOne {
  678. cloudBrainType = models.TypeCloudBrainOne
  679. aiCenter = ""
  680. }
  681. if aiCenter == models.AICenterOfCloudBrainTwo {
  682. cloudBrainType = models.TypeCloudBrainTwo
  683. aiCenter = ""
  684. }
  685. if aiCenter == models.AICenterOfChengdu {
  686. cloudBrainType = models.TypeCDCenter
  687. aiCenter = ""
  688. }
  689. }
  690. page := ctx.QueryInt("page")
  691. pageSize := ctx.QueryInt("pagesize")
  692. if page <= 0 {
  693. page = 1
  694. }
  695. if pageSize <= 0 {
  696. pageSize = 10
  697. }
  698. var jobTypes []string
  699. jobTypeNot := false
  700. if jobType == string(models.JobTypeBenchmark) {
  701. jobTypes = append(jobTypes, string(models.JobTypeBenchmark), string(models.JobTypeModelSafety), string(models.JobTypeBrainScore), string(models.JobTypeSnn4imagenet))
  702. } else if jobType != "all" && jobType != "" {
  703. jobTypes = append(jobTypes, jobType)
  704. }
  705. var jobStatuses []string
  706. jobStatusNot := false
  707. if jobStatus == "other" {
  708. jobStatusNot = true
  709. jobStatuses = append(jobStatuses, string(models.ModelArtsTrainJobWaiting), string(models.ModelArtsTrainJobFailed), string(models.ModelArtsRunning), string(models.ModelArtsTrainJobCompleted),
  710. string(models.ModelArtsStarting), string(models.ModelArtsRestarting), string(models.ModelArtsStartFailed),
  711. string(models.ModelArtsStopping), string(models.ModelArtsStopped), string(models.JobSucceeded))
  712. } else if jobStatus != "all" && jobStatus != "" {
  713. jobStatuses = append(jobStatuses, jobStatus)
  714. }
  715. keyword := strings.Trim(ctx.Query("q"), " ")
  716. ciTasks, count, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  717. ListOptions: models.ListOptions{
  718. Page: page,
  719. PageSize: pageSize,
  720. },
  721. Keyword: keyword,
  722. Type: cloudBrainType,
  723. ComputeResource: listType,
  724. JobTypeNot: jobTypeNot,
  725. JobStatusNot: jobStatusNot,
  726. JobStatus: jobStatuses,
  727. JobTypes: jobTypes,
  728. NeedRepoInfo: true,
  729. BeginTimeUnix: int64(recordBeginTime),
  730. EndTimeUnix: endTime.Unix(),
  731. AiCenter: aiCenter,
  732. NeedDeleteInfo: needDeleteInfo,
  733. })
  734. if err != nil {
  735. ctx.ServerError("Get job failed:", err)
  736. return
  737. }
  738. models.LoadSpecs4CloudbrainInfo(ciTasks)
  739. nilTime := time.Time{}
  740. tasks := []models.TaskDetail{}
  741. for i, task := range ciTasks {
  742. task = cloudbrainService.UpdateCloudbrainAiCenter(task)
  743. var taskDetail models.TaskDetail
  744. taskDetail.ID = ciTasks[i].Cloudbrain.ID
  745. taskDetail.JobID = ciTasks[i].Cloudbrain.JobID
  746. taskDetail.JobName = ciTasks[i].JobName
  747. taskDetail.DisplayJobName = ciTasks[i].DisplayJobName
  748. taskDetail.Status = ciTasks[i].Status
  749. taskDetail.JobType = ciTasks[i].JobType
  750. taskDetail.CreatedUnix = ciTasks[i].Cloudbrain.CreatedUnix
  751. taskDetail.RunTime = ciTasks[i].Cloudbrain.TrainJobDuration
  752. taskDetail.StartTime = ciTasks[i].StartTime
  753. taskDetail.EndTime = ciTasks[i].EndTime
  754. taskDetail.ComputeResource = ciTasks[i].ComputeResource
  755. taskDetail.Type = ciTasks[i].Cloudbrain.Type
  756. taskDetail.UserName = ciTasks[i].User.Name
  757. taskDetail.RepoID = ciTasks[i].RepoID
  758. taskDetail.AiCenter = repo.GetAiCenterNameByCode(task.Cloudbrain.AiCenter, ctx.Language())
  759. if ciTasks[i].Repo != nil {
  760. taskDetail.RepoName = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Name
  761. taskDetail.RepoAlias = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Alias
  762. }
  763. if ciTasks[i].Cloudbrain.WorkServerNumber >= 1 {
  764. taskDetail.WorkServerNum = int64(ciTasks[i].Cloudbrain.WorkServerNumber)
  765. } else {
  766. taskDetail.WorkServerNum = 1
  767. }
  768. taskDetail.CardDuration = repo.GetCloudbrainCardDuration(ciTasks[i].Cloudbrain)
  769. taskDetail.WaitTime = repo.GetCloudbrainWaitTime(ciTasks[i].Cloudbrain)
  770. if ciTasks[i].Cloudbrain.DeletedAt != nilTime || ciTasks[i].Repo == nil {
  771. taskDetail.IsDelete = true
  772. } else {
  773. taskDetail.IsDelete = false
  774. }
  775. taskDetail.Spec = ciTasks[i].Spec
  776. tasks = append(tasks, taskDetail)
  777. }
  778. pager := context.NewPagination(int(count), pageSize, page, getTotalPage(count, pageSize))
  779. pager.SetDefaultParams(ctx)
  780. pager.AddParam(ctx, "listType", "ListType")
  781. ctx.JSON(http.StatusOK, map[string]interface{}{
  782. "Title": ctx.Tr("kanban.cloudBrains"),
  783. "Tasks": tasks,
  784. "Keyword": keyword,
  785. "pager": pager,
  786. "count": count,
  787. })
  788. }
  789. func GetCloudbrainsCreateHoursData(ctx *context.Context) {
  790. recordCloudbrain, err := models.GetRecordBeginTime()
  791. if err != nil {
  792. log.Error("Can not get record begin time", err)
  793. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  794. return
  795. }
  796. recordBeginTime := time.Unix(int64(recordCloudbrain[0].Cloudbrain.CreatedUnix), 0)
  797. // recordBeginTime, _ := time.Parse("2006-01-02 15:04:05", recordTime)
  798. now := time.Now()
  799. queryType := ctx.QueryTrim("type")
  800. beginTimeStr := ctx.QueryTrim("beginTime")
  801. endTimeStr := ctx.QueryTrim("endTime")
  802. var beginTime time.Time
  803. var endTime time.Time
  804. createHourPeriodCount := make(map[string]interface{})
  805. if queryType != "" {
  806. if queryType == "all" {
  807. beginTime = recordBeginTime
  808. endTime = now.AddDate(0, 0, 1)
  809. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  810. } else if queryType == "today" {
  811. beginTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  812. endTime = now.AddDate(0, 0, 1)
  813. } else if queryType == "yesterday" {
  814. beginTime = now.AddDate(0, 0, -1)
  815. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  816. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  817. } else if queryType == "current_week" {
  818. beginTime = now.AddDate(0, 0, -int(time.Now().Weekday())+1) //begin from monday
  819. endTime = now
  820. } else if queryType == "current_month" {
  821. beginTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  822. endTime = now.AddDate(0, 0, 1)
  823. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  824. } else if queryType == "monthly" {
  825. endTime = now
  826. beginTime = now.AddDate(0, -1, 0)
  827. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  828. } else if queryType == "current_year" {
  829. beginTime = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  830. endTime = now.AddDate(0, 0, 1)
  831. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  832. } else if queryType == "last_month" {
  833. lastMonthTime := now.AddDate(0, -1, 0)
  834. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  835. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  836. } else if queryType == "last_7day" {
  837. beginTime = now.AddDate(0, 0, -6)
  838. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  839. endTime = now.AddDate(0, 0, 1)
  840. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  841. } else if queryType == "last_30day" {
  842. beginTime = now.AddDate(0, 0, -29)
  843. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  844. endTime = now.AddDate(0, 0, 1)
  845. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  846. }
  847. } else {
  848. if beginTimeStr == "" || endTimeStr == "" {
  849. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  850. beginTime = recordBeginTime
  851. endTime = now.AddDate(0, 0, 1)
  852. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  853. } else {
  854. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  855. if err != nil {
  856. log.Error("Can not ParseInLocation.", err)
  857. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  858. return
  859. }
  860. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  861. if err != nil {
  862. log.Error("Can not ParseInLocation.", err)
  863. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  864. return
  865. }
  866. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  867. }
  868. }
  869. dateBeginTime := beginTime.Format("2006-01-02")
  870. dateEndTime := endTime.Format("2006-01-02")
  871. createHourPeriodCount, err = models.GetCreateHourPeriodCount(dateBeginTime, dateEndTime)
  872. if err != nil {
  873. log.Error("Can not query hourPeriodCount.", err)
  874. ctx.Error(http.StatusBadRequest, ctx.Tr("hourPeriodCount_get_error"))
  875. return
  876. }
  877. runHourPeriodCount, err := models.GetRunHourPeriodCount(dateBeginTime, dateEndTime)
  878. if err != nil {
  879. log.Error("Can not query runHourPeriodCount.", err)
  880. ctx.Error(http.StatusBadRequest, ctx.Tr("runHourPeriodCount_get_error"))
  881. return
  882. }
  883. ctx.JSON(http.StatusOK, map[string]interface{}{
  884. "recordBeginTime": recordCloudbrain[0].Cloudbrain.CreatedUnix,
  885. "updateTime": now.Unix(),
  886. "createHourPeriodCount": createHourPeriodCount,
  887. "runHourPeriodCount": runHourPeriodCount,
  888. })
  889. }
  890. func GetWaittingTop(ctx *context.Context) {
  891. ciTasks, err := models.GetWaittingTop()
  892. if err != nil {
  893. log.Error("Can not get record begin time", err)
  894. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  895. return
  896. }
  897. tasks := []models.TaskDetail{}
  898. for i, task := range ciTasks {
  899. ciTasks[i].Cloudbrain.ComputeResource = task.ComputeResource
  900. var taskDetail models.TaskDetail
  901. taskDetail.DisplayJobName = ciTasks[i].DisplayJobName
  902. taskDetail.UserName = ciTasks[i].User.Name
  903. taskDetail.RepoID = ciTasks[i].RepoID
  904. if ciTasks[i].Repo != nil {
  905. taskDetail.RepoName = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Name
  906. }
  907. WaitTimeInt := time.Now().Unix() - ciTasks[i].Cloudbrain.CreatedUnix.AsTime().Unix()
  908. taskDetail.WaitTime = models.ConvertDurationToStr(WaitTimeInt)
  909. if WaitTimeInt < 0 {
  910. taskDetail.WaitTime = "00:00:00"
  911. }
  912. tasks = append(tasks, taskDetail)
  913. }
  914. ctx.JSON(http.StatusOK, map[string]interface{}{
  915. "tasks": tasks,
  916. })
  917. }
  918. func GetRunningTop(ctx *context.Context) {
  919. ciTasks, err := models.GetRunningTop()
  920. if err != nil {
  921. log.Error("Can not get record begin time", err)
  922. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  923. return
  924. }
  925. tasks := []models.TaskDetail{}
  926. for i, task := range ciTasks {
  927. ciTasks[i].Cloudbrain.ComputeResource = task.ComputeResource
  928. var taskDetail models.TaskDetail
  929. taskDetail.DisplayJobName = ciTasks[i].DisplayJobName
  930. taskDetail.RunTime = ciTasks[i].Cloudbrain.TrainJobDuration
  931. taskDetail.UserName = ciTasks[i].User.Name
  932. taskDetail.RepoID = ciTasks[i].RepoID
  933. if ciTasks[i].Repo != nil {
  934. taskDetail.RepoName = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Name
  935. }
  936. tasks = append(tasks, taskDetail)
  937. }
  938. ctx.JSON(http.StatusOK, map[string]interface{}{
  939. "tasks": tasks,
  940. })
  941. }
  942. func getCloudbrainCount(beginTime time.Time, endTime time.Time, cloudbrains []*models.CloudbrainInfo) (map[string]int, map[string]int, map[string]int, map[string]int, map[int]int, map[string]int) {
  943. cloudOneJobTypeRes := make(map[string]int)
  944. cloudTwoJobTypeRes := make(map[string]int)
  945. intelligentNetJobTypeRes := make(map[string]int)
  946. cDCenterJobTypeRes := make(map[string]int)
  947. cloudBrainPeriodNum := make(map[int]int)
  948. cloudBrainComputeResource := make(map[string]int)
  949. for _, cloudbrain := range cloudbrains {
  950. if int64(cloudbrain.Cloudbrain.CreatedUnix) >= beginTime.Unix() && int64(cloudbrain.Cloudbrain.CreatedUnix) < endTime.Unix() {
  951. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne {
  952. if _, ok := cloudOneJobTypeRes[cloudbrain.Cloudbrain.JobType]; !ok {
  953. cloudOneJobTypeRes[cloudbrain.Cloudbrain.JobType] = 1
  954. } else {
  955. cloudOneJobTypeRes[cloudbrain.Cloudbrain.JobType] += 1
  956. }
  957. }
  958. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo {
  959. if _, ok := cloudTwoJobTypeRes[cloudbrain.JobType]; !ok {
  960. cloudTwoJobTypeRes[cloudbrain.Cloudbrain.JobType] = 1
  961. } else {
  962. cloudTwoJobTypeRes[cloudbrain.Cloudbrain.JobType] += 1
  963. }
  964. }
  965. if _, ok := cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type]; !ok {
  966. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] = 1
  967. } else {
  968. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] += 1
  969. }
  970. if _, ok := cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource]; !ok {
  971. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] = 1
  972. } else {
  973. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] += 1
  974. }
  975. }
  976. }
  977. ComputeResourceList := []string{"CPU/GPU", "NPU"}
  978. for _, v := range ComputeResourceList {
  979. if _, ok := cloudBrainComputeResource[v]; !ok {
  980. cloudBrainComputeResource[v] = 0
  981. }
  982. }
  983. jobTypeList := []string{"DEBUG", "BENCHMARK", "INFERENCE", "TRAIN", "SNN4IMAGENET", "BRAINSCORE"}
  984. cloudBrainTypeList := []int{0, 1, 2, 3}
  985. for _, v := range jobTypeList {
  986. if _, ok := cloudOneJobTypeRes[v]; !ok {
  987. cloudOneJobTypeRes[v] = 0
  988. }
  989. if _, ok := cloudTwoJobTypeRes[v]; !ok {
  990. cloudTwoJobTypeRes[v] = 0
  991. }
  992. if _, ok := intelligentNetJobTypeRes[v]; !ok {
  993. intelligentNetJobTypeRes[v] = 0
  994. }
  995. if _, ok := cDCenterJobTypeRes[v]; !ok {
  996. cDCenterJobTypeRes[v] = 0
  997. }
  998. }
  999. for _, v := range cloudBrainTypeList {
  1000. if _, ok := cloudBrainPeriodNum[v]; !ok {
  1001. cloudBrainPeriodNum[v] = 0
  1002. }
  1003. }
  1004. cloudBrainPeriodNum[-1] = cloudBrainPeriodNum[0] + cloudBrainPeriodNum[1] + cloudBrainPeriodNum[2] + cloudBrainPeriodNum[3]
  1005. return cloudOneJobTypeRes, cloudTwoJobTypeRes, intelligentNetJobTypeRes, cDCenterJobTypeRes, cloudBrainPeriodNum, cloudBrainComputeResource
  1006. }
  1007. func getDayCloudbrainNum(beginTime time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  1008. var endTimeTemp time.Time
  1009. endTimeTemp = beginTime.AddDate(0, 0, 1)
  1010. cloudbrains, _, err := models.CloudbrainAllStatic(&models.CloudbrainsOptions{
  1011. Type: models.TypeCloudBrainAll,
  1012. BeginTimeUnix: beginTime.Unix(),
  1013. EndTimeUnix: endTime.Unix(),
  1014. })
  1015. if err != nil {
  1016. log.Error("Get cloudbrains failed:", err)
  1017. return nil, err
  1018. }
  1019. dayCloudbrainNum := make([]DateCloudbrainNum, 0)
  1020. for endTimeTemp.Before(endTime) || endTimeTemp.Equal(endTime) {
  1021. cloudOneJobTypeRes, cloudTwoJobTypeRes, intelligentNetJobTypeRes, cDCenterJobTypeRes, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(beginTime, endTimeTemp, cloudbrains)
  1022. dayCloudbrainNum = append(dayCloudbrainNum, DateCloudbrainNum{
  1023. Date: beginTime.Format("2006/01/02"),
  1024. CloudOneJobTypeRes: cloudOneJobTypeRes,
  1025. CloudTwoJobTypeRes: cloudTwoJobTypeRes,
  1026. IntelligentNetJobTypeRes: intelligentNetJobTypeRes,
  1027. CDCenterJobTypeRes: cDCenterJobTypeRes,
  1028. CloudBrainPeriodNum: cloudBrainPeriodNum,
  1029. CloudBrainComputeResource: cloudBrainComputeResource,
  1030. })
  1031. if endTime.Before(endTimeTemp.AddDate(0, 0, 1)) && endTimeTemp.Before(endTime) {
  1032. beginTime = endTimeTemp
  1033. endTimeTemp = endTime
  1034. } else {
  1035. beginTime = endTimeTemp
  1036. endTimeTemp = beginTime.AddDate(0, 0, 1)
  1037. }
  1038. }
  1039. return dayCloudbrainNum, nil
  1040. }
  1041. func getMonthCloudbrainNum(beginTime time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  1042. var endTimeTemp time.Time
  1043. now := time.Now()
  1044. endTimeTemp = beginTime.AddDate(0, 1, 0)
  1045. endTimeTemp = time.Date(endTimeTemp.Year(), endTimeTemp.Month(), 1, 0, 0, 0, 0, now.Location())
  1046. monthCloudbrainNum := make([]DateCloudbrainNum, 0)
  1047. cloudbrains, _, err := models.CloudbrainAllStatic(&models.CloudbrainsOptions{
  1048. Type: models.TypeCloudBrainAll,
  1049. BeginTimeUnix: beginTime.Unix(),
  1050. EndTimeUnix: endTime.Unix(),
  1051. })
  1052. if err != nil {
  1053. log.Error("Getcloudbrains failed:%v", err)
  1054. return nil, err
  1055. }
  1056. for endTimeTemp.Before(endTime) || endTimeTemp.Equal(endTime) {
  1057. cloudOneJobTypeRes, cloudTwoJobTypeRes, intelligentNetJobTypeRes, cDCenterJobTypeRes, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(beginTime, endTimeTemp, cloudbrains)
  1058. monthCloudbrainNum = append(monthCloudbrainNum, DateCloudbrainNum{
  1059. Date: beginTime.Format("2006/01"),
  1060. CloudOneJobTypeRes: cloudOneJobTypeRes,
  1061. CloudTwoJobTypeRes: cloudTwoJobTypeRes,
  1062. IntelligentNetJobTypeRes: intelligentNetJobTypeRes,
  1063. CDCenterJobTypeRes: cDCenterJobTypeRes,
  1064. CloudBrainPeriodNum: cloudBrainPeriodNum,
  1065. CloudBrainComputeResource: cloudBrainComputeResource,
  1066. })
  1067. if endTime.Before(endTimeTemp.AddDate(0, 1, 0)) && endTimeTemp.Before(endTime) {
  1068. beginTime = endTimeTemp
  1069. endTimeTemp = endTime
  1070. } else {
  1071. beginTime = endTimeTemp
  1072. endTimeTemp = beginTime.AddDate(0, 1, 0)
  1073. }
  1074. }
  1075. return monthCloudbrainNum, nil
  1076. }
  1077. func getDayCloudbrainInfo(beginTime time.Time, endTime time.Time) ([]DateCloudbrainInfo, int, error) {
  1078. now := time.Now()
  1079. endTimeTemp := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  1080. if endTimeTemp.Equal(endTime) {
  1081. endTimeTemp = endTimeTemp.AddDate(0, 0, -1)
  1082. }
  1083. cloudbrains, _, err := models.CloudbrainAllStatic(&models.CloudbrainsOptions{
  1084. Type: models.TypeCloudBrainAll,
  1085. BeginTimeUnix: beginTime.Unix(),
  1086. EndTimeUnix: endTime.Unix(),
  1087. })
  1088. if err != nil {
  1089. log.Error("Getcloudbrains failed:%v", err)
  1090. return nil, 0, err
  1091. }
  1092. dayCloudbrainInfo := make([]DateCloudbrainInfo, 0)
  1093. count := 0
  1094. for beginTime.Before(endTimeTemp) || beginTime.Equal(endTimeTemp) {
  1095. _, _, _, _, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(endTimeTemp, endTime, cloudbrains)
  1096. dayCloudbrainInfo = append(dayCloudbrainInfo, DateCloudbrainInfo{
  1097. Date: endTimeTemp.Format("2006/01/02"),
  1098. CloudBrainPeriodNum: cloudBrainPeriodNum,
  1099. CloudBrainComputeResource: cloudBrainComputeResource,
  1100. })
  1101. endTime = endTimeTemp
  1102. endTimeTemp = endTimeTemp.AddDate(0, 0, -1)
  1103. count += 1
  1104. }
  1105. return dayCloudbrainInfo, count, nil
  1106. }
  1107. func getMonthCloudbrainInfo(beginTime time.Time, endTime time.Time) ([]DateCloudbrainInfo, int, error) {
  1108. now := time.Now()
  1109. endTimeTemp := time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1110. if endTimeTemp.Equal(endTime) {
  1111. endTimeTemp = endTimeTemp.AddDate(0, -1, 0)
  1112. }
  1113. cloudbrains, _, err := models.CloudbrainAllStatic(&models.CloudbrainsOptions{
  1114. Type: models.TypeCloudBrainAll,
  1115. BeginTimeUnix: beginTime.Unix(),
  1116. EndTimeUnix: endTime.Unix(),
  1117. })
  1118. if err != nil {
  1119. log.Error("Getcloudbrains failed:%v", err)
  1120. return nil, 0, err
  1121. }
  1122. dayCloudbrainInfo := make([]DateCloudbrainInfo, 0)
  1123. count := 0
  1124. for beginTime.Before(endTimeTemp) || beginTime.Equal(endTimeTemp) || (endTimeTemp.Before(beginTime) && beginTime.Before(endTime)) {
  1125. _, _, _, _, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(endTimeTemp, endTime, cloudbrains)
  1126. dayCloudbrainInfo = append(dayCloudbrainInfo, DateCloudbrainInfo{
  1127. Date: endTimeTemp.Format("2006/01"),
  1128. CloudBrainPeriodNum: cloudBrainPeriodNum,
  1129. CloudBrainComputeResource: cloudBrainComputeResource,
  1130. })
  1131. endTime = endTimeTemp
  1132. endTimeTemp = endTimeTemp.AddDate(0, -1, 0)
  1133. count += 1
  1134. }
  1135. return dayCloudbrainInfo, count, nil
  1136. }
  1137. func DownloadCloudBrainBoard(ctx *context.Context) {
  1138. recordCloudbrain, err := models.GetRecordBeginTime()
  1139. if err != nil {
  1140. log.Error("Can not get recordCloudbrain", err)
  1141. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  1142. return
  1143. }
  1144. page := 1
  1145. pageSize := 300
  1146. var cloudBrain = ctx.Tr("repo.cloudbrain")
  1147. fileName := getCloudbrainFileName(cloudBrain)
  1148. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  1149. now := time.Now()
  1150. endTime := now
  1151. _, total, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  1152. ListOptions: models.ListOptions{
  1153. Page: page,
  1154. PageSize: pageSize,
  1155. },
  1156. Type: models.TypeCloudBrainAll,
  1157. BeginTimeUnix: int64(recordBeginTime),
  1158. EndTimeUnix: endTime.Unix(),
  1159. })
  1160. log.Info("totalcountisis:", total)
  1161. if err != nil {
  1162. log.Warn("Can not get cloud brain info", err)
  1163. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.cloudbrain_query_fail"))
  1164. return
  1165. }
  1166. totalPage := getTotalPage(total, pageSize)
  1167. f := excelize.NewFile()
  1168. index := f.NewSheet(cloudBrain)
  1169. f.DeleteSheet("Sheet1")
  1170. for k, v := range allCloudbrainHeader(ctx) {
  1171. f.SetCellValue(cloudBrain, k, v)
  1172. }
  1173. var row = 2
  1174. for i := 0; i < totalPage; i++ {
  1175. pageRecords, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  1176. ListOptions: models.ListOptions{
  1177. Page: page,
  1178. PageSize: pageSize,
  1179. },
  1180. Type: models.TypeCloudBrainAll,
  1181. BeginTimeUnix: int64(recordBeginTime),
  1182. EndTimeUnix: endTime.Unix(),
  1183. NeedRepoInfo: true,
  1184. })
  1185. if err != nil {
  1186. log.Warn("Can not get cloud brain info", err)
  1187. continue
  1188. }
  1189. models.LoadSpecs4CloudbrainInfo(pageRecords)
  1190. for _, record := range pageRecords {
  1191. record = cloudbrainService.UpdateCloudbrainAiCenter(record)
  1192. record.Cloudbrain.AiCenter = repo.GetAiCenterNameByCode(record.Cloudbrain.AiCenter, ctx.Language())
  1193. for k, v := range allCloudbrainValues(row, record, ctx) {
  1194. f.SetCellValue(cloudBrain, k, v)
  1195. }
  1196. row++
  1197. }
  1198. page++
  1199. }
  1200. f.SetActiveSheet(index)
  1201. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(fileName))
  1202. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  1203. f.WriteTo(ctx.Resp)
  1204. }
  1205. func getCloudbrainFileName(baseName string) string {
  1206. return baseName + "_" + time.Now().Format(EXCEL_DATE_FORMAT) + ".xlsx"
  1207. }
  1208. func allCloudbrainHeader(ctx *context.Context) map[string]string {
  1209. return map[string]string{"A1": ctx.Tr("repo.cloudbrain_task"), "B1": ctx.Tr("repo.cloudbrain_type"), "C1": ctx.Tr("repo.modelarts.status"), "D1": ctx.Tr("repo.cloudbrain_task_type"),
  1210. "E1": ctx.Tr("repo.modelarts.createtime"), "F1": ctx.Tr("repo.modelarts.train_job.wait_time"), "G1": ctx.Tr("repo.modelarts.train_job.dura_time"),
  1211. "H1": ctx.Tr("cloudbrain.card_duration"),
  1212. "I1": ctx.Tr("repo.modelarts.train_job.start_time"), "J1": ctx.Tr("repo.modelarts.train_job.end_time"),
  1213. "K1": ctx.Tr("repo.modelarts.computing_resources"), "L1": ctx.Tr("cloudbrain.card_type"),
  1214. "M1": ctx.Tr("repo.modelarts.train_job.amount_of_compute_node"), "N1": ctx.Tr("repo.grampus.train_job.ai_center"),
  1215. "O1": ctx.Tr("cloudbrain.resource_specification"), "P1": ctx.Tr("repo.cloudbrain_creator"), "Q1": ctx.Tr("repo.repo_name"),
  1216. "R1": ctx.Tr("repo.cloudbrain_task_name"), "S1": ctx.Tr("repo.modelarts.deletetime")}
  1217. }
  1218. func allCloudbrainValues(row int, rs *models.CloudbrainInfo, ctx *context.Context) map[string]string {
  1219. return map[string]string{getCellName("A", row): rs.DisplayJobName, getCellName("B", row): getCloudbrainType(rs, ctx), getCellName("C", row): rs.Status, getCellName("D", row): rs.JobType,
  1220. getCellName("E", row): time.Unix(int64(rs.Cloudbrain.CreatedUnix), 0).Format(CREATE_TIME_FORMAT), getCellName("F", row): repo.GetCloudbrainWaitTime(rs.Cloudbrain),
  1221. getCellName("G", row): rs.TrainJobDuration, getCellName("H", row): repo.GetCloudbrainCardDuration(rs.Cloudbrain),
  1222. getCellName("I", row): getBrainStartTime(rs),
  1223. getCellName("J", row): getBrainEndTime(rs), getCellName("K", row): rs.ComputeResource, getCellName("L", row): getCloudbrainCardType(rs),
  1224. getCellName("M", row): getWorkServerNum(rs), getCellName("N", row): rs.Cloudbrain.AiCenter,
  1225. getCellName("O", row): getCloudbrainFlavorName(rs), getCellName("P", row): rs.Name,
  1226. getCellName("Q", row): getBrainRepo(rs), getCellName("R", row): rs.JobName, getCellName("S", row): getBrainDeleteTime(rs),
  1227. }
  1228. }
  1229. func getWorkServerNum(rs *models.CloudbrainInfo) string {
  1230. if rs.Cloudbrain.WorkServerNumber >= 1 {
  1231. return fmt.Sprint(rs.Cloudbrain.WorkServerNumber)
  1232. } else {
  1233. return "1"
  1234. }
  1235. }
  1236. func getBrainRepo(rs *models.CloudbrainInfo) string {
  1237. if rs.Repo != nil {
  1238. return rs.Repo.OwnerName + "/" + rs.Repo.Alias
  1239. }
  1240. return ""
  1241. }
  1242. func getBrainStartTime(rs *models.CloudbrainInfo) string {
  1243. timeString := time.Unix(int64(rs.Cloudbrain.StartTime), 0).Format(CREATE_TIME_FORMAT)
  1244. if timeString != "1970/01/01 08:00:00" {
  1245. return timeString
  1246. } else {
  1247. return "0"
  1248. }
  1249. }
  1250. func getBrainEndTime(rs *models.CloudbrainInfo) string {
  1251. timeString := time.Unix(int64(rs.Cloudbrain.EndTime), 0).Format(CREATE_TIME_FORMAT)
  1252. if timeString != "1970/01/01 08:00:00" {
  1253. return timeString
  1254. } else {
  1255. return "0"
  1256. }
  1257. }
  1258. func getCloudbrainType(rs *models.CloudbrainInfo, ctx *context.Context) string {
  1259. if rs.Cloudbrain.Type == models.TypeCloudBrainOne {
  1260. return ctx.Tr("repo.cloudbrain1")
  1261. } else if rs.Cloudbrain.Type == models.TypeCloudBrainTwo {
  1262. return ctx.Tr("repo.cloudbrain2")
  1263. } else if rs.Cloudbrain.Type == models.TypeC2Net {
  1264. return ctx.Tr("repo.intelligent_net")
  1265. } else {
  1266. return ctx.Tr("repo.cloudbrain_untype")
  1267. }
  1268. }
  1269. func getCloudbrainCardType(rs *models.CloudbrainInfo) string {
  1270. if rs.Cloudbrain.Spec != nil {
  1271. return rs.Cloudbrain.Spec.AccCardType
  1272. } else {
  1273. return ""
  1274. }
  1275. }
  1276. func getCloudbrainFlavorName(rs *models.CloudbrainInfo) string {
  1277. flavorName := repo.GetCloudbrainFlavorName(rs.Cloudbrain)
  1278. return flavorName
  1279. }
  1280. func getBrainDeleteTime(rs *models.CloudbrainInfo) string {
  1281. nilTime := time.Time{}
  1282. if rs.Cloudbrain.DeletedAt != nilTime {
  1283. return rs.Cloudbrain.DeletedAt.Format("2006-01-02 15:04:05")
  1284. } else {
  1285. return ""
  1286. }
  1287. }
  1288. func getCloudbrainTimePeroid(ctx *context.Context, recordBeginTime time.Time) (time.Time, time.Time, error) {
  1289. queryType := ctx.QueryTrim("type")
  1290. beginTimeStr := ctx.QueryTrim("beginTime")
  1291. endTimeStr := ctx.QueryTrim("endTime")
  1292. now := time.Now()
  1293. var beginTime time.Time
  1294. var endTime time.Time
  1295. var err error
  1296. if queryType != "" {
  1297. if queryType == "all" {
  1298. beginTime = recordBeginTime
  1299. endTime = now
  1300. } else if queryType == "today" {
  1301. endTime = now
  1302. beginTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  1303. } else if queryType == "yesterday" {
  1304. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  1305. beginTime = endTime.AddDate(0, 0, -1)
  1306. } else if queryType == "last_7day" {
  1307. beginTime = now.AddDate(0, 0, -6)
  1308. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1309. endTime = now
  1310. } else if queryType == "last_30day" {
  1311. beginTime = now.AddDate(0, 0, -29)
  1312. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1313. endTime = now
  1314. } else if queryType == "current_month" {
  1315. endTime = now
  1316. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1317. } else if queryType == "monthly" {
  1318. endTime = now
  1319. beginTime = now.AddDate(0, -1, 1)
  1320. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1321. } else if queryType == "current_year" {
  1322. endTime = now
  1323. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  1324. } else if queryType == "last_month" {
  1325. lastMonthTime := now.AddDate(0, -1, 0)
  1326. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1327. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  1328. } else {
  1329. return now, now, fmt.Errorf("The value of type parameter is wrong.")
  1330. }
  1331. } else {
  1332. if beginTimeStr == "" || endTimeStr == "" {
  1333. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  1334. beginTime = recordBeginTime
  1335. endTime = now
  1336. } else {
  1337. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  1338. if err != nil {
  1339. return now, now, err
  1340. }
  1341. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  1342. if err != nil {
  1343. return now, now, err
  1344. }
  1345. }
  1346. }
  1347. if beginTime.Before(recordBeginTime) {
  1348. beginTime = recordBeginTime
  1349. }
  1350. return beginTime, endTime, nil
  1351. }
  1352. func GetCloudbrainResourceOverview(ctx *context.Context) {
  1353. var recordBeginTime timeutil.TimeStamp
  1354. recordCloudbrainDuration, err := models.GetDurationRecordBeginTime()
  1355. if err != nil {
  1356. log.Error("Can not get GetDurationRecordBeginTime", err)
  1357. return
  1358. }
  1359. if len(recordCloudbrainDuration) > 0 && err == nil {
  1360. recordBeginTime = recordCloudbrainDuration[0].DateTimeUnix
  1361. } else {
  1362. recordBeginTime = timeutil.TimeStamp(time.Now().Unix())
  1363. }
  1364. recordUpdateTime := time.Now().Unix()
  1365. resourceQueues, err := models.GetCanUseCardInfo()
  1366. if err != nil {
  1367. log.Info("GetCanUseCardInfo err: %v", err)
  1368. return
  1369. }
  1370. OpenIResourceDetail := []models.ResourceDetail{}
  1371. C2NetResourceDetail := []models.ResourceDetail{}
  1372. for _, resourceQueue := range resourceQueues {
  1373. if resourceQueue.Cluster == models.OpenICluster {
  1374. aiCenterName := repo.GetAiCenterNameByCode(resourceQueue.AiCenterCode, ctx.Language())
  1375. var resourceDetail models.ResourceDetail
  1376. resourceDetail.QueueCode = resourceQueue.QueueCode
  1377. resourceDetail.Cluster = resourceQueue.Cluster
  1378. resourceDetail.AiCenterCode = resourceQueue.AiCenterCode
  1379. resourceDetail.AiCenterName = resourceQueue.AiCenterCode + "/" + aiCenterName
  1380. resourceDetail.ComputeResource = resourceQueue.ComputeResource
  1381. resourceDetail.AccCardType = resourceQueue.AccCardType + "(" + resourceQueue.ComputeResource + ")"
  1382. resourceDetail.CardsTotalNum = resourceQueue.CardsTotalNum
  1383. resourceDetail.IsAutomaticSync = resourceQueue.IsAutomaticSync
  1384. OpenIResourceDetail = append(OpenIResourceDetail, resourceDetail)
  1385. }
  1386. if resourceQueue.Cluster == models.C2NetCluster {
  1387. aiCenterName := repo.GetAiCenterNameByCode(resourceQueue.AiCenterCode, ctx.Language())
  1388. var resourceDetail models.ResourceDetail
  1389. resourceDetail.QueueCode = resourceQueue.QueueCode
  1390. resourceDetail.Cluster = resourceQueue.Cluster
  1391. resourceDetail.AiCenterCode = resourceQueue.AiCenterCode
  1392. resourceDetail.AiCenterName = resourceQueue.AiCenterCode + "/" + aiCenterName
  1393. resourceDetail.ComputeResource = resourceQueue.ComputeResource
  1394. resourceDetail.AccCardType = resourceQueue.AccCardType + "(" + resourceQueue.ComputeResource + ")"
  1395. resourceDetail.CardsTotalNum = resourceQueue.CardsTotalNum
  1396. resourceDetail.IsAutomaticSync = resourceQueue.IsAutomaticSync
  1397. C2NetResourceDetail = append(C2NetResourceDetail, resourceDetail)
  1398. }
  1399. }
  1400. openIResourceNum := make(map[string]map[string]int)
  1401. for _, openIResourceDetail := range OpenIResourceDetail {
  1402. if _, ok := openIResourceNum[openIResourceDetail.AiCenterName]; !ok {
  1403. openIResourceNum[openIResourceDetail.AiCenterName] = make(map[string]int)
  1404. }
  1405. if _, ok := openIResourceNum[openIResourceDetail.AiCenterName][openIResourceDetail.AccCardType]; !ok {
  1406. openIResourceNum[openIResourceDetail.AiCenterName][openIResourceDetail.AccCardType] = openIResourceDetail.CardsTotalNum
  1407. } else {
  1408. openIResourceNum[openIResourceDetail.AiCenterName][openIResourceDetail.AccCardType] += openIResourceDetail.CardsTotalNum
  1409. }
  1410. }
  1411. c2NetResourceNum := make(map[string]map[string]int)
  1412. for _, c2NetResourceDetail := range C2NetResourceDetail {
  1413. if _, ok := c2NetResourceNum[c2NetResourceDetail.AiCenterName]; !ok {
  1414. c2NetResourceNum[c2NetResourceDetail.AiCenterName] = make(map[string]int)
  1415. }
  1416. if _, ok := c2NetResourceNum[c2NetResourceDetail.AiCenterName][c2NetResourceDetail.AccCardType]; !ok {
  1417. c2NetResourceNum[c2NetResourceDetail.AiCenterName][c2NetResourceDetail.AccCardType] = c2NetResourceDetail.CardsTotalNum
  1418. } else {
  1419. c2NetResourceNum[c2NetResourceDetail.AiCenterName][c2NetResourceDetail.AccCardType] += c2NetResourceDetail.CardsTotalNum
  1420. }
  1421. }
  1422. ctx.JSON(http.StatusOK, map[string]interface{}{
  1423. "openI": openIResourceNum,
  1424. "c2Net": c2NetResourceNum,
  1425. "recordUpdateTime": recordUpdateTime,
  1426. "recordBeginTime": recordBeginTime,
  1427. })
  1428. }
  1429. func GetCloudbrainResourceUsageDetail(ctx *context.Context) {
  1430. aiCenterCode := ctx.QueryTrim("aiCenterCode")
  1431. if aiCenterCode == "" {
  1432. aiCenterCode = models.AICenterOfCloudBrainOne
  1433. }
  1434. beginTime, endTime := getBeginAndEndTime(ctx)
  1435. dayCloudbrainDuration, count, err := getDayCloudbrainDuration(beginTime, endTime, aiCenterCode)
  1436. if err != nil {
  1437. log.Error("Can not query dayCloudbrainDuration.", err)
  1438. return
  1439. }
  1440. hourCloudbrainDuration, err := getHourCloudbrainDuration(beginTime, endTime, aiCenterCode)
  1441. if err != nil {
  1442. log.Error("Can not query hourCloudbrainDuration.", err)
  1443. return
  1444. }
  1445. page := ctx.QueryInt("page")
  1446. if page <= 0 {
  1447. page = 1
  1448. }
  1449. pagesize := ctx.QueryInt("pagesize")
  1450. if pagesize <= 0 {
  1451. pagesize = 36500
  1452. }
  1453. pageDateCloudbrainDuration := getPageDateCloudbrainDuration(dayCloudbrainDuration, page, pagesize)
  1454. ctx.JSON(http.StatusOK, map[string]interface{}{
  1455. "totalCount": count,
  1456. "pageDateCloudbrainDuration": pageDateCloudbrainDuration,
  1457. "hourCloudbrainDuration": hourCloudbrainDuration,
  1458. })
  1459. }
  1460. func GetDurationRateStatistic(ctx *context.Context) {
  1461. beginTime, endTime := getBeginAndEndTime(ctx)
  1462. OpenIDurationRate, C2NetDurationRate, totalUsageRate := getDurationStatistic(beginTime, endTime)
  1463. ctx.JSON(http.StatusOK, map[string]interface{}{
  1464. "openIDurationRate": OpenIDurationRate,
  1465. "c2NetDurationRate": C2NetDurationRate,
  1466. "totalUsageRate": totalUsageRate,
  1467. })
  1468. }
  1469. func CloudbrainDurationStatisticForTest(ctx *context.Context) {
  1470. repo.CloudbrainDurationStatisticHour()
  1471. ctx.JSON(http.StatusOK, map[string]interface{}{
  1472. "message": 0,
  1473. })
  1474. }
  1475. func getBeginAndEndTime(ctx *context.Context) (time.Time, time.Time) {
  1476. queryType := ctx.QueryTrim("type")
  1477. now := time.Now()
  1478. beginTimeStr := ctx.QueryTrim("beginTime")
  1479. endTimeStr := ctx.QueryTrim("endTime")
  1480. var brainRecordBeginTime time.Time
  1481. var beginTime time.Time
  1482. var endTime time.Time
  1483. var err error
  1484. if queryType != "" {
  1485. if queryType == "all" {
  1486. recordCloudbrainDuration, err := models.GetDurationRecordBeginTime()
  1487. if err != nil {
  1488. log.Error("Can not get GetDurationRecordBeginTime", err)
  1489. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  1490. return beginTime, endTime
  1491. }
  1492. if len(recordCloudbrainDuration) > 0 && err == nil {
  1493. brainRecordBeginTime = recordCloudbrainDuration[0].DateTimeUnix.AsTime()
  1494. } else {
  1495. brainRecordBeginTime = now
  1496. }
  1497. beginTime = brainRecordBeginTime
  1498. endTime = now
  1499. } else if queryType == "today" {
  1500. beginTime = now.AddDate(0, 0, 0)
  1501. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1502. endTime = now
  1503. } else if queryType == "yesterday" {
  1504. beginTime = now.AddDate(0, 0, -1)
  1505. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1506. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  1507. } else if queryType == "last_7day" {
  1508. beginTime = now.AddDate(0, 0, -6)
  1509. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1510. endTime = now
  1511. } else if queryType == "last_30day" {
  1512. beginTime = now.AddDate(0, 0, -29)
  1513. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1514. endTime = now
  1515. } else if queryType == "current_month" {
  1516. endTime = now
  1517. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1518. } else if queryType == "current_year" {
  1519. endTime = now
  1520. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  1521. } else if queryType == "last_month" {
  1522. lastMonthTime := now.AddDate(0, -1, 0)
  1523. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1524. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  1525. }
  1526. } else {
  1527. if beginTimeStr == "" || endTimeStr == "" {
  1528. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  1529. recordCloudbrainDuration, err := models.GetDurationRecordBeginTime()
  1530. if err != nil {
  1531. log.Error("Can not get recordCloudbrain", err)
  1532. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  1533. return beginTime, endTime
  1534. }
  1535. if len(recordCloudbrainDuration) > 0 && err == nil {
  1536. brainRecordBeginTime = recordCloudbrainDuration[0].DateTimeUnix.AsTime()
  1537. } else {
  1538. brainRecordBeginTime = now
  1539. }
  1540. beginTime = brainRecordBeginTime
  1541. endTime = now
  1542. } else {
  1543. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  1544. if err != nil {
  1545. log.Error("Can not ParseInLocation.", err)
  1546. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  1547. return beginTime, endTime
  1548. }
  1549. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  1550. if err != nil {
  1551. log.Error("Can not ParseInLocation.", err)
  1552. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  1553. return beginTime, endTime
  1554. }
  1555. if endTime.After(time.Now()) {
  1556. endTime = time.Now()
  1557. }
  1558. }
  1559. }
  1560. return beginTime, endTime
  1561. }
  1562. func getAiCenterUsageDuration(beginTime time.Time, endTime time.Time, cloudbrainStatistics []*models.CloudbrainDurationStatistic) (int, int, float64) {
  1563. totalDuration := int(0)
  1564. usageDuration := int(0)
  1565. usageRate := float64(0)
  1566. for _, cloudbrainStatistic := range cloudbrainStatistics {
  1567. if int64(cloudbrainStatistic.DateTimeUnix) >= beginTime.Unix() && int64(cloudbrainStatistic.DateTimeUnix) < endTime.Unix() {
  1568. totalDuration += cloudbrainStatistic.CardsTotalDuration
  1569. usageDuration += cloudbrainStatistic.CardsUseDuration
  1570. }
  1571. }
  1572. if totalDuration == 0 || usageDuration == 0 {
  1573. usageRate = 0
  1574. } else {
  1575. usageRate = float64(usageDuration) / float64(totalDuration)
  1576. }
  1577. return totalDuration, usageDuration, usageRate
  1578. }
  1579. func getDurationStatistic(beginTime time.Time, endTime time.Time) (models.DurationRateStatistic, models.DurationRateStatistic, float64) {
  1580. OpenITotalDuration := make(map[string]int)
  1581. OpenIUsageDuration := make(map[string]int)
  1582. OpenIUsageRate := make(map[string]float64)
  1583. C2NetTotalDuration := make(map[string]int)
  1584. C2NetUsageDuration := make(map[string]int)
  1585. OpenIDurationRate := models.DurationRateStatistic{}
  1586. C2NetDurationRate := models.DurationRateStatistic{}
  1587. cardDurationStatistics, err := models.GetCardDurationStatistics(&models.DurationStatisticOptions{
  1588. BeginTime: beginTime,
  1589. EndTime: endTime,
  1590. })
  1591. if err != nil {
  1592. log.Error("GetCardDurationStatistics error:", err)
  1593. return OpenIDurationRate, C2NetDurationRate, 0
  1594. }
  1595. for _, cloudbrainStatistic := range cardDurationStatistics {
  1596. aiCenterName := cloudbrainStatistic.AiCenterCode + "/" + repo.GetAiCenterNameByCode(cloudbrainStatistic.AiCenterCode, "zh-CN")
  1597. if cloudbrainStatistic.Cluster == models.OpenICluster {
  1598. if _, ok := OpenITotalDuration[aiCenterName]; !ok {
  1599. OpenITotalDuration[aiCenterName] = cloudbrainStatistic.CardsTotalDuration
  1600. } else {
  1601. OpenITotalDuration[aiCenterName] += cloudbrainStatistic.CardsTotalDuration
  1602. }
  1603. if _, ok := OpenIUsageDuration[aiCenterName]; !ok {
  1604. OpenIUsageDuration[aiCenterName] = cloudbrainStatistic.CardsUseDuration
  1605. } else {
  1606. OpenIUsageDuration[aiCenterName] += cloudbrainStatistic.CardsUseDuration
  1607. }
  1608. }
  1609. if cloudbrainStatistic.Cluster == models.C2NetCluster {
  1610. if _, ok := C2NetTotalDuration[aiCenterName]; !ok {
  1611. C2NetTotalDuration[aiCenterName] = cloudbrainStatistic.CardsTotalDuration
  1612. } else {
  1613. C2NetTotalDuration[aiCenterName] += cloudbrainStatistic.CardsTotalDuration
  1614. }
  1615. if _, ok := C2NetUsageDuration[aiCenterName]; !ok {
  1616. C2NetUsageDuration[aiCenterName] = cloudbrainStatistic.CardsUseDuration
  1617. } else {
  1618. C2NetUsageDuration[aiCenterName] += cloudbrainStatistic.CardsUseDuration
  1619. }
  1620. }
  1621. }
  1622. ResourceAiCenterRes, err := models.GetResourceAiCenters()
  1623. if err != nil {
  1624. log.Error("Can not get ResourceAiCenterRes.", err)
  1625. return OpenIDurationRate, C2NetDurationRate, 0
  1626. }
  1627. for _, v := range ResourceAiCenterRes {
  1628. aiCenterName := v.AiCenterCode + "/" + repo.GetAiCenterNameByCode(v.AiCenterCode, "zh-CN")
  1629. if cutString(v.AiCenterCode, 4) == cutString(models.AICenterOfCloudBrainOne, 4) {
  1630. if _, ok := OpenIUsageDuration[aiCenterName]; !ok {
  1631. OpenIUsageDuration[aiCenterName] = 0
  1632. }
  1633. if _, ok := OpenITotalDuration[aiCenterName]; !ok {
  1634. OpenITotalDuration[aiCenterName] = 0
  1635. }
  1636. } else {
  1637. if _, ok := C2NetUsageDuration[aiCenterName]; !ok {
  1638. C2NetUsageDuration[aiCenterName] = 0
  1639. }
  1640. }
  1641. }
  1642. totalCanUse := float64(0)
  1643. totalUse := float64(0)
  1644. totalUsageRate := float64(0)
  1645. for k, v := range OpenITotalDuration {
  1646. for i, j := range OpenIUsageDuration {
  1647. if k == i {
  1648. OpenIUsageRate[k] = float64(j) / float64(v)
  1649. }
  1650. }
  1651. }
  1652. for _, v := range OpenITotalDuration {
  1653. totalCanUse += float64(v)
  1654. }
  1655. for _, v := range OpenIUsageDuration {
  1656. totalUse += float64(v)
  1657. }
  1658. if totalCanUse == 0 || totalUse == 0 {
  1659. totalUsageRate = 0
  1660. } else {
  1661. totalUsageRate = totalUse / totalCanUse
  1662. }
  1663. delete(C2NetUsageDuration, "/")
  1664. OpenIDurationRate.AiCenterTotalDurationStat = OpenITotalDuration
  1665. OpenIDurationRate.AiCenterUsageDurationStat = OpenIUsageDuration
  1666. OpenIDurationRate.UsageRate = OpenIUsageRate
  1667. C2NetDurationRate.AiCenterTotalDurationStat = C2NetTotalDuration
  1668. C2NetDurationRate.AiCenterUsageDurationStat = C2NetUsageDuration
  1669. return OpenIDurationRate, C2NetDurationRate, totalUsageRate
  1670. }
  1671. func cutString(str string, lens int) string {
  1672. if len(str) < lens {
  1673. return str
  1674. }
  1675. return str[:lens]
  1676. }
  1677. func getDayCloudbrainDuration(beginTime time.Time, endTime time.Time, aiCenterCode string) ([]models.DateUsageStatistic, int, error) {
  1678. now := time.Now()
  1679. endTimeTemp := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  1680. if endTimeTemp.Equal(endTime) {
  1681. endTimeTemp = endTimeTemp.AddDate(0, 0, -1)
  1682. }
  1683. cardDurationStatistics, err := models.GetCardDurationStatistics(&models.DurationStatisticOptions{
  1684. BeginTime: beginTime,
  1685. EndTime: endTime,
  1686. AiCenterCode: aiCenterCode,
  1687. })
  1688. if err != nil {
  1689. log.Error("GetCardDurationStatistics error:", err)
  1690. return nil, 0, err
  1691. }
  1692. dayCloudbrainInfo := make([]models.DateUsageStatistic, 0)
  1693. count := 0
  1694. for beginTime.Before(endTimeTemp) || beginTime.Equal(endTimeTemp) {
  1695. TotalDuration, UsageDuration, UsageRate := getAiCenterUsageDuration(endTimeTemp, endTime, cardDurationStatistics)
  1696. dayCloudbrainInfo = append(dayCloudbrainInfo, models.DateUsageStatistic{
  1697. Date: endTimeTemp.Format("2006/01/02"),
  1698. UsageDuration: UsageDuration,
  1699. TotalDuration: TotalDuration,
  1700. UsageRate: UsageRate,
  1701. })
  1702. endTime = endTimeTemp
  1703. endTimeTemp = endTimeTemp.AddDate(0, 0, -1)
  1704. if endTimeTemp.Before(beginTime) && beginTime.Before(endTime) {
  1705. endTimeTemp = beginTime
  1706. }
  1707. count += 1
  1708. }
  1709. return dayCloudbrainInfo, count, nil
  1710. }
  1711. func getHourCloudbrainDuration(beginTime time.Time, endTime time.Time, aiCenterCode string) (models.HourTimeStatistic, error) {
  1712. hourTimeTotalDuration := make(map[string]int)
  1713. hourTimeUsageDuration := make(map[string]int)
  1714. hourTimeUsageRate := make(map[string]float64)
  1715. hourTimeStatistic := models.HourTimeStatistic{}
  1716. cardDurationStatistics, err := models.GetCardDurationStatistics(&models.DurationStatisticOptions{
  1717. BeginTime: beginTime,
  1718. EndTime: endTime,
  1719. })
  1720. if err != nil {
  1721. log.Error("GetCardDurationStatistics error:", err)
  1722. return hourTimeStatistic, err
  1723. }
  1724. for _, cloudbrainStatistic := range cardDurationStatistics {
  1725. if cloudbrainStatistic.AiCenterCode == aiCenterCode {
  1726. if _, ok := hourTimeTotalDuration[strconv.Itoa(cloudbrainStatistic.HourTime)]; !ok {
  1727. hourTimeTotalDuration[strconv.Itoa(cloudbrainStatistic.HourTime)] = cloudbrainStatistic.CardsTotalDuration
  1728. } else {
  1729. hourTimeTotalDuration[strconv.Itoa(cloudbrainStatistic.HourTime)] += cloudbrainStatistic.CardsTotalDuration
  1730. }
  1731. if _, ok := hourTimeUsageDuration[strconv.Itoa(cloudbrainStatistic.HourTime)]; !ok {
  1732. hourTimeUsageDuration[strconv.Itoa(cloudbrainStatistic.HourTime)] = cloudbrainStatistic.CardsUseDuration
  1733. } else {
  1734. hourTimeUsageDuration[strconv.Itoa(cloudbrainStatistic.HourTime)] += cloudbrainStatistic.CardsUseDuration
  1735. }
  1736. }
  1737. }
  1738. hourTimeList := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"}
  1739. for _, v := range hourTimeList {
  1740. if _, ok := hourTimeUsageDuration[v]; !ok {
  1741. hourTimeUsageDuration[v] = 0
  1742. }
  1743. if _, ok := hourTimeTotalDuration[v]; !ok {
  1744. hourTimeTotalDuration[v] = 0
  1745. }
  1746. }
  1747. for k, v := range hourTimeTotalDuration {
  1748. for i, j := range hourTimeUsageDuration {
  1749. if k == i {
  1750. if v == 0 || j == 0 {
  1751. hourTimeUsageRate[k] = 0
  1752. } else {
  1753. hourTimeUsageRate[k] = float64(j) / float64(v)
  1754. }
  1755. }
  1756. }
  1757. }
  1758. hourTimeStatistic.HourTimeTotalDuration = hourTimeTotalDuration
  1759. hourTimeStatistic.HourTimeUsageDuration = hourTimeUsageDuration
  1760. hourTimeStatistic.HourTimeUsageRate = hourTimeUsageRate
  1761. return hourTimeStatistic, nil
  1762. }
  1763. func CloudbrainUpdateAiCenter(ctx *context.Context) {
  1764. repo.CloudbrainDurationStatisticHour()
  1765. ctx.JSON(http.StatusOK, map[string]interface{}{
  1766. "message": 0,
  1767. })
  1768. }
  1769. func GetResourceQueues(ctx *context.Context) {
  1770. resourceQueues, err := models.GetCanUseCardInfo()
  1771. if err != nil {
  1772. log.Error("GetCanUseCardInfo err: %v", err)
  1773. return
  1774. }
  1775. Resource := make([]*models.ResourceQueue, 0)
  1776. aiCenterCodeMap := make(map[string]string)
  1777. for _, resourceQueue := range resourceQueues {
  1778. if _, ok := aiCenterCodeMap[resourceQueue.AiCenterCode]; !ok {
  1779. resourceQueue.AiCenterName = repo.GetAiCenterNameByCode(resourceQueue.AiCenterCode, ctx.Language())
  1780. aiCenterCodeMap[resourceQueue.AiCenterCode] = resourceQueue.AiCenterCode
  1781. Resource = append(Resource, resourceQueue)
  1782. }
  1783. }
  1784. ctx.JSON(http.StatusOK, map[string]interface{}{
  1785. "resourceQueues": Resource,
  1786. })
  1787. }