goploy/model/ServerAgentLogModel.go
2021-11-09 14:12:28 +08:00

72 lines
1.5 KiB
Go

package model
import (
sq "github.com/Masterminds/squirrel"
)
const serverAgentLogTable = "`server_agent_log`"
// ServerAgentLog -
type ServerAgentLog struct {
ServerId int64 `json:"serverId,omitempty"`
Type int `json:"type,omitempty"`
Item string `json:"item"`
Value string `json:"value"`
ReportTime string `json:"reportTime"`
}
const (
Undefined = iota
CPUUsage
RAMUsage
)
// ServerAgentLogs -
type ServerAgentLogs []ServerAgentLog
// GetListBetweenTime -
func (sal ServerAgentLog) GetListBetweenTime(low, high string) (ServerAgentLogs, error) {
rows, err := sq.
Select(
"item",
"value",
"report_time",
).
From(serverAgentLogTable).
Where(sq.Eq{"server_id": sal.ServerId}).
Where(sq.Eq{"type": sal.Type}).
Where(sq.Expr("report_time BETWEEN ? AND ?", low, high)).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
serverAgentLogs := ServerAgentLogs{}
for rows.Next() {
var serverAgentLog ServerAgentLog
if err := rows.Scan(
&serverAgentLog.Item,
&serverAgentLog.Value,
&serverAgentLog.ReportTime,
); err != nil {
return nil, err
}
serverAgentLogs = append(serverAgentLogs, serverAgentLog)
}
return serverAgentLogs, nil
}
// AddRow return LastInsertId
func (sal ServerAgentLog) AddRow() error {
_, err := sq.
Insert(serverAgentLogTable).
Columns("server_id", "type", "item", "value", "report_time").
Values(sal.ServerId, sal.Type, sal.Item, sal.Value, sal.ReportTime).
RunWith(DB).
Exec()
if err != nil {
return err
}
return err
}