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