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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package models
  2. import (
  3. "strconv"
  4. "time"
  5. "code.gitea.io/gitea/modules/log"
  6. )
  7. func GetJobWaitingPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  8. countSql := "SELECT count(*) FROM " +
  9. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  10. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  11. " and status ='" + string(JobWaiting) + "'"
  12. return x.SQL(countSql).Count()
  13. }
  14. func GetJobRunningPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  15. countSql := "SELECT count(*) FROM " +
  16. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  17. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  18. " and status ='" + string(JobRunning) + "'"
  19. return x.SQL(countSql).Count()
  20. }
  21. func GetJobStoppedPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  22. countSql := "SELECT count(*) FROM " +
  23. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  24. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  25. " and status ='" + string(JobStopped) + "'"
  26. return x.SQL(countSql).Count()
  27. }
  28. func GetJobSucceededPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  29. countSql := "SELECT count(*) FROM " +
  30. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  31. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  32. " and status ='" + string(JobSucceeded) + "'"
  33. return x.SQL(countSql).Count()
  34. }
  35. func GetJobFailedPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  36. countSql := "SELECT count(*) FROM " +
  37. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  38. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  39. " and status ='" + string(JobFailed) + "'"
  40. return x.SQL(countSql).Count()
  41. }
  42. func GetDebugOnePeriodCount(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. " and job_type ='" + string(JobTypeDebug) + "'" +
  47. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  48. return x.SQL(countSql).Count()
  49. }
  50. func GetDebugOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  51. 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")
  52. if err != nil {
  53. return 0, err
  54. }
  55. return total, nil
  56. }
  57. func GetTrainOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  58. countSql := "SELECT count(*) FROM " +
  59. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  60. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  61. " and job_type ='" + string(JobTypeTrain) + "'" +
  62. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  63. return x.SQL(countSql).Count()
  64. }
  65. func GetTrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  66. 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")
  67. if err != nil {
  68. return 0, err
  69. }
  70. return total, nil
  71. }
  72. func GetBenchmarkOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  73. countSql := "SELECT count(*) FROM " +
  74. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  75. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  76. " and job_type ='" + string(JobTypeBenchmark) + "'" +
  77. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  78. return x.SQL(countSql).Count()
  79. }
  80. func GetBenchmarkOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  81. 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")
  82. if err != nil {
  83. return 0, err
  84. }
  85. return total, nil
  86. }
  87. func GetDebugTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  88. countSql := "SELECT count(*) FROM " +
  89. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  90. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  91. " and job_type ='" + string(JobTypeDebug) + "'" +
  92. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  93. return x.SQL(countSql).Count()
  94. }
  95. func GetDebugTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  96. 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")
  97. if err != nil {
  98. return 0, err
  99. }
  100. return total, nil
  101. }
  102. func GetTrainTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  103. countSql := "SELECT count(*) FROM " +
  104. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  105. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  106. " and job_type ='" + string(JobTypeTrain) + "'" +
  107. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  108. return x.SQL(countSql).Count()
  109. }
  110. func GetTrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  111. 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")
  112. if err != nil {
  113. return 0, err
  114. }
  115. return total, nil
  116. }
  117. func GetInferenceTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  118. countSql := "SELECT count(*) FROM " +
  119. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  120. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  121. " and job_type ='" + string(JobTypeInference) + "'" +
  122. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  123. return x.SQL(countSql).Count()
  124. }
  125. func GetInferenceTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  126. 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")
  127. if err != nil {
  128. return 0, err
  129. }
  130. return total, nil
  131. }
  132. func GetCloudBrainOnePeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  133. countSql := "SELECT count(*) FROM " +
  134. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  135. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  136. " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  137. return x.SQL(countSql).Count()
  138. }
  139. func GetCloudBrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  140. 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")
  141. if err != nil {
  142. return 0, err
  143. }
  144. return total, nil
  145. }
  146. func GetCloudBrainTwoPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  147. countSql := "SELECT count(*) FROM " +
  148. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  149. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10) +
  150. " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  151. return x.SQL(countSql).Count()
  152. }
  153. func GetCloudBrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) {
  154. 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")
  155. if err != nil {
  156. return 0, err
  157. }
  158. return total, nil
  159. }
  160. func GetCloudBrainOneCount() (int64, error) {
  161. countSql := "SELECT count(*) FROM " +
  162. "public.cloudbrain where type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
  163. return x.SQL(countSql).Count()
  164. }
  165. func GetCloudBrainOneDuration() (int64, error) {
  166. total, err := x.Where("type = ? ", TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
  167. if err != nil {
  168. return 0, err
  169. }
  170. return total, nil
  171. }
  172. func GetCloudBrainTwoCount() (int64, error) {
  173. countSql := "SELECT count(*) FROM " +
  174. "public.cloudbrain where type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
  175. return x.SQL(countSql).Count()
  176. }
  177. func GetCloudBrainTwoDuration() (int64, error) {
  178. total, err := x.Where("type = ? ", TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
  179. if err != nil {
  180. return 0, err
  181. }
  182. return total, nil
  183. }
  184. func GetCreatorPeriodCount(beginTime time.Time, endTime time.Time) (int64, error) {
  185. countSql := "SELECT count(distinct user_id) FROM " +
  186. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  187. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  188. return x.SQL(countSql).Count()
  189. }
  190. func GetCreatorCount() (int64, error) {
  191. countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
  192. return x.SQL(countSql).Count()
  193. }
  194. func GetJobWaitingCount() (int64, error) {
  195. countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobWaiting) + "'"
  196. return x.SQL(countSql).Count()
  197. }
  198. func GetJobStoppedCount() (int64, error) {
  199. countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobStopped) + "'"
  200. return x.SQL(countSql).Count()
  201. }
  202. func GetJobSucceededCount() (int64, error) {
  203. countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobSucceeded) + "'"
  204. return x.SQL(countSql).Count()
  205. }
  206. func GetJobFailedCount() (int64, error) {
  207. countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobFailed) + "'"
  208. return x.SQL(countSql).Count()
  209. }
  210. func GetJobRunningCount() (int64, error) {
  211. countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobRunning) + "'"
  212. return x.SQL(countSql).Count()
  213. }
  214. func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  215. countSql := "SELECT count(*) FROM " +
  216. "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime +
  217. "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime +
  218. "' and to_char(to_timestamp(created_unix), ' HH24:MI:SS') >= '" + hourBeginTime +
  219. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') <= '" + hourEndTime + "'"
  220. return x.SQL(countSql).Count()
  221. }
  222. //SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime))
  223. func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  224. countSql := "SELECT count(*) FROM " +
  225. "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime +
  226. "') or (to_char(to_timestamp(end_time), 'HH24:MI:SS') < '" + hourBeginTime + "'))" +
  227. " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime +
  228. "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')"
  229. return x.SQL(countSql).Count()
  230. }
  231. func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  232. //0 to 23 for each hour,
  233. dateHourMap := make(map[string]interface{})
  234. var slice = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
  235. for key, value := range slice {
  236. hourBeginHour := strconv.Itoa(value) + ":00:00"
  237. hourEndHour := strconv.Itoa(value+1) + ":00:00"
  238. cout, err := getCreatePeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  239. if err != nil {
  240. log.Error("Can not query getCreatePeriodCount.", err)
  241. return nil, nil
  242. }
  243. dateHourMap[strconv.Itoa(key)] = cout
  244. }
  245. return dateHourMap, nil
  246. }
  247. func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  248. //0 to 23 for each hour,
  249. dateHourMap := make(map[string]interface{})
  250. var slice = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
  251. for key, value := range slice {
  252. hourBeginHour := strconv.Itoa(value) + ":00:00"
  253. hourEndHour := strconv.Itoa(value+1) + ":00:00"
  254. cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  255. if err != nil {
  256. log.Error("Can not query getRunPeriodCount.", err)
  257. return nil, nil
  258. }
  259. dateHourMap[strconv.Itoa(key)] = cout
  260. }
  261. return dateHourMap, nil
  262. }