|
- package models
-
- import (
- "strconv"
- "time"
-
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/timeutil"
- "code.gitea.io/gitea/modules/util"
- "xorm.io/builder"
- )
-
- type TaskDetail struct {
- ID int64 `json:"ID"`
- JobID string `json:"JobID"`
- JobName string `json:"JobName"`
- DisplayJobName string `json:"DisplayJobName"`
- Status string `json:"Status"`
- JobType string `json:"JobType"`
- CreatedUnix timeutil.TimeStamp `json:"CreatedUnix"`
- WaitTime string `json:"WaitTime"`
- RunTime string `json:"RunTime"`
- StartTime timeutil.TimeStamp `json:"StartTime"`
- EndTime timeutil.TimeStamp `json:"EndTime"`
- ComputeResource string `json:"ComputeResource"`
- Type int `json:"Type"`
- UserName string `json:"UserName"`
- RepoName string `json:"RepoName"`
- RepoAlias string `json:"RepoAlias"`
- RepoID int64 `json:"RepoID"`
- IsDelete bool `json:"IsDelete"`
- CardNum int `json:"CardNum"`
- CardType string `json:"CardType"`
- CardDuration string `json:"CardDuration"`
- AiCenter string `json:"AiCenter"`
- FlavorName string `json:"FlavorName"`
- }
-
- func GetTodayCreatorCount(beginTime time.Time, endTime time.Time) (int64, error) {
- countSql := "SELECT count(distinct user_id) FROM " +
- "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
- " and created_unix<" + strconv.FormatInt(endTime.Unix(), 10)
- return x.SQL(countSql).Count()
- }
- func GetTodayCloudbrainCount(beginTime time.Time, endTime time.Time) (int64, error) {
- countSql := "SELECT count FROM " +
- "public.cloudbrain where created_unix >=" + strconv.FormatInt(beginTime.Unix(), 10) +
- " and created_unix<=" + strconv.FormatInt(endTime.Unix(), 10)
- return x.SQL(countSql).Count()
- }
- func GetCreatorCount() (int64, error) {
- countSql := "SELECT count(distinct user_id) FROM public.cloudbrain"
- return x.SQL(countSql).Count()
- }
-
- func GetRecordBeginTime() ([]*CloudbrainInfo, error) {
- sess := x.NewSession()
- defer sess.Close()
- sess.OrderBy("cloudbrain.id ASC limit 1")
- cloudbrains := make([]*CloudbrainInfo, 0)
- if err := sess.Table(&Cloudbrain{}).Unscoped().
- Find(&cloudbrains); err != nil {
- log.Info("find error.")
- }
- return cloudbrains, nil
- }
-
- func GetAllStatusCloudBrain() map[string]int {
- sess := x.NewSession()
- defer sess.Close()
- cloudbrains := make([]*CloudbrainInfo, 0)
- if err := sess.Table(&Cloudbrain{}).Unscoped().
- Find(&cloudbrains); err != nil {
- log.Info("find error.")
- }
- cloudBrainStatusResult := make(map[string]int)
- for _, cloudbrain := range cloudbrains {
- if _, ok := cloudBrainStatusResult[cloudbrain.Status]; !ok {
- cloudBrainStatusResult[cloudbrain.Status] = 1
- } else {
- cloudBrainStatusResult[cloudbrain.Status] += 1
- }
- }
- return cloudBrainStatusResult
- }
-
- func GetWaittingTop() ([]*CloudbrainInfo, error) {
- sess := x.NewSession()
- defer sess.Close()
- var cond = builder.NewCond()
- cond = cond.And(
- builder.Eq{"cloudbrain.status": string(JobWaiting)},
- )
- sess.OrderBy("cloudbrain.created_unix ASC limit 10")
- cloudbrains := make([]*CloudbrainInfo, 0, 10)
- if err := sess.Table(&Cloudbrain{}).Where(cond).
- Find(&cloudbrains); err != nil {
- log.Info("find error.")
- }
- return cloudbrains, nil
- }
-
- func GetRunningTop() ([]*CloudbrainInfo, error) {
- sess := x.NewSession()
- defer sess.Close()
- var cond = builder.NewCond()
- cond = cond.And(
- builder.Eq{"cloudbrain.status": string(JobRunning)},
- )
- sess.OrderBy("cloudbrain.duration DESC limit 10")
- cloudbrains := make([]*CloudbrainInfo, 0, 10)
- if err := sess.Table(&Cloudbrain{}).Where(cond).
- Find(&cloudbrains); err != nil {
- log.Info("find error.")
- }
- return cloudbrains, nil
- }
-
- func getCreatePeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
- countSql := "SELECT count(*) FROM " +
- "public.cloudbrain where to_char(to_timestamp(created_unix), 'YYYY-MM-DD') >= '" + dateBeginTime +
- "' and to_char(to_timestamp(created_unix), 'YYYY-MM-DD') < '" + dateEndTime +
- "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') >= '" + hourBeginTime +
- "' and to_char(to_timestamp(created_unix), 'HH24:MI:SS') < '" + hourEndTime + "'"
- return x.SQL(countSql).Count()
- }
-
- //SELECT * FROM xxx WHERE NOT ((endTime < hourBeginTime) OR (startTime > hourEndTime))
- func getRunPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) {
- countSql := "SELECT count(*) FROM " +
- "public.cloudbrain where not ((to_char(to_timestamp(start_time), ' HH24:MI:SS') > '" + hourEndTime +
- "') or (to_char(to_timestamp(end_time), 'HH24:MI:SS') < '" + hourBeginTime + "'))" +
- " and (to_char(to_timestamp(start_time), 'YYYY-MM-DD') >= '" + dateBeginTime +
- "' and to_char(to_timestamp(start_time), 'YYYY-MM-DD') < '" + dateEndTime + "')"
- return x.SQL(countSql).Count()
-
- }
-
- func GetCreateHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
- //0 to 23 for each hour,
- dateHourMap := make(map[string]interface{})
- 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}
- for key, value := range slice {
- hourBeginHour := util.AddZero(value) + ":00:00"
- hourEndHour := util.AddZero(value+1) + ":00:00"
- cout, err := getCreatePeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
- if err != nil {
- log.Error("Can not query getCreatePeriodCount.", err)
- return nil, nil
- }
- dateHourMap[strconv.Itoa(key)] = cout
- }
- return dateHourMap, nil
- }
-
- func GetRunHourPeriodCount(dateBeginTime string, dateEndTime string) (map[string]interface{}, error) {
- dateHourMap := make(map[string]interface{})
- 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}
- for key, value := range slice {
- hourBeginHour := util.AddZero(value) + ":00:00"
- hourEndHour := util.AddZero(value+1) + ":00:00"
- cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour)
- if err != nil {
- log.Error("Can not query getRunPeriodCount.", err)
- return nil, nil
- }
- dateHourMap[strconv.Itoa(key)] = cout
- }
- return dateHourMap, nil
- }
|