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