From 927427f35396eb5b308a3ac9ceefaff3a633d24e Mon Sep 17 00:00:00 2001 From: zicla Date: Sun, 2 Dec 2018 18:38:39 +0800 Subject: [PATCH] Add the dashboard monitor. --- rest/alien_service.go | 4 ++- rest/dashboard_controller.go | 51 +++++++++++++++++++++++++++++++++- rest/dashboard_dao.go | 28 +++++++++++++++++-- rest/dashboard_service.go | 6 ++-- rest/download_token_dao.go | 2 +- rest/footprint_dao.go | 1 + rest/image_cache_controller.go | 15 ++++++++-- rest/image_cache_dao.go | 1 + rest/logger.go | 12 ++++---- rest/matter_controller.go | 25 ++++++++++++----- rest/matter_dao.go | 1 + rest/preference_dao.go | 1 + rest/router.go | 11 +++++--- rest/session_dao.go | 3 ++ rest/upload_token_dao.go | 2 +- rest/user_controller.go | 22 +++++++++++---- rest/util_cache.go | 2 +- rest/util_framework.go | 16 +++++++++++ 18 files changed, 169 insertions(+), 34 deletions(-) create mode 100644 rest/util_framework.go diff --git a/rest/alien_service.go b/rest/alien_service.go index 81bff0d..60f7862 100644 --- a/rest/alien_service.go +++ b/rest/alien_service.go @@ -132,6 +132,8 @@ func (this *AlienService) PreviewOrDownload( } //文件下载次数加一,为了加快访问速度,异步进行 - go this.matterDao.TimesIncrement(uuid) + go SafeMethod(func() { + this.matterDao.TimesIncrement(uuid) + }) } diff --git a/rest/dashboard_controller.go b/rest/dashboard_controller.go index 8a9d6b3..7841a3f 100644 --- a/rest/dashboard_controller.go +++ b/rest/dashboard_controller.go @@ -2,6 +2,7 @@ package rest import ( "net/http" + "strconv" ) type DashboardController struct { @@ -33,7 +34,7 @@ func (this *DashboardController) RegisterRoutes() map[string]func(writer http.Re routeMap := make(map[string]func(writer http.ResponseWriter, request *http.Request)) //每个Controller需要主动注册自己的路由。 - routeMap["/api/dashboard/invoke/list"] = this.Wrap(this.InvokeList, USER_ROLE_ADMINISTRATOR) + routeMap["/api/dashboard/page"] = this.Wrap(this.Page, USER_ROLE_ADMINISTRATOR) return routeMap } @@ -44,3 +45,51 @@ func (this *DashboardController) InvokeList(writer http.ResponseWriter, request return this.Success("") } + +//按照分页的方式获取某个图片缓存夹下图片缓存和子图片缓存夹的列表,通常情况下只有一页。 +func (this *DashboardController) Page(writer http.ResponseWriter, request *http.Request) *WebResult { + + //如果是根目录,那么就传入root. + pageStr := request.FormValue("page") + pageSizeStr := request.FormValue("pageSize") + orderCreateTime := request.FormValue("orderCreateTime") + orderUpdateTime := request.FormValue("orderUpdateTime") + orderSort := request.FormValue("orderSort") + orderDt := request.FormValue("orderDt") + + var page int + if pageStr != "" { + page, _ = strconv.Atoi(pageStr) + } + + pageSize := 200 + if pageSizeStr != "" { + tmp, err := strconv.Atoi(pageSizeStr) + if err == nil { + pageSize = tmp + } + } + + sortArray := []OrderPair{ + { + key: "create_time", + value: orderCreateTime, + }, + { + key: "update_time", + value: orderUpdateTime, + }, + { + key: "sort", + value: orderSort, + }, + { + key: "dt", + value: orderDt, + }, + } + + pager := this.dashboardDao.Page(page, pageSize, "", sortArray) + + return this.Success(pager) +} diff --git a/rest/dashboard_dao.go b/rest/dashboard_dao.go index 0abe669..9c403e9 100644 --- a/rest/dashboard_dao.go +++ b/rest/dashboard_dao.go @@ -1,6 +1,7 @@ package rest import ( + "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" "github.com/nu7hatch/gouuid" "time" @@ -17,6 +18,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard { dashboard.Uuid = string(timeUUID.String()) dashboard.CreateTime = time.Now() dashboard.UpdateTime = time.Now() + dashboard.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(dashboard) this.PanicError(db.Error) @@ -33,7 +35,6 @@ func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard { return dashboard } - //删除一条记录 func (this *DashboardDao) Delete(dashboard *Dashboard) { @@ -41,7 +42,6 @@ func (this *DashboardDao) Delete(dashboard *Dashboard) { this.PanicError(db.Error) } - //按照dt查询 func (this *DashboardDao) FindByDt(dt string) *Dashboard { @@ -53,3 +53,27 @@ func (this *DashboardDao) FindByDt(dt string) *Dashboard { } return &dashboard } + +//获取某个文件夹下所有的文件和子文件 +func (this *DashboardDao) Page(page int, pageSize int, dt string, sortArray []OrderPair) *Pager { + + var wp = &WherePair{} + + if dt != "" { + wp = wp.And(&WherePair{Query: "dt = ?", Args: []interface{}{dt}}) + } + + var conditionDB *gorm.DB + conditionDB = CONTEXT.DB.Model(&Dashboard{}).Where(wp.Query, wp.Args...) + + count := 0 + db := conditionDB.Count(&count) + this.PanicError(db.Error) + + var dashboards []*Dashboard + db = conditionDB.Order(this.GetSortString(sortArray)).Offset(page * pageSize).Limit(pageSize).Find(&dashboards) + this.PanicError(db.Error) + pager := NewPager(page, pageSize, count, dashboards) + + return pager +} diff --git a/rest/dashboard_service.go b/rest/dashboard_service.go index 5874d15..7f61fb2 100644 --- a/rest/dashboard_service.go +++ b/rest/dashboard_service.go @@ -47,7 +47,8 @@ func (this *DashboardService) Init() { } //立即执行数据清洗任务 - go this.maintain() + go SafeMethod(this.maintain) + } //每日清洗离线数据表。 @@ -59,7 +60,7 @@ func (this *DashboardService) maintain() { duration := nextTime.Sub(now) this.logger.Info("每日数据汇总,下次时间:%s ", ConvertTimeToDateTimeString(nextTime)) this.maintainTimer = time.AfterFunc(duration, func() { - go this.maintain() + go SafeMethod(this.maintain) }) //准备日期开始结尾 @@ -125,4 +126,5 @@ func (this *DashboardService) maintain() { } this.dashboardDao.Create(dashboard) + } diff --git a/rest/download_token_dao.go b/rest/download_token_dao.go index 2bc062a..004b61d 100644 --- a/rest/download_token_dao.go +++ b/rest/download_token_dao.go @@ -42,7 +42,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke downloadToken.CreateTime = time.Now() downloadToken.UpdateTime = time.Now() - + downloadToken.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(downloadToken) this.PanicError(db.Error) diff --git a/rest/footprint_dao.go b/rest/footprint_dao.go index 169ee1b..aa6fbd2 100644 --- a/rest/footprint_dao.go +++ b/rest/footprint_dao.go @@ -66,6 +66,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint { footprint.Uuid = string(timeUUID.String()) footprint.CreateTime = time.Now() footprint.UpdateTime = time.Now() + footprint.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(footprint) this.PanicError(db.Error) diff --git a/rest/image_cache_controller.go b/rest/image_cache_controller.go index 25838cb..c8811c4 100644 --- a/rest/image_cache_controller.go +++ b/rest/image_cache_controller.go @@ -67,13 +67,15 @@ func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *ht //按照分页的方式获取某个图片缓存夹下图片缓存和子图片缓存夹的列表,通常情况下只有一页。 func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http.Request) *WebResult { - //如果是根目录,那么就传入root. pageStr := request.FormValue("page") pageSizeStr := request.FormValue("pageSize") + orderCreateTime := request.FormValue("orderCreateTime") + orderUpdateTime := request.FormValue("orderUpdateTime") + orderSort := request.FormValue("orderSort") + userUuid := request.FormValue("userUuid") matterUuid := request.FormValue("matterUuid") - orderCreateTime := request.FormValue("orderCreateTime") orderSize := request.FormValue("orderSize") user := this.checkUser(writer, request) @@ -99,6 +101,15 @@ func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http key: "create_time", value: orderCreateTime, }, + { + key: "update_time", + value: orderUpdateTime, + }, + { + key: "sort", + value: orderSort, + }, + { key: "size", value: orderSize, diff --git a/rest/image_cache_dao.go b/rest/image_cache_dao.go index ae54d15..6dbb332 100644 --- a/rest/image_cache_dao.go +++ b/rest/image_cache_dao.go @@ -122,6 +122,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache { imageCache.Uuid = string(timeUUID.String()) imageCache.CreateTime = time.Now() imageCache.UpdateTime = time.Now() + imageCache.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(imageCache) this.PanicError(db.Error) diff --git a/rest/logger.go b/rest/logger.go index ce53917..70f52b9 100644 --- a/rest/logger.go +++ b/rest/logger.go @@ -70,9 +70,9 @@ func (this *Logger) Init() { //日志需要自我备份,自我维护。明天第一秒触发 nextTime := FirstSecondOfDay(Tomorrow()) duration := nextTime.Sub(time.Now()) - go this.Info("%vs后将进行下一次日志维护 下次时间%v ", int64(duration/time.Second), nextTime) + this.Info("%vs后将进行下一次日志维护 下次时间%v ", int64(duration/time.Second), nextTime) this.maintainTimer = time.AfterFunc(duration, func() { - go this.maintain() + go SafeMethod(this.maintain) }) } @@ -83,7 +83,7 @@ func (this *Logger) maintain() { this.Lock() defer this.Unlock() - go this.Info("每日维护日志") + this.Info("每日维护日志") //首先关闭文件。 this.closeFile() @@ -94,7 +94,7 @@ func (this *Logger) maintain() { //直接重命名文件 err := os.Rename(this.fileName(), destPath) if err != nil { - go this.Error("重命名文件出错", err.Error()) + this.Error("重命名文件出错", err.Error()) } //再次打开文件 @@ -104,9 +104,9 @@ func (this *Logger) maintain() { now := time.Now() nextTime := FirstSecondOfDay(Tomorrow()) duration := nextTime.Sub(now) - go this.Info("%vs 后将进行下一次日志维护 下次时间维护时间:%v ", int64(duration/time.Second), nextTime) + this.Info("%vs 后将进行下一次日志维护 下次时间维护时间:%v ", int64(duration/time.Second), nextTime) this.maintainTimer = time.AfterFunc(duration, func() { - go this.maintain() + go SafeMethod(this.maintain) }) } diff --git a/rest/matter_controller.go b/rest/matter_controller.go index d4bcb23..9abcd56 100644 --- a/rest/matter_controller.go +++ b/rest/matter_controller.go @@ -1,12 +1,12 @@ package rest import ( + "fmt" "net/http" "regexp" "strconv" "strings" "time" - "fmt" ) type MatterController struct { @@ -152,14 +152,17 @@ func (this *MatterController) CreateDirectory(writer http.ResponseWriter, reques func (this *MatterController) Page(writer http.ResponseWriter, request *http.Request) *WebResult { //如果是根目录,那么就传入root. - puuid := request.FormValue("puuid") pageStr := request.FormValue("page") pageSizeStr := request.FormValue("pageSize") + orderCreateTime := request.FormValue("orderCreateTime") + orderUpdateTime := request.FormValue("orderUpdateTime") + orderSort := request.FormValue("orderSort") + + puuid := request.FormValue("puuid") userUuid := request.FormValue("userUuid") name := request.FormValue("name") dir := request.FormValue("dir") orderDir := request.FormValue("orderDir") - orderCreateTime := request.FormValue("orderCreateTime") orderSize := request.FormValue("orderSize") orderName := request.FormValue("orderName") extensionsStr := request.FormValue("extensions") @@ -194,14 +197,22 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req } sortArray := []OrderPair{ - { - key: "dir", - value: orderDir, - }, { key: "create_time", value: orderCreateTime, }, + { + key: "update_time", + value: orderUpdateTime, + }, + { + key: "sort", + value: orderSort, + }, + { + key: "dir", + value: orderDir, + }, { key: "size", value: orderSize, diff --git a/rest/matter_dao.go b/rest/matter_dao.go index b9bc373..6a33338 100644 --- a/rest/matter_dao.go +++ b/rest/matter_dao.go @@ -200,6 +200,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter { matter.Uuid = string(timeUUID.String()) matter.CreateTime = time.Now() matter.UpdateTime = time.Now() + matter.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(matter) this.PanicError(db.Error) diff --git a/rest/preference_dao.go b/rest/preference_dao.go index fad3a25..d97d3a7 100644 --- a/rest/preference_dao.go +++ b/rest/preference_dao.go @@ -39,6 +39,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference { preference.Uuid = string(timeUUID.String()) preference.CreateTime = time.Now() preference.UpdateTime = time.Now() + preference.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(preference) this.PanicError(db.Error) diff --git a/rest/router.go b/rest/router.go index 10dda03..900fa1e 100644 --- a/rest/router.go +++ b/rest/router.go @@ -87,7 +87,9 @@ func (this *Router) GlobalPanicHandler(writer http.ResponseWriter, request *http } //错误情况记录。 - go this.footprintService.Trace(writer, request, time.Now().Sub(startTime), false) + go SafeMethod(func() { + this.footprintService.Trace(writer, request, time.Now().Sub(startTime), false) + }) } } @@ -123,14 +125,15 @@ func (this *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) } if !canHandle { - panic(fmt.Sprintf("没有找到能够处理%s的方法\n", path)) + panic(CustomWebResult(CODE_WRAPPER_NOT_FOUND, fmt.Sprintf("没有找到能够处理%s的方法", path))) } } //正常的访问记录会落到这里。 - go this.footprintService.Trace(writer, request, time.Now().Sub(startTime), true) - + go SafeMethod(func() { + this.footprintService.Trace(writer, request, time.Now().Sub(startTime), true) + }) } else { //当作静态资源处理。默认从当前文件下面的static文件夹中取东西。 dir := GetHtmlPath() diff --git a/rest/session_dao.go b/rest/session_dao.go index 15a00c0..063fd21 100644 --- a/rest/session_dao.go +++ b/rest/session_dao.go @@ -45,6 +45,9 @@ func (this *SessionDao) Create(session *Session) *Session { timeUUID, _ := uuid.NewV4() session.Uuid = string(timeUUID.String()) + session.CreateTime = time.Now() + session.UpdateTime = time.Now() + session.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(session) this.PanicError(db.Error) diff --git a/rest/upload_token_dao.go b/rest/upload_token_dao.go index 537508d..7719d73 100644 --- a/rest/upload_token_dao.go +++ b/rest/upload_token_dao.go @@ -32,7 +32,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken { uploadToken.CreateTime = time.Now() uploadToken.UpdateTime = time.Now() - + uploadToken.Sort = time.Now().UnixNano() / 1e6 db := CONTEXT.DB.Create(uploadToken) this.PanicError(db.Error) diff --git a/rest/user_controller.go b/rest/user_controller.go index 7604691..2dcccb6 100644 --- a/rest/user_controller.go +++ b/rest/user_controller.go @@ -248,15 +248,17 @@ func (this *UserController) Logout(writer http.ResponseWriter, request *http.Req //获取用户列表 管理员的权限。 func (this *UserController) Page(writer http.ResponseWriter, request *http.Request) *WebResult { - //如果是根目录,那么就传入root. pageStr := request.FormValue("page") pageSizeStr := request.FormValue("pageSize") + orderCreateTime := request.FormValue("orderCreateTime") + orderUpdateTime := request.FormValue("orderUpdateTime") + orderSort := request.FormValue("orderSort") + username := request.FormValue("username") email := request.FormValue("email") phone := request.FormValue("phone") status := request.FormValue("status") orderLastTime := request.FormValue("orderLastTime") - orderCreateTime := request.FormValue("orderCreateTime") var page int if pageStr != "" { @@ -272,14 +274,22 @@ func (this *UserController) Page(writer http.ResponseWriter, request *http.Reque } sortArray := []OrderPair{ - { - key: "last_time", - value: orderLastTime, - }, { key: "create_time", value: orderCreateTime, }, + { + key: "update_time", + value: orderUpdateTime, + }, + { + key: "sort", + value: orderSort, + }, + { + key: "last_time", + value: orderLastTime, + }, } pager := this.userDao.Page(page, pageSize, username, email, phone, status, sortArray) diff --git a/rest/util_cache.go b/rest/util_cache.go index 61bba35..d869a1a 100644 --- a/rest/util_cache.go +++ b/rest/util_cache.go @@ -196,7 +196,7 @@ func (table *CacheTable) checkExpire() { table.cleanupInterval = smallestDuration if smallestDuration > 0 { table.cleanupTimer = time.AfterFunc(smallestDuration, func() { - go table.checkExpire() + go SafeMethod(table.checkExpire) }) } table.Unlock() diff --git a/rest/util_framework.go b/rest/util_framework.go new file mode 100644 index 0000000..bf53fcc --- /dev/null +++ b/rest/util_framework.go @@ -0,0 +1,16 @@ +package rest + + +//带有panic恢复的方法 +func PanicHandler() { + if err := recover(); err != nil { + LOGGER.Error("异步任务错误: %v", err) + } +} + +//带有panic恢复的方法 +func SafeMethod(f func()) { + defer PanicHandler() + //执行函数 + f() +}