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

cloudbrain_dashboard.go 72 kB

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