103 lines
2.8 KiB
Go
103 lines
2.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
"hy2xs-admin/model/constant"
|
|
"hy2xs-admin/model/entity"
|
|
)
|
|
|
|
func SaveTrafficSample(sample entity.TrafficSample) error {
|
|
if tx := sqliteDB.Save(&sample); tx.Error != nil {
|
|
logrus.Errorf("%v", tx.Error)
|
|
return errors.New(constant.SysError)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpsertTrafficAggregateHourly(peerId int64, hourStart int64, rxBytes int64, txBytes int64) error {
|
|
if rxBytes == 0 && txBytes == 0 {
|
|
return nil
|
|
}
|
|
agg := entity.TrafficAggregateHourly{
|
|
PeerId: &peerId,
|
|
HourStart: &hourStart,
|
|
RxBytes: &rxBytes,
|
|
TxBytes: &txBytes,
|
|
}
|
|
if tx := sqliteDB.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "peer_id"}, {Name: "hour_start"}},
|
|
DoUpdates: clause.Assignments(map[string]interface{}{
|
|
"rx_bytes": gormExprAdd("rx_bytes", rxBytes),
|
|
"tx_bytes": gormExprAdd("tx_bytes", txBytes),
|
|
}),
|
|
}).Create(&agg); tx.Error != nil {
|
|
logrus.Errorf("%v", tx.Error)
|
|
return errors.New(constant.SysError)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func UpsertTrafficAggregateDaily(peerId int64, dayStart int64, rxBytes int64, txBytes int64) error {
|
|
if rxBytes == 0 && txBytes == 0 {
|
|
return nil
|
|
}
|
|
agg := entity.TrafficAggregateDaily{
|
|
PeerId: &peerId,
|
|
DayStart: &dayStart,
|
|
RxBytes: &rxBytes,
|
|
TxBytes: &txBytes,
|
|
}
|
|
if tx := sqliteDB.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "peer_id"}, {Name: "day_start"}},
|
|
DoUpdates: clause.Assignments(map[string]interface{}{
|
|
"rx_bytes": gormExprAdd("rx_bytes", rxBytes),
|
|
"tx_bytes": gormExprAdd("tx_bytes", txBytes),
|
|
}),
|
|
}).Create(&agg); tx.Error != nil {
|
|
logrus.Errorf("%v", tx.Error)
|
|
return errors.New(constant.SysError)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func gormExprAdd(column string, delta int64) interface{} {
|
|
return gorm.Expr(fmt.Sprintf("%s + ?", column), delta)
|
|
}
|
|
|
|
func CleanupTrafficSample(olderThanMs int64) error {
|
|
if !tableExists("traffic_sample") {
|
|
return nil
|
|
}
|
|
if tx := sqliteDB.Exec("DELETE FROM traffic_sample WHERE sampled_at < ?", olderThanMs); tx.Error != nil {
|
|
logrus.Errorf("%v", tx.Error)
|
|
return errors.New(constant.SysError)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CleanupTrafficAggregateHourly(olderThanMs int64) error {
|
|
if !tableExists("traffic_aggregate_hourly") {
|
|
return nil
|
|
}
|
|
if tx := sqliteDB.Exec("DELETE FROM traffic_aggregate_hourly WHERE hour_start < ?", olderThanMs); tx.Error != nil {
|
|
logrus.Errorf("%v", tx.Error)
|
|
return errors.New(constant.SysError)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CleanupTrafficAggregateDaily(olderThanMs int64) error {
|
|
if !tableExists("traffic_aggregate_daily") {
|
|
return nil
|
|
}
|
|
if tx := sqliteDB.Exec("DELETE FROM traffic_aggregate_daily WHERE day_start < ?", olderThanMs); tx.Error != nil {
|
|
logrus.Errorf("%v", tx.Error)
|
|
return errors.New(constant.SysError)
|
|
}
|
|
return nil
|
|
}
|