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 12 kB

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