Skip to content

Commit

Permalink
Finish the dashboard summary things.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicla committed Dec 1, 2018
1 parent 439d23d commit 2d1a955
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 62 deletions.
15 changes: 7 additions & 8 deletions rest/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ func (this *Context) Init() {
this.Router = NewRouter()
}


func (this *Context) OpenDb() {

var err error = nil
this.DB, err = gorm.Open("mysql", CONFIG.MysqlUrl)

//是否打开sql日志
this.DB.LogMode(false)
if err != nil {
panic("failed to connect mysql database")
LOGGER.Panic("failed to connect mysql database")
}

//是否打开sql日志(在调试阶段可以打开,以方便查看执行的SQL)
this.DB.LogMode(false)
}

func (this *Context) CloseDb() {
Expand Down Expand Up @@ -91,8 +91,7 @@ func (this *Context) registerBean(bean IBean) {
}

} else {
err := fmt.Sprintf("注册的【%s】不是Bean类型。", typeName)
panic(err)
LOGGER.Panic("注册的【%s】不是Bean类型。", typeName)
}

}
Expand Down Expand Up @@ -154,8 +153,8 @@ func (this *Context) GetBean(bean IBean) IBean {
if val, ok := this.BeanMap[typeName]; ok {
return val
} else {
err := fmt.Sprintf("【%s】没有注册。", typeName)
panic(err)
LOGGER.Panic("【%s】没有注册。", typeName)
return nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion rest/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func (this *DashboardController) RegisterRoutes() map[string]func(writer http.Re
//过去七天分时调用量
func (this *DashboardController) InvokeList(writer http.ResponseWriter, request *http.Request) *WebResult {

return this.Success(this.dashboardDao.InvokeList())
return this.Success("")

}
84 changes: 42 additions & 42 deletions rest/dashboard_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,54 @@ package rest

import (
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/nu7hatch/gouuid"
"time"
)

type DashboardDao struct {
BaseDao
}

//过去七天调用量
func (this *DashboardDao) InvokeList() []*DashboardInvoke {

//过去几天
var dayNum = 15;
var tableName = Footprint{}.TableName()
now := time.Now()
startDate := now.AddDate(0, 0, 1-dayNum)
rows, err := CONTEXT.DB.Raw("SELECT COUNT(uuid) AS invoke_num,COUNT(DISTINCT(ip)) AS uv,dt FROM "+tableName+" WHERE dt>= ? AND dt <= ? GROUP BY dt",
ConvertTimeToDateString(startDate),
ConvertTimeToDateString(now)).Rows()
this.PanicError(err)
defer rows.Close()

var invokeMap = make(map[string]*DashboardInvoke)
var dashboardInvokes []*DashboardInvoke
for rows.Next() {
var invokeNum int64 = 0;
var uv int64 = 0;
var dt string;
rows.Scan(&invokeNum, &uv, &dt)
invokeMap[dt] = &DashboardInvoke{
InvokeNum: invokeNum,
Uv: uv,
Dt: dt,
}
}
for i := 1 - dayNum; i <= 0; i++ {
date := now.AddDate(0, 0, i)
dt := ConvertTimeToDateString(date)
v, ok := invokeMap[dt]
if ok {
dashboardInvokes = append(dashboardInvokes, v)
} else {
dashboardInvokes = append(dashboardInvokes, &DashboardInvoke{
InvokeNum: 0,
Uv: 0,
Dt: dt,
})
}
}
//创建
func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard {

timeUUID, _ := uuid.NewV4()
dashboard.Uuid = string(timeUUID.String())
dashboard.CreateTime = time.Now()
dashboard.UpdateTime = time.Now()
db := CONTEXT.DB.Create(dashboard)
this.PanicError(db.Error)

return dashboard
}

//修改一条记录
func (this *DashboardDao) Save(dashboard *Dashboard) *Dashboard {

dashboard.UpdateTime = time.Now()
db := CONTEXT.DB.Save(dashboard)
this.PanicError(db.Error)

return dashboard
}


//删除一条记录
func (this *DashboardDao) Delete(dashboard *Dashboard) {

db := CONTEXT.DB.Delete(&dashboard)
this.PanicError(db.Error)
}


return dashboardInvokes
//按照dt查询
func (this *DashboardDao) FindByDt(dt string) *Dashboard {

// Read
var dashboard Dashboard
db := CONTEXT.DB.Where(&Dashboard{Dt: dt}).First(&dashboard)
if db.Error != nil {
return nil
}
return &dashboard
}
4 changes: 2 additions & 2 deletions rest/dashboard_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package rest
*/
type Dashboard struct {
Base
VisitNum int64 `json:"visitNum"`
TotalVisitNum int64 `json:"totalVisitNum"`
InvokeNum int64 `json:"invokeNum"`
TotalInvokeNum int64 `json:"totalInvokeNum"`
Uv int64 `json:"uv"`
TotalUv int64 `json:"totalUv"`
MatterNum int64 `json:"matterNum"`
Expand Down
108 changes: 105 additions & 3 deletions rest/dashboard_service.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package rest

import (
"time"
)

//@Service
type DashboardService struct {
Bean
dashboardDao *DashboardDao
userDao *UserDao
dashboardDao *DashboardDao
footprintDao *FootprintDao
matterDao *MatterDao
imageCacheDao *ImageCacheDao
userDao *UserDao
//每天凌晨定时整理器
maintainTimer *time.Timer
}


//初始化方法
func (this *DashboardService) Init() {
this.Bean.Init()
Expand All @@ -18,9 +26,103 @@ func (this *DashboardService) Init() {
this.dashboardDao = b
}

b = CONTEXT.GetBean(this.footprintDao)
if b, ok := b.(*FootprintDao); ok {
this.footprintDao = b
}

b = CONTEXT.GetBean(this.matterDao)
if b, ok := b.(*MatterDao); ok {
this.matterDao = b
}

b = CONTEXT.GetBean(this.imageCacheDao)
if b, ok := b.(*ImageCacheDao); ok {
this.imageCacheDao = b
}

b = CONTEXT.GetBean(this.userDao)
if b, ok := b.(*UserDao); ok {
this.userDao = b
}

//立即执行数据清洗任务
go this.maintain()
}

//每日清洗离线数据表。
func (this *DashboardService) maintain() {

//准备好下次维护日志的时间。
now := time.Now()
nextTime := FirstMinuteOfDay(Tomorrow())
duration := nextTime.Sub(now)
this.logger.Info("每日数据汇总,下次时间:%s ", ConvertTimeToDateTimeString(nextTime))
this.maintainTimer = time.AfterFunc(duration, func() {
go this.maintain()
})

//准备日期开始结尾
startTime := FirstSecondOfDay(Yesterday())
endTime := LastSecondOfDay(Yesterday())
dt := ConvertTimeToDateString(startTime)
longTimeAgo := time.Now()
longTimeAgo = longTimeAgo.AddDate(-20, 0, 0)

this.logger.Info("统计汇总表 %s -> %s", ConvertTimeToDateTimeString(startTime), ConvertTimeToDateTimeString(endTime))

//判断昨天的记录是否已经生成,如果生成了就直接删除掉
dbDashboard := this.dashboardDao.FindByDt(dt)
if dbDashboard != nil {
this.logger.Info(" %s 的汇总已经存在了,删除以进行更新", dt)
this.dashboardDao.Delete(dbDashboard)
}

invokeNum := this.footprintDao.CountBetweenTime(startTime, endTime)
this.logger.Info("调用数:%d", invokeNum)

totalInvokeNum := this.footprintDao.CountBetweenTime(longTimeAgo, endTime)
this.logger.Info("历史总调用数:%d", totalInvokeNum)

uv := this.footprintDao.UvBetweenTime(startTime, endTime)
this.logger.Info("UV:%d", uv)

totalUv := this.footprintDao.UvBetweenTime(longTimeAgo, endTime)
this.logger.Info("历史总UV:%d", totalUv)

matterNum := this.matterDao.CountBetweenTime(startTime, endTime)
this.logger.Info("文件数量数:%d", matterNum)

totalMatterNum := this.matterDao.CountBetweenTime(longTimeAgo, endTime)
this.logger.Info("历史文件总数:%d", totalMatterNum)

matterSize := this.matterDao.SizeBetweenTime(startTime, endTime)
this.logger.Info("文件大小:%d", matterSize)

totalMatterSize := this.matterDao.SizeBetweenTime(longTimeAgo, endTime)
this.logger.Info("历史文件总大小:%d", totalMatterSize)

cacheSize := this.imageCacheDao.SizeBetweenTime(startTime, endTime)
this.logger.Info("缓存大小:%d", cacheSize)

totalCacheSize := this.imageCacheDao.SizeBetweenTime(longTimeAgo, endTime)
this.logger.Info("历史缓存总大小:%d", totalCacheSize)

avgCost := this.footprintDao.AvgCostBetweenTime(startTime, endTime)
this.logger.Info("平均耗时:%d ms", avgCost)

dashboard := &Dashboard{
InvokeNum: invokeNum,
TotalInvokeNum: totalInvokeNum,
Uv: uv,
TotalUv: totalUv,
MatterNum: matterNum,
TotalMatterNum: totalMatterNum,
FileSize: matterSize + cacheSize,
TotalFileSize: totalMatterSize + totalCacheSize,
AvgCost: avgCost,
Dt: dt,
}

this.dashboardDao.Create(dashboard)
}
28 changes: 28 additions & 0 deletions rest/footprint_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,31 @@ func (this *FootprintDao) Delete(footprint *Footprint) {
db := CONTEXT.DB.Delete(&footprint)
this.PanicError(db.Error)
}

//获取一段时间中,总的数量
func (this *FootprintDao) CountBetweenTime(startTime time.Time, endTime time.Time) int64 {
var count int64
db := CONTEXT.DB.Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Count(&count)
this.PanicError(db.Error)
return count
}

//获取一段时间中UV的数量
func (this *FootprintDao) UvBetweenTime(startTime time.Time, endTime time.Time) int64 {
var count int64
db := CONTEXT.DB.Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("COUNT(DISTINCT(ip))")
this.PanicError(db.Error)
row := db.Row()
row.Scan(&count)
return count
}

//获取一段时间中平均耗时
func (this *FootprintDao) AvgCostBetweenTime(startTime time.Time, endTime time.Time) int64 {
var cost float64
db := CONTEXT.DB.Model(&Footprint{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("AVG(cost)")
this.PanicError(db.Error)
row := db.Row()
row.Scan(&cost)
return int64(cost)
}
1 change: 0 additions & 1 deletion rest/footprint_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type Footprint struct {
Params string `json:"params"`
Cost int64 `json:"cost"`
Success bool `json:"success"`
Dt string `json:"dt"`
}

// set File's table name to be `profiles`
Expand Down
1 change: 0 additions & 1 deletion rest/footprint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (this *FootprintService) Trace(writer http.ResponseWriter, request *http.Re
Params: paramsString,
Cost: int64(duration / time.Millisecond),
Success: success,
Dt: ConvertTimeToDateString(time.Now()),
}

footprint = this.footprintDao.Create(footprint)
Expand Down
10 changes: 10 additions & 0 deletions rest/image_cache_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,13 @@ func (this *ImageCacheDao) DeleteByMatterUuid(matterUuid string) {
}

}

//获取一段时间中文件总大小
func (this *ImageCacheDao) SizeBetweenTime(startTime time.Time, endTime time.Time) int64 {
var size int64
db := CONTEXT.DB.Model(&ImageCache{}).Where("create_time >= ? AND create_time <= ?", startTime, endTime).Select("SUM(size)")
this.PanicError(db.Error)
row := db.Row()
row.Scan(&size)
return size
}
10 changes: 6 additions & 4 deletions rest/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func (this *Logger) log(prefix string, format string, v ...interface{}) {
defer this.Unlock()

//控制台中打印日志
fmt.Printf(format+"\r\n", v...)
var consoleFormat = fmt.Sprintf("%s%s %s\r\n", prefix, ConvertTimeToTimeString(time.Now()), format)
fmt.Printf(consoleFormat, v...)

this.goLogger.SetPrefix(prefix)
this.goLogger.Printf(format, v...)
//每一行我们加上换行符
var fileFormat = fmt.Sprintf("%s\r\n", format)
this.goLogger.Printf(fileFormat, v...)
}

//处理日志的统一方法。
Expand Down Expand Up @@ -101,7 +104,7 @@ func (this *Logger) maintain() {
now := time.Now()
nextTime := FirstSecondOfDay(Tomorrow())
duration := nextTime.Sub(now)
go this.Info("%vs后将进行下一次日志维护 下次时间维护时间:%v ", int64(duration/time.Second), nextTime)
go this.Info("%vs 后将进行下一次日志维护 下次时间维护时间:%v ", int64(duration/time.Second), nextTime)
this.maintainTimer = time.AfterFunc(duration, func() {
go this.maintain()
})
Expand Down Expand Up @@ -133,7 +136,6 @@ func (this *Logger) closeFile() {
panic("尝试关闭日志时出错: " + err.Error())
}
}

}

func (this *Logger) Destroy() {
Expand Down
Loading

0 comments on commit 2d1a955

Please sign in to comment.