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