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