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