package models import ( "strconv" "time" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" ) type TaskDetail struct { ID int64 `json:"ID"` JobName string `json:"JobName"` DisplayJobName string `json:"DisplayJobName"` Status string `json:"Status"` JobType string `json:"JobType"` CreatedUnix timeutil.TimeStamp `json:"CreatedUnix"` WaitTime timeutil.TimeStamp `json:"WaitTime"` RunTime timeutil.TimeStamp `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"` RepoID int64 `json:"RepoID"` } func GetDebugOnePeriodCount(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) + " and job_type ='" + string(JobTypeDebug) + "'" + " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'" return x.SQL(countSql).Count() } func GetDebugOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetTrainOnePeriodCount(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) + " and job_type ='" + string(JobTypeTrain) + "'" + " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'" return x.SQL(countSql).Count() } func GetTrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetBenchmarkOnePeriodCount(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) + " and job_type ='" + string(JobTypeBenchmark) + "'" + " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'" return x.SQL(countSql).Count() } func GetBenchmarkOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetDebugTwoPeriodCount(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) + " and job_type ='" + string(JobTypeDebug) + "'" + " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'" return x.SQL(countSql).Count() } func GetDebugTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetTrainTwoPeriodCount(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) + " and job_type ='" + string(JobTypeTrain) + "'" + " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'" return x.SQL(countSql).Count() } func GetTrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetInferenceTwoPeriodCount(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) + " and job_type ='" + string(JobTypeInference) + "'" + " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'" return x.SQL(countSql).Count() } func GetInferenceTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetCloudBrainOnePeriodCount(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) + " and type='" + strconv.Itoa(TypeCloudBrainOne) + "'" return x.SQL(countSql).Count() } func GetCloudBrainOnePeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } func GetCloudBrainTwoPeriodCount(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) + " and type='" + strconv.Itoa(TypeCloudBrainTwo) + "'" return x.SQL(countSql).Count() } func GetCloudBrainTwoPeriodDuration(beginTime time.Time, endTime time.Time) (int64, error) { 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") if err != nil { return 0, err } return total, nil } 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 GetCreatorCount() (int64, error) { countSql := "SELECT count(distinct user_id) FROM public.cloudbrain" return x.SQL(countSql).Count() } func GetAllCloudBrain() ([]*CloudbrainInfo, error) { sess := x.NewSession() defer sess.Close() cloudbrains := make([]*CloudbrainInfo, 0) if err := sess.Table(&Cloudbrain{}).Unscoped(). Find(&cloudbrains); err != nil { log.Info("find error.") } return cloudbrains, nil } func GetRecordBeginTime() ([]*CloudbrainInfo, error) { // sess := x.NewSession() // defer sess.Close() // cloudbrains := make([]*CloudbrainInfo, 0) // if err := sess.Table(&Cloudbrain{}). // Find(&cloudbrains); err != nil { // return nil, fmt.Errorf("Find: %v", err) // } // return cloudbrains, nil 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 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 = []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} for key, value := range slice { hourBeginHour := strconv.Itoa(value) + ":00:00" hourEndHour := strconv.Itoa(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) { //0 to 23 for each hour, dateHourMap := make(map[string]interface{}) 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} for key, value := range slice { hourBeginHour := strconv.Itoa(value) + ":00:00" hourEndHour := strconv.Itoa(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 }