From 096eb7c73e58baa166cb9b72543f0f17fbe06425 Mon Sep 17 00:00:00 2001 From: lishuang Date: Sat, 11 Jul 2020 14:40:03 +0800 Subject: [PATCH] Add two preference edit api. --- code/rest/dashboard_controller.go | 7 +++ code/rest/dashboard_service.go | 12 ++--- code/rest/footprint_service.go | 13 ++---- code/rest/preference_controller.go | 32 +++++++++++-- code/rest/preference_dao.go | 2 +- code/rest/preference_model.go | 56 +++++++++++++++++++++- code/rest/preference_service.go | 11 +++++ code/rest/task_service.go | 75 ++++++++++++++++++++++++++++++ code/support/tank_context.go | 3 ++ 9 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 code/rest/task_service.go diff --git a/code/rest/dashboard_controller.go b/code/rest/dashboard_controller.go index 7cc8449..4a15f3d 100644 --- a/code/rest/dashboard_controller.go +++ b/code/rest/dashboard_controller.go @@ -34,6 +34,7 @@ func (this *DashboardController) RegisterRoutes() map[string]func(writer http.Re routeMap["/api/dashboard/page"] = this.Wrap(this.Page, USER_ROLE_ADMINISTRATOR) routeMap["/api/dashboard/active/ip/top10"] = this.Wrap(this.ActiveIpTop10, USER_ROLE_ADMINISTRATOR) + routeMap["/api/dashboard/etl"] = this.Wrap(this.Etl, USER_ROLE_ADMINISTRATOR) return routeMap } @@ -89,3 +90,9 @@ func (this *DashboardController) ActiveIpTop10(writer http.ResponseWriter, reque list := this.dashboardDao.ActiveIpTop10() return this.Success(list) } + +func (this *DashboardController) Etl(writer http.ResponseWriter, request *http.Request) *result.WebResult { + + this.dashboardService.Etl() + return this.Success("OK") +} diff --git a/code/rest/dashboard_service.go b/code/rest/dashboard_service.go index 12caafd..04281bc 100644 --- a/code/rest/dashboard_service.go +++ b/code/rest/dashboard_service.go @@ -3,7 +3,6 @@ package rest import ( "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/util" - "github.com/robfig/cron" "time" ) @@ -49,19 +48,14 @@ func (this *DashboardService) Init() { func (this *DashboardService) Bootstrap() { - this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data.") - expression := "0 5 0 * * ?" - cronJob := cron.New() - err := cronJob.AddFunc(expression, this.etl) - core.PanicError(err) - cronJob.Start() + this.logger.Info("Immediately ETL dashboard data.") //do the etl method now. - go core.RunWithRecovery(this.etl) + go core.RunWithRecovery(this.Etl) } // handle the dashboard data. -func (this *DashboardService) etl() { +func (this *DashboardService) Etl() { this.logger.Info("ETL dashboard data.") diff --git a/code/rest/footprint_service.go b/code/rest/footprint_service.go index 414d33d..dc18ac6 100644 --- a/code/rest/footprint_service.go +++ b/code/rest/footprint_service.go @@ -5,7 +5,6 @@ import ( "github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/tool/util" - "github.com/robfig/cron" "net/http" "time" ) @@ -94,18 +93,12 @@ func (this *FootprintService) Trace(request *http.Request, duration time.Duratio func (this *FootprintService) Bootstrap() { - this.logger.Info("[cron job] Every day 00:10 delete Footprint data 8 days ago.") - expression := "0 10 0 * * ?" - cronJob := cron.New() - err := cronJob.AddFunc(expression, this.cleanOldData) - core.PanicError(err) - cronJob.Start() - - go core.RunWithRecovery(this.cleanOldData) + this.logger.Info("Immediately delete Footprint data of 8 days ago.") + go core.RunWithRecovery(this.CleanOldData) } -func (this *FootprintService) cleanOldData() { +func (this *FootprintService) CleanOldData() { day8Ago := time.Now() day8Ago = day8Ago.AddDate(0, 0, -8) diff --git a/code/rest/preference_controller.go b/code/rest/preference_controller.go index 35b7ee3..6ebfc56 100644 --- a/code/rest/preference_controller.go +++ b/code/rest/preference_controller.go @@ -41,6 +41,8 @@ func (this *PreferenceController) RegisterRoutes() map[string]func(writer http.R routeMap["/api/preference/ping"] = this.Wrap(this.Ping, USER_ROLE_GUEST) routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, USER_ROLE_GUEST) routeMap["/api/preference/edit"] = this.Wrap(this.Edit, USER_ROLE_ADMINISTRATOR) + routeMap["/api/preference/edit/preview/config"] = this.Wrap(this.EditPreviewConfig, USER_ROLE_ADMINISTRATOR) + routeMap["/api/preference/edit/scan/config"] = this.Wrap(this.EditScanConfig, USER_ROLE_ADMINISTRATOR) routeMap["/api/preference/system/cleanup"] = this.Wrap(this.SystemCleanup, USER_ROLE_ADMINISTRATOR) return routeMap @@ -60,6 +62,7 @@ func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *htt return this.Success(preference) } +//edit basic info. func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult { name := request.FormValue("name") @@ -72,7 +75,6 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http downloadDirMaxNumStr := request.FormValue("downloadDirMaxNum") defaultTotalSizeLimitStr := request.FormValue("defaultTotalSizeLimit") allowRegisterStr := request.FormValue("allowRegister") - previewConfig := request.FormValue("previewConfig") if name == "" { panic(result.BadRequest("name cannot be null")) @@ -120,12 +122,34 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http preference.DownloadDirMaxNum = downloadDirMaxNum preference.DefaultTotalSizeLimit = defaultTotalSizeLimit preference.AllowRegister = allowRegister + + preference = this.preferenceService.Save(preference) + + return this.Success(preference) +} + +//edit preview config. +func (this *PreferenceController) EditPreviewConfig(writer http.ResponseWriter, request *http.Request) *result.WebResult { + + previewConfig := request.FormValue("previewConfig") + + preference := this.preferenceDao.Fetch() preference.PreviewConfig = previewConfig - preference = this.preferenceDao.Save(preference) + preference = this.preferenceService.Save(preference) - //reset the preference cache - this.preferenceService.Reset() + return this.Success(preference) +} + +func (this *PreferenceController) EditScanConfig(writer http.ResponseWriter, request *http.Request) *result.WebResult { + + scanConfig := request.FormValue("scanConfig") + + preference := this.preferenceDao.Fetch() + + preference.ScanConfig = scanConfig + + preference = this.preferenceService.Save(preference) return this.Success(preference) } diff --git a/code/rest/preference_dao.go b/code/rest/preference_dao.go index ae287fd..23f8660 100644 --- a/code/rest/preference_dao.go +++ b/code/rest/preference_dao.go @@ -22,8 +22,8 @@ func (this *PreferenceDao) Fetch() *Preference { if db.Error.Error() == result.DB_ERROR_NOT_FOUND { preference.Name = "EyeblueTank" preference.Version = core.VERSION - //Default Office preview url. https://view.officeapps.live.com/op/embed.aspx?src= preference.PreviewConfig = "{}" + preference.ScanConfig = "{}" this.Create(preference) return preference } else { diff --git a/code/rest/preference_model.go b/code/rest/preference_model.go index 45b652a..8be1bb7 100644 --- a/code/rest/preference_model.go +++ b/code/rest/preference_model.go @@ -1,6 +1,9 @@ package rest -import "github.com/eyebluecn/tank/code/core" +import ( + "github.com/eyebluecn/tank/code/core" + jsoniter "github.com/json-iterator/go" +) type Preference struct { Base @@ -14,6 +17,7 @@ type Preference struct { DefaultTotalSizeLimit int64 `json:"defaultTotalSizeLimit" gorm:"type:bigint(20) not null;default:-1"` AllowRegister bool `json:"allowRegister" gorm:"type:tinyint(1) not null;default:0"` PreviewConfig string `json:"previewConfig" gorm:"type:text"` + ScanConfig string `json:"scanConfig" gorm:"type:text"` Version string `json:"version" gorm:"-"` } @@ -21,3 +25,53 @@ type Preference struct { func (this *Preference) TableName() string { return core.TABLE_PREFIX + "preference" } + +const ( + //scan scope all. + SCAN_SCOPE_ALL = "ALL" + //scan scope custom. + SCAN_SCOPE_CUSTOM = "CUSTOM" +) + +//scan config struct. +type ScanConfig struct { + //whether enable the scan task. + Enable bool `json:"enable"` + //when to process the task. + Cron string `json:"cron"` + //username + Usernames []string `json:"usernames"` + //scan scope. see SCAN_SCOPE + Scope string `json:"scope"` +} + +//fetch the scan config +func (this *Preference) FetchScanConfig() *ScanConfig { + + json := this.ScanConfig + if json == "" || json == EMPTY_JSON_MAP { + + return &ScanConfig{ + Enable: false, + } + } else { + m := &ScanConfig{} + + err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(json), &m) + if err != nil { + panic(err) + } + return m + } +} + +//set the scan config +func (this *Preference) SetScanConfig(scanConfigJson string) { + + b, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(scanConfigJson) + if err != nil { + panic(err) + } + + this.ScanConfig = string(b) +} diff --git a/code/rest/preference_service.go b/code/rest/preference_service.go index 743e911..0ebe651 100644 --- a/code/rest/preference_service.go +++ b/code/rest/preference_service.go @@ -56,6 +56,17 @@ func (this *PreferenceService) Reset() { } +//清空单例配置。 +func (this *PreferenceService) Save(preference *Preference) *Preference { + + preference = this.preferenceDao.Save(preference) + + //clean cache. + this.Reset() + + return preference +} + //System cleanup. func (this *PreferenceService) Cleanup() { diff --git a/code/rest/task_service.go b/code/rest/task_service.go new file mode 100644 index 0000000..4e00498 --- /dev/null +++ b/code/rest/task_service.go @@ -0,0 +1,75 @@ +package rest + +import ( + "github.com/eyebluecn/tank/code/core" + "github.com/robfig/cron" +) + +// system tasks service +//@Service +type TaskService struct { + BaseBean + footprintService *FootprintService + dashboardService *DashboardService +} + +func (this *TaskService) Init() { + this.BaseBean.Init() + + b := core.CONTEXT.GetBean(this.footprintService) + if b, ok := b.(*FootprintService); ok { + this.footprintService = b + } + + b = core.CONTEXT.GetBean(this.dashboardService) + if b, ok := b.(*DashboardService); ok { + this.dashboardService = b + } + +} + +//init the clean footprint task. +func (this *TaskService) InitCleanFootprintTask() { + + this.logger.Info("[cron job] Every day 00:10 delete Footprint data of 8 days ago.") + expression := "0 10 0 * * ?" + cronJob := cron.New() + err := cronJob.AddFunc(expression, this.footprintService.CleanOldData) + core.PanicError(err) + cronJob.Start() + +} + +//init the elt task. +func (this *TaskService) InitEtlTask() { + + this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data.") + expression := "0 5 0 * * ?" + cronJob := cron.New() + err := cronJob.AddFunc(expression, this.dashboardService.Etl) + core.PanicError(err) + cronJob.Start() + +} + +//init the scan task. +func (this *TaskService) InitScanTask() { + + this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data.") + expression := "0 5 0 * * ?" + cronJob := cron.New() + err := cronJob.AddFunc(expression, this.dashboardService.Etl) + core.PanicError(err) + cronJob.Start() + +} + +func (this *TaskService) Bootstrap() { + + //load the clean footprint task. + this.InitCleanFootprintTask() + + //load the etl task. + this.InitEtlTask() + +} diff --git a/code/support/tank_context.go b/code/support/tank_context.go index 32dc75a..e6a2530 100644 --- a/code/support/tank_context.go +++ b/code/support/tank_context.go @@ -169,6 +169,9 @@ func (this *TankContext) registerBeans() { //uploadToken this.registerBean(new(rest.UploadTokenDao)) + //task + this.registerBean(new(rest.TaskService)) + //user this.registerBean(new(rest.UserController)) this.registerBean(new(rest.UserDao))