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 8.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 GenerateDebugOneCount(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 job_type ='" + string(JobTypeDebug) + "'" +
  38. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  39. return x.SQL(countSql).Count()
  40. }
  41. func GetDebugOneDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  42. 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")
  43. if err != nil {
  44. return 0, err
  45. }
  46. return total, nil
  47. }
  48. func GenerateTrainOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
  49. countSql := "SELECT count(*) FROM " +
  50. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  51. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  52. " and job_type ='" + string(JobTypeTrain) + "'" +
  53. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  54. return x.SQL(countSql).Count()
  55. }
  56. func GetTrainOneDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  57. 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")
  58. if err != nil {
  59. return 0, err
  60. }
  61. return total, nil
  62. }
  63. func GenerateBenchmarkOneCount(beginTime time.Time, endTime time.Time) (int64, error) {
  64. countSql := "SELECT count(*) FROM " +
  65. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  66. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  67. " and job_type ='" + string(JobTypeBenchmark) + "'" +
  68. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  69. return x.SQL(countSql).Count()
  70. }
  71. func GetBenchmarkOneDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  72. 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")
  73. if err != nil {
  74. return 0, err
  75. }
  76. return total, nil
  77. }
  78. func GenerateDebugTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
  79. countSql := "SELECT count(*) FROM " +
  80. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  81. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  82. " and job_type ='" + string(JobTypeDebug) + "'" +
  83. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  84. return x.SQL(countSql).Count()
  85. }
  86. func GetDebugTwoDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  87. 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")
  88. if err != nil {
  89. return 0, err
  90. }
  91. return total, nil
  92. }
  93. func GenerateTrainTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
  94. countSql := "SELECT count(*) FROM " +
  95. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  96. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  97. " and job_type ='" + string(JobTypeTrain) + "'" +
  98. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  99. return x.SQL(countSql).Count()
  100. }
  101. func GetTrainTwoDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  102. 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")
  103. if err != nil {
  104. return 0, err
  105. }
  106. return total, nil
  107. }
  108. func GenerateInferenceTwoCount(beginTime time.Time, endTime time.Time) (int64, error) {
  109. countSql := "SELECT count(*) FROM " +
  110. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  111. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  112. " and job_type ='" + string(JobTypeInference) + "'" +
  113. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  114. return x.SQL(countSql).Count()
  115. }
  116. func GetInferenceTwoDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  117. 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")
  118. if err != nil {
  119. return 0, err
  120. }
  121. return total, nil
  122. }
  123. func DeleteCloudbrainStatisticDaily(date string) error {
  124. sess := xStatistic.NewSession()
  125. defer sess.Close()
  126. if err := sess.Begin(); err != nil {
  127. return fmt.Errorf("Begin: %v", err)
  128. }
  129. if _, err := sess.Where("date = ?", date).Delete(&CloudbrainStatistic{}); err != nil {
  130. return fmt.Errorf("Delete: %v", err)
  131. }
  132. if err := sess.Commit(); err != nil {
  133. sess.Close()
  134. return fmt.Errorf("Commit: %v", err)
  135. }
  136. sess.Close()
  137. return nil
  138. }
  139. func GetHourStatTime(timeStr string, whichHour int64) (time.Time, time.Time) {
  140. t, _ := time.Parse("2006-01-02", timeStr)
  141. timeNumber := t.Unix()
  142. beginTimeNumber := timeNumber - 8*60*60 + whichHour*60*60
  143. endTimeNumber := beginTimeNumber + 1*60*60
  144. beginTime := time.Unix(beginTimeNumber, 0)
  145. endTime := time.Unix(endTimeNumber, 0)
  146. log.Info("%s, %s", beginTime, endTime)
  147. return beginTime, endTime
  148. }
  149. func GetDayStatTime(timeStr string) (time.Time, time.Time) {
  150. t, _ := time.Parse("2006-01-02", timeStr)
  151. timeNumber := t.Unix()
  152. beginTimeNumber := timeNumber - 8*60*60
  153. endTimeNumber := beginTimeNumber + 16*60*60
  154. beginTime := time.Unix(beginTimeNumber, 0)
  155. endTime := time.Unix(endTimeNumber, 0)
  156. log.Info("%s, %s", beginTime, endTime)
  157. return beginTime, endTime
  158. }
  159. func CreateCloudbrainStatistic(cloudbrainStat *CloudbrainStatistic) (err error) {
  160. if _, err = xStatistic.Insert(cloudbrainStat); err != nil {
  161. return err
  162. }
  163. return nil
  164. }