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