goploy/model/MonitorModel.go

218 lines
4.8 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package model
import (
"errors"
2020-08-04 20:00:21 +08:00
2020-08-04 14:28:25 +08:00
sq "github.com/Masterminds/squirrel"
)
const monitorTable = "`monitor`"
2020-08-15 13:38:06 +08:00
// Monitor -
2020-08-04 14:28:25 +08:00
type Monitor struct {
ID int64 `json:"id"`
NamespaceID int64 `json:"namespaceId"`
Name string `json:"name"`
Domain string `json:"domain"`
Port int `json:"port"`
Second int `json:"second"`
Times uint16 `json:"times"`
NotifyType uint8 `json:"notifyType"`
NotifyTarget string `json:"notifyTarget"`
2020-09-11 15:09:38 +08:00
NotifyTimes uint16 `json:"notifyTimes"`
2020-08-04 14:28:25 +08:00
Description string `json:"description"`
2020-09-11 15:09:38 +08:00
ErrorContent string `json:"errorContent"`
2020-08-04 14:28:25 +08:00
State uint8 `json:"state"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
2020-08-15 13:38:06 +08:00
// Monitors -
2020-08-04 14:28:25 +08:00
type Monitors []Monitor
2020-08-15 13:38:06 +08:00
// GetList -
2020-08-04 14:28:25 +08:00
func (m Monitor) GetList(pagination Pagination) (Monitors, error) {
rows, err := sq.
2020-09-11 15:09:38 +08:00
Select("id, name, domain, port, second, times, notify_type, notify_target, notify_times, description, error_content, state, insert_time, update_time").
2020-08-04 14:28:25 +08:00
From(monitorTable).
Where(sq.Eq{
"namespace_id": m.NamespaceID,
}).
Limit(pagination.Rows).
Offset((pagination.Page - 1) * pagination.Rows).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, err
}
monitors := Monitors{}
for rows.Next() {
var monitor Monitor
if err := rows.Scan(
&monitor.ID,
&monitor.Name,
&monitor.Domain,
&monitor.Port,
&monitor.Second,
&monitor.Times,
&monitor.NotifyType,
&monitor.NotifyTarget,
2020-09-11 15:09:38 +08:00
&monitor.NotifyTimes,
2020-08-04 14:28:25 +08:00
&monitor.Description,
2020-09-11 15:09:38 +08:00
&monitor.ErrorContent,
2020-08-04 14:28:25 +08:00
&monitor.State,
&monitor.InsertTime,
&monitor.UpdateTime); err != nil {
return nil, err
}
monitors = append(monitors, monitor)
}
return monitors, nil
}
2020-08-15 13:38:06 +08:00
// GetTotal -
2020-08-04 14:28:25 +08:00
func (m Monitor) GetTotal() (int64, error) {
var total int64
err := sq.
Select("COUNT(*) AS count").
From(monitorTable).
Where(sq.Eq{
"namespace_id": m.NamespaceID,
}).
RunWith(DB).
QueryRow().
Scan(&total)
if err != nil {
return 0, err
}
return total, nil
}
2020-08-15 13:38:06 +08:00
// GetData -
2020-08-04 14:28:25 +08:00
func (m Monitor) GetData() (Monitor, error) {
var monitor Monitor
err := sq.
2020-09-11 15:09:38 +08:00
Select("id, name, domain, ip, port, second, times, notify_type, notify_target, notify_times, state").
2020-08-04 14:28:25 +08:00
From(monitorTable).
Where(sq.Eq{"id": m.ID}).
OrderBy("id DESC").
RunWith(DB).
QueryRow().
2020-09-11 15:09:38 +08:00
Scan(&monitor.ID, &monitor.Name, &monitor.Domain, &monitor.Port, &monitor.Second, &monitor.Times, &monitor.NotifyType, &monitor.NotifyTarget, &monitor.NotifyTimes, &monitor.State)
2020-08-04 14:28:25 +08:00
if err != nil {
return monitor, errors.New("数据查询失败")
}
return monitor, nil
}
2020-08-04 20:00:21 +08:00
// GetAllByState -
2020-08-04 14:28:25 +08:00
func (m Monitor) GetAllByState() (Monitors, error) {
rows, err := sq.
2020-09-11 15:09:38 +08:00
Select("id, name, domain, port, second, times, notify_type, notify_target, notify_times, description").
2020-08-04 14:28:25 +08:00
From(monitorTable).
Where(sq.Eq{
"state": m.State,
}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
monitors := Monitors{}
for rows.Next() {
var monitor Monitor
if err := rows.Scan(
&monitor.ID,
&monitor.Name,
&monitor.Domain,
&monitor.Port,
&monitor.Second,
&monitor.Times,
&monitor.NotifyType,
&monitor.NotifyTarget,
2020-09-11 15:09:38 +08:00
&monitor.NotifyTimes,
2020-08-04 14:28:25 +08:00
&monitor.Description); err != nil {
return nil, err
}
monitors = append(monitors, monitor)
}
return monitors, nil
}
2020-08-15 13:38:06 +08:00
// AddRow return LastInsertId
2020-08-04 14:28:25 +08:00
func (m Monitor) AddRow() (int64, error) {
result, err := sq.
Insert(monitorTable).
2020-09-11 15:09:38 +08:00
Columns("namespace_id", "name", "domain", "port", "second", "times", "notify_type", "notify_target", "notify_times", "description").
Values(m.NamespaceID, m.Name, m.Domain, m.Port, m.Second, m.Times, m.NotifyType, m.NotifyTarget, m.NotifyTimes, m.Description).
2020-08-04 14:28:25 +08:00
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
2020-08-15 13:38:06 +08:00
// EditRow -
2020-08-04 14:28:25 +08:00
func (m Monitor) EditRow() error {
_, err := sq.
Update(monitorTable).
SetMap(sq.Eq{
"name": m.Name,
"domain": m.Domain,
"port": m.Port,
"second": m.Second,
"times": m.Times,
"notify_type": m.NotifyType,
"notify_target": m.NotifyTarget,
2020-09-11 15:09:38 +08:00
"notify_times": m.NotifyTimes,
2020-08-04 14:28:25 +08:00
"description": m.Description,
}).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-04 20:00:21 +08:00
// ToggleState -
2020-08-04 14:28:25 +08:00
func (m Monitor) ToggleState() error {
_, err := sq.
Update(monitorTable).
SetMap(sq.Eq{
"state": sq.Expr("!state"),
}).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// DeleteRow -
2020-08-04 14:28:25 +08:00
func (m Monitor) DeleteRow() error {
_, err := sq.
Delete(monitorTable).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}
2020-09-11 15:09:38 +08:00
// TurnOff -
func (m Monitor) TurnOff(errorContent string) error {
_, err := sq.
Update(monitorTable).
SetMap(sq.Eq{
"state": Disable,
"error_content": errorContent,
}).
Where(sq.Eq{"id": m.ID}).
RunWith(DB).
Exec()
return err
}