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