Add two preference edit api.

This commit is contained in:
lishuang 2020-07-11 14:40:03 +08:00
parent 69820bfcb9
commit 096eb7c73e
9 changed files with 186 additions and 25 deletions

View File

@ -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/page"] = this.Wrap(this.Page, USER_ROLE_ADMINISTRATOR)
routeMap["/api/dashboard/active/ip/top10"] = this.Wrap(this.ActiveIpTop10, 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 return routeMap
} }
@ -89,3 +90,9 @@ func (this *DashboardController) ActiveIpTop10(writer http.ResponseWriter, reque
list := this.dashboardDao.ActiveIpTop10() list := this.dashboardDao.ActiveIpTop10()
return this.Success(list) return this.Success(list)
} }
func (this *DashboardController) Etl(writer http.ResponseWriter, request *http.Request) *result.WebResult {
this.dashboardService.Etl()
return this.Success("OK")
}

View File

@ -3,7 +3,6 @@ package rest
import ( import (
"github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/util" "github.com/eyebluecn/tank/code/tool/util"
"github.com/robfig/cron"
"time" "time"
) )
@ -49,19 +48,14 @@ func (this *DashboardService) Init() {
func (this *DashboardService) Bootstrap() { func (this *DashboardService) Bootstrap() {
this.logger.Info("[cron job] Everyday 00:05 ETL dashboard data.") this.logger.Info("Immediately ETL dashboard data.")
expression := "0 5 0 * * ?"
cronJob := cron.New()
err := cronJob.AddFunc(expression, this.etl)
core.PanicError(err)
cronJob.Start()
//do the etl method now. //do the etl method now.
go core.RunWithRecovery(this.etl) go core.RunWithRecovery(this.Etl)
} }
// handle the dashboard data. // handle the dashboard data.
func (this *DashboardService) etl() { func (this *DashboardService) Etl() {
this.logger.Info("ETL dashboard data.") this.logger.Info("ETL dashboard data.")

View File

@ -5,7 +5,6 @@ import (
"github.com/eyebluecn/tank/code/core" "github.com/eyebluecn/tank/code/core"
"github.com/eyebluecn/tank/code/tool/util" "github.com/eyebluecn/tank/code/tool/util"
"github.com/robfig/cron"
"net/http" "net/http"
"time" "time"
) )
@ -94,18 +93,12 @@ func (this *FootprintService) Trace(request *http.Request, duration time.Duratio
func (this *FootprintService) Bootstrap() { func (this *FootprintService) Bootstrap() {
this.logger.Info("[cron job] Every day 00:10 delete Footprint data 8 days ago.") this.logger.Info("Immediately delete Footprint data of 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)
go core.RunWithRecovery(this.CleanOldData)
} }
func (this *FootprintService) cleanOldData() { func (this *FootprintService) CleanOldData() {
day8Ago := time.Now() day8Ago := time.Now()
day8Ago = day8Ago.AddDate(0, 0, -8) day8Ago = day8Ago.AddDate(0, 0, -8)

View File

@ -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/ping"] = this.Wrap(this.Ping, USER_ROLE_GUEST)
routeMap["/api/preference/fetch"] = this.Wrap(this.Fetch, 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"] = 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) routeMap["/api/preference/system/cleanup"] = this.Wrap(this.SystemCleanup, USER_ROLE_ADMINISTRATOR)
return routeMap return routeMap
@ -60,6 +62,7 @@ func (this *PreferenceController) Fetch(writer http.ResponseWriter, request *htt
return this.Success(preference) return this.Success(preference)
} }
//edit basic info.
func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult { func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult {
name := request.FormValue("name") name := request.FormValue("name")
@ -72,7 +75,6 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http
downloadDirMaxNumStr := request.FormValue("downloadDirMaxNum") downloadDirMaxNumStr := request.FormValue("downloadDirMaxNum")
defaultTotalSizeLimitStr := request.FormValue("defaultTotalSizeLimit") defaultTotalSizeLimitStr := request.FormValue("defaultTotalSizeLimit")
allowRegisterStr := request.FormValue("allowRegister") allowRegisterStr := request.FormValue("allowRegister")
previewConfig := request.FormValue("previewConfig")
if name == "" { if name == "" {
panic(result.BadRequest("name cannot be null")) panic(result.BadRequest("name cannot be null"))
@ -120,12 +122,34 @@ func (this *PreferenceController) Edit(writer http.ResponseWriter, request *http
preference.DownloadDirMaxNum = downloadDirMaxNum preference.DownloadDirMaxNum = downloadDirMaxNum
preference.DefaultTotalSizeLimit = defaultTotalSizeLimit preference.DefaultTotalSizeLimit = defaultTotalSizeLimit
preference.AllowRegister = allowRegister 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.PreviewConfig = previewConfig
preference = this.preferenceDao.Save(preference) preference = this.preferenceService.Save(preference)
//reset the preference cache return this.Success(preference)
this.preferenceService.Reset() }
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) return this.Success(preference)
} }

View File

@ -22,8 +22,8 @@ func (this *PreferenceDao) Fetch() *Preference {
if db.Error.Error() == result.DB_ERROR_NOT_FOUND { if db.Error.Error() == result.DB_ERROR_NOT_FOUND {
preference.Name = "EyeblueTank" preference.Name = "EyeblueTank"
preference.Version = core.VERSION preference.Version = core.VERSION
//Default Office preview url. https://view.officeapps.live.com/op/embed.aspx?src=
preference.PreviewConfig = "{}" preference.PreviewConfig = "{}"
preference.ScanConfig = "{}"
this.Create(preference) this.Create(preference)
return preference return preference
} else { } else {

View File

@ -1,6 +1,9 @@
package rest 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 { type Preference struct {
Base Base
@ -14,6 +17,7 @@ type Preference struct {
DefaultTotalSizeLimit int64 `json:"defaultTotalSizeLimit" gorm:"type:bigint(20) not null;default:-1"` DefaultTotalSizeLimit int64 `json:"defaultTotalSizeLimit" gorm:"type:bigint(20) not null;default:-1"`
AllowRegister bool `json:"allowRegister" gorm:"type:tinyint(1) not null;default:0"` AllowRegister bool `json:"allowRegister" gorm:"type:tinyint(1) not null;default:0"`
PreviewConfig string `json:"previewConfig" gorm:"type:text"` PreviewConfig string `json:"previewConfig" gorm:"type:text"`
ScanConfig string `json:"scanConfig" gorm:"type:text"`
Version string `json:"version" gorm:"-"` Version string `json:"version" gorm:"-"`
} }
@ -21,3 +25,53 @@ type Preference struct {
func (this *Preference) TableName() string { func (this *Preference) TableName() string {
return core.TABLE_PREFIX + "preference" 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)
}

View File

@ -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. //System cleanup.
func (this *PreferenceService) Cleanup() { func (this *PreferenceService) Cleanup() {

75
code/rest/task_service.go Normal file
View File

@ -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()
}

View File

@ -169,6 +169,9 @@ func (this *TankContext) registerBeans() {
//uploadToken //uploadToken
this.registerBean(new(rest.UploadTokenDao)) this.registerBean(new(rest.UploadTokenDao))
//task
this.registerBean(new(rest.TaskService))
//user //user
this.registerBean(new(rest.UserController)) this.registerBean(new(rest.UserController))
this.registerBean(new(rest.UserDao)) this.registerBean(new(rest.UserDao))