mirror of
https://gitee.com/goploy/goploy.git
synced 2024-12-05 13:38:47 +08:00
92 lines
2.0 KiB
Go
92 lines
2.0 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"`
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// GetCycleValue -
|
|
func (sal ServerAgentLog) GetCycleValue(groupCycle int, formula string) (value string, err error) {
|
|
var builder sq.SelectBuilder
|
|
switch formula {
|
|
case "min":
|
|
builder = sq.
|
|
Select("min(value) as value")
|
|
case "max":
|
|
builder = sq.
|
|
Select("max(value) as value")
|
|
default:
|
|
builder = sq.
|
|
Select("avg(value) as value")
|
|
}
|
|
|
|
err = builder.
|
|
From(serverAgentLogTable).
|
|
Where(sq.Eq{"server_id": sal.ServerId}).
|
|
Where(sq.Eq{"item": sal.Item}).
|
|
Where("now() - interval ? minute", groupCycle).
|
|
RunWith(DB).
|
|
QueryRow().
|
|
Scan(&value)
|
|
return
|
|
}
|
|
|
|
// 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
|
|
}
|