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

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