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 6.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 GetTodayCreatorCount(beginTime time.Time, endTime time.Time) (int64, error) {
  36. countSql := "SELECT count(distinct user_id) FROM " +
  37. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  38. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  39. return x.SQL(countSql).Count()
  40. }
  41. func GetTodayCloudbrainCount(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. return x.SQL(countSql).Count()
  46. }
  47. func GetCreatorCount() (int64, error) {
  48. countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
  49. return x.SQL(countSql).Count()
  50. }
  51. func GetRecordBeginTime() ([]*CloudbrainInfo, error) {
  52. sess := x.NewSession()
  53. defer sess.Close()
  54. sess.OrderBy("cloudbrain.id ASC limit 1")
  55. cloudbrains := make([]*CloudbrainInfo, 0)
  56. if err := sess.Table(&Cloudbrain{}).Unscoped().
  57. Find(&cloudbrains); err != nil {
  58. log.Info("find error.")
  59. }
  60. return cloudbrains, nil
  61. }
  62. func GetAllStatusCloudBrain() map[string]int {
  63. sess := x.NewSession()
  64. defer sess.Close()
  65. cloudbrains := make([]*CloudbrainInfo, 0)
  66. if err := sess.Table(&Cloudbrain{}).Unscoped().
  67. Find(&cloudbrains); err != nil {
  68. log.Info("find error.")
  69. }
  70. cloudBrainStatusResult := make(map[string]int)
  71. for _, cloudbrain := range cloudbrains {
  72. if _, ok := cloudBrainStatusResult[cloudbrain.Status]; !ok {
  73. cloudBrainStatusResult[cloudbrain.Status] = 1
  74. } else {
  75. cloudBrainStatusResult[cloudbrain.Status] += 1
  76. }
  77. }
  78. return cloudBrainStatusResult
  79. }
  80. func GetWaittingTop() ([]*CloudbrainInfo, error) {
  81. sess := x.NewSession()
  82. defer sess.Close()
  83. var cond = builder.NewCond()
  84. cond = cond.And(
  85. builder.Eq{"cloudbrain.status": string(JobWaiting)},
  86. )
  87. sess.OrderBy("cloudbrain.created_unix ASC limit 10")
  88. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  89. if err := sess.Table(&Cloudbrain{}).Where(cond).
  90. Find(&cloudbrains); err != nil {
  91. log.Info("find error.")
  92. }
  93. return cloudbrains, nil
  94. }
  95. func GetRunningTop() ([]*CloudbrainInfo, error) {
  96. sess := x.NewSession()
  97. defer sess.Close()
  98. var cond = builder.NewCond()
  99. cond = cond.And(
  100. builder.Eq{"cloudbrain.status": string(JobRunning)},
  101. )
  102. sess.OrderBy("cloudbrain.duration DESC limit 10")
  103. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  104. if err := sess.Table(&Cloudbrain{}).Where(cond).
  105. Find(&cloudbrains); err != nil {
  106. log.Info("find error.")
  107. }
  108. return cloudbrains, nil
  109. }
  110. func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  111. countSql := "SELECT count(*) FROM " +
  112. "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime +
  113. "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime +
  114. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') >= '" + hourBeginTime +
  115. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') < '" + hourEndTime + "'"
  116. return x.SQL(countSql).Count()
  117. }
  118. //SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime))
  119. func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  120. countSql := "SELECT count(*) FROM " +
  121. "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime +
  122. "') or (to_char(to_timestamp(end_time), 'HH24:MI:SS') < '" + hourBeginTime + "'))" +
  123. " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime +
  124. "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')"
  125. return x.SQL(countSql).Count()
  126. }
  127. func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  128. //0 to 23 for each hour,
  129. dateHourMap := make(map[string]interface{})
  130. 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}
  131. for key, value := range slice {
  132. hourBeginHour := util.AddZero(value) + ":00:00"
  133. hourEndHour := util.AddZero(value+1) + ":00:00"
  134. cout, err := getCreatePeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  135. if err != nil {
  136. log.Error("Can not query getCreatePeriodCount.", err)
  137. return nil, nil
  138. }
  139. dateHourMap[strconv.Itoa(key)] = cout
  140. }
  141. return dateHourMap, nil
  142. }
  143. func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  144. dateHourMap := make(map[string]interface{})
  145. 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}
  146. for key, value := range slice {
  147. hourBeginHour := util.AddZero(value) + ":00:00"
  148. hourEndHour := util.AddZero(value+1) + ":00:00"
  149. cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  150. if err != nil {
  151. log.Error("Can not query getRunPeriodCount.", err)
  152. return nil, nil
  153. }
  154. dateHourMap[strconv.Itoa(key)] = cout
  155. }
  156. return dateHourMap, nil
  157. }