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 13 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 GetJobWaitingPeriodCount(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 GetJobRunningPeriodCount(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 GetJobStoppedPeriodCount(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(JobStopped) + "'"
  52. return x.SQL(countSql).Count()
  53. }
  54. func GetJobSucceededPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  55. countSql := "SELECT 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(JobSucceeded) + "'"
  59. return x.SQL(countSql).Count()
  60. }
  61. func GetJobFailedPeriodCount(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 status ='" + string(JobFailed) + "'"
  66. return x.SQL(countSql).Count()
  67. }
  68. // func GetWaitingCount(beginTime time.Time, endTime time.Time) (int64, error) {
  69. // countSql := "SELECT distinct count(*) FROM " +
  70. // "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  71. // " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  72. // " and status ='" + string(JobWaiting) + "'"
  73. // return x.SQL(countSql).Count()
  74. // }
  75. func GetDebugOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  76. countSql := "SELECT count(*) FROM " +
  77. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  78. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  79. " and job_type ='" + string(JobTypeDebug) + "'" +
  80. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  81. return x.SQL(countSql).Count()
  82. }
  83. func GetDebugOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  84. 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")
  85. if err != nil {
  86. return 0, err
  87. }
  88. return total, nil
  89. }
  90. func GetTrainOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  91. countSql := "SELECT count(*) FROM " +
  92. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  93. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  94. " and job_type ='" + string(JobTypeTrain) + "'" +
  95. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  96. return x.SQL(countSql).Count()
  97. }
  98. func GetTrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  99. 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")
  100. if err != nil {
  101. return 0, err
  102. }
  103. return total, nil
  104. }
  105. func GetBenchmarkOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  106. countSql := "SELECT count(*) FROM " +
  107. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  108. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  109. " and job_type ='" + string(JobTypeBenchmark) + "'" +
  110. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  111. return x.SQL(countSql).Count()
  112. }
  113. func GetBenchmarkOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  114. 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")
  115. if err != nil {
  116. return 0, err
  117. }
  118. return total, nil
  119. }
  120. func GetDebugTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  121. countSql := "SELECT count(*) FROM " +
  122. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  123. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  124. " and job_type ='" + string(JobTypeDebug) + "'" +
  125. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  126. return x.SQL(countSql).Count()
  127. }
  128. func GetDebugTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  129. 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")
  130. if err != nil {
  131. return 0, err
  132. }
  133. return total, nil
  134. }
  135. func GetTrainTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  136. countSql := "SELECT count(*) FROM " +
  137. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  138. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  139. " and job_type ='" + string(JobTypeTrain) + "'" +
  140. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  141. return x.SQL(countSql).Count()
  142. }
  143. func GetTrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  144. 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")
  145. if err != nil {
  146. return 0, err
  147. }
  148. return total, nil
  149. }
  150. func GetInferenceTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  151. countSql := "SELECT count(*) FROM " +
  152. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  153. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  154. " and job_type ='" + string(JobTypeInference) + "'" +
  155. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  156. return x.SQL(countSql).Count()
  157. }
  158. func GetInferenceTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  159. 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")
  160. if err != nil {
  161. return 0, err
  162. }
  163. return total, nil
  164. }
  165. func GetCloudBrainOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  166. countSql := "SELECT count(*) FROM " +
  167. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  168. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  169. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  170. return x.SQL(countSql).Count()
  171. }
  172. func GetCloudBrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  173. 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")
  174. if err != nil {
  175. return 0, err
  176. }
  177. return total, nil
  178. }
  179. func GetCloudBrainTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  180. countSql := "SELECT count(*) FROM " +
  181. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  182. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  183. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  184. return x.SQL(countSql).Count()
  185. }
  186. func GetCloudBrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  187. 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")
  188. if err != nil {
  189. return 0, err
  190. }
  191. return total, nil
  192. }
  193. func GetCloudBrainOneCount() (int64, error) {
  194. countSql := "SELECT count(*) FROM " +
  195. "public.cloudbrain where type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  196. return x.SQL(countSql).Count()
  197. }
  198. func GetCloudBrainOneDuration() (int64, error) {
  199. total, err := x.Where("type = ? ", TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
  200. if err != nil {
  201. return 0, err
  202. }
  203. return total, nil
  204. }
  205. func GetCloudBrainTwoCount() (int64, error) {
  206. countSql := "SELECT count(*) FROM " +
  207. "public.cloudbrain where type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  208. return x.SQL(countSql).Count()
  209. }
  210. func GetCloudBrainTwoDuration() (int64, error) {
  211. total, err := x.Where("type = ? ", TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
  212. if err != nil {
  213. return 0, err
  214. }
  215. return total, nil
  216. }
  217. func GetCreatorPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  218. countSql := "SELECT count(distinct user_id) FROM " +
  219. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  220. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  221. return x.SQL(countSql).Count()
  222. }
  223. func GetCreatorCount() (int64, error) {
  224. countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
  225. return x.SQL(countSql).Count()
  226. }
  227. func DeleteCloudbrainStatisticDaily(date string) error {
  228. sess := xStatistic.NewSession()
  229. defer sess.Close()
  230. if err := sess.Begin(); err != nil {
  231. return fmt.Errorf("Begin: %v", err)
  232. }
  233. if _, err := sess.Where("date = ?", date).Delete(&CloudbrainStatistic{}); err != nil {
  234. return fmt.Errorf("Delete: %v", err)
  235. }
  236. if err := sess.Commit(); err != nil {
  237. sess.Close()
  238. return fmt.Errorf("Commit: %v", err)
  239. }
  240. sess.Close()
  241. return nil
  242. }
  243. func GetHourStatTime(timeStr string, whichHour int64) (time.Time, time.Time) {
  244. t, _ := time.Parse("2006-01-02", timeStr)
  245. timeNumber := t.Unix()
  246. beginTimeNumber := timeNumber - 8*60*60 + whichHour*60*60
  247. endTimeNumber := beginTimeNumber + 1*60*60
  248. beginTime := time.Unix(beginTimeNumber, 0)
  249. endTime := time.Unix(endTimeNumber, 0)
  250. log.Info("%s, %s", beginTime, endTime)
  251. return beginTime, endTime
  252. }
  253. func GetDayStatTime(timeStr string) (time.Time, time.Time) {
  254. t, _ := time.Parse("2006-01-02", timeStr)
  255. timeNumber := t.Unix()
  256. beginTimeNumber := timeNumber - 8*60*60
  257. endTimeNumber := beginTimeNumber + 16*60*60
  258. beginTime := time.Unix(beginTimeNumber, 0)
  259. endTime := time.Unix(endTimeNumber, 0)
  260. log.Info("%s, %s", beginTime, endTime)
  261. return beginTime, endTime
  262. }
  263. func CreateCloudbrainStatistic(cloudbrainStat *CloudbrainStatistic) (err error) {
  264. if _, err = xStatistic.Insert(cloudbrainStat); err != nil {
  265. return err
  266. }
  267. return nil
  268. }