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_static.go 14 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package models
  2. import (
  3. "strconv"
  4. "time"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/modules/timeutil"
  7. )
  8. type TaskDetail struct {
  9. ID int64 `json:"ID"`
  10. JobName string `json:"JobName"`
  11. DisplayJobName string `json:"DisplayJobName"`
  12. Status string `json:"Status"`
  13. JobType string `json:"JobType"`
  14. CreatedUnix timeutil.TimeStamp `json:"CreatedUnix"`
  15. WaitTime timeutil.TimeStamp `json:"WaitTime"`
  16. RunTime timeutil.TimeStamp `json:"RunTime"`
  17. StartTime timeutil.TimeStamp `json:"StartTime"`
  18. EndTime timeutil.TimeStamp `json:"EndTime"`
  19. ComputeResource string `json:"ComputeResource"`
  20. Type int `json:"Type"`
  21. UserName string `json:"UserName"`
  22. RepoName string `json:"RepoName"`
  23. RepoID int64 `json:"RepoID"`
  24. }
  25. // func GetJobWaitingPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  26. // countSql := "SELECT count(*) FROM " +
  27. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  28. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  29. // " and status ='" + string(JobWaiting) + "'"
  30. // return x.SQL(countSql).Count()
  31. // }
  32. // func GetJobRunningPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  33. // countSql := "SELECT count(*) FROM " +
  34. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  35. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  36. // " and status ='" + string(JobRunning) + "'"
  37. // return x.SQL(countSql).Count()
  38. // }
  39. // func GetJobStoppedPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  40. // countSql := "SELECT count(*) FROM " +
  41. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  42. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  43. // " and status ='" + string(JobStopped) + "'"
  44. // return x.SQL(countSql).Count()
  45. // }
  46. // func GetJobSucceededPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  47. // countSql := "SELECT count(*) FROM " +
  48. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  49. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  50. // " and status ='" + string(JobSucceeded) + "'"
  51. // return x.SQL(countSql).Count()
  52. // }
  53. // func GetJobFailedPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  54. // countSql := "SELECT count(*) FROM " +
  55. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  56. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  57. // " and status ='" + string(JobFailed) + "'"
  58. // return x.SQL(countSql).Count()
  59. // }
  60. func GetDebugOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  61. countSql := "SELECT count(*) FROM " +
  62. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  63. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  64. " and job_type ='" + string(JobTypeDebug) + "'" +
  65. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  66. return x.SQL(countSql).Count()
  67. }
  68. func GetDebugOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  69. total, err := x.Where("created_unix >= ? And created_unix < ? And job_type = ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), JobTypeDebug, TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
  70. if err != nil {
  71. return 0, err
  72. }
  73. return total, nil
  74. }
  75. func GetTrainOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  76. countSql := "SELECT count(*) FROM " +
  77. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  78. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  79. " and job_type ='" + string(JobTypeTrain) + "'" +
  80. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  81. return x.SQL(countSql).Count()
  82. }
  83. func GetTrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  84. total, err := x.Where("created_unix >= ? And created_unix < ? And job_type = ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), JobTypeTrain, TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
  85. if err != nil {
  86. return 0, err
  87. }
  88. return total, nil
  89. }
  90. func GetBenchmarkOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  91. countSql := "SELECT count(*) FROM " +
  92. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  93. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  94. " and job_type ='" + string(JobTypeBenchmark) + "'" +
  95. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  96. return x.SQL(countSql).Count()
  97. }
  98. func GetBenchmarkOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  99. total, err := x.Where("created_unix >= ? And created_unix < ? And job_type = ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), JobTypeBenchmark, TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
  100. if err != nil {
  101. return 0, err
  102. }
  103. return total, nil
  104. }
  105. func GetDebugTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  106. countSql := "SELECT count(*) FROM " +
  107. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  108. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  109. " and job_type ='" + string(JobTypeDebug) + "'" +
  110. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  111. return x.SQL(countSql).Count()
  112. }
  113. func GetDebugTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  114. total, err := x.Where("created_unix >= ? And created_unix < ? And job_type = ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), JobTypeDebug, TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
  115. if err != nil {
  116. return 0, err
  117. }
  118. return total, nil
  119. }
  120. func GetTrainTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  121. countSql := "SELECT count(*) FROM " +
  122. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  123. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  124. " and job_type ='" + string(JobTypeTrain) + "'" +
  125. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  126. return x.SQL(countSql).Count()
  127. }
  128. func GetTrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  129. total, err := x.Where("created_unix >= ? And created_unix < ? And job_type = ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), JobTypeTrain, TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
  130. if err != nil {
  131. return 0, err
  132. }
  133. return total, nil
  134. }
  135. func GetInferenceTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  136. countSql := "SELECT count(*) FROM " +
  137. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  138. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  139. " and job_type ='" + string(JobTypeInference) + "'" +
  140. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  141. return x.SQL(countSql).Count()
  142. }
  143. func GetInferenceTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  144. total, err := x.Where("created_unix >= ? And created_unix < ? And job_type = ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), JobTypeInference, TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
  145. if err != nil {
  146. return 0, err
  147. }
  148. return total, nil
  149. }
  150. func GetCloudBrainOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  151. countSql := "SELECT count(*) FROM " +
  152. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  153. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  154. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  155. return x.SQL(countSql).Count()
  156. }
  157. func GetCloudBrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  158. total, err := x.Where("created_unix >= ? And created_unix < ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
  159. if err != nil {
  160. return 0, err
  161. }
  162. return total, nil
  163. }
  164. func GetCloudBrainTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  165. countSql := "SELECT count(*) FROM " +
  166. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  167. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  168. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  169. return x.SQL(countSql).Count()
  170. }
  171. func GetCloudBrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  172. total, err := x.Where("created_unix >= ? And created_unix < ? And type = ? ", strconv.FormatInt(beginTime.Unix(), 10), strconv.FormatInt(endTime.Unix(), 10), TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
  173. if err != nil {
  174. return 0, err
  175. }
  176. return total, nil
  177. }
  178. func GetTodayCreatorCount(beginTime time.Time, endTime time.Time) (int64, error) {
  179. countSql := "SELECT count(distinct user_id) FROM " +
  180. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  181. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  182. return x.SQL(countSql).Count()
  183. }
  184. func GetCreatorCount() (int64, error) {
  185. countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
  186. return x.SQL(countSql).Count()
  187. }
  188. func GetAllCloudBrain() ([]*CloudbrainInfo, error) {
  189. sess := x.NewSession()
  190. defer sess.Close()
  191. cloudbrains := make([]*CloudbrainInfo, 0)
  192. if err := sess.Table(&Cloudbrain{}).Unscoped().
  193. Find(&cloudbrains); err != nil {
  194. log.Info("find error.")
  195. }
  196. return cloudbrains, nil
  197. }
  198. // func GetCloudBrains(opts *CloudbrainsOptions) ([]*CloudbrainInfo, error) {
  199. // sess := x.NewSession()
  200. // defer sess.Close()
  201. // var cond = builder.NewCond()
  202. // if len(opts.JobStatus) > 0 {
  203. // if opts.JobStatusNot {
  204. // cond = cond.And(
  205. // builder.NotIn("cloudbrain.status", opts.JobStatus),
  206. // )
  207. // } else {
  208. // cond = cond.And(
  209. // builder.In("cloudbrain.status", opts.JobStatus),
  210. // )
  211. // }
  212. // }
  213. // if opts.Page >= 0 && opts.PageSize > 0 {
  214. // var start int
  215. // if opts.Page == 0 {
  216. // start = 0
  217. // } else {
  218. // start = (opts.Page - 1) * opts.PageSize
  219. // }
  220. // sess.Limit(opts.PageSize, start)
  221. // }
  222. // sess.OrderBy("cloudbrain.created_unix DESC")
  223. // cloudbrains := make([]*CloudbrainInfo, 0, setting.UI.IssuePagingNum)
  224. // if err := sess.Table(&Cloudbrain{}).Where(cond).Unscoped().
  225. // Find(&cloudbrains); err != nil {
  226. // log.Info("find error.")
  227. // }
  228. // return cloudbrains, nil
  229. // }
  230. func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  231. countSql := "SELECT count(*) FROM " +
  232. "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime +
  233. "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime +
  234. "' and to_char(to_timestamp(created_unix), ' HH24:MI:SS') >= '" + hourBeginTime +
  235. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') <= '" + hourEndTime + "'"
  236. return x.SQL(countSql).Count()
  237. }
  238. //SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime))
  239. func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  240. countSql := "SELECT count(*) FROM " +
  241. "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime +
  242. "') or (to_char(to_timestamp(end_time), 'HH24:MI:SS') < '" + hourBeginTime + "'))" +
  243. " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime +
  244. "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')"
  245. return x.SQL(countSql).Count()
  246. }
  247. func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  248. //0 to 23 for each hour,
  249. dateHourMap := make(map[string]interface{})
  250. var slice = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
  251. for key, value := range slice {
  252. hourBeginHour := strconv.Itoa(value) + ":00:00"
  253. hourEndHour := strconv.Itoa(value+1) + ":00:00"
  254. cout, err := getCreatePeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  255. if err != nil {
  256. log.Error("Can not query getCreatePeriodCount.", err)
  257. return nil, nil
  258. }
  259. dateHourMap[strconv.Itoa(key)] = cout
  260. }
  261. return dateHourMap, nil
  262. }
  263. func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  264. //0 to 23 for each hour,
  265. dateHourMap := make(map[string]interface{})
  266. var slice = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
  267. for key, value := range slice {
  268. hourBeginHour := strconv.Itoa(value) + ":00:00"
  269. hourEndHour := strconv.Itoa(value+1) + ":00:00"
  270. cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  271. if err != nil {
  272. log.Error("Can not query getRunPeriodCount.", err)
  273. return nil, nil
  274. }
  275. dateHourMap[strconv.Itoa(key)] = cout
  276. }
  277. return dateHourMap, nil
  278. }