diff --git a/models/cloudbrain_static.go b/models/cloudbrain_static.go index 5b4883acb..3cf8dfb80 100644 --- a/models/cloudbrain_static.go +++ b/models/cloudbrain_static.go @@ -1,41 +1,12 @@ 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) + @@ -244,56 +215,6 @@ func GetCreatorCount() (int64, error) { 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() @@ -315,7 +236,7 @@ func GetJobRunningCount() (int64, error) { return x.SQL(countSql).Count() } -func getDateAndHourPeriodCount(dateBeginTime string, dateEndTime string, hourBeginTime string, hourEndTime string) (int64, error) { +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 + @@ -324,19 +245,47 @@ func getDateAndHourPeriodCount(dateBeginTime string, dateEndTime string, hourBeg return x.SQL(countSql).Count() } -func GetHourPeriodCount(dateBeginTime string, dateEndTime string) (map[int]int64, error) { +//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[int]int64) + 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 := getDateAndHourPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour) + cout, err := getRunPeriodCount(dateBeginTime, dateEndTime, hourBeginHour, hourEndHour) if err != nil { - log.Error("Can not query getDateAndHourPeriodCount.", err) + log.Error("Can not query getRunPeriodCount.", err) return nil, nil } - dateHourMap[key] = cout + dateHourMap[strconv.Itoa(key)] = cout } return dateHourMap, nil } diff --git a/models/models.go b/models/models.go index cf81e7eff..2a2e119fb 100755 --- a/models/models.go +++ b/models/models.go @@ -160,7 +160,6 @@ func init() { new(UserLoginLog), new(UserMetrics), new(UserAnalysisPara), - new(CloudbrainStatistic), ) gonicNames := []string{"SSL", "UID"} diff --git a/modules/cron/tasks_basic.go b/modules/cron/tasks_basic.go index 5b7013d3d..b3a6c02a1 100755 --- a/modules/cron/tasks_basic.go +++ b/modules/cron/tasks_basic.go @@ -173,16 +173,6 @@ func registerHandleRepoAndUserStatistic() { return nil }) } -func registerHandleCloudbrainStatistic() { - RegisterTaskFatal("handle_cloudbrain_statistic", &BaseConfig{ - Enabled: true, - RunAtStart: false, - Schedule: "@daily", - }, func(ctx context.Context, _ *models.User, _ Config) error { - repo.CloudbrainStatisticAuto() - return nil - }) -} func registerHandleSummaryStatistic() { RegisterTaskFatal("handle_summary_statistic", &BaseConfig{ @@ -233,7 +223,6 @@ func initBasicTasks() { registerHandleBlockChainUnSuccessCommits() registerHandleRepoAndUserStatistic() - registerHandleCloudbrainStatistic() registerHandleSummaryStatistic() registerSyncCloudbrainStatus() diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 999a4a05e..a4df6143e 100755 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -566,7 +566,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("/trend", repo.GetAllCloudbrainsTrend) m.Get("/status_analysis", repo.GetCloudbrainsStatusAnalysis) m.Get("/detail_data", repo.GetCloudbrainsDetailData) - m.Get("/hours_data", repo.GetCloudbrainsHoursData) + m.Get("/create_hours_data", repo.GetCloudbrainsCreateHoursData) + m.Get("/run_hours_data", repo.GetCloudbrainsRunHoursData) }) }, operationReq) diff --git a/routers/api/v1/repo/cloudbrain_dashboard.go b/routers/api/v1/repo/cloudbrain_dashboard.go index ba0458535..3a80abc20 100644 --- a/routers/api/v1/repo/cloudbrain_dashboard.go +++ b/routers/api/v1/repo/cloudbrain_dashboard.go @@ -490,8 +490,8 @@ func GetCloudbrainsDetailData(ctx *context.Context) { }) } -func GetCloudbrainsHoursData(ctx *context.Context) { - hourPeriodCount := make(map[int]int64) +func GetCloudbrainsCreateHoursData(ctx *context.Context) { + createHourPeriodCount := make(map[string]interface{}) brainRecordBeginTime, err := getBrainRecordBeginTime() if err != nil { log.Error("Can not get brain record begin time", err) @@ -499,27 +499,138 @@ func GetCloudbrainsHoursData(ctx *context.Context) { return } queryType := ctx.QueryTrim("type") + beginTimeStr := ctx.QueryTrim("beginTime") + endTimeStr := ctx.QueryTrim("endTime") now := time.Now() + var beginTime time.Time + var endTime time.Time if queryType != "" { if queryType == "all" { - beginTime := brainRecordBeginTime - endTime := now - dateBeginTime := beginTime.Format("2006-01-02") - dateEndTime := endTime.Format("2006-01-02") - hourPeriodCount, err = models.GetHourPeriodCount(dateBeginTime, dateEndTime) + beginTime = brainRecordBeginTime + endTime = now + } else if queryType == "yesterday" { + beginTime = now.AddDate(0, 0, -1) + endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + } else if queryType == "current_week" { + beginTime = now.AddDate(0, 0, -int(time.Now().Weekday())+1) //begin from monday + endTime = now + } else if queryType == "current_month" { + endTime = now + beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location()) + } else if queryType == "monthly" { + endTime = now + beginTime = now.AddDate(0, -1, 0) + beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location()) + } else if queryType == "current_year" { + endTime = now + beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location()) + } else if queryType == "last_month" { + lastMonthTime := now.AddDate(0, -1, 0) + beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location()) + endTime = time.Date(now.Year(), now.Month(), 2, 0, 0, 0, 0, now.Location()) + } + } else { + if beginTimeStr == "" || endTimeStr == "" { + //如果查询类型和开始时间结束时间都未设置,按queryType=all处理 + beginTime = brainRecordBeginTime + endTime = now + } else { + beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local) if err != nil { - log.Error("Can not query hourPeriodCount.", err) - ctx.Error(http.StatusBadRequest, ctx.Tr("hourPeriodCount_get_error")) + log.Error("Can not ParseInLocation.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error")) return } - } else { - return + endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local) + if err != nil { + log.Error("Can not ParseInLocation.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error")) + return + } + } + } + dateBeginTime := beginTime.Format("2006-01-02") + dateEndTime := endTime.Format("2006-01-02") + createHourPeriodCount, err = models.GetCreateHourPeriodCount(dateBeginTime, dateEndTime) + if err != nil { + log.Error("Can not query hourPeriodCount.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("hourPeriodCount_get_error")) + return + } + ctx.JSON(http.StatusOK, map[string]interface{}{ + "createHourPeriodCount": createHourPeriodCount, + }) + +} +func GetCloudbrainsRunHoursData(ctx *context.Context) { + runHourPeriodCount := make(map[string]interface{}) + brainRecordBeginTime, err := getBrainRecordBeginTime() + if err != nil { + log.Error("Can not get brain record begin time", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("repo.brain_record_begintime_get_err")) + return + } + queryType := ctx.QueryTrim("type") + beginTimeStr := ctx.QueryTrim("beginTime") + endTimeStr := ctx.QueryTrim("endTime") + now := time.Now() + var beginTime time.Time + var endTime time.Time + if queryType != "" { + if queryType == "all" { + beginTime = brainRecordBeginTime + endTime = now + } else if queryType == "yesterday" { + beginTime = now.AddDate(0, 0, -1) + endTime = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + } else if queryType == "current_week" { + beginTime = now.AddDate(0, 0, -int(time.Now().Weekday())+1) //begin from monday + endTime = now + } else if queryType == "current_month" { + endTime = now + beginTime = time.Date(endTime.Year(), endTime.Month(), 1, 0, 0, 0, 0, now.Location()) + } else if queryType == "monthly" { + endTime = now + beginTime = now.AddDate(0, -1, 0) + beginTime = time.Date(beginTime.Year(), beginTime.Month(), beginTime.Day(), 0, 0, 0, 0, now.Location()) + } else if queryType == "current_year" { + endTime = now + beginTime = time.Date(endTime.Year(), 1, 1, 0, 0, 0, 0, now.Location()) + } else if queryType == "last_month" { + lastMonthTime := now.AddDate(0, -1, 0) + beginTime = time.Date(lastMonthTime.Year(), lastMonthTime.Month(), 1, 0, 0, 0, 0, now.Location()) + endTime = time.Date(now.Year(), now.Month(), 2, 0, 0, 0, 0, now.Location()) } } else { + if beginTimeStr == "" || endTimeStr == "" { + //如果查询类型和开始时间结束时间都未设置,按queryType=all处理 + beginTime = brainRecordBeginTime + endTime = now + } else { + beginTime, err = time.ParseInLocation("2006-01-02", beginTimeStr, time.Local) + if err != nil { + log.Error("Can not ParseInLocation.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error")) + return + } + endTime, err = time.ParseInLocation("2006-01-02", endTimeStr, time.Local) + if err != nil { + log.Error("Can not ParseInLocation.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("ParseInLocation_get_error")) + return + } + } + } + dateBeginTime := beginTime.Format("2006-01-02") + dateEndTime := endTime.Format("2006-01-02") + runHourPeriodCount, err = models.GetRunHourPeriodCount(dateBeginTime, dateEndTime) + if err != nil { + log.Error("Can not query runHourPeriodCount.", err) + ctx.Error(http.StatusBadRequest, ctx.Tr("runHourPeriodCount_get_error")) return } ctx.JSON(http.StatusOK, map[string]interface{}{ - "hourPeriodCount": hourPeriodCount, + "runHourPeriodCount": runHourPeriodCount, }) } diff --git a/routers/repo/cloudbrain_statistic.go b/routers/repo/cloudbrain_statistic.go deleted file mode 100644 index 023ed441b..000000000 --- a/routers/repo/cloudbrain_statistic.go +++ /dev/null @@ -1,121 +0,0 @@ -package repo - -import ( - "time" - - "code.gitea.io/gitea/models" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" - "code.gitea.io/gitea/services/mailer" -) - -func CloudbrainStatisticAuto() { - yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02") - CloudbrainStatisticDaily(yesterday) -} - -func CloudbrainStatisticDaily(date string) { - log.Info("%s", date) - log.Info("begin Cloudbrain Statistic") - warnEmailMessage := "云脑任务统计信息入库失败,请尽快定位。" - if err := models.DeleteCloudbrainStatisticDaily(date); err != nil { - log.Error("DeleteCloudbrainStatisticDaily failed: %v", err.Error()) - mailer.SendWarnNotifyMail(setting.Warn_Notify_Mails, warnEmailMessage) - return - } - - //0 to 23 for each hour, 25 for all 24 hours - var WhichHour [25]int64 = [25]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, 25} - for _, whichHour := range WhichHour { - log.Info("start statistic: %s", date) - var beginTime, endTime time.Time - beginTime, endTime = models.GetHourStatTime(date, whichHour) - if whichHour == 25 { - beginTime, endTime = models.GetDayStatTime(date) - } - numDubugOne, err := models.GetDebugOnePeriodCount(beginTime, endTime) - if err != nil { - log.Error("GetDebugOneCount failed(%s): %v", numDubugOne, err) - } - numBenchmarkOne, err := models.GetBenchmarkOnePeriodCount(beginTime, endTime) - if err != nil { - log.Error("GetBenchmarkOneCount failed(%s): %v", numBenchmarkOne, err) - } - numTrainOne, err := models.GetTrainOnePeriodCount(beginTime, endTime) - if err != nil { - log.Error("GetTrainOneCount failed(%s): %v", numTrainOne, err) - } - numDebugTwo, err := models.GetDebugTwoPeriodCount(beginTime, endTime) - if err != nil { - log.Error("GetDebugTwoCount failed(%s): %v", numDebugTwo, err) - } - numTrainTwo, err := models.GetTrainTwoPeriodCount(beginTime, endTime) - if err != nil { - log.Error("GetTrainTwoCount failed(%s): %v", numTrainTwo, err) - } - numInferenceTwo, err := models.GetInferenceTwoPeriodCount(beginTime, endTime) - if err != nil { - log.Error("GetInferenceTwoCount failed(%s): %v", numInferenceTwo, err) - } - numCloudOneAll := numDubugOne + numBenchmarkOne + numTrainOne - numCloudTwoAll := numDebugTwo + numTrainTwo + numInferenceTwo - - durationDubugOne, err := models.GetDebugOnePeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetDebugOneDuration failed(%s): %v", durationDubugOne, err) - } - durationBenchmarkOne, err := models.GetBenchmarkOnePeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetBenchmarkOneDuration failed(%s): %v", durationBenchmarkOne, err) - } - durationTrainOne, err := models.GetTrainOnePeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetTrainOneDuration failed(%s): %v", durationTrainOne, err) - } - durationDebugTwo, err := models.GetDebugTwoPeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetDebugTwoDuration failed(%s): %v", durationDebugTwo, err) - } - durationTrainTwo, err := models.GetTrainTwoPeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetTrainTwoDuration failed(%s): %v", durationTrainTwo, err) - } - durationInferenceTwo, err := models.GetInferenceTwoPeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetInferenceTwoDuration failed(%s): %v", durationInferenceTwo, err) - } - durationCloudOneAll, err := models.GetCloudBrainOnePeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetCloudBrainOnePeriodDuration failed(%s): %v", durationCloudOneAll, err) - } - durationCloudTwoAll, err := models.GetCloudBrainTwoPeriodDuration(beginTime, endTime) - if err != nil { - log.Error("GetCloudBrainTwoPeriodDuration failed(%s): %v", durationCloudTwoAll, err) - } - - err = models.CreateCloudbrainStatistic(&models.CloudbrainStatistic{ - Date: date, - WhichHour: whichHour, - NumDubugOne: numDubugOne, - NumBenchmarkOne: numBenchmarkOne, - NumTrainOne: numTrainOne, - NumDubugTwo: numDebugTwo, - NumTrainTwo: numTrainTwo, - NumInferenceTwo: numInferenceTwo, - NumCloudOneAll: numCloudOneAll, - NumCloudTwoAll: numCloudTwoAll, - DurationDubugOne: durationDubugOne, - DurationBenchmarkOne: durationBenchmarkOne, - DurationTrainOne: durationTrainOne, - DurationDebugTwo: durationDebugTwo, - DurationTrainTwo: durationTrainTwo, - DurationInferenceTwo: durationInferenceTwo, - DurationCloudOneAll: durationCloudOneAll, - DurationCloudTwoAll: durationCloudTwoAll, - }) - if err != nil { - log.Error("CreateCloudbrainStatistic(%s) failed:%v", date, err.Error()) - return - } - } -} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 701df7147..159aee5d2 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -328,6 +328,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/all/search/", routers.Search) m.Get("/all/search/", routers.EmptySearch) m.Get("/all/dosearch/", routers.SearchApi) + m.Post("/user/login/kanban", user.SignInPostAPI) m.Get("/home/term", routers.HomeTerm) m.Group("/explore", func() { m.Get("", func(ctx *context.Context) { @@ -355,6 +356,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/user", func() { m.Get("/login", user.SignIn) m.Get("/login/cloud_brain", user.SignInCloudBrain) + m.Post("/login", bindIgnErr(auth.SignInForm{}), user.SignInPost) m.Group("", func() { m.Combo("/login/openid"). @@ -1031,7 +1033,6 @@ func RegisterRoutes(m *macaron.Macaron) { }, context.RepoRef()) m.Group("/cloudbrain", func() { - m.Get("/test", repo.CloudbrainStatisticAuto) m.Group("/:id", func() { m.Get("", reqRepoCloudBrainReader, repo.CloudBrainShow) m.Get("/debug", cloudbrain.AdminOrJobCreaterRight, repo.CloudBrainDebug) diff --git a/routers/user/auth.go b/routers/user/auth.go index a02cf24dc..faef017d4 100755 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -176,6 +176,48 @@ func SignInCloudBrain(ctx *context.Context) { ctx.HTML(200, tplSignInCloudBrain) } + + + +func SignInPostAPI(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("sign_in") + UserName := ctx.Query("UserName") + Password := ctx.Query("Password") + log.Info("0000000") + orderedOAuth2Names, oauth2Providers, err := models.GetActiveOAuth2Providers() + if err != nil { + ctx.ServerError("UserSignIn", err) + return + } + ctx.Data["OrderedOAuth2Names"] = orderedOAuth2Names + ctx.Data["OAuth2Providers"] = oauth2Providers + ctx.Data["Title"] = ctx.Tr("sign_in") + ctx.Data["SignInLink"] = setting.AppSubURL + "/user/login" + ctx.Data["PageIsSignIn"] = true + ctx.Data["PageIsLogin"] = true + ctx.Data["IsCourse"] = ctx.QueryBool("course") + ctx.Data["EnableSSPI"] = models.IsSSPIEnabled() + + if ctx.HasError() { + ctx.HTML(200, tplSignIn) + return + } + log.Info("1111111") + u, err := models.UserSignIn(UserName, Password) + log.Info("22222") + if err != nil { + ctx.ServerError("UserSignIn", err) + return + } + log.Info("3333") + models.SaveLoginInfoToDb(ctx.Req.Request, u) + // If this user is enrolled in 2FA, we can't sign the user in just yet. + // Instead, redirect them to the 2FA authentication page. + //handleSignInFull(ctx, u, form.Remember, false) + handleSignInFullNotRedirect(ctx, u, true, false) +} + + // SignInPost response for sign in request func SignInPost(ctx *context.Context, form auth.SignInForm) { ctx.Data["Title"] = ctx.Tr("sign_in") @@ -518,6 +560,69 @@ func handleSignIn(ctx *context.Context, u *models.User, remember bool) { handleSignInFull(ctx, u, remember, true) } +func handleSignInFullNotRedirect(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) string { + + log.Info("enter here.") + if remember { + days := 86400 * setting.LogInRememberDays + ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true) + ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd), + setting.CookieRememberName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true) + } + + _ = ctx.Session.Delete("openid_verified_uri") + _ = ctx.Session.Delete("openid_signin_remember") + _ = ctx.Session.Delete("openid_determined_email") + _ = ctx.Session.Delete("openid_determined_username") + _ = ctx.Session.Delete("twofaUid") + _ = ctx.Session.Delete("twofaRemember") + _ = ctx.Session.Delete("u2fChallenge") + _ = ctx.Session.Delete("linkAccount") + if err := ctx.Session.Set("uid", u.ID); err != nil { + log.Error("Error setting uid %d in session: %v", u.ID, err) + } + if err := ctx.Session.Set("uname", u.Name); err != nil { + log.Error("Error setting uname %s session: %v", u.Name, err) + } + if err := ctx.Session.Release(); err != nil { + log.Error("Unable to store session: %v", err) + } + + // If the user does not have a locale set, we save the current one. + if len(u.Language) == 0 { + if len(ctx.GetCookie("lang")) != 0 { + u.Language = ctx.GetCookie("lang") + } else { + u.Language = ctx.Locale.Language() + } + + if err := models.UpdateUserCols(u, "language"); err != nil { + log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", u.ID, u.Language)) + return setting.AppSubURL + "/dashboard" + } + } else { + // Language setting of the user use the one previously set + if len(ctx.GetCookie("lang")) != 0 { + u.Language = ctx.GetCookie("lang") + } + } + + ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true) + + // Clear whatever CSRF has right now, force to generate a new one + ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true) + + // Register last login + u.SetLastLogin() + if err := models.UpdateUserCols(u, "last_login_unix"); err != nil { + ctx.ServerError("UpdateUserCols", err) + return setting.AppSubURL + "/dashboard" + } + + + return setting.AppSubURL + "/dashboard" +} + func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) string { if remember { days := 86400 * setting.LogInRememberDays