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