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 9.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package models
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/timeutil"
  8. )
  9. // Cloudbrain statistic info of all CloudbrainTasks
  10. type CloudbrainStatistic struct {
  11. ID int64 `xorm:"pk autoincr" json:"-"`
  12. Date string `xorm:"NOT NULL DEFAULT 0" json:"date"`
  13. WhichHour int64 `xorm:"NOT NULL DEFAULT -1" json:"whichHour"`
  14. NumDubugOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numDubugOne"`
  15. NumBenchmarkOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numBenchmarkOne"`
  16. NumTrainOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numTrainOne"`
  17. NumDubugTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"numDubugTwo"`
  18. NumTrainTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"numTrainTwo"`
  19. NumInferenceTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"numInferenceTwo"`
  20. NumCloudOneAll int64 `xorm:"NOT NULL DEFAULT 0" json:"numCloudOneAll"`
  21. NumCloudTwoAll int64 `xorm:"NOT NULL DEFAULT 0" json:"numCloudTwoAll"`
  22. DurationDubugOne int64 `xorm:"NOT NULL DEFAULT 0" json:"durationDubugOne"`
  23. DurationBenchmarkOne int64 `xorm:"NOT NULL DEFAULT 0" json:"durationBenchmarkOne"`
  24. DurationTrainOne int64 `xorm:"NOT NULL DEFAULT 0" json:"durationTrainOne"`
  25. DurationDebugTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"durationDebugTwo"`
  26. DurationTrainTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"durationTrainTwo"`
  27. DurationInferenceTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"durationInferenceTwo"`
  28. DurationCloudOneAll int64 `xorm:"NOT NULL DEFAULT 0" json:"durationCloudOneAll"`
  29. DurationCloudTwoAll int64 `xorm:"NOT NULL DEFAULT 0" json:"durationCloudTwoAll"`
  30. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created" json:"-"`
  31. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated" json:"-"`
  32. }
  33. func GetJobWaitingCount(beginTime time.Time, endTime time.Time) (int64, error) {
  34. countSql := "SELECT count(*) FROM " +
  35. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  36. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  37. " and status ='" + string(JobWaiting) + "'"
  38. return x.SQL(countSql).Count()
  39. }
  40. func GetJobRunningCount(beginTime time.Time, endTime time.Time) (int64, error) {
  41. countSql := "SELECT count(*) FROM " +
  42. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  43. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  44. " and status ='" + string(JobRunning) + "'"
  45. return x.SQL(countSql).Count()
  46. }
  47. func GetJobSucceededCount(beginTime time.Time, endTime time.Time) (int64, error) {
  48. countSql := "SELECT count(*) FROM " +
  49. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  50. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  51. " and status ='" + string(JobSucceeded) + "'"
  52. return x.SQL(countSql).Count()
  53. }
  54. // func GetWaitingCount(beginTime time.Time, endTime time.Time) (int64, error) {
  55. // countSql := "SELECT distinct count(*) FROM " +
  56. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  57. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  58. // " and status ='" + string(JobWaiting) + "'"
  59. // return x.SQL(countSql).Count()
  60. // }
  61. func GetDebugOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
  62. countSql := "SELECT count(*) FROM " +
  63. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  64. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  65. " and job_type ='" + string(JobTypeDebug) + "'" +
  66. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  67. return x.SQL(countSql).Count()
  68. }
  69. func GetDebugOneDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  70. 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")
  71. if err != nil {
  72. return 0, err
  73. }
  74. return total, nil
  75. }
  76. func GetTrainOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
  77. countSql := "SELECT count(*) FROM " +
  78. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  79. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  80. " and job_type ='" + string(JobTypeTrain) + "'" +
  81. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  82. return x.SQL(countSql).Count()
  83. }
  84. func GetTrainOneDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  85. 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")
  86. if err != nil {
  87. return 0, err
  88. }
  89. return total, nil
  90. }
  91. func GetBenchmarkOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
  92. countSql := "SELECT count(*) FROM " +
  93. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  94. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  95. " and job_type ='" + string(JobTypeBenchmark) + "'" +
  96. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  97. return x.SQL(countSql).Count()
  98. }
  99. func GetBenchmarkOneDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  100. 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")
  101. if err != nil {
  102. return 0, err
  103. }
  104. return total, nil
  105. }
  106. func GetDebugTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
  107. countSql := "SELECT count(*) FROM " +
  108. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  109. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  110. " and job_type ='" + string(JobTypeDebug) + "'" +
  111. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  112. return x.SQL(countSql).Count()
  113. }
  114. func GetDebugTwoDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  115. 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")
  116. if err != nil {
  117. return 0, err
  118. }
  119. return total, nil
  120. }
  121. func GetTrainTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
  122. countSql := "SELECT count(*) FROM " +
  123. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  124. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  125. " and job_type ='" + string(JobTypeTrain) + "'" +
  126. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  127. return x.SQL(countSql).Count()
  128. }
  129. func GetTrainTwoDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  130. 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")
  131. if err != nil {
  132. return 0, err
  133. }
  134. return total, nil
  135. }
  136. func GetInferenceTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
  137. countSql := "SELECT count(*) FROM " +
  138. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  139. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  140. " and job_type ='" + string(JobTypeInference) + "'" +
  141. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  142. return x.SQL(countSql).Count()
  143. }
  144. func GetInferenceTwoDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  145. 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")
  146. if err != nil {
  147. return 0, err
  148. }
  149. return total, nil
  150. }
  151. func DeleteCloudbrainStatisticDaily(date string) error {
  152. sess := xStatistic.NewSession()
  153. defer sess.Close()
  154. if err := sess.Begin(); err != nil {
  155. return fmt.Errorf("Begin: %v", err)
  156. }
  157. if _, err := sess.Where("date = ?", date).Delete(&CloudbrainStatistic{}); err != nil {
  158. return fmt.Errorf("Delete: %v", err)
  159. }
  160. if err := sess.Commit(); err != nil {
  161. sess.Close()
  162. return fmt.Errorf("Commit: %v", err)
  163. }
  164. sess.Close()
  165. return nil
  166. }
  167. func GetHourStatTime(timeStr string, whichHour int64) (time.Time, time.Time) {
  168. t, _ := time.Parse("2006-01-02", timeStr)
  169. timeNumber := t.Unix()
  170. beginTimeNumber := timeNumber - 8*60*60 + whichHour*60*60
  171. endTimeNumber := beginTimeNumber + 1*60*60
  172. beginTime := time.Unix(beginTimeNumber, 0)
  173. endTime := time.Unix(endTimeNumber, 0)
  174. log.Info("%s, %s", beginTime, endTime)
  175. return beginTime, endTime
  176. }
  177. func GetDayStatTime(timeStr string) (time.Time, time.Time) {
  178. t, _ := time.Parse("2006-01-02", timeStr)
  179. timeNumber := t.Unix()
  180. beginTimeNumber := timeNumber - 8*60*60
  181. endTimeNumber := beginTimeNumber + 16*60*60
  182. beginTime := time.Unix(beginTimeNumber, 0)
  183. endTime := time.Unix(endTimeNumber, 0)
  184. log.Info("%s, %s", beginTime, endTime)
  185. return beginTime, endTime
  186. }
  187. func CreateCloudbrainStatistic(cloudbrainStat *CloudbrainStatistic) (err error) {
  188. if _, err = xStatistic.Insert(cloudbrainStat); err != nil {
  189. return err
  190. }
  191. return nil
  192. }