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