Refine the rest structure.

This commit is contained in:
zicla
2019-04-26 11:43:54 +08:00
parent c55e8699b4
commit e88930e13a
49 changed files with 201 additions and 183 deletions

120
code/rest/dashboard_dao.go Normal file
View File

@ -0,0 +1,120 @@
package rest
import (
"github.com/jinzhu/gorm"
"github.com/nu7hatch/gouuid"
"tank/code/tool/builder"
"time"
)
type DashboardDao struct {
BaseDao
}
//创建
func (this *DashboardDao) Create(dashboard *Dashboard) *Dashboard {
timeUUID, _ := uuid.NewV4()
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)
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)
}
//按照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
}
//获取某个文件夹下所有的文件和子文件
func (this *DashboardDao) Page(page int, pageSize int, dt string, sortArray []builder.OrderPair) *Pager {
var wp = &builder.WherePair{}
if dt != "" {
wp = wp.And(&builder.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
}
//获取最活跃的前10个ip
func (this *DashboardDao) ActiveIpTop10() []*DashboardIpTimes {
var dashboardIpTimes []*DashboardIpTimes
sortArray := []builder.OrderPair{
{
key: "times",
value: "DESC",
},
}
rows, err := CONTEXT.DB.Model(&Footprint{}).
Select("ip,COUNT(uuid) as times").
Group("ip").
Order(this.GetSortString(sortArray)).
Offset(0).
Limit(10).
Rows()
this.PanicError(err)
for rows.Next() {
var ip string;
var times int64 = 0;
rows.Scan(&ip, &times)
item := &DashboardIpTimes{
Ip: ip,
Times: times,
}
dashboardIpTimes = append(dashboardIpTimes, item)
}
return dashboardIpTimes
}
//执行清理操作
func (this *DashboardDao) Cleanup() {
this.logger.Info("[DashboardDao]执行清理清除数据库中所有Dashboard记录。")
db := CONTEXT.DB.Where("uuid is not null").Delete(Dashboard{})
this.PanicError(db.Error)
}