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
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. "code.gitea.io/gitea/modules/util"
  9. "xorm.io/builder"
  10. )
  11. type TaskDetail struct {
  12. ID int64 `json:"ID"`
  13. JobID string `json:"JobID"`
  14. JobName string `json:"JobName"`
  15. DisplayJobName string `json:"DisplayJobName"`
  16. Status string `json:"Status"`
  17. JobType string `json:"JobType"`
  18. CreatedUnix timeutil.TimeStamp `json:"CreatedUnix"`
  19. WaitTime string `json:"WaitTime"`
  20. RunTime string `json:"RunTime"`
  21. StartTime timeutil.TimeStamp `json:"StartTime"`
  22. EndTime timeutil.TimeStamp `json:"EndTime"`
  23. ComputeResource string `json:"ComputeResource"`
  24. Type int `json:"Type"`
  25. UserName string `json:"UserName"`
  26. RepoName string `json:"RepoName"`
  27. RepoAlias string `json:"RepoAlias"`
  28. RepoID int64 `json:"RepoID"`
  29. IsDelete bool `json:"IsDelete"`
  30. CardNum int `json:"CardNum"`
  31. CardType string `json:"CardType"`
  32. CardDuration string `json:"CardDuration"`
  33. AiCenter string `json:"AiCenter"`
  34. FlavorName string `json:"FlavorName"`
  35. WorkServerNum int64 `json:"WorkServerNum"`
  36. Spec *Specification `json:"Spec"`
  37. }
  38. type CloudbrainDurationStatistic struct {
  39. ID int64 `xorm:"pk autoincr"`
  40. Cluster string
  41. AiCenterCode string
  42. AiCenterName string
  43. ComputeResource string
  44. AccCardType string
  45. TotalCanUse bool
  46. DateTime string
  47. DayTime string
  48. HourTime int
  49. CardsTotalDuration int
  50. CardsTotalNum int
  51. DeletedUnix timeutil.TimeStamp `xorm:"deleted"`
  52. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  53. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  54. }
  55. type DurationStatisticOptions struct {
  56. BeginTime time.Time
  57. EndTime time.Time
  58. AiCenterCode string
  59. }
  60. type DurationRateStatistic struct {
  61. AiCenterTotalDurationStat map[string]int `json:"aiCenterTotalDurationStat"`
  62. AiCenterUsageDurationStat map[string]int `json:"aiCenterUsageDurationStat"`
  63. TotalUsageRate float64 `json:"totalUsageRate"`
  64. }
  65. type DateCloudbrainStatistic struct {
  66. Date string `json:"date"`
  67. AiCenterUsageDuration map[string]int `json:"aiCenterUsageDuration"`
  68. AiCenterTotalDuration map[string]int `json:"aiCenterTotalDuration"`
  69. AiCenterUsageRate map[string]float64 `json:"aiCenterUsageRate"`
  70. }
  71. type HourTimeStatistic struct {
  72. HourTimeUsageDuration map[int]int `json:"hourTimeUsageDuration"`
  73. HourTimeTotalDuration map[int]int `json:"hourTimeTotalDuration"`
  74. HourTimeUsageRate map[int]float64 `json:"hourTimeUsageRate"`
  75. }
  76. func GetTodayCreatorCount(beginTime time.Time, endTime time.Time) (int64, error) {
  77. countSql := "SELECT count(distinct user_id) FROM " +
  78. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  79. " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
  80. return x.SQL(countSql).Count()
  81. }
  82. func GetTodayCloudbrainCount(beginTime time.Time, endTime time.Time) (int64, error) {
  83. countSql := "SELECT count(*) FROM " +
  84. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  85. " and created_unix<=" + strconv.FormatInt(endTime.Unix(), 10)
  86. return x.SQL(countSql).Count()
  87. }
  88. func GetTodayRunningCount(beginTime time.Time, endTime time.Time) (int64, error) {
  89. countSql := "SELECT count(*) FROM " +
  90. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  91. " and created_unix<=" + strconv.FormatInt(endTime.Unix(), 10) + " and (status='" + string(JobRunning) + "'" +
  92. " or status='" + string(ModelArtsTrainJobInit) + "')"
  93. return x.SQL(countSql).Count()
  94. }
  95. func GetTodayWaitingCount(beginTime time.Time, endTime time.Time) (int64, error) {
  96. countSql := "SELECT count(*) FROM " +
  97. "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
  98. " and created_unix<=" + strconv.FormatInt(endTime.Unix(), 10) + " and status='" + string(JobWaiting) + "'"
  99. return x.SQL(countSql).Count()
  100. }
  101. func GetCreatorCount() (int64, error) {
  102. countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
  103. return x.SQL(countSql).Count()
  104. }
  105. func GetCloudbrainTypeCount() ([]map[string]string, error) {
  106. countSql := "SELECT type,count(*) num FROM public.cloudbrain group by type order by num desc"
  107. return x.QueryString(countSql)
  108. }
  109. func GetCloudbrainStatusCount() ([]map[string]string, error) {
  110. countSql := "SELECT status,count(*) num FROM public.cloudbrain group by status order by num desc"
  111. return x.QueryString(countSql)
  112. }
  113. func GetCloudbrainTpyeDurationSum() ([]map[string]string, error) {
  114. countSql := "SELECT type,sum(duration) FROM public.cloudbrain group by type order by sum(duration) desc"
  115. return x.QueryString(countSql)
  116. }
  117. func GetRecordBeginTime() ([]*CloudbrainInfo, error) {
  118. sess := x.NewSession()
  119. defer sess.Close()
  120. sess.OrderBy("cloudbrain.id ASC limit 1")
  121. cloudbrains := make([]*CloudbrainInfo, 0)
  122. if err := sess.Table(&Cloudbrain{}).Unscoped().
  123. Find(&cloudbrains); err != nil {
  124. log.Info("find error.")
  125. }
  126. return cloudbrains, nil
  127. }
  128. func GetAllStatusCloudBrain() map[string]int {
  129. sess := x.NewSession()
  130. defer sess.Close()
  131. cloudbrains := make([]*CloudbrainInfo, 0)
  132. if err := sess.Table(&Cloudbrain{}).Unscoped().
  133. Find(&cloudbrains); err != nil {
  134. log.Info("find error.")
  135. }
  136. cloudBrainStatusResult := make(map[string]int)
  137. for _, cloudbrain := range cloudbrains {
  138. if _, ok := cloudBrainStatusResult[cloudbrain.Status]; !ok {
  139. cloudBrainStatusResult[cloudbrain.Status] = 1
  140. } else {
  141. cloudBrainStatusResult[cloudbrain.Status] += 1
  142. }
  143. }
  144. return cloudBrainStatusResult
  145. }
  146. func GetWaittingTop() ([]*CloudbrainInfo, error) {
  147. sess := x.NewSession()
  148. defer sess.Close()
  149. var cond = builder.NewCond()
  150. cond = cond.And(
  151. builder.Eq{"cloudbrain.status": string(JobWaiting)},
  152. )
  153. sess.OrderBy("cloudbrain.created_unix ASC limit 10")
  154. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  155. if err := sess.Table(&Cloudbrain{}).Where(cond).
  156. Find(&cloudbrains); err != nil {
  157. log.Info("find error.")
  158. }
  159. return cloudbrains, nil
  160. }
  161. func GetRunningTop() ([]*CloudbrainInfo, error) {
  162. sess := x.NewSession()
  163. defer sess.Close()
  164. var cond = builder.NewCond()
  165. cond = cond.And(
  166. builder.Eq{"cloudbrain.status": string(JobRunning)},
  167. )
  168. sess.OrderBy("cloudbrain.duration DESC limit 10")
  169. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  170. if err := sess.Table(&Cloudbrain{}).Where(cond).
  171. Find(&cloudbrains); err != nil {
  172. log.Info("find error.")
  173. }
  174. return cloudbrains, nil
  175. }
  176. func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  177. countSql := "SELECT count(*) FROM " +
  178. "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime +
  179. "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime +
  180. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') >= '" + hourBeginTime +
  181. "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') < '" + hourEndTime + "'"
  182. return x.SQL(countSql).Count()
  183. }
  184. //SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime))
  185. func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
  186. countSql := "SELECT count(*) FROM " +
  187. "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime +
  188. "') or (to_char(to_timestamp(end_time), 'HH24:MI:SS') < '" + hourBeginTime + "'))" +
  189. " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime +
  190. "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')"
  191. return x.SQL(countSql).Count()
  192. }
  193. func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  194. //0 to 23 for each hour,
  195. dateHourMap := make(map[string]interface{})
  196. var slice = []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
  197. for key, value := range slice {
  198. hourBeginHour := util.AddZero(value) + ":00:00"
  199. hourEndHour := util.AddZero(value+1) + ":00:00"
  200. cout, err := getCreatePeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  201. if err != nil {
  202. log.Error("Can not query getCreatePeriodCount.", err)
  203. return nil, nil
  204. }
  205. dateHourMap[strconv.Itoa(key)] = cout
  206. }
  207. return dateHourMap, nil
  208. }
  209. func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
  210. dateHourMap := make(map[string]interface{})
  211. var slice = []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
  212. for key, value := range slice {
  213. hourBeginHour := util.AddZero(value) + ":00:00"
  214. hourEndHour := util.AddZero(value+1) + ":00:00"
  215. cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
  216. if err != nil {
  217. log.Error("Can not query getRunPeriodCount.", err)
  218. return nil, nil
  219. }
  220. dateHourMap[strconv.Itoa(key)] = cout
  221. }
  222. return dateHourMap, nil
  223. }
  224. func GetCloudbrainRunning() ([]*CloudbrainInfo, error) {
  225. sess := x.NewSession()
  226. defer sess.Close()
  227. var cond = builder.NewCond()
  228. cond = cond.And(
  229. builder.Eq{"cloudbrain.status": string(JobRunning)},
  230. )
  231. sess.OrderBy("cloudbrain.created_unix ASC")
  232. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  233. if err := sess.Table(&Cloudbrain{}).Where(cond).
  234. Find(&cloudbrains); err != nil {
  235. log.Info("find error.")
  236. }
  237. return cloudbrains, nil
  238. }
  239. func GetCloudbrainCompleteByTime(beginTime int64, endTime int64) ([]*CloudbrainInfo, error) {
  240. sess := x.NewSession()
  241. defer sess.Close()
  242. var cond = builder.NewCond()
  243. cond = cond.And(
  244. builder.And(builder.Gte{"cloudbrain.end_time": beginTime}, builder.Lte{"cloudbrain.end_time": endTime}),
  245. )
  246. sess.OrderBy("cloudbrain.created_unix ASC")
  247. cloudbrains := make([]*CloudbrainInfo, 0, 10)
  248. if err := sess.Table(&Cloudbrain{}).Unscoped().Where(cond).
  249. Find(&cloudbrains); err != nil {
  250. log.Info("find error.")
  251. }
  252. return cloudbrains, nil
  253. }
  254. func GetSpecByAiCenterCodeAndType(aiCenterCode string, accCardType string) ([]*CloudbrainSpec, error) {
  255. sess := x.NewSession()
  256. defer sess.Close()
  257. var cond = builder.NewCond()
  258. cond = cond.And(
  259. builder.And(builder.Eq{"cloudbrain_spec.ai_center_code": aiCenterCode}, builder.Eq{"cloudbrain_spec.acc_card_type": accCardType}),
  260. )
  261. cloudbrainSpecs := make([]*CloudbrainSpec, 0, 10)
  262. if err := sess.Table(&CloudbrainSpec{}).Where(cond).
  263. Find(&cloudbrainSpecs); err != nil {
  264. log.Info("find error.")
  265. }
  266. return cloudbrainSpecs, nil
  267. }
  268. func InsertCloudbrainDurationStatistic(cloudbrainDurationStatistic *CloudbrainDurationStatistic) (int64, error) {
  269. return xStatistic.Insert(cloudbrainDurationStatistic)
  270. }
  271. func DeleteCloudbrainDurationStatisticHour(date string, hour int, aiCenterCode string, accCardType string, totalCanUse bool) error {
  272. sess := xStatistic.NewSession()
  273. defer sess.Close()
  274. if err := sess.Begin(); err != nil {
  275. return fmt.Errorf("Begin: %v", err)
  276. }
  277. if _, err := sess.Where("day_time = ? AND hour_time = ? AND ai_center_code = ? AND acc_card_type = ? And total_can_use = ?", date, hour, aiCenterCode, accCardType, totalCanUse).Delete(&CloudbrainDurationStatistic{}); err != nil {
  278. return fmt.Errorf("Delete: %v", err)
  279. }
  280. if err := sess.Commit(); err != nil {
  281. sess.Close()
  282. return fmt.Errorf("Commit: %v", err)
  283. }
  284. sess.Close()
  285. return nil
  286. }
  287. func GetCanUseCardInfo() ([]*ResourceQueue, error) {
  288. sess := x.NewSession()
  289. defer sess.Close()
  290. var cond = builder.NewCond()
  291. cond = cond.And(
  292. builder.And(builder.Eq{"resource_queue.is_automatic_sync": false}),
  293. )
  294. ResourceQueues := make([]*ResourceQueue, 0, 10)
  295. if err := sess.Table(&ResourceQueue{}).Where(cond).
  296. Find(&ResourceQueues); err != nil {
  297. log.Info("find error.")
  298. }
  299. return ResourceQueues, nil
  300. }
  301. func GetCardDurationStatistics(opts *DurationStatisticOptions) ([]*CloudbrainDurationStatistic, error) {
  302. sess := xStatistic.NewSession()
  303. defer sess.Close()
  304. var cond = builder.NewCond()
  305. if opts.BeginTime.Unix() > 0 && opts.EndTime.Unix() > 0 {
  306. cond = cond.And(
  307. builder.And(builder.Gte{"cloudbrain_duration_statistic.created_unix": opts.BeginTime.Unix()}, builder.Lte{"cloudbrain_duration_statistic.created_unix": opts.EndTime.Unix()}),
  308. )
  309. }
  310. if opts.AiCenterCode != "" {
  311. cond = cond.And(
  312. builder.Eq{"cloudbrain_duration_statistic.ai_center_code": opts.AiCenterCode},
  313. )
  314. }
  315. CloudbrainDurationStatistics := make([]*CloudbrainDurationStatistic, 0, 10)
  316. if err := sess.Table(&CloudbrainDurationStatistic{}).Where(cond).
  317. Find(&CloudbrainDurationStatistics); err != nil {
  318. log.Info("find error.")
  319. }
  320. return CloudbrainDurationStatistics, nil
  321. }
  322. func GetDurationRecordBeginTime() ([]*CloudbrainDurationStatistic, error) {
  323. sess := xStatistic.NewSession()
  324. defer sess.Close()
  325. sess.OrderBy("cloudbrain_duration_statistic.id ASC limit 1")
  326. CloudbrainDurationStatistics := make([]*CloudbrainDurationStatistic, 0)
  327. if err := sess.Table(&CloudbrainDurationStatistic{}).Find(&CloudbrainDurationStatistics); err != nil {
  328. log.Info("find error.")
  329. }
  330. return CloudbrainDurationStatistics, nil
  331. }