Add the dashboard monitor.

This commit is contained in:
zicla
2018-12-02 18:38:39 +08:00
parent 2d1a95594f
commit 927427f353
18 changed files with 169 additions and 34 deletions

View File

@ -132,6 +132,8 @@ func (this *AlienService) PreviewOrDownload(
} }
//文件下载次数加一,为了加快访问速度,异步进行 //文件下载次数加一,为了加快访问速度,异步进行
go this.matterDao.TimesIncrement(uuid) go SafeMethod(func() {
this.matterDao.TimesIncrement(uuid)
})
} }

View File

@ -2,6 +2,7 @@ package rest
import ( import (
"net/http" "net/http"
"strconv"
) )
type DashboardController struct { 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)) routeMap := make(map[string]func(writer http.ResponseWriter, request *http.Request))
//每个Controller需要主动注册自己的路由。 //每个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 return routeMap
} }
@ -44,3 +45,51 @@ func (this *DashboardController) InvokeList(writer http.ResponseWriter, request
return this.Success("") 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)
}

View File

@ -1,6 +1,7 @@
package rest package rest
import ( import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql" _ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/nu7hatch/gouuid" "github.com/nu7hatch/gouuid"
"time" "time"
@ -17,6 +18,7 @@ func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard {
dashboard.Uuid = string(timeUUID.String()) dashboard.Uuid = string(timeUUID.String())
dashboard.CreateTime = time.Now() dashboard.CreateTime = time.Now()
dashboard.UpdateTime = time.Now() dashboard.UpdateTime = time.Now()
dashboard.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(dashboard) db := CONTEXT.DB.Create(dashboard)
this.PanicError(db.Error) this.PanicError(db.Error)
@ -33,7 +35,6 @@ func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard {
return dashboard return dashboard
} }
//删除一条记录 //删除一条记录
func (this *DashboardDao) Delete(dashboard *Dashboard) { func (this *DashboardDao) Delete(dashboard *Dashboard) {
@ -41,7 +42,6 @@ func (this *DashboardDao) Delete(dashboard *Dashboard) {
this.PanicError(db.Error) this.PanicError(db.Error)
} }
//按照dt查询 //按照dt查询
func (this *DashboardDao) FindByDt(dt string) *Dashboard { func (this *DashboardDao) FindByDt(dt string) *Dashboard {
@ -53,3 +53,27 @@ func (this *DashboardDao) FindByDt(dt string) *Dashboard {
} }
return &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
}

View File

@ -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) duration := nextTime.Sub(now)
this.logger.Info("每日数据汇总,下次时间:%s ", ConvertTimeToDateTimeString(nextTime)) this.logger.Info("每日数据汇总,下次时间:%s ", ConvertTimeToDateTimeString(nextTime))
this.maintainTimer = time.AfterFunc(duration, func() { 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) this.dashboardDao.Create(dashboard)
} }

View File

@ -42,7 +42,7 @@ func (this *DownloadTokenDao) Create(downloadToken *DownloadToken) *DownloadToke
downloadToken.CreateTime = time.Now() downloadToken.CreateTime = time.Now()
downloadToken.UpdateTime = time.Now() downloadToken.UpdateTime = time.Now()
downloadToken.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(downloadToken) db := CONTEXT.DB.Create(downloadToken)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -66,6 +66,7 @@ func (this *FootprintDao) Create(footprint *Footprint) *Footprint {
footprint.Uuid = string(timeUUID.String()) footprint.Uuid = string(timeUUID.String())
footprint.CreateTime = time.Now() footprint.CreateTime = time.Now()
footprint.UpdateTime = time.Now() footprint.UpdateTime = time.Now()
footprint.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(footprint) db := CONTEXT.DB.Create(footprint)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -67,13 +67,15 @@ func (this *ImageCacheController) Detail(writer http.ResponseWriter, request *ht
//按照分页的方式获取某个图片缓存夹下图片缓存和子图片缓存夹的列表,通常情况下只有一页。 //按照分页的方式获取某个图片缓存夹下图片缓存和子图片缓存夹的列表,通常情况下只有一页。
func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http.Request) *WebResult { func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http.Request) *WebResult {
//如果是根目录那么就传入root. //如果是根目录那么就传入root.
pageStr := request.FormValue("page") pageStr := request.FormValue("page")
pageSizeStr := request.FormValue("pageSize") pageSizeStr := request.FormValue("pageSize")
orderCreateTime := request.FormValue("orderCreateTime")
orderUpdateTime := request.FormValue("orderUpdateTime")
orderSort := request.FormValue("orderSort")
userUuid := request.FormValue("userUuid") userUuid := request.FormValue("userUuid")
matterUuid := request.FormValue("matterUuid") matterUuid := request.FormValue("matterUuid")
orderCreateTime := request.FormValue("orderCreateTime")
orderSize := request.FormValue("orderSize") orderSize := request.FormValue("orderSize")
user := this.checkUser(writer, request) user := this.checkUser(writer, request)
@ -99,6 +101,15 @@ func (this *ImageCacheController) Page(writer http.ResponseWriter, request *http
key: "create_time", key: "create_time",
value: orderCreateTime, value: orderCreateTime,
}, },
{
key: "update_time",
value: orderUpdateTime,
},
{
key: "sort",
value: orderSort,
},
{ {
key: "size", key: "size",
value: orderSize, value: orderSize,

View File

@ -122,6 +122,7 @@ func (this *ImageCacheDao) Create(imageCache *ImageCache) *ImageCache {
imageCache.Uuid = string(timeUUID.String()) imageCache.Uuid = string(timeUUID.String())
imageCache.CreateTime = time.Now() imageCache.CreateTime = time.Now()
imageCache.UpdateTime = time.Now() imageCache.UpdateTime = time.Now()
imageCache.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(imageCache) db := CONTEXT.DB.Create(imageCache)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -70,9 +70,9 @@ func (this *Logger) Init() {
//日志需要自我备份,自我维护。明天第一秒触发 //日志需要自我备份,自我维护。明天第一秒触发
nextTime := FirstSecondOfDay(Tomorrow()) nextTime := FirstSecondOfDay(Tomorrow())
duration := nextTime.Sub(time.Now()) 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() { this.maintainTimer = time.AfterFunc(duration, func() {
go this.maintain() go SafeMethod(this.maintain)
}) })
} }
@ -83,7 +83,7 @@ func (this *Logger) maintain() {
this.Lock() this.Lock()
defer this.Unlock() defer this.Unlock()
go this.Info("每日维护日志") this.Info("每日维护日志")
//首先关闭文件。 //首先关闭文件。
this.closeFile() this.closeFile()
@ -94,7 +94,7 @@ func (this *Logger) maintain() {
//直接重命名文件 //直接重命名文件
err := os.Rename(this.fileName(), destPath) err := os.Rename(this.fileName(), destPath)
if err != nil { if err != nil {
go this.Error("重命名文件出错", err.Error()) this.Error("重命名文件出错", err.Error())
} }
//再次打开文件 //再次打开文件
@ -104,9 +104,9 @@ func (this *Logger) maintain() {
now := time.Now() now := time.Now()
nextTime := FirstSecondOfDay(Tomorrow()) nextTime := FirstSecondOfDay(Tomorrow())
duration := nextTime.Sub(now) 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() { this.maintainTimer = time.AfterFunc(duration, func() {
go this.maintain() go SafeMethod(this.maintain)
}) })
} }

View File

@ -1,12 +1,12 @@
package rest package rest
import ( import (
"fmt"
"net/http" "net/http"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"fmt"
) )
type MatterController struct { 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 { func (this *MatterController) Page(writer http.ResponseWriter, request *http.Request) *WebResult {
//如果是根目录那么就传入root. //如果是根目录那么就传入root.
puuid := request.FormValue("puuid")
pageStr := request.FormValue("page") pageStr := request.FormValue("page")
pageSizeStr := request.FormValue("pageSize") pageSizeStr := request.FormValue("pageSize")
orderCreateTime := request.FormValue("orderCreateTime")
orderUpdateTime := request.FormValue("orderUpdateTime")
orderSort := request.FormValue("orderSort")
puuid := request.FormValue("puuid")
userUuid := request.FormValue("userUuid") userUuid := request.FormValue("userUuid")
name := request.FormValue("name") name := request.FormValue("name")
dir := request.FormValue("dir") dir := request.FormValue("dir")
orderDir := request.FormValue("orderDir") orderDir := request.FormValue("orderDir")
orderCreateTime := request.FormValue("orderCreateTime")
orderSize := request.FormValue("orderSize") orderSize := request.FormValue("orderSize")
orderName := request.FormValue("orderName") orderName := request.FormValue("orderName")
extensionsStr := request.FormValue("extensions") extensionsStr := request.FormValue("extensions")
@ -194,14 +197,22 @@ func (this *MatterController) Page(writer http.ResponseWriter, request *http.Req
} }
sortArray := []OrderPair{ sortArray := []OrderPair{
{
key: "dir",
value: orderDir,
},
{ {
key: "create_time", key: "create_time",
value: orderCreateTime, value: orderCreateTime,
}, },
{
key: "update_time",
value: orderUpdateTime,
},
{
key: "sort",
value: orderSort,
},
{
key: "dir",
value: orderDir,
},
{ {
key: "size", key: "size",
value: orderSize, value: orderSize,

View File

@ -200,6 +200,7 @@ func (this *MatterDao) Create(matter *Matter) *Matter {
matter.Uuid = string(timeUUID.String()) matter.Uuid = string(timeUUID.String())
matter.CreateTime = time.Now() matter.CreateTime = time.Now()
matter.UpdateTime = time.Now() matter.UpdateTime = time.Now()
matter.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(matter) db := CONTEXT.DB.Create(matter)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -39,6 +39,7 @@ func (this *PreferenceDao) Create(preference *Preference) *Preference {
preference.Uuid = string(timeUUID.String()) preference.Uuid = string(timeUUID.String())
preference.CreateTime = time.Now() preference.CreateTime = time.Now()
preference.UpdateTime = time.Now() preference.UpdateTime = time.Now()
preference.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(preference) db := CONTEXT.DB.Create(preference)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -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 { 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 { } else {
//当作静态资源处理。默认从当前文件下面的static文件夹中取东西。 //当作静态资源处理。默认从当前文件下面的static文件夹中取东西。
dir := GetHtmlPath() dir := GetHtmlPath()

View File

@ -45,6 +45,9 @@ func (this *SessionDao) Create(session *Session) *Session {
timeUUID, _ := uuid.NewV4() timeUUID, _ := uuid.NewV4()
session.Uuid = string(timeUUID.String()) session.Uuid = string(timeUUID.String())
session.CreateTime = time.Now()
session.UpdateTime = time.Now()
session.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(session) db := CONTEXT.DB.Create(session)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -32,7 +32,7 @@ func (this *UploadTokenDao) Create(uploadToken *UploadToken) *UploadToken {
uploadToken.CreateTime = time.Now() uploadToken.CreateTime = time.Now()
uploadToken.UpdateTime = time.Now() uploadToken.UpdateTime = time.Now()
uploadToken.Sort = time.Now().UnixNano() / 1e6
db := CONTEXT.DB.Create(uploadToken) db := CONTEXT.DB.Create(uploadToken)
this.PanicError(db.Error) this.PanicError(db.Error)

View File

@ -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 { func (this *UserController) Page(writer http.ResponseWriter, request *http.Request) *WebResult {
//如果是根目录那么就传入root.
pageStr := request.FormValue("page") pageStr := request.FormValue("page")
pageSizeStr := request.FormValue("pageSize") pageSizeStr := request.FormValue("pageSize")
orderCreateTime := request.FormValue("orderCreateTime")
orderUpdateTime := request.FormValue("orderUpdateTime")
orderSort := request.FormValue("orderSort")
username := request.FormValue("username") username := request.FormValue("username")
email := request.FormValue("email") email := request.FormValue("email")
phone := request.FormValue("phone") phone := request.FormValue("phone")
status := request.FormValue("status") status := request.FormValue("status")
orderLastTime := request.FormValue("orderLastTime") orderLastTime := request.FormValue("orderLastTime")
orderCreateTime := request.FormValue("orderCreateTime")
var page int var page int
if pageStr != "" { if pageStr != "" {
@ -272,14 +274,22 @@ func (this *UserController) Page(writer http.ResponseWriter, request *http.Reque
} }
sortArray := []OrderPair{ sortArray := []OrderPair{
{
key: "last_time",
value: orderLastTime,
},
{ {
key: "create_time", key: "create_time",
value: orderCreateTime, 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) pager := this.userDao.Page(page, pageSize, username, email, phone, status, sortArray)

View File

@ -196,7 +196,7 @@ func (table *CacheTable) checkExpire() {
table.cleanupInterval = smallestDuration table.cleanupInterval = smallestDuration
if smallestDuration > 0 { if smallestDuration > 0 {
table.cleanupTimer = time.AfterFunc(smallestDuration, func() { table.cleanupTimer = time.AfterFunc(smallestDuration, func() {
go table.checkExpire() go SafeMethod(table.checkExpire)
}) })
} }
table.Unlock() table.Unlock()

16
rest/util_framework.go Normal file
View File

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