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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 GetCreatorCount() (int64, error) {
  155. countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
  156. return x.SQL(countSql).Count()
  157. }
  158. func GetRecordBeginTime() ([]*CloudbrainInfo, error) {
  159. sess := x.NewSession()
  160. defer sess.Close()
  161. sess.OrderBy("cloudbrain.id ASC limit 1")
  162. cloudbrains := make([]*CloudbrainInfo, 0)
  163. if err := sess.Table(&Cloudbrain{}).Unscoped().
  164. Find(&cloudbrains); err != nil {
  165. log.Info("find error.")
  166. }
  167. return cloudbrains, nil
  168. }
  169. func GetAllStatusCloudBrain() map[string]int {
  170. sess := x.NewSession()
  171. defer sess.Close()
  172. cloudbrains := make([]*CloudbrainInfo, 0)
  173. if err := sess.Table(&Cloudbrain{}).Unscoped().
  174. Find(&cloudbrains); err != nil {
  175. log.Info("find error.")
  176. }
  177. cloudBrainStatusResult := make(map[string]int)
  178. for _, cloudbrain := range cloudbrains {
  179. if _, ok := cloudBrainStatusResult[cloudbrain.Status]; !ok {
  180. cloudBrainStatusResult[cloudbrain.Status] = 1
  181. } else {
  182. cloudBrainStatusResult[cloudbrain.Status] += 1
  183. }
  184. }
  185. return cloudBrainStatusResult
  186. }
  187. func GetWaittingTop() ([]*CloudbrainInfo, error) {
  188. sess := x.NewSession()
  189. defer sess.Close()
  190. var cond = builder.NewCond()
  191. cond = cond.And(
  192. builder.Eq{"cloudbrain.status": string(JobWaiting)},
  193. )
  194. sess.OrderBy("(cloudbrain.start_time-cloudbrain.created_unix) DESC limit 10")
  195. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  196. if err := sess.Table(&Cloudbrain{}).Where(cond).
  197. Find(&cloudbrains); err != nil {
  198. log.Info("find error.")
  199. }
  200. return cloudbrains, nil
  201. }
  202. func GetRunningTop() ([]*CloudbrainInfo, error) {
  203. sess := x.NewSession()
  204. defer sess.Close()
  205. var cond = builder.NewCond()
  206. cond = cond.And(
  207. builder.Eq{"cloudbrain.status": string(JobRunning)},
  208. )
  209. sess.OrderBy("(cloudbrain.end_time-cloudbrain.start_time) DESC limit 10")
  210. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  211. if err := sess.Table(&Cloudbrain{}).Where(cond).
  212. Find(&cloudbrains); err != nil {
  213. log.Info("find error.")
  214. }
  215. return cloudbrains, nil
  216. }
  217. func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  218. countSql := "SELECT count(*) FROM " +
  219. "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime +
  220. "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime +
  221. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') >= '" + hourBeginTime +
  222. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') < '" + hourEndTime + "'"
  223. return x.SQL(countSql).Count()
  224. }
  225. //SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime))
  226. func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  227. countSql := "SELECT count(*) FROM " +
  228. "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime +
  229. "') or (to_char(to_timestamp(end_time), 'HH24:MI:SS') < '" + hourBeginTime + "'))" +
  230. " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime +
  231. "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')"
  232. return x.SQL(countSql).Count()
  233. }
  234. func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  235. //0 to 23 for each hour,
  236. dateHourMap := make(map[string]interface{})
  237. 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}
  238. for key, value := range slice {
  239. hourBeginHour := util.AddZero(value) + ":00:00"
  240. hourEndHour := util.AddZero(value+1) + ":00:00"
  241. cout, err := getCreatePeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  242. if err != nil {
  243. log.Error("Can not query getCreatePeriodCount.", err)
  244. return nil, nil
  245. }
  246. dateHourMap[strconv.Itoa(key)] = cout
  247. }
  248. return dateHourMap, nil
  249. }
  250. func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  251. dateHourMap := make(map[string]interface{})
  252. 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}
  253. for key, value := range slice {
  254. hourBeginHour := util.AddZero(value) + ":00:00"
  255. hourEndHour := util.AddZero(value+1) + ":00:00"
  256. cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  257. if err != nil {
  258. log.Error("Can not query getRunPeriodCount.", err)
  259. return nil, nil
  260. }
  261. dateHourMap[strconv.Itoa(key)] = cout
  262. }
  263. return dateHourMap, nil
  264. cloudBrainStatusResult := make(map[string]int)
  265. for _, cloudbrain := range cloudbrains {
  266. if _, ok := cloudBrainStatusResult[cloudbrain.Status]; !ok {
  267. cloudBrainStatusResult[cloudbrain.Status] = 1
  268. } else {
  269. cloudBrainStatusResult[cloudbrain.Status] += 1
  270. }
  271. }
  272. return cloudBrainStatusResult
  273. }