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 51 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "github.com/360EntSecGroup-Skylar/excelize/v2"
  12. )
  13. type DateCloudbrainNum struct {
  14. Date string `json:"date"`
  15. CloudOneJobTypeRes map[string]int `json:"cloudOneJobTypeRes"`
  16. CloudTwoJobTypeRes map[string]int `json:"cloudTwoJobTypeRes"`
  17. IntelligentNetJobTypeRes map[string]int `json:"intelligentNetJobTypeRes"`
  18. CloudBrainPeriodNum map[int]int `json:"cloudBrainPeriodNum"`
  19. CloudBrainComputeResource map[string]int `json:"cloudBrainComputeResource"`
  20. }
  21. type DateCloudbrainInfo struct {
  22. Date string `json:"date"`
  23. CloudBrainPeriodNum map[int]int `json:"cloudBrainPeriodNum"`
  24. CloudBrainComputeResource map[string]int `json:"cloudBrainComputeResource"`
  25. }
  26. func GetAllCloudbrainsOverview(ctx *context.Context) {
  27. recordCloudbrain, err := models.GetRecordBeginTime()
  28. if err != nil {
  29. log.Error("Can not get recordCloudbrain", err)
  30. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  31. return
  32. }
  33. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  34. now := time.Now()
  35. beginTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  36. endTime := now
  37. todayCreatorCount, err := models.GetTodayCreatorCount(beginTime, endTime)
  38. if err != nil {
  39. log.Error("Can not query todayCreatorCount.", err)
  40. return
  41. }
  42. creatorCount, err := models.GetCreatorCount()
  43. if err != nil {
  44. log.Error("Can not query creatorCount.", err)
  45. return
  46. }
  47. todayStatusResult := make(map[string]int)
  48. cloudBrainNum := make(map[int]int)
  49. cloudBrainOneDuration := int64(0)
  50. cloudBrainTwoDuration := int64(0)
  51. intelligentNetDuration := int64(0)
  52. todayNewJobCount := 0
  53. page := 1
  54. pagesize := 1000
  55. count := pagesize
  56. for count == pagesize && count != 0 {
  57. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  58. ListOptions: models.ListOptions{
  59. Page: page,
  60. PageSize: pagesize,
  61. },
  62. Type: models.TypeCloudBrainAll,
  63. BeginTimeUnix: int64(recordBeginTime),
  64. EndTimeUnix: endTime.Unix(),
  65. })
  66. if err != nil {
  67. ctx.ServerError("Get cloudbrains failed:", err)
  68. return
  69. }
  70. for _, cloudbrain := range cloudbrains {
  71. if int64(cloudbrain.Cloudbrain.CreatedUnix) >= beginTime.Unix() && int64(cloudbrain.Cloudbrain.CreatedUnix) < endTime.Unix() {
  72. todayNewJobCount += 1
  73. if _, ok := todayStatusResult[cloudbrain.Status]; !ok {
  74. todayStatusResult[cloudbrain.Status] = 1
  75. } else {
  76. todayStatusResult[cloudbrain.Status] += 1
  77. }
  78. }
  79. if _, ok := cloudBrainNum[cloudbrain.Cloudbrain.Type]; !ok {
  80. cloudBrainNum[cloudbrain.Cloudbrain.Type] = 1
  81. } else {
  82. cloudBrainNum[cloudbrain.Cloudbrain.Type] += 1
  83. }
  84. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne {
  85. cloudBrainOneDuration = cloudBrainOneDuration + cloudbrain.Cloudbrain.Duration
  86. }
  87. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo {
  88. cloudBrainTwoDuration = cloudBrainTwoDuration + cloudbrain.Cloudbrain.Duration
  89. }
  90. if cloudbrain.Cloudbrain.Type == models.TypeIntelligentNet {
  91. intelligentNetDuration = intelligentNetDuration + cloudbrain.Cloudbrain.Duration
  92. }
  93. }
  94. count = len(cloudbrains)
  95. page += 1
  96. }
  97. statusNameList := []string{string(models.ModelArtsTrainJobCompleted), string(models.JobFailed), string(models.ModelArtsTrainJobInit),
  98. string(models.JobRunning), string(models.ModelArtsStartFailed), string(models.JobStopped), string(models.JobSucceeded),
  99. string(models.JobWaiting), string(models.ModelArtsTrainJobKilled)}
  100. for _, v := range statusNameList {
  101. if _, ok := todayStatusResult[v]; !ok {
  102. todayStatusResult[v] = 0
  103. }
  104. }
  105. todayRunningCount := todayStatusResult[string(models.JobRunning)]
  106. todayCompletedCount := todayStatusResult[string(models.ModelArtsTrainJobCompleted)] + todayStatusResult[string(models.JobFailed)] +
  107. todayStatusResult[string(models.ModelArtsStartFailed)] + todayStatusResult[string(models.JobStopped)] + todayStatusResult[string(models.JobSucceeded)] + todayStatusResult[string(models.ModelArtsTrainJobKilled)]
  108. todayWaitingCount := todayStatusResult[string(models.ModelArtsTrainJobInit)] + todayStatusResult[string(models.JobWaiting)]
  109. ctx.JSON(http.StatusOK, map[string]interface{}{
  110. "recordBeginTime": recordBeginTime,
  111. "updateTime": now.Unix(),
  112. "cloudBrainNum": cloudBrainNum,
  113. "cloudBrainOneDuration": cloudBrainOneDuration,
  114. "cloudBrainTwoDuration": cloudBrainTwoDuration,
  115. "intelligentNetDuration": intelligentNetDuration,
  116. "todayCreatorCount": todayCreatorCount,
  117. "creatorCount": creatorCount,
  118. "todayRunningCount": todayRunningCount,
  119. "todayCompletedCount": todayCompletedCount,
  120. "todayWaitingCount": todayWaitingCount,
  121. "todayNewJobCount": todayNewJobCount,
  122. })
  123. }
  124. func GetAllCloudbrainsTrend(ctx *context.Context) {
  125. queryType := ctx.QueryTrim("type")
  126. now := time.Now()
  127. beginTimeStr := ctx.QueryTrim("beginTime")
  128. endTimeStr := ctx.QueryTrim("endTime")
  129. var beginTime time.Time
  130. var endTime time.Time
  131. dateCloudbrainNum := make([]DateCloudbrainNum, 0)
  132. var err error
  133. if queryType != "" {
  134. if queryType == "all" {
  135. recordCloudbrain, err := models.GetRecordBeginTime()
  136. if err != nil {
  137. log.Error("Can not get recordCloudbrain", err)
  138. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  139. return
  140. }
  141. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  142. beginTime = brainRecordBeginTime
  143. endTime = now
  144. dateCloudbrainNum, err = getMonthCloudbrainNum(beginTime, endTime)
  145. if err != nil {
  146. log.Error("Can not query getMonthCloudbrainNum.", err)
  147. ctx.Error(http.StatusBadRequest, ctx.Tr("getMonthCloudbrainNum_get_error"))
  148. return
  149. }
  150. } else if queryType == "today" {
  151. beginTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  152. // endTimeTemp = now
  153. endTime = beginTime.AddDate(0, 0, 1)
  154. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  155. if err != nil {
  156. log.Error("Can not query getDayCloudbrainNum.", err)
  157. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  158. return
  159. }
  160. } else if queryType == "yesterday" {
  161. beginTime = now.AddDate(0, 0, -1)
  162. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  163. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  164. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  165. if err != nil {
  166. log.Error("Can not query getDayCloudbrainNum.", err)
  167. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  168. return
  169. }
  170. } else if queryType == "last_7day" {
  171. beginTime = now.AddDate(0, 0, -6)
  172. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  173. endTime = now
  174. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  175. if err != nil {
  176. log.Error("Can not query getDayCloudbrainNum.", err)
  177. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  178. return
  179. }
  180. } else if queryType == "last_30day" {
  181. beginTime = now.AddDate(0, 0, -29)
  182. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  183. endTime = now
  184. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  185. if err != nil {
  186. log.Error("Can not query getDayCloudbrainNum.", err)
  187. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  188. return
  189. }
  190. } else if queryType == "current_month" {
  191. endTime = now
  192. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  193. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  194. if err != nil {
  195. log.Error("Can not query getDayCloudbrainNum.", err)
  196. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  197. return
  198. }
  199. } else if queryType == "current_year" {
  200. endTime = now
  201. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  202. // endTimeTemp = beginTime.AddDate(0, 1, 0)
  203. dateCloudbrainNum, err = getMonthCloudbrainNum(beginTime, endTime)
  204. if err != nil {
  205. log.Error("Can not query getMonthCloudbrainNum.", err)
  206. ctx.Error(http.StatusBadRequest, ctx.Tr("getMonthCloudbrainNum_get_error"))
  207. return
  208. }
  209. } else if queryType == "last_month" {
  210. lastMonthTime := now.AddDate(0, -1, 0)
  211. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  212. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  213. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  214. if err != nil {
  215. log.Error("Can not query getDayCloudbrainNum.", err)
  216. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  217. return
  218. }
  219. }
  220. } else {
  221. if beginTimeStr == "" || endTimeStr == "" {
  222. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  223. recordCloudbrain, err := models.GetRecordBeginTime()
  224. if err != nil {
  225. log.Error("Can not get recordCloudbrain", err)
  226. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  227. return
  228. }
  229. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  230. beginTime = brainRecordBeginTime
  231. endTime = now
  232. dateCloudbrainNum, err = getMonthCloudbrainNum(beginTime, endTime)
  233. if err != nil {
  234. log.Error("Can not query getMonthCloudbrainNum.", err)
  235. ctx.Error(http.StatusBadRequest, ctx.Tr("getMonthCloudbrainNum_get_error"))
  236. return
  237. }
  238. } else {
  239. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  240. if err != nil {
  241. log.Error("Can not ParseInLocation.", err)
  242. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  243. return
  244. }
  245. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  246. if err != nil {
  247. log.Error("Can not ParseInLocation.", err)
  248. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  249. return
  250. }
  251. if endTime.After(time.Now()) {
  252. endTime = time.Now()
  253. }
  254. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTime)
  255. if err != nil {
  256. log.Error("Can not query getDayCloudbrainNum.", err)
  257. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  258. return
  259. }
  260. }
  261. }
  262. ctx.JSON(http.StatusOK, map[string]interface{}{
  263. "timeCloudbrainNum": dateCloudbrainNum,
  264. })
  265. }
  266. func GetAllCloudbrainsTrendDetail(ctx *context.Context) {
  267. queryType := ctx.QueryTrim("type")
  268. now := time.Now()
  269. beginTimeStr := ctx.QueryTrim("beginTime")
  270. endTimeStr := ctx.QueryTrim("endTime")
  271. var beginTime time.Time
  272. var endTime time.Time
  273. var endTimeTemp time.Time
  274. dayCloudbrainInfo := make([]DateCloudbrainInfo, 0)
  275. var err error
  276. var count int
  277. if queryType != "" {
  278. if queryType == "all" {
  279. recordCloudbrain, err := models.GetRecordBeginTime()
  280. if err != nil {
  281. log.Error("Can not get recordCloudbrain", err)
  282. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  283. return
  284. }
  285. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  286. beginTime = brainRecordBeginTime
  287. endTime = now
  288. dayCloudbrainInfo, count, err = getMonthCloudbrainInfo(beginTime, endTime)
  289. if err != nil {
  290. log.Error("Can not query getDayCloudbrainInfo.", err)
  291. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  292. return
  293. }
  294. } else if queryType == "today" {
  295. beginTime = now.AddDate(0, 0, 0)
  296. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  297. endTime = now
  298. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  299. if err != nil {
  300. log.Error("Can not query getDayCloudbrainInfo.", err)
  301. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  302. return
  303. }
  304. } else if queryType == "yesterday" {
  305. beginTime = now.AddDate(0, 0, -1)
  306. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  307. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  308. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  309. if err != nil {
  310. log.Error("Can not query getDayCloudbrainInfo.", err)
  311. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  312. return
  313. }
  314. } else if queryType == "last_7day" {
  315. beginTime = now.AddDate(0, 0, -6)
  316. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  317. endTime = now
  318. endTimeTemp = time.Date(endTimeTemp.Year(), endTimeTemp.Month(), endTimeTemp.Day(), 0, 0, 0, 0, now.Location())
  319. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  320. if err != nil {
  321. log.Error("Can not query getDayCloudbrainInfo.", err)
  322. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  323. return
  324. }
  325. } else if queryType == "last_30day" {
  326. beginTime = now.AddDate(0, 0, -29)
  327. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  328. endTime = now
  329. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  330. if err != nil {
  331. log.Error("Can not query getDayCloudbrainInfo.", err)
  332. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  333. return
  334. }
  335. } else if queryType == "current_month" {
  336. endTime = now
  337. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  338. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  339. if err != nil {
  340. log.Error("Can not query getDayCloudbrainInfo.", err)
  341. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  342. return
  343. }
  344. } else if queryType == "current_year" {
  345. endTime = now
  346. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  347. dayCloudbrainInfo, count, err = getMonthCloudbrainInfo(beginTime, endTime)
  348. if err != nil {
  349. log.Error("Can not query getDayCloudbrainInfo.", err)
  350. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  351. return
  352. }
  353. } else if queryType == "last_month" {
  354. lastMonthTime := now.AddDate(0, -1, 0)
  355. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  356. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  357. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  358. if err != nil {
  359. log.Error("Can not query getDayCloudbrainInfo.", err)
  360. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  361. return
  362. }
  363. }
  364. } else {
  365. if beginTimeStr == "" || endTimeStr == "" {
  366. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  367. recordCloudbrain, err := models.GetRecordBeginTime()
  368. if err != nil {
  369. log.Error("Can not get recordCloudbrain", err)
  370. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  371. return
  372. }
  373. brainRecordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix.AsTime()
  374. beginTime = brainRecordBeginTime
  375. endTime = now
  376. dayCloudbrainInfo, count, err = getMonthCloudbrainInfo(beginTime, endTime)
  377. if err != nil {
  378. log.Error("Can not query getDayCloudbrainInfo.", err)
  379. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  380. return
  381. }
  382. } else {
  383. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  384. if err != nil {
  385. log.Error("Can not ParseInLocation.", err)
  386. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  387. return
  388. }
  389. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  390. if err != nil {
  391. log.Error("Can not ParseInLocation.", err)
  392. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  393. return
  394. }
  395. if endTime.After(time.Now()) {
  396. endTime = time.Now()
  397. }
  398. endTimeTemp = beginTime.AddDate(0, 0, 1)
  399. dayCloudbrainInfo, count, err = getDayCloudbrainInfo(beginTime, endTime)
  400. if err != nil {
  401. log.Error("Can not query getDayCloudbrainInfo.", err)
  402. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainInfo_get_error"))
  403. return
  404. }
  405. }
  406. }
  407. page := ctx.QueryInt("page")
  408. if page <= 0 {
  409. page = 1
  410. }
  411. pagesize := ctx.QueryInt("pagesize")
  412. if pagesize <= 0 {
  413. pagesize = 5
  414. }
  415. pageDateCloudbrainInfo := getPageDateCloudbrainInfo(dayCloudbrainInfo, page, pagesize)
  416. ctx.JSON(http.StatusOK, map[string]interface{}{
  417. "totalCount": count,
  418. "timeCloudbrainInfo": pageDateCloudbrainInfo,
  419. })
  420. }
  421. func getPageDateCloudbrainInfo(dateCloudbrainInfo []DateCloudbrainInfo, page int, pagesize int) []DateCloudbrainInfo {
  422. begin := (page - 1) * pagesize
  423. end := (page) * pagesize
  424. if begin > len(dateCloudbrainInfo)-1 {
  425. return nil
  426. }
  427. if end > len(dateCloudbrainInfo)-1 {
  428. return dateCloudbrainInfo[begin:]
  429. } else {
  430. return dateCloudbrainInfo[begin:end]
  431. }
  432. }
  433. func GetAllCloudbrainsPeriodDistribution(ctx *context.Context) {
  434. queryType := ctx.QueryTrim("type")
  435. beginTimeStr := ctx.QueryTrim("beginTime")
  436. endTimeStr := ctx.QueryTrim("endTime")
  437. recordCloudbrain, err := models.GetRecordBeginTime()
  438. if err != nil {
  439. log.Error("Can not get record begin time", err)
  440. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  441. return
  442. }
  443. recordBeginTime := time.Unix(int64(recordCloudbrain[0].Cloudbrain.CreatedUnix), 0)
  444. beginTime, endTime, err := getCloudbrainTimePeroid(ctx, recordBeginTime)
  445. if err != nil {
  446. log.Error("Parameter is wrong", err)
  447. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong"))
  448. return
  449. }
  450. cloudOneJobTypeRes := make(map[string]int)
  451. cloudTwoJobTypeRes := make(map[string]int)
  452. intelligentNetJobTypeRes := make(map[string]int)
  453. cloudBrainPeriodNum := make(map[int]int)
  454. cloudBrainComputeResource := make(map[string]int)
  455. beginTimeTemp := beginTime.Unix()
  456. if queryType == "all" || (queryType == "" && (beginTimeStr == "" || endTimeStr == "")) {
  457. beginTimeTemp = int64(recordCloudbrain[0].Cloudbrain.CreatedUnix)
  458. }
  459. page := 1
  460. pagesize := 1000
  461. count := pagesize
  462. //Each time a maximum of 1000 pieces of data are detected to the memory, batch processing
  463. for count == pagesize && count != 0 {
  464. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  465. ListOptions: models.ListOptions{
  466. Page: page,
  467. PageSize: pagesize,
  468. },
  469. Type: models.TypeCloudBrainAll,
  470. BeginTimeUnix: beginTimeTemp,
  471. EndTimeUnix: endTime.Unix(),
  472. })
  473. if err != nil {
  474. ctx.ServerError("Get cloudbrains failed:", err)
  475. return
  476. }
  477. for _, cloudbrain := range cloudbrains {
  478. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne {
  479. if _, ok := cloudOneJobTypeRes[cloudbrain.JobType]; !ok {
  480. cloudOneJobTypeRes[cloudbrain.JobType] = 1
  481. } else {
  482. cloudOneJobTypeRes[cloudbrain.JobType] += 1
  483. }
  484. }
  485. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo {
  486. if _, ok := cloudTwoJobTypeRes[cloudbrain.JobType]; !ok {
  487. cloudTwoJobTypeRes[cloudbrain.JobType] = 1
  488. } else {
  489. cloudTwoJobTypeRes[cloudbrain.JobType] += 1
  490. }
  491. }
  492. if cloudbrain.Cloudbrain.Type == models.TypeIntelligentNet {
  493. if _, ok := intelligentNetJobTypeRes[cloudbrain.JobType]; !ok {
  494. intelligentNetJobTypeRes[cloudbrain.JobType] = 1
  495. } else {
  496. intelligentNetJobTypeRes[cloudbrain.JobType] += 1
  497. }
  498. }
  499. if _, ok := cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type]; !ok {
  500. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] = 1
  501. } else {
  502. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] += 1
  503. }
  504. if _, ok := cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource]; !ok {
  505. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] = 1
  506. } else {
  507. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] += 1
  508. }
  509. }
  510. count = len(cloudbrains)
  511. page += 1
  512. }
  513. jobTypeList := []string{string(models.JobTypeDebug), string(models.JobTypeTrain), string(models.JobTypeInference), string(models.JobTypeBenchmark),
  514. string(models.JobTypeBrainScore), string(models.JobTypeSnn4imagenet)}
  515. for _, v := range jobTypeList {
  516. if _, ok := cloudOneJobTypeRes[v]; !ok {
  517. cloudOneJobTypeRes[v] = 0
  518. }
  519. if _, ok := cloudTwoJobTypeRes[v]; !ok {
  520. cloudTwoJobTypeRes[v] = 0
  521. }
  522. if _, ok := intelligentNetJobTypeRes[v]; !ok {
  523. intelligentNetJobTypeRes[v] = 0
  524. }
  525. }
  526. cloudBrainTypeList := []int{0, 1, 2}
  527. for _, v := range cloudBrainTypeList {
  528. if _, ok := cloudBrainPeriodNum[v]; !ok {
  529. cloudBrainPeriodNum[v] = 0
  530. }
  531. }
  532. ComputeResourceList := []string{"CPU/GPU", "NPU"}
  533. for _, v := range ComputeResourceList {
  534. if _, ok := cloudBrainComputeResource[v]; !ok {
  535. cloudBrainComputeResource[v] = 0
  536. }
  537. }
  538. cloudOneJobTypeRes["EVALUATION"] = cloudOneJobTypeRes[string(models.JobTypeBenchmark)] + cloudOneJobTypeRes[string(models.JobTypeSnn4imagenet)] + cloudOneJobTypeRes[string(models.JobTypeBrainScore)]
  539. cloudTwoJobTypeRes["EVALUATION"] = cloudTwoJobTypeRes[string(models.JobTypeBenchmark)] + cloudTwoJobTypeRes[string(models.JobTypeSnn4imagenet)] + cloudTwoJobTypeRes[string(models.JobTypeBrainScore)]
  540. intelligentNetJobTypeRes["EVALUATION"] = intelligentNetJobTypeRes[string(models.JobTypeBenchmark)] + intelligentNetJobTypeRes[string(models.JobTypeSnn4imagenet)] + intelligentNetJobTypeRes[string(models.JobTypeBrainScore)]
  541. ctx.JSON(http.StatusOK, map[string]interface{}{
  542. "cloudOneJobTypeRes": cloudOneJobTypeRes,
  543. "cloudTwoJobTypeRes": cloudTwoJobTypeRes,
  544. "intelligentNetJobTypeRes": intelligentNetJobTypeRes,
  545. "cloudBrainPeriodNum": cloudBrainPeriodNum,
  546. "cloudBrainComputeResource": cloudBrainComputeResource,
  547. })
  548. }
  549. func GetCloudbrainsStatusAnalysis(ctx *context.Context) {
  550. recordCloudbrain, err := models.GetRecordBeginTime()
  551. if err != nil {
  552. log.Error("Can not get recordCloudbrain", err)
  553. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  554. return
  555. }
  556. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  557. endTime := time.Now()
  558. cloudBrainStatusResult := make(map[string]int)
  559. cloudBrainStatusAnalysis := make(map[string]int)
  560. totalCount := 0
  561. page := 1
  562. pagesize := 1000
  563. count := pagesize
  564. for count == pagesize && count != 0 {
  565. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  566. ListOptions: models.ListOptions{
  567. Page: page,
  568. PageSize: pagesize,
  569. },
  570. Type: models.TypeCloudBrainAll,
  571. BeginTimeUnix: int64(recordBeginTime),
  572. EndTimeUnix: endTime.Unix(),
  573. })
  574. if err != nil {
  575. ctx.ServerError("Get cloudbrains failed:", err)
  576. return
  577. }
  578. for _, cloudbrain := range cloudbrains {
  579. if _, ok := cloudBrainStatusResult[cloudbrain.Status]; !ok {
  580. cloudBrainStatusResult[cloudbrain.Status] = 1
  581. } else {
  582. cloudBrainStatusResult[cloudbrain.Status] += 1
  583. }
  584. }
  585. count = len(cloudbrains)
  586. totalCount = totalCount + count
  587. page += 1
  588. }
  589. var jobStatuses []string
  590. jobStatuses = append(jobStatuses, string(models.ModelArtsTrainJobWaiting), string(models.ModelArtsTrainJobFailed), string(models.ModelArtsRunning), string(models.ModelArtsTrainJobCompleted),
  591. string(models.ModelArtsStarting), string(models.ModelArtsRestarting), string(models.ModelArtsStartFailed),
  592. string(models.ModelArtsStopping), string(models.ModelArtsStopped), string(models.JobSucceeded))
  593. jobStatusesCount := 0
  594. for _, v := range jobStatuses {
  595. if _, ok := cloudBrainStatusResult[v]; !ok {
  596. cloudBrainStatusAnalysis[v] = 0
  597. } else {
  598. cloudBrainStatusAnalysis[v] = cloudBrainStatusResult[v]
  599. }
  600. jobStatusesCount = jobStatusesCount + cloudBrainStatusResult[v]
  601. }
  602. cloudBrainStatusAnalysis["OTHER"] = totalCount - jobStatusesCount
  603. ctx.JSON(http.StatusOK, map[string]interface{}{
  604. "cloudBrainStatusResult": cloudBrainStatusAnalysis,
  605. })
  606. }
  607. func GetCloudbrainsDetailData(ctx *context.Context) {
  608. recordCloudbrain, err := models.GetRecordBeginTime()
  609. if err != nil {
  610. log.Error("Can not get recordCloudbrain", err)
  611. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  612. return
  613. }
  614. recordBeginTime := recordCloudbrain[0].Cloudbrain.CreatedUnix
  615. endTime := time.Now()
  616. listType := ctx.Query("listType")
  617. jobType := ctx.Query("jobType")
  618. jobStatus := ctx.Query("jobStatus")
  619. cloudBrainType := ctx.QueryInt("Type")
  620. page := ctx.QueryInt("page")
  621. pageSize := ctx.QueryInt("pagesize")
  622. if page <= 0 {
  623. page = 1
  624. }
  625. if pageSize <= 0 {
  626. pageSize = 10
  627. }
  628. var jobTypes []string
  629. jobTypeNot := false
  630. if jobType == string(models.JobTypeBenchmark) {
  631. jobTypes = append(jobTypes, string(models.JobTypeBenchmark), string(models.JobTypeBrainScore), string(models.JobTypeSnn4imagenet))
  632. } else if jobType != "all" && jobType != "" {
  633. jobTypes = append(jobTypes, jobType)
  634. }
  635. var jobStatuses []string
  636. jobStatusNot := false
  637. if jobStatus == "other" {
  638. jobStatusNot = true
  639. jobStatuses = append(jobStatuses, string(models.ModelArtsTrainJobWaiting), string(models.ModelArtsTrainJobFailed), string(models.ModelArtsRunning), string(models.ModelArtsTrainJobCompleted),
  640. string(models.ModelArtsStarting), string(models.ModelArtsRestarting), string(models.ModelArtsStartFailed),
  641. string(models.ModelArtsStopping), string(models.ModelArtsStopped), string(models.JobSucceeded))
  642. } else if jobStatus != "all" && jobStatus != "" {
  643. jobStatuses = append(jobStatuses, jobStatus)
  644. }
  645. keyword := strings.Trim(ctx.Query("q"), " ")
  646. ciTasks, count, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  647. ListOptions: models.ListOptions{
  648. Page: page,
  649. PageSize: pageSize,
  650. },
  651. Keyword: keyword,
  652. Type: cloudBrainType,
  653. ComputeResource: listType,
  654. JobTypeNot: jobTypeNot,
  655. JobStatusNot: jobStatusNot,
  656. JobStatus: jobStatuses,
  657. JobTypes: jobTypes,
  658. NeedRepoInfo: true,
  659. BeginTimeUnix: int64(recordBeginTime),
  660. EndTimeUnix: endTime.Unix(),
  661. })
  662. if err != nil {
  663. ctx.ServerError("Get job failed:", err)
  664. return
  665. }
  666. nilTime := time.Time{}
  667. tasks := []models.TaskDetail{}
  668. for i, task := range ciTasks {
  669. ciTasks[i].Cloudbrain.ComputeResource = task.ComputeResource
  670. var taskDetail models.TaskDetail
  671. taskDetail.ID = ciTasks[i].Cloudbrain.ID
  672. taskDetail.JobName = ciTasks[i].JobName
  673. taskDetail.DisplayJobName = ciTasks[i].DisplayJobName
  674. taskDetail.Status = ciTasks[i].Status
  675. taskDetail.JobType = ciTasks[i].JobType
  676. taskDetail.CreatedUnix = ciTasks[i].Cloudbrain.CreatedUnix
  677. taskDetail.RunTime = ciTasks[i].Cloudbrain.TrainJobDuration
  678. taskDetail.StartTime = ciTasks[i].StartTime
  679. taskDetail.EndTime = ciTasks[i].EndTime
  680. taskDetail.ComputeResource = ciTasks[i].ComputeResource
  681. taskDetail.Type = ciTasks[i].Cloudbrain.Type
  682. taskDetail.UserName = ciTasks[i].User.Name
  683. taskDetail.RepoID = ciTasks[i].RepoID
  684. if ciTasks[i].Repo != nil {
  685. taskDetail.RepoName = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Name
  686. taskDetail.RepoAlias = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Alias
  687. }
  688. if ciTasks[i].Cloudbrain.Status == string(models.JobWaiting) {
  689. WaitTimeInt := time.Now().Unix() - ciTasks[i].Cloudbrain.CreatedUnix.AsTime().Unix()
  690. taskDetail.WaitTime = models.ConvertDurationToStr(WaitTimeInt)
  691. if WaitTimeInt < 0 {
  692. taskDetail.WaitTime = "00:00:00"
  693. }
  694. } else if ciTasks[i].Cloudbrain.Status == string(models.JobStopped) && ciTasks[i].Cloudbrain.StartTime.AsTime().Unix() == 0 {
  695. WaitTimeInt := ciTasks[i].Cloudbrain.EndTime.AsTime().Unix() - ciTasks[i].Cloudbrain.CreatedUnix.AsTime().Unix()
  696. taskDetail.WaitTime = models.ConvertDurationToStr(WaitTimeInt)
  697. if WaitTimeInt < 0 {
  698. taskDetail.WaitTime = "00:00:00"
  699. }
  700. } else {
  701. WaitTimeInt := ciTasks[i].Cloudbrain.StartTime.AsTime().Unix() - ciTasks[i].Cloudbrain.CreatedUnix.AsTime().Unix()
  702. taskDetail.WaitTime = models.ConvertDurationToStr(WaitTimeInt)
  703. if WaitTimeInt < 0 {
  704. taskDetail.WaitTime = "00:00:00"
  705. }
  706. }
  707. if ciTasks[i].Cloudbrain.Type == models.TypeCloudBrainTwo || (ciTasks[i].Cloudbrain.Type == models.TypeCloudBrainOne && ciTasks[i].Cloudbrain.JobType == "TRAIN") {
  708. taskDetail.JobID = ciTasks[i].Cloudbrain.JobID
  709. }
  710. if ciTasks[i].Cloudbrain.DeletedAt != nilTime {
  711. taskDetail.IsDelete = true
  712. } else {
  713. taskDetail.IsDelete = false
  714. }
  715. tasks = append(tasks, taskDetail)
  716. }
  717. pager := context.NewPagination(int(count), pageSize, page, getTotalPage(count, pageSize))
  718. pager.SetDefaultParams(ctx)
  719. pager.AddParam(ctx, "listType", "ListType")
  720. ctx.JSON(http.StatusOK, map[string]interface{}{
  721. "Title": ctx.Tr("kanban.cloudBrains"),
  722. "Tasks": tasks,
  723. "Keyword": keyword,
  724. "pager": pager,
  725. "count": count,
  726. })
  727. }
  728. func GetCloudbrainsCreateHoursData(ctx *context.Context) {
  729. recordCloudbrain, err := models.GetRecordBeginTime()
  730. if err != nil {
  731. log.Error("Can not get record begin time", err)
  732. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  733. return
  734. }
  735. recordBeginTime := time.Unix(int64(recordCloudbrain[0].Cloudbrain.CreatedUnix), 0)
  736. // recordBeginTime, _ := time.Parse("2006-01-02 15:04:05", recordTime)
  737. now := time.Now()
  738. queryType := ctx.QueryTrim("type")
  739. beginTimeStr := ctx.QueryTrim("beginTime")
  740. endTimeStr := ctx.QueryTrim("endTime")
  741. var beginTime time.Time
  742. var endTime time.Time
  743. createHourPeriodCount := make(map[string]interface{})
  744. if queryType != "" {
  745. if queryType == "all" {
  746. beginTime = recordBeginTime
  747. endTime = now.AddDate(0, 0, 1)
  748. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  749. } else if queryType == "today" {
  750. beginTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  751. endTime = now.AddDate(0, 0, 1)
  752. } else if queryType == "yesterday" {
  753. beginTime = now.AddDate(0, 0, -1)
  754. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  755. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  756. } else if queryType == "current_week" {
  757. beginTime = now.AddDate(0, 0, -int(time.Now().Weekday())+1) //begin from monday
  758. endTime = now
  759. } else if queryType == "current_month" {
  760. beginTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  761. endTime = now.AddDate(0, 0, 1)
  762. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  763. } else if queryType == "monthly" {
  764. endTime = now
  765. beginTime = now.AddDate(0, -1, 0)
  766. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  767. } else if queryType == "current_year" {
  768. beginTime = time.Date(now.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  769. endTime = now.AddDate(0, 0, 1)
  770. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  771. } else if queryType == "last_month" {
  772. lastMonthTime := now.AddDate(0, -1, 0)
  773. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  774. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  775. } else if queryType == "last_7day" {
  776. beginTime = now.AddDate(0, 0, -6)
  777. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  778. endTime = now.AddDate(0, 0, 1)
  779. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  780. } else if queryType == "last_30day" {
  781. beginTime = now.AddDate(0, 0, -29)
  782. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  783. endTime = now.AddDate(0, 0, 1)
  784. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  785. }
  786. } else {
  787. if beginTimeStr == "" || endTimeStr == "" {
  788. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  789. beginTime = recordBeginTime
  790. endTime = now.AddDate(0, 0, 1)
  791. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  792. } else {
  793. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  794. if err != nil {
  795. log.Error("Can not ParseInLocation.", err)
  796. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  797. return
  798. }
  799. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  800. if err != nil {
  801. log.Error("Can not ParseInLocation.", err)
  802. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  803. return
  804. }
  805. endTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  806. }
  807. }
  808. dateBeginTime := beginTime.Format("2006-01-02")
  809. dateEndTime := endTime.Format("2006-01-02")
  810. createHourPeriodCount, err = models.GetCreateHourPeriodCount(dateBeginTime, dateEndTime)
  811. if err != nil {
  812. log.Error("Can not query hourPeriodCount.", err)
  813. ctx.Error(http.StatusBadRequest, ctx.Tr("hourPeriodCount_get_error"))
  814. return
  815. }
  816. runHourPeriodCount, err := models.GetRunHourPeriodCount(dateBeginTime, dateEndTime)
  817. if err != nil {
  818. log.Error("Can not query runHourPeriodCount.", err)
  819. ctx.Error(http.StatusBadRequest, ctx.Tr("runHourPeriodCount_get_error"))
  820. return
  821. }
  822. ctx.JSON(http.StatusOK, map[string]interface{}{
  823. "recordBeginTime": recordCloudbrain[0].Cloudbrain.CreatedUnix,
  824. "updateTime": now.Unix(),
  825. "createHourPeriodCount": createHourPeriodCount,
  826. "runHourPeriodCount": runHourPeriodCount,
  827. })
  828. }
  829. func GetWaittingTop(ctx *context.Context) {
  830. ciTasks, err := models.GetWaittingTop()
  831. if err != nil {
  832. log.Error("Can not get record begin time", err)
  833. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  834. return
  835. }
  836. tasks := []models.TaskDetail{}
  837. for i, task := range ciTasks {
  838. ciTasks[i].Cloudbrain.ComputeResource = task.ComputeResource
  839. var taskDetail models.TaskDetail
  840. taskDetail.DisplayJobName = ciTasks[i].DisplayJobName
  841. taskDetail.UserName = ciTasks[i].User.Name
  842. taskDetail.RepoID = ciTasks[i].RepoID
  843. if ciTasks[i].Repo != nil {
  844. taskDetail.RepoName = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Name
  845. }
  846. WaitTimeInt := time.Now().Unix() - ciTasks[i].Cloudbrain.CreatedUnix.AsTime().Unix()
  847. taskDetail.WaitTime = models.ConvertDurationToStr(WaitTimeInt)
  848. if WaitTimeInt < 0 {
  849. taskDetail.WaitTime = "00:00:00"
  850. }
  851. tasks = append(tasks, taskDetail)
  852. }
  853. ctx.JSON(http.StatusOK, map[string]interface{}{
  854. "tasks": tasks,
  855. })
  856. }
  857. func GetRunningTop(ctx *context.Context) {
  858. ciTasks, err := models.GetRunningTop()
  859. if err != nil {
  860. log.Error("Can not get record begin time", err)
  861. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  862. return
  863. }
  864. tasks := []models.TaskDetail{}
  865. for i, task := range ciTasks {
  866. ciTasks[i].Cloudbrain.ComputeResource = task.ComputeResource
  867. var taskDetail models.TaskDetail
  868. taskDetail.DisplayJobName = ciTasks[i].DisplayJobName
  869. taskDetail.RunTime = ciTasks[i].Cloudbrain.TrainJobDuration
  870. taskDetail.UserName = ciTasks[i].User.Name
  871. taskDetail.RepoID = ciTasks[i].RepoID
  872. if ciTasks[i].Repo != nil {
  873. taskDetail.RepoName = ciTasks[i].Repo.OwnerName + "/" + ciTasks[i].Repo.Name
  874. }
  875. tasks = append(tasks, taskDetail)
  876. }
  877. ctx.JSON(http.StatusOK, map[string]interface{}{
  878. "tasks": tasks,
  879. })
  880. }
  881. func getCloudbrainCount(beginTime time.Time, endTime time.Time, cloudbrains []*models.CloudbrainInfo) (map[string]int, map[string]int, map[string]int, map[int]int, map[string]int) {
  882. cloudOneJobTypeRes := make(map[string]int)
  883. cloudTwoJobTypeRes := make(map[string]int)
  884. intelligentNetJobTypeRes := make(map[string]int)
  885. cloudBrainPeriodNum := make(map[int]int)
  886. cloudBrainComputeResource := make(map[string]int)
  887. for _, cloudbrain := range cloudbrains {
  888. if int64(cloudbrain.Cloudbrain.CreatedUnix) >= beginTime.Unix() && int64(cloudbrain.Cloudbrain.CreatedUnix) < endTime.Unix() {
  889. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainOne {
  890. if _, ok := cloudOneJobTypeRes[cloudbrain.Cloudbrain.JobType]; !ok {
  891. cloudOneJobTypeRes[cloudbrain.Cloudbrain.JobType] = 1
  892. } else {
  893. cloudOneJobTypeRes[cloudbrain.Cloudbrain.JobType] += 1
  894. }
  895. }
  896. if cloudbrain.Cloudbrain.Type == models.TypeCloudBrainTwo {
  897. if _, ok := cloudTwoJobTypeRes[cloudbrain.JobType]; !ok {
  898. cloudTwoJobTypeRes[cloudbrain.Cloudbrain.JobType] = 1
  899. } else {
  900. cloudTwoJobTypeRes[cloudbrain.Cloudbrain.JobType] += 1
  901. }
  902. }
  903. if _, ok := cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type]; !ok {
  904. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] = 1
  905. } else {
  906. cloudBrainPeriodNum[cloudbrain.Cloudbrain.Type] += 1
  907. }
  908. if _, ok := cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource]; !ok {
  909. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] = 1
  910. } else {
  911. cloudBrainComputeResource[cloudbrain.Cloudbrain.ComputeResource] += 1
  912. }
  913. }
  914. }
  915. ComputeResourceList := []string{"CPU/GPU", "NPU"}
  916. for _, v := range ComputeResourceList {
  917. if _, ok := cloudBrainComputeResource[v]; !ok {
  918. cloudBrainComputeResource[v] = 0
  919. }
  920. }
  921. jobTypeList := []string{"DEBUG", "BENCHMARK", "INFERENCE", "TRAIN", "SNN4IMAGENET", "BRAINSCORE"}
  922. cloudBrainTypeList := []int{0, 1, 2}
  923. for _, v := range jobTypeList {
  924. if _, ok := cloudOneJobTypeRes[v]; !ok {
  925. cloudOneJobTypeRes[v] = 0
  926. }
  927. if _, ok := cloudTwoJobTypeRes[v]; !ok {
  928. cloudTwoJobTypeRes[v] = 0
  929. }
  930. if _, ok := intelligentNetJobTypeRes[v]; !ok {
  931. intelligentNetJobTypeRes[v] = 0
  932. }
  933. }
  934. for _, v := range cloudBrainTypeList {
  935. if _, ok := cloudBrainPeriodNum[v]; !ok {
  936. cloudBrainPeriodNum[v] = 0
  937. }
  938. }
  939. cloudBrainPeriodNum[-1] = cloudBrainPeriodNum[0] + cloudBrainPeriodNum[1] + cloudBrainPeriodNum[2]
  940. return cloudOneJobTypeRes, cloudTwoJobTypeRes, intelligentNetJobTypeRes, cloudBrainPeriodNum, cloudBrainComputeResource
  941. }
  942. func getDayCloudbrainNum(beginTime time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  943. var endTimeTemp time.Time
  944. endTimeTemp = beginTime.AddDate(0, 0, 1)
  945. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  946. Type: models.TypeCloudBrainAll,
  947. BeginTimeUnix: beginTime.Unix(),
  948. EndTimeUnix: endTime.Unix(),
  949. })
  950. if err != nil {
  951. log.Error("Get cloudbrains failed:", err)
  952. return nil, err
  953. }
  954. dayCloudbrainNum := make([]DateCloudbrainNum, 0)
  955. for endTimeTemp.Before(endTime) || endTimeTemp.Equal(endTime) {
  956. cloudOneJobTypeRes, cloudTwoJobTypeRes, intelligentNetJobTypeRes, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(beginTime, endTimeTemp, cloudbrains)
  957. dayCloudbrainNum = append(dayCloudbrainNum, DateCloudbrainNum{
  958. Date: beginTime.Format("2006/01/02"),
  959. CloudOneJobTypeRes: cloudOneJobTypeRes,
  960. CloudTwoJobTypeRes: cloudTwoJobTypeRes,
  961. IntelligentNetJobTypeRes: intelligentNetJobTypeRes,
  962. CloudBrainPeriodNum: cloudBrainPeriodNum,
  963. CloudBrainComputeResource: cloudBrainComputeResource,
  964. })
  965. if endTime.Before(endTimeTemp.AddDate(0, 0, 1)) && endTimeTemp.Before(endTime) {
  966. beginTime = endTimeTemp
  967. endTimeTemp = endTime
  968. } else {
  969. beginTime = endTimeTemp
  970. endTimeTemp = beginTime.AddDate(0, 0, 1)
  971. }
  972. }
  973. return dayCloudbrainNum, nil
  974. }
  975. func getMonthCloudbrainNum(beginTime time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  976. var endTimeTemp time.Time
  977. now := time.Now()
  978. endTimeTemp = beginTime.AddDate(0, 1, 0)
  979. endTimeTemp = time.Date(endTimeTemp.Year(), endTimeTemp.Month(), 1, 0, 0, 0, 0, now.Location())
  980. monthCloudbrainNum := make([]DateCloudbrainNum, 0)
  981. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  982. Type: models.TypeCloudBrainAll,
  983. BeginTimeUnix: beginTime.Unix(),
  984. EndTimeUnix: endTime.Unix(),
  985. })
  986. if err != nil {
  987. log.Error("Getcloudbrains failed:%v", err)
  988. return nil, err
  989. }
  990. for endTimeTemp.Before(endTime) || endTimeTemp.Equal(endTime) {
  991. cloudOneJobTypeRes, cloudTwoJobTypeRes, intelligentNetJobTypeRes, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(beginTime, endTimeTemp, cloudbrains)
  992. monthCloudbrainNum = append(monthCloudbrainNum, DateCloudbrainNum{
  993. Date: beginTime.Format("2006/01"),
  994. CloudOneJobTypeRes: cloudOneJobTypeRes,
  995. CloudTwoJobTypeRes: cloudTwoJobTypeRes,
  996. IntelligentNetJobTypeRes: intelligentNetJobTypeRes,
  997. CloudBrainPeriodNum: cloudBrainPeriodNum,
  998. CloudBrainComputeResource: cloudBrainComputeResource,
  999. })
  1000. if endTime.Before(endTimeTemp.AddDate(0, 1, 0)) && endTimeTemp.Before(endTime) {
  1001. beginTime = endTimeTemp
  1002. endTimeTemp = endTime
  1003. } else {
  1004. beginTime = endTimeTemp
  1005. endTimeTemp = beginTime.AddDate(0, 1, 0)
  1006. }
  1007. }
  1008. return monthCloudbrainNum, nil
  1009. }
  1010. func getDayCloudbrainInfo(beginTime time.Time, endTime time.Time) ([]DateCloudbrainInfo, int, error) {
  1011. now := time.Now()
  1012. endTimeTemp := time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  1013. if endTimeTemp.Equal(endTime) {
  1014. endTimeTemp = endTimeTemp.AddDate(0, 0, -1)
  1015. }
  1016. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  1017. Type: models.TypeCloudBrainAll,
  1018. BeginTimeUnix: beginTime.Unix(),
  1019. EndTimeUnix: endTime.Unix(),
  1020. })
  1021. if err != nil {
  1022. log.Error("Getcloudbrains failed:%v", err)
  1023. return nil, 0, err
  1024. }
  1025. dayCloudbrainInfo := make([]DateCloudbrainInfo, 0)
  1026. count := 0
  1027. for beginTime.Before(endTimeTemp) || beginTime.Equal(endTimeTemp) {
  1028. _, _, _, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(endTimeTemp, endTime, cloudbrains)
  1029. dayCloudbrainInfo = append(dayCloudbrainInfo, DateCloudbrainInfo{
  1030. Date: endTimeTemp.Format("2006/01/02"),
  1031. CloudBrainPeriodNum: cloudBrainPeriodNum,
  1032. CloudBrainComputeResource: cloudBrainComputeResource,
  1033. })
  1034. endTime = endTimeTemp
  1035. endTimeTemp = endTimeTemp.AddDate(0, 0, -1)
  1036. count += 1
  1037. }
  1038. return dayCloudbrainInfo, count, nil
  1039. }
  1040. func getMonthCloudbrainInfo(beginTime time.Time, endTime time.Time) ([]DateCloudbrainInfo, int, error) {
  1041. now := time.Now()
  1042. endTimeTemp := time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1043. if endTimeTemp.Equal(endTime) {
  1044. endTimeTemp = endTimeTemp.AddDate(0, -1, 0)
  1045. }
  1046. cloudbrains, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  1047. Type: models.TypeCloudBrainAll,
  1048. BeginTimeUnix: beginTime.Unix(),
  1049. EndTimeUnix: endTime.Unix(),
  1050. })
  1051. if err != nil {
  1052. log.Error("Getcloudbrains failed:%v", err)
  1053. return nil, 0, err
  1054. }
  1055. dayCloudbrainInfo := make([]DateCloudbrainInfo, 0)
  1056. count := 0
  1057. for beginTime.Before(endTimeTemp) || beginTime.Equal(endTimeTemp) || (endTimeTemp.Before(beginTime) && beginTime.Before(endTime)) {
  1058. _, _, _, cloudBrainPeriodNum, cloudBrainComputeResource := getCloudbrainCount(endTimeTemp, endTime, cloudbrains)
  1059. dayCloudbrainInfo = append(dayCloudbrainInfo, DateCloudbrainInfo{
  1060. Date: endTimeTemp.Format("2006/01"),
  1061. CloudBrainPeriodNum: cloudBrainPeriodNum,
  1062. CloudBrainComputeResource: cloudBrainComputeResource,
  1063. })
  1064. endTime = endTimeTemp
  1065. endTimeTemp = endTimeTemp.AddDate(0, -1, 0)
  1066. count += 1
  1067. }
  1068. return dayCloudbrainInfo, count, nil
  1069. type CloudbrainsStatusAnalysis struct {
  1070. JobWaitingCount int64 `json:"jobWaitingCount"`
  1071. JobRunningCount int64 `json:"jobRunningCount"`
  1072. JobStoppedCount int64 `json:"jobStoppedCount"`
  1073. JobCompletedCount int64 `json:"jobCompletedCount"`
  1074. JobFailedCount int64 `json:"jobFailedCount"`
  1075. JobKilledCount int64 `json:"jobKilledCount"`
  1076. JobInitCount int64 `json:"jobInitCount"`
  1077. }
  1078. func DownloadCloudBrainBoard(ctx *context.Context) {
  1079. page := 1
  1080. pageSize := 300
  1081. var cloudBrain = ctx.Tr("repo.cloudbrain")
  1082. fileName := getCloudbrainFileName(cloudBrain)
  1083. _, total, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  1084. ListOptions: models.ListOptions{
  1085. Page: page,
  1086. PageSize: pageSize,
  1087. },
  1088. Type: models.TypeCloudBrainAll,
  1089. NeedRepoInfo: false,
  1090. })
  1091. if err != nil {
  1092. log.Warn("Can not get cloud brain info", err)
  1093. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.cloudbrain_query_fail"))
  1094. return
  1095. }
  1096. totalPage := getTotalPage(total, pageSize)
  1097. f := excelize.NewFile()
  1098. index := f.NewSheet(cloudBrain)
  1099. f.DeleteSheet("Sheet1")
  1100. for k, v := range allCloudbrainHeader(ctx) {
  1101. f.SetCellValue(cloudBrain, k, v)
  1102. }
  1103. var row = 2
  1104. for i := 0; i < totalPage; i++ {
  1105. pageRecords, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  1106. ListOptions: models.ListOptions{
  1107. Page: page,
  1108. PageSize: pageSize,
  1109. },
  1110. Type: models.TypeCloudBrainAll,
  1111. NeedRepoInfo: true,
  1112. })
  1113. if err != nil {
  1114. log.Warn("Can not get cloud brain info", err)
  1115. continue
  1116. }
  1117. for _, record := range pageRecords {
  1118. for k, v := range allCloudbrainValues(row, record, ctx) {
  1119. f.SetCellValue(cloudBrain, k, v)
  1120. }
  1121. row++
  1122. }
  1123. page++
  1124. }
  1125. f.SetActiveSheet(index)
  1126. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(fileName))
  1127. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  1128. f.WriteTo(ctx.Resp)
  1129. }
  1130. func getCloudbrainFileName(baseName string) string {
  1131. return baseName + "_" + time.Now().Format(EXCEL_DATE_FORMAT) + ".xlsx"
  1132. }
  1133. func allCloudbrainHeader(ctx *context.Context) map[string]string {
  1134. 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"),
  1135. "E1": ctx.Tr("repo.modelarts.createtime"), "F1": ctx.Tr("repo.modelarts.train_job.wait_time"), "G1": ctx.Tr("repo.modelarts.train_job.dura_time"),
  1136. "H1": ctx.Tr("repo.modelarts.train_job.start_time"),
  1137. "I1": ctx.Tr("repo.modelarts.train_job.end_time"), "J1": ctx.Tr("repo.modelarts.computing_resources"),
  1138. "K1": ctx.Tr("repo.cloudbrain_creator"), "L1": ctx.Tr("repo.repo_name"), "M1": ctx.Tr("repo.cloudbrain_task_name"), "N1": ctx.Tr("repo.modelarts.deletetime")}
  1139. }
  1140. func allCloudbrainValues(row int, rs *models.CloudbrainInfo, ctx *context.Context) map[string]string {
  1141. 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,
  1142. getCellName("E", row): time.Unix(int64(rs.Cloudbrain.CreatedUnix), 0).Format(CREATE_TIME_FORMAT), getCellName("F", row): getBrainWaitTime(rs),
  1143. getCellName("G", row): rs.TrainJobDuration, getCellName("H", row): getBrainStartTime(rs),
  1144. getCellName("I", row): getBrainEndTime(rs),
  1145. getCellName("J", row): rs.ComputeResource, getCellName("K", row): rs.Name, getCellName("L", row): getBrainRepo(rs),
  1146. getCellName("M", row): rs.JobName, getCellName("N", row): getBrainDeleteTime(rs),
  1147. }
  1148. }
  1149. func getBrainRepo(rs *models.CloudbrainInfo) string {
  1150. if rs.Repo != nil {
  1151. return rs.Repo.OwnerName + "/" + rs.Repo.Alias
  1152. }
  1153. return ""
  1154. }
  1155. func getBrainStartTime(rs *models.CloudbrainInfo) string {
  1156. timeString := time.Unix(int64(rs.Cloudbrain.StartTime), 0).Format(CREATE_TIME_FORMAT)
  1157. if timeString != "1970/01/01 08:00:00" {
  1158. return timeString
  1159. } else {
  1160. return "0"
  1161. }
  1162. }
  1163. func getBrainEndTime(rs *models.CloudbrainInfo) string {
  1164. timeString := time.Unix(int64(rs.Cloudbrain.EndTime), 0).Format(CREATE_TIME_FORMAT)
  1165. if timeString != "1970/01/01 08:00:00" {
  1166. return timeString
  1167. } else {
  1168. return "0"
  1169. }
  1170. }
  1171. func getBrainWaitTime(rs *models.CloudbrainInfo) string {
  1172. var waitTime int64
  1173. if rs.Cloudbrain.Status == string(models.JobWaiting) {
  1174. waitTime = time.Now().Unix() - rs.Cloudbrain.CreatedUnix.AsTime().Unix()
  1175. } else {
  1176. waitTime = int64(rs.Cloudbrain.StartTime - rs.Cloudbrain.CreatedUnix)
  1177. }
  1178. if waitTime <= 0 {
  1179. return "00:00:00"
  1180. } else {
  1181. return models.ConvertDurationToStr(waitTime)
  1182. }
  1183. }
  1184. func getCloudbrainType(rs *models.CloudbrainInfo, ctx *context.Context) string {
  1185. if rs.Cloudbrain.Type == models.TypeCloudBrainOne {
  1186. return ctx.Tr("repo.cloudbrain1")
  1187. } else if rs.Cloudbrain.Type == models.TypeCloudBrainTwo {
  1188. return ctx.Tr("repo.cloudbrain2")
  1189. } else if rs.Cloudbrain.Type == models.TypeIntelligentNet {
  1190. return ctx.Tr("repo.intelligent_net")
  1191. } else {
  1192. return ctx.Tr("repo.cloudbrain_untype")
  1193. }
  1194. }
  1195. func getBrainDeleteTime(rs *models.CloudbrainInfo) string {
  1196. nilTime := time.Time{}
  1197. if rs.Cloudbrain.DeletedAt != nilTime {
  1198. return rs.Cloudbrain.DeletedAt.Format("2006-01-02 15:04:05")
  1199. } else {
  1200. return ""
  1201. }
  1202. }
  1203. func getCloudbrainTimePeroid(ctx *context.Context, recordBeginTime time.Time) (time.Time, time.Time, error) {
  1204. queryType := ctx.QueryTrim("type")
  1205. beginTimeStr := ctx.QueryTrim("beginTime")
  1206. endTimeStr := ctx.QueryTrim("endTime")
  1207. now := time.Now()
  1208. var beginTime time.Time
  1209. var endTime time.Time
  1210. var err error
  1211. if queryType != "" {
  1212. if queryType == "all" {
  1213. beginTime = recordBeginTime
  1214. endTime = now
  1215. } else if queryType == "today" {
  1216. endTime = now
  1217. beginTime = time.Date(endTime.Year(), endTime.Month(), endTime.Day(), 0, 0, 0, 0, now.Location())
  1218. } else if queryType == "yesterday" {
  1219. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  1220. beginTime = endTime.AddDate(0, 0, -1)
  1221. } else if queryType == "last_7day" {
  1222. beginTime = now.AddDate(0, 0, -6)
  1223. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1224. endTime = now
  1225. } else if queryType == "last_30day" {
  1226. beginTime = now.AddDate(0, 0, -29)
  1227. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1228. endTime = now
  1229. } else if queryType == "current_month" {
  1230. endTime = now
  1231. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1232. } else if queryType == "monthly" {
  1233. endTime = now
  1234. beginTime = now.AddDate(0, -1, 1)
  1235. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  1236. } else if queryType == "current_year" {
  1237. endTime = now
  1238. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  1239. } else if queryType == "last_month" {
  1240. lastMonthTime := now.AddDate(0, -1, 0)
  1241. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  1242. endTime = time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  1243. } else {
  1244. return now, now, fmt.Errorf("The value of type parameter is wrong.")
  1245. }
  1246. } else {
  1247. if beginTimeStr == "" || endTimeStr == "" {
  1248. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  1249. beginTime = recordBeginTime
  1250. endTime = now
  1251. } else {
  1252. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local)
  1253. if err != nil {
  1254. return now, now, err
  1255. }
  1256. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  1257. if err != nil {
  1258. return now, now, err
  1259. }
  1260. }
  1261. }
  1262. if beginTime.Before(recordBeginTime) {
  1263. beginTime = recordBeginTime
  1264. }
  1265. return beginTime, endTime, nil
  1266. }