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