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 23 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. package repo
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. "github.com/360EntSecGroup-Skylar/excelize/v2"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. type CloudbrainsPeriodData struct {
  14. DebugOnePeriodCount int64 `json:"debugOnePeriodCount"`
  15. BenchmarkOnePeriodCount int64 `json:"benchmarkOnePeriodCount"`
  16. TrainOnePeriodCount int64 `json:"trainOnePeriodCount"`
  17. DebugTwoPeriodCount int64 `json:"debugTwoPeriodCount"`
  18. TrainTwoPeriodCount int64 `json:"trainTwoPeriodCount"`
  19. InferenceTwoPeriodCount int64 `json:"inferenceTwoPeriodCount"`
  20. JobWaitingPeriodCount int64 `json:"jobWaitingPeriodCount"`
  21. JobRunningPeriodCount int64 `json:"jobRunningPeriodCount"`
  22. JobSucceededPeriodCount int64 `json:"jobRunningPeriodCount"`
  23. CloudBrainOnePeriodCount int64 `json:"cloudBrainOnePeriodCount"`
  24. CloudBrainTwoPeriodCount int64 `json:"cloudBrainTwoPeriodCount"`
  25. }
  26. type CloudbrainsOverviewData struct {
  27. JobWaitingPeriodCount int64 `json:"jobWaitingPeriodCount"`
  28. JobRunningPeriodCount int64 `json:"jobRunningPeriodCount"`
  29. JobStoppedPeriodCount int64 `json:"jobStoppedPeriodCount"`
  30. JobSucceededPeriodCount int64 `json:"jobSucceededPeriodCount"`
  31. JobFailedPeriodCount int64 `json:"jobFailedPeriodCount"`
  32. CloudBrainOneCount int64 `json:"cloudBrainOneCount"`
  33. CloudBrainTwoCount int64 `json:"cloudBrainTwoCount"`
  34. CloudBrainOneDuration int64 `json:"cloudBrainOneDuration"`
  35. CloudBrainTwoDuration int64 `json:"cloudBrainTwoDuration"`
  36. CreatorPeriodCount int64 `json:"creatorPeriodCount"`
  37. CreatorCount int64 `json:"creatorCount"`
  38. }
  39. type TimeCloudbrainsNum struct {
  40. TimeCloudbrainNum []DateCloudbrainNum `json:"dateCloudbrainNum"`
  41. }
  42. type DateCloudbrainNum struct {
  43. Date string `json:"date"`
  44. DebugOneCount int64 `json:"debugOneCount"`
  45. BenchmarkOneCount int64 `json:"benchmarkOneCount"`
  46. TrainOneCount int64 `json:"trainOneCount"`
  47. DebugTwoCount int64 `json:"debugTwoCount"`
  48. TrainTwoCount int64 `json:"trainTwoCount"`
  49. InferenceTwoCount int64 `json:"inferenceTwoCount"`
  50. CloudbrainOneCount int64 `json:"cloudbrainOneCount"`
  51. CloudbrainTwoCount int64 `json:"cloudbrainTwoCount"`
  52. CloudbrainCount int64 `json:"cloudbrainCount"`
  53. }
  54. func GetAllCloudbrainsOverview(ctx *context.Context) {
  55. now := time.Now()
  56. //today overview
  57. beginTime := now.AddDate(0, 0, 0)
  58. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  59. log.Info("beginTime:", beginTime)
  60. endTime := now
  61. log.Info("endTime:", endTime)
  62. jobWaitingPeriodCount, err := models.GetJobWaitingPeriodCount(beginTime, endTime)
  63. if err != nil {
  64. log.Error("Can not query jobWaitingCount.", err)
  65. return
  66. }
  67. jobRunningPeriodCount, err := models.GetJobRunningPeriodCount(beginTime, endTime)
  68. if err != nil {
  69. log.Error("Can not query jobRunningCount.", err)
  70. return
  71. }
  72. jobStoppedPeriodCount, err := models.GetJobStoppedPeriodCount(beginTime, endTime)
  73. if err != nil {
  74. log.Error("Can not query jobStoppedCount.", err)
  75. return
  76. }
  77. jobSucceededPeriodCount, err := models.GetJobSucceededPeriodCount(beginTime, endTime)
  78. if err != nil {
  79. log.Error("Can not query jobSucceededCount.", err)
  80. return
  81. }
  82. jobFailedPeriodCount, err := models.GetJobFailedPeriodCount(beginTime, endTime)
  83. if err != nil {
  84. log.Error("Can not query jobFailedCount.", err)
  85. return
  86. }
  87. cloudBrainOneCount, err := models.GetCloudBrainOneCount()
  88. if err != nil {
  89. log.Error("Can not query cloudBrainOneCount.", err)
  90. return
  91. }
  92. cloudBrainOneDuration, err := models.GetCloudBrainOneDuration()
  93. if err != nil {
  94. log.Error("Can not query cloudBrainOneDuration.", err)
  95. return
  96. }
  97. cloudBrainTwoCount, err := models.GetCloudBrainTwoCount()
  98. if err != nil {
  99. log.Error("Can not query cloudBrainTwoCount.", err)
  100. return
  101. }
  102. cloudBrainTwoDuration, err := models.GetCloudBrainTwoDuration()
  103. if err != nil {
  104. log.Error("Can not query cloudBrainTwoDuration.", err)
  105. return
  106. }
  107. creatorPeriodCount, err := models.GetCreatorPeriodCount(beginTime, endTime)
  108. if err != nil {
  109. log.Error("Can not query creatorPeriodCount.", err)
  110. return
  111. }
  112. creatorCount, err := models.GetCreatorCount()
  113. if err != nil {
  114. log.Error("Can not query creatorCount.", err)
  115. return
  116. }
  117. CloudbrainsOverviewData := CloudbrainsOverviewData{
  118. JobWaitingPeriodCount: jobWaitingPeriodCount,
  119. JobRunningPeriodCount: jobRunningPeriodCount,
  120. JobStoppedPeriodCount: jobStoppedPeriodCount,
  121. JobSucceededPeriodCount: jobSucceededPeriodCount,
  122. JobFailedPeriodCount: jobFailedPeriodCount,
  123. CloudBrainOneCount: cloudBrainOneCount,
  124. CloudBrainTwoCount: cloudBrainTwoCount,
  125. CloudBrainOneDuration: cloudBrainOneDuration,
  126. CloudBrainTwoDuration: cloudBrainTwoDuration,
  127. CreatorPeriodCount: creatorPeriodCount,
  128. CreatorCount: creatorCount,
  129. }
  130. ctx.JSON(http.StatusOK, CloudbrainsOverviewData)
  131. }
  132. func GetAllCloudbrainsTrend(ctx *context.Context) {
  133. brainRecordBeginTime, err := getBrainRecordBeginTime()
  134. if err != nil {
  135. log.Error("Can not get brain record begin time", err)
  136. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.brain_record_begintime_get_err"))
  137. return
  138. }
  139. queryType := ctx.QueryTrim("type")
  140. now := time.Now()
  141. beginTimeStr := ctx.QueryTrim("beginTime")
  142. endTimeStr := ctx.QueryTrim("endTime")
  143. var beginTime time.Time
  144. var endTime time.Time
  145. var endTimeTemp time.Time
  146. dateCloudbrainNum := make([]DateCloudbrainNum, 0)
  147. if queryType != "" {
  148. if queryType == "all" {
  149. beginTime = brainRecordBeginTime
  150. endTime = now
  151. endTimeTemp = beginTime.AddDate(0, 1, 0)
  152. dateCloudbrainNum, err = getYearCloudbrainNum(beginTime, endTimeTemp, endTime)
  153. if err != nil {
  154. log.Error("Can not query getYearCloudbrainNum.", err)
  155. ctx.Error(http.StatusBadRequest, ctx.Tr("getYearCloudbrainNum_get_error"))
  156. return
  157. }
  158. } else if queryType == "yesterday" {
  159. beginTime = now.AddDate(0, 0, -1)
  160. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  161. endTimeTemp = beginTime.Add(time.Hour)
  162. endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  163. dateCloudbrainNum, err = getHourCloudbrainNum(beginTime, endTimeTemp, endTime)
  164. if err != nil {
  165. log.Error("Can not query getHourCloudbrainNum.", err)
  166. ctx.Error(http.StatusBadRequest, ctx.Tr("getYearCloudbrainNum_get_error"))
  167. return
  168. }
  169. } else if queryType == "current_week" {
  170. beginTime = now.AddDate(0, 0, -int(time.Now().Weekday())+1) //begin from monday
  171. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  172. endTime = now
  173. endTimeTemp = beginTime.AddDate(0, 0, 1)
  174. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTimeTemp, endTime)
  175. if err != nil {
  176. log.Error("Can not query getDayCloudbrainNum.", err)
  177. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  178. return
  179. }
  180. } else if queryType == "current_month" {
  181. endTime = now
  182. beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location())
  183. endTimeTemp = beginTime.AddDate(0, 0, 1)
  184. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTimeTemp, endTime)
  185. if err != nil {
  186. log.Error("Can not query getDayCloudbrainNum.", err)
  187. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  188. return
  189. }
  190. } else if queryType == "monthly" {
  191. endTime = now
  192. beginTime = now.AddDate(0, -1, 0)
  193. beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location())
  194. endTimeTemp = beginTime.AddDate(0, 0, 1)
  195. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTimeTemp, endTime)
  196. if err != nil {
  197. log.Error("Can not query getDayCloudbrainNum.", err)
  198. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  199. return
  200. }
  201. } else if queryType == "current_year" {
  202. endTime = now
  203. beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location())
  204. endTimeTemp = beginTime.AddDate(0, 1, 0)
  205. dateCloudbrainNum, err = getYearCloudbrainNum(beginTime, endTimeTemp, endTime)
  206. if err != nil {
  207. log.Error("Can not query getDayCloudbrainNum.", err)
  208. ctx.Error(http.StatusBadRequest, ctx.Tr("getYearCloudbrainNum_get_error"))
  209. return
  210. }
  211. } else if queryType == "last_month" {
  212. lastMonthTime := now.AddDate(0, -1, 0)
  213. beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location())
  214. endTime = time.Date(now.Year(), now.Month(), 2, 0, 0, 0, 0, now.Location())
  215. endTimeTemp = beginTime.AddDate(0, 0, 1)
  216. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTimeTemp, endTime)
  217. if err != nil {
  218. log.Error("Can not query getDayCloudbrainNum.", err)
  219. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  220. return
  221. }
  222. }
  223. } else {
  224. if beginTimeStr == "" || endTimeStr == "" {
  225. //如果查询类型和开始时间结束时间都未设置,按queryType=all处理
  226. beginTime = brainRecordBeginTime
  227. endTime = now
  228. endTimeTemp = beginTime.AddDate(0, 1, 0)
  229. dateCloudbrainNum, err = getYearCloudbrainNum(beginTime, endTimeTemp, endTime)
  230. if err != nil {
  231. log.Error("Can not query getDayCloudbrainNum.", err)
  232. ctx.Error(http.StatusBadRequest, ctx.Tr("getYearCloudbrainNum_get_error"))
  233. return
  234. }
  235. } else {
  236. beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, 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. endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local)
  243. if err != nil {
  244. log.Error("Can not ParseInLocation.", err)
  245. ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error"))
  246. return
  247. }
  248. days := (endTime.Unix() - beginTime.Unix()) / 3600 / 24
  249. if 1 < days {
  250. endTimeTemp = beginTime.AddDate(0, 0, 1)
  251. endTime = endTime.AddDate(0, 0, 2)
  252. dateCloudbrainNum, err = getDayCloudbrainNum(beginTime, endTimeTemp, endTime)
  253. if err != nil {
  254. log.Error("Can not query getDayCloudbrainNum.", err)
  255. ctx.Error(http.StatusBadRequest, ctx.Tr("getDayCloudbrainNum_get_error"))
  256. return
  257. }
  258. } else if 0 < days || days <= 1 {
  259. endTimeTemp = beginTime.Add(time.Hour)
  260. dateCloudbrainNum, err = getHourCloudbrainNum(beginTime, endTimeTemp, endTime)
  261. if err != nil {
  262. log.Error("Can not query getHourCloudbrainNum.", err)
  263. ctx.Error(http.StatusBadRequest, ctx.Tr("getHourCloudbrainNum_get_error"))
  264. return
  265. }
  266. } else {
  267. return
  268. }
  269. }
  270. }
  271. cloudbrainsPeriodData := TimeCloudbrainsNum{
  272. TimeCloudbrainNum: dateCloudbrainNum,
  273. }
  274. ctx.JSON(http.StatusOK, cloudbrainsPeriodData)
  275. }
  276. func GetAllCloudbrainsPeriodDistribution(ctx *context.Context) {
  277. recordBeginTime, err := getBrainRecordBeginTime()
  278. if err != nil {
  279. log.Error("Can not get record begin time", err)
  280. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.record_begintime_get_err"))
  281. return
  282. }
  283. beginTime, endTime, err := getTimePeroid(ctx, recordBeginTime)
  284. if err != nil {
  285. log.Error("Parameter is wrong", err)
  286. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.parameter_is_wrong"))
  287. return
  288. }
  289. debugOnePeriodCount, err := models.GetDebugOnePeriodCount(beginTime, endTime)
  290. if err != nil {
  291. log.Error("Can not query debugOneCount.", err)
  292. ctx.Error(http.StatusBadRequest, ctx.Tr("debugOneCount_get_error"))
  293. return
  294. }
  295. benchmarkOnePeriodCount, err := models.GetBenchmarkOnePeriodCount(beginTime, endTime)
  296. if err != nil {
  297. log.Error("Can not query benchmarkCount.", err)
  298. ctx.Error(http.StatusBadRequest, ctx.Tr("benchmarkOneCount_get_error"))
  299. return
  300. }
  301. trainOnePeriodCount, err := models.GetTrainOnePeriodCount(beginTime, endTime)
  302. if err != nil {
  303. log.Error("Can not query trainOneCount.", err)
  304. ctx.Error(http.StatusBadRequest, ctx.Tr("trainOneCount_get_error"))
  305. return
  306. }
  307. debugTwoPeriodCount, err := models.GetDebugTwoPeriodCount(beginTime, endTime)
  308. if err != nil {
  309. log.Error("Can not query debugTwoCount.", err)
  310. ctx.Error(http.StatusBadRequest, ctx.Tr("debugTwoCount_get_error"))
  311. return
  312. }
  313. trainTwoPeriodCount, err := models.GetTrainTwoPeriodCount(beginTime, endTime)
  314. if err != nil {
  315. log.Error("Can not query DebugOneTotal count.", err)
  316. ctx.Error(http.StatusBadRequest, ctx.Tr("total_count_get_error"))
  317. return
  318. }
  319. inferenceTwoPeriodCount, err := models.GetInferenceTwoPeriodCount(beginTime, endTime)
  320. if err != nil {
  321. log.Error("Can not query inferenceTwoCount.", err)
  322. ctx.Error(http.StatusBadRequest, ctx.Tr("inferenceTwoCount_get_error"))
  323. return
  324. }
  325. cloudBrainOnePeriodCount, err := models.GetCloudBrainOnePeriodCount(beginTime, endTime)
  326. if err != nil {
  327. log.Error("Can not query cloudBrainOnePeriodCount.", err)
  328. ctx.Error(http.StatusBadRequest, ctx.Tr("cloudBrainOnePeriodCount_get_error"))
  329. return
  330. }
  331. cloudBrainTwoPeriodCount, err := models.GetCloudBrainTwoPeriodCount(beginTime, endTime)
  332. if err != nil {
  333. log.Error("Can not query cloudBrainTwoPeriodCount.", err)
  334. ctx.Error(http.StatusBadRequest, ctx.Tr("cloudBrainTwoPeriodCount_get_error"))
  335. return
  336. }
  337. cloudbrainsPeriodData := CloudbrainsPeriodData{
  338. DebugOnePeriodCount: debugOnePeriodCount,
  339. BenchmarkOnePeriodCount: benchmarkOnePeriodCount,
  340. TrainOnePeriodCount: trainOnePeriodCount,
  341. DebugTwoPeriodCount: debugTwoPeriodCount,
  342. TrainTwoPeriodCount: trainTwoPeriodCount,
  343. InferenceTwoPeriodCount: inferenceTwoPeriodCount,
  344. CloudBrainOnePeriodCount: cloudBrainOnePeriodCount,
  345. CloudBrainTwoPeriodCount: cloudBrainTwoPeriodCount,
  346. }
  347. ctx.JSON(http.StatusOK, cloudbrainsPeriodData)
  348. }
  349. func GetCloudbrainsStatusAnalysis(ctx *context.Context) {
  350. }
  351. func getCloudbrainCount(beginTime time.Time, endTime time.Time) (int64, int64, int64, int64, int64, int64, int64, int64, int64, error) {
  352. debugOneCount, err := models.GetDebugOnePeriodCount(beginTime, endTime)
  353. if err != nil {
  354. log.Error("Can not query debugOneCount.", err)
  355. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  356. }
  357. benchmarkOneCount, err := models.GetBenchmarkOnePeriodCount(beginTime, endTime)
  358. if err != nil {
  359. log.Error("Can not query benchmarkCount.", err)
  360. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  361. }
  362. trainOneCount, err := models.GetTrainOnePeriodCount(beginTime, endTime)
  363. if err != nil {
  364. log.Error("Can not query trainOneCount.", err)
  365. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  366. }
  367. debugTwoCount, err := models.GetDebugTwoPeriodCount(beginTime, endTime)
  368. if err != nil {
  369. log.Error("Can not query debugTwoCount.", err)
  370. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  371. }
  372. trainTwoCount, err := models.GetTrainTwoPeriodCount(beginTime, endTime)
  373. if err != nil {
  374. log.Error("Can not query DebugOneTotal count.", err)
  375. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  376. }
  377. inferenceTwoCount, err := models.GetInferenceTwoPeriodCount(beginTime, endTime)
  378. if err != nil {
  379. log.Error("Can not query inferenceTwoCount.", err)
  380. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  381. }
  382. cloudbrainOneCount, err := models.GetCloudBrainOnePeriodCount(beginTime, endTime)
  383. if err != nil {
  384. log.Error("Can not query cloudbrainOneCount.", err)
  385. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  386. }
  387. cloudbrainTwoCount, err := models.GetCloudBrainTwoPeriodCount(beginTime, endTime)
  388. if err != nil {
  389. log.Error("Can not query cloudbrainOneCount.", err)
  390. return 0, 0, 0, 0, 0, 0, 0, 0, 0, err
  391. }
  392. cloudbrainCount := cloudbrainOneCount + cloudbrainTwoCount
  393. return debugOneCount, benchmarkOneCount, trainOneCount, debugTwoCount, trainTwoCount, inferenceTwoCount, cloudbrainOneCount, cloudbrainTwoCount, cloudbrainCount, err
  394. }
  395. func getHourCloudbrainNum(beginTime time.Time, endTimeTemp time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  396. dayCloudbrainNum := make([]DateCloudbrainNum, 0)
  397. for endTimeTemp.Before(endTime) || endTimeTemp.Equal(endTime) {
  398. debugOneCount, benchmarkOneCount, trainOneCount, debugTwoCount, trainTwoCount, inferenceTwoCount, cloudbrainOneCount, cloudbrainTwoCount, cloudbrainCount, err := getCloudbrainCount(beginTime, endTimeTemp)
  399. if err != nil {
  400. log.Error("Can not query getCloudbrainCount.", err)
  401. return nil, err
  402. }
  403. dayCloudbrainNum = append(dayCloudbrainNum, DateCloudbrainNum{
  404. Date: beginTime.Format(time.RFC3339),
  405. DebugOneCount: debugOneCount,
  406. BenchmarkOneCount: benchmarkOneCount,
  407. TrainOneCount: trainOneCount,
  408. DebugTwoCount: debugTwoCount,
  409. TrainTwoCount: trainTwoCount,
  410. InferenceTwoCount: inferenceTwoCount,
  411. CloudbrainOneCount: cloudbrainOneCount,
  412. CloudbrainTwoCount: cloudbrainTwoCount,
  413. CloudbrainCount: cloudbrainCount,
  414. })
  415. beginTime = endTimeTemp
  416. endTimeTemp = beginTime.Add(time.Hour)
  417. }
  418. return dayCloudbrainNum, nil
  419. }
  420. func getDayCloudbrainNum(beginTime time.Time, endTimeTemp time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  421. dayCloudbrainNum := make([]DateCloudbrainNum, 0)
  422. for endTimeTemp.Before(endTime) {
  423. debugOneCount, benchmarkOneCount, trainOneCount, debugTwoCount, trainTwoCount, inferenceTwoCount, cloudbrainOneCount,
  424. cloudbrainTwoCount, cloudbrainCount, err := getCloudbrainCount(beginTime, endTimeTemp)
  425. if err != nil {
  426. log.Error("Can not query getCloudbrainCount.", err)
  427. return nil, err
  428. }
  429. dayCloudbrainNum = append(dayCloudbrainNum, DateCloudbrainNum{
  430. Date: beginTime.Format("2006-01-02"),
  431. DebugOneCount: debugOneCount,
  432. BenchmarkOneCount: benchmarkOneCount,
  433. TrainOneCount: trainOneCount,
  434. DebugTwoCount: debugTwoCount,
  435. TrainTwoCount: trainTwoCount,
  436. InferenceTwoCount: inferenceTwoCount,
  437. CloudbrainOneCount: cloudbrainOneCount,
  438. CloudbrainTwoCount: cloudbrainTwoCount,
  439. CloudbrainCount: cloudbrainCount,
  440. })
  441. beginTime = endTimeTemp
  442. endTimeTemp = beginTime.AddDate(0, 0, 1)
  443. }
  444. return dayCloudbrainNum, nil
  445. }
  446. func getYearCloudbrainNum(beginTime time.Time, endTimeTemp time.Time, endTime time.Time) ([]DateCloudbrainNum, error) {
  447. yearCloudbrainNum := make([]DateCloudbrainNum, 0)
  448. for endTimeTemp.Before(endTime) {
  449. debugOneCount, benchmarkOneCount, trainOneCount, debugTwoCount, trainTwoCount, inferenceTwoCount, cloudbrainOneCount,
  450. cloudbrainTwoCount, cloudbrainCount, err := getCloudbrainCount(beginTime, endTimeTemp)
  451. if err != nil {
  452. log.Error("Can not query getCloudbrainCount.", err)
  453. return nil, err
  454. }
  455. yearCloudbrainNum = append(yearCloudbrainNum, DateCloudbrainNum{
  456. Date: beginTime.Format("2006-01"),
  457. DebugOneCount: debugOneCount,
  458. BenchmarkOneCount: benchmarkOneCount,
  459. TrainOneCount: trainOneCount,
  460. DebugTwoCount: debugTwoCount,
  461. TrainTwoCount: trainTwoCount,
  462. InferenceTwoCount: inferenceTwoCount,
  463. CloudbrainOneCount: cloudbrainOneCount,
  464. CloudbrainTwoCount: cloudbrainTwoCount,
  465. CloudbrainCount: cloudbrainCount,
  466. })
  467. beginTime = endTimeTemp
  468. endTimeTemp = beginTime.AddDate(0, 1, 0)
  469. }
  470. return yearCloudbrainNum, nil
  471. }
  472. func getBrainRecordBeginTime() (time.Time, error) {
  473. return time.ParseInLocation(DATE_FORMAT, setting.BrainRecordBeginTime, time.Local)
  474. }
  475. func DownloadCloudBrainBoard(ctx *context.Context) {
  476. page := 1
  477. pageSize := 300
  478. var cloudBrain = ctx.Tr("repo.cloudbrain")
  479. fileName := getCloudbrainFileName(cloudBrain)
  480. _, total, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  481. ListOptions: models.ListOptions{
  482. Page: page,
  483. PageSize: pageSize,
  484. },
  485. Type: models.TypeCloudBrainAll,
  486. NeedRepoInfo: false,
  487. })
  488. if err != nil {
  489. log.Warn("Can not get cloud brain info", err)
  490. ctx.Error(http.StatusBadRequest, ctx.Tr("repo.cloudbrain_query_fail"))
  491. return
  492. }
  493. totalPage := getTotalPage(total, pageSize)
  494. fmt.Printf("total:%v", total)
  495. fmt.Printf("totalPage:%v", totalPage)
  496. f := excelize.NewFile()
  497. index := f.NewSheet(cloudBrain)
  498. f.DeleteSheet("Sheet1")
  499. for k, v := range allCloudbrainHeader(ctx) {
  500. f.SetCellValue(cloudBrain, k, v)
  501. }
  502. var row = 2
  503. for i := 0; i < totalPage; i++ {
  504. pageRecords, _, err := models.CloudbrainAll(&models.CloudbrainsOptions{
  505. ListOptions: models.ListOptions{
  506. Page: page,
  507. PageSize: pageSize,
  508. },
  509. Type: models.TypeCloudBrainAll,
  510. NeedRepoInfo: true,
  511. })
  512. if err != nil {
  513. log.Warn("Can not get cloud brain info", err)
  514. continue
  515. }
  516. for _, record := range pageRecords {
  517. for k, v := range allCloudbrainValues(row, record, ctx) {
  518. f.SetCellValue(cloudBrain, k, v)
  519. }
  520. row++
  521. }
  522. page++
  523. }
  524. f.SetActiveSheet(index)
  525. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+url.QueryEscape(fileName))
  526. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  527. f.WriteTo(ctx.Resp)
  528. }
  529. func getCloudbrainFileName(baseName string) string {
  530. return baseName + "_" + time.Now().Format(EXCEL_DATE_FORMAT) + ".xlsx"
  531. }
  532. func allCloudbrainHeader(ctx *context.Context) map[string]string {
  533. return map[string]string{"A1": ctx.Tr("repo.cloudbrain_task"), "B1": ctx.Tr("repo.cloudbrain_task_type"), "C1": ctx.Tr("repo.modelarts.status"),
  534. "D1": ctx.Tr("repo.modelarts.createtime"), "E1": ctx.Tr("repo.modelarts.train_job.wait_time"), "F1": ctx.Tr("repo.modelarts.train_job.dura_time"),
  535. "G1": ctx.Tr("repo.modelarts.train_job.start_time"),
  536. "H1": ctx.Tr("repo.modelarts.train_job.end_time"), "I1": ctx.Tr("repo.modelarts.computing_resources"),
  537. "J1": ctx.Tr("repo.cloudbrain_creator"), "K1": ctx.Tr("repo.repo_name"), "L1": ctx.Tr("repo.cloudbrain_task_name")}
  538. }
  539. func allCloudbrainValues(row int, rs *models.CloudbrainInfo, ctx *context.Context) map[string]string {
  540. return map[string]string{getCellName("A", row): rs.DisplayJobName, getCellName("B", row): rs.JobType, getCellName("C", row): rs.Status,
  541. getCellName("D", row): time.Unix(int64(rs.Cloudbrain.CreatedUnix), 0).Format(CREATE_TIME_FORMAT), getCellName("E", row): getBrainWaitTime(rs),
  542. getCellName("F", row): rs.TrainJobDuration, getCellName("G", row): getBrainStartTime(rs),
  543. getCellName("H", row): getBrainEndTime(rs),
  544. getCellName("I", row): rs.ComputeResource, getCellName("J", row): rs.Name, getCellName("K", row): getBrainRepo(rs),
  545. getCellName("L", row): rs.JobName,
  546. }
  547. }
  548. func getBrainRepo(rs *models.CloudbrainInfo) string {
  549. if rs.Repo != nil {
  550. return rs.Repo.OwnerName + "/" + rs.Repo.Alias
  551. }
  552. return ""
  553. }
  554. func getBrainStartTime(rs *models.CloudbrainInfo) string {
  555. timeString := time.Unix(int64(rs.Cloudbrain.StartTime), 0).Format(CREATE_TIME_FORMAT)
  556. if timeString != "1970/01/01 08:00:00" {
  557. return timeString
  558. } else {
  559. return "0"
  560. }
  561. }
  562. func getBrainEndTime(rs *models.CloudbrainInfo) string {
  563. timeString := time.Unix(int64(rs.Cloudbrain.EndTime), 0).Format(CREATE_TIME_FORMAT)
  564. if timeString != "1970/01/01 08:00:00" {
  565. return timeString
  566. } else {
  567. return "0"
  568. }
  569. }
  570. func getBrainWaitTime(rs *models.CloudbrainInfo) string {
  571. waitTime := rs.Cloudbrain.StartTime - rs.Cloudbrain.CreatedUnix
  572. if waitTime <= 0 {
  573. return "0"
  574. } else {
  575. return models.ConvertDurationToStr(int64(waitTime))
  576. }
  577. }