|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- package models
-
- import (
- "fmt"
- "strconv"
- "time"
-
- "code.gitea.io/gitea/modules/log"
- "code.gitea.io/gitea/modules/timeutil"
- )
-
- // Cloudbrain statistic info of all CloudbrainTasks
- type CloudbrainStatistic struct {
- ID int64 `xorm:"pk autoincr" json:"-"`
- Date string `xorm:"NOT NULL DEFAULT 0" json:"date"`
- WhichHour int64 `xorm:"NOT NULL DEFAULT -1" json:"whichHour"`
- NumDubugOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numDubugOne"`
- NumBenchmarkOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numBenchmarkOne"`
- NumTrainOne int64 `xorm:"NOT NULL DEFAULT 0" json:"numTrainOne"`
- NumDubugTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"numDubugTwo"`
- NumTrainTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"numTrainTwo"`
- NumInferenceTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"numInferenceTwo"`
- NumCloudOneAll int64 `xorm:"NOT NULL DEFAULT 0" json:"numCloudOneAll"`
- NumCloudTwoAll int64 `xorm:"NOT NULL DEFAULT 0" json:"numCloudTwoAll"`
-
- DurationDubugOne int64 `xorm:"NOT NULL DEFAULT 0" json:"durationDubugOne"`
- DurationBenchmarkOne int64 `xorm:"NOT NULL DEFAULT 0" json:"durationBenchmarkOne"`
- DurationTrainOne int64 `xorm:"NOT NULL DEFAULT 0" json:"durationTrainOne"`
- DurationDebugTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"durationDebugTwo"`
- DurationTrainTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"durationTrainTwo"`
- DurationInferenceTwo int64 `xorm:"NOT NULL DEFAULT 0" json:"durationInferenceTwo"`
- DurationCloudOneAll int64 `xorm:"NOT NULL DEFAULT 0" json:"durationCloudOneAll"`
- DurationCloudTwoAll int64 `xorm:"NOT NULL DEFAULT 0" json:"durationCloudTwoAll"`
-
- CreatedUnix timeutil.TimeStamp `xorm:"INDEX created" json:"-"`
- UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated" json:"-"`
- }
-
- func GetJobWaitingPeriodCount(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 status ='" + string(JobWaiting) + "'"
-
- return x.SQL(countSql).Count()
- }
- func GetJobRunningPeriodCount(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 status ='" + string(JobRunning) + "'"
-
- return x.SQL(countSql).Count()
- }
- func GetJobStoppedPeriodCount(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 status ='" + string(JobStopped) + "'"
-
- return x.SQL(countSql).Count()
- }
- func GetJobSucceededPeriodCount(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 status ='" + string(JobSucceeded) + "'"
-
- return x.SQL(countSql).Count()
- }
- func GetJobFailedPeriodCount(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 status ='" + string(JobFailed) + "'"
-
- return x.SQL(countSql).Count()
- }
-
- 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 GetCloudBrainOneCount() (int64, error) {
- countSql := "SELECT count(*) FROM " +
- "public.cloudbrain where type='" + strconv.Itoa(TypeCloudBrainOne) + "'"
-
- return x.SQL(countSql).Count()
- }
- func GetCloudBrainOneDuration() (int64, error) {
- total, err := x.Where("type = ? ", TypeCloudBrainOne).SumInt(&Cloudbrain{}, "duration")
- if err != nil {
- return 0, err
- }
-
- return total, nil
- }
- func GetCloudBrainTwoCount() (int64, error) {
- countSql := "SELECT count(*) FROM " +
- "public.cloudbrain where type='" + strconv.Itoa(TypeCloudBrainTwo) + "'"
-
- return x.SQL(countSql).Count()
- }
- func GetCloudBrainTwoDuration() (int64, error) {
- total, err := x.Where("type = ? ", TypeCloudBrainTwo).SumInt(&Cloudbrain{}, "duration")
- if err != nil {
- return 0, err
- }
-
- return total, nil
- }
-
- func GetCreatorPeriodCount(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 DeleteCloudbrainStatisticDaily(date string) error {
- sess := xStatistic.NewSession()
- defer sess.Close()
- if err := sess.Begin(); err != nil {
- return fmt.Errorf("Begin: %v", err)
- }
-
- if _, err := sess.Where("date = ?", date).Delete(&CloudbrainStatistic{}); err != nil {
- return fmt.Errorf("Delete: %v", err)
- }
-
- if err := sess.Commit(); err != nil {
- sess.Close()
- return fmt.Errorf("Commit: %v", err)
- }
-
- sess.Close()
- return nil
- }
-
- func GetHourStatTime(timeStr string, whichHour int64) (time.Time, time.Time) {
- t, _ := time.Parse("2006-01-02", timeStr)
- timeNumber := t.Unix()
- beginTimeNumber := timeNumber - 8*60*60 + whichHour*60*60
- endTimeNumber := beginTimeNumber + 1*60*60
- beginTime := time.Unix(beginTimeNumber, 0)
- endTime := time.Unix(endTimeNumber, 0)
- log.Info("%s, %s", beginTime, endTime)
-
- return beginTime, endTime
- }
- func GetDayStatTime(timeStr string) (time.Time, time.Time) {
- t, _ := time.Parse("2006-01-02", timeStr)
- timeNumber := t.Unix()
- beginTimeNumber := timeNumber - 8*60*60
- endTimeNumber := beginTimeNumber + 16*60*60
- beginTime := time.Unix(beginTimeNumber, 0)
- endTime := time.Unix(endTimeNumber, 0)
- log.Info("%s, %s", beginTime, endTime)
-
- return beginTime, endTime
- }
-
- func CreateCloudbrainStatistic(cloudbrainStat *CloudbrainStatistic) (err error) {
- if _, err = xStatistic.Insert(cloudbrainStat); err != nil {
- return err
- }
- return nil
- }
-
- func GetJobWaitingCount() (int64, error) {
- countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobWaiting) + "'"
- return x.SQL(countSql).Count()
- }
- func GetJobStoppedCount() (int64, error) {
- countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobStopped) + "'"
- return x.SQL(countSql).Count()
- }
- func GetJobSucceededCount() (int64, error) {
- countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobSucceeded) + "'"
- return x.SQL(countSql).Count()
- }
- func GetJobFailedCount() (int64, error) {
- countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobFailed) + "'"
- return x.SQL(countSql).Count()
- }
- func GetJobRunningCount() (int64, error) {
- countSql := "SELECT count(*) FROM " + "public.cloudbrain where status ='" + string(JobRunning) + "'"
- return x.SQL(countSql).Count()
- }
-
- 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
- }
|