104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package service
|
|
|
|
import (
|
|
"s-ui/database"
|
|
"s-ui/database/model"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type onlines struct {
|
|
Inbound []string `json:"inbound,omitempty"`
|
|
User []string `json:"user,omitempty"`
|
|
Outbound []string `json:"outbound,omitempty"`
|
|
}
|
|
|
|
var onlineResources = &onlines{}
|
|
|
|
type StatsService struct {
|
|
}
|
|
|
|
func (s *StatsService) SaveStats() error {
|
|
if !corePtr.IsRunning() {
|
|
return nil
|
|
}
|
|
stats := corePtr.GetInstance().StatsTracker().GetStats()
|
|
|
|
// Reset onlines
|
|
onlineResources.Inbound = nil
|
|
onlineResources.Outbound = nil
|
|
onlineResources.User = nil
|
|
|
|
if len(*stats) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var err error
|
|
db := database.GetDB()
|
|
tx := db.Begin()
|
|
defer func() {
|
|
if err == nil {
|
|
tx.Commit()
|
|
} else {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
for _, stat := range *stats {
|
|
if stat.Resource == "user" {
|
|
if stat.Direction {
|
|
err = tx.Model(model.Client{}).Where("name = ?", stat.Tag).
|
|
UpdateColumn("up", gorm.Expr("up + ?", stat.Traffic)).Error
|
|
} else {
|
|
err = tx.Model(model.Client{}).Where("name = ?", stat.Tag).
|
|
UpdateColumn("down", gorm.Expr("down + ?", stat.Traffic)).Error
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if stat.Direction {
|
|
switch stat.Resource {
|
|
case "inbound":
|
|
onlineResources.Inbound = append(onlineResources.Inbound, stat.Tag)
|
|
case "outbound":
|
|
onlineResources.Outbound = append(onlineResources.Outbound, stat.Tag)
|
|
case "user":
|
|
onlineResources.User = append(onlineResources.User, stat.Tag)
|
|
}
|
|
}
|
|
}
|
|
|
|
err = tx.Create(&stats).Error
|
|
return err
|
|
}
|
|
|
|
func (s *StatsService) GetStats(resource string, tag string, limit int) ([]model.Stats, error) {
|
|
var err error
|
|
var result []model.Stats
|
|
|
|
currentTime := time.Now().Unix()
|
|
timeDiff := currentTime - (int64(limit) * 3600)
|
|
|
|
db := database.GetDB()
|
|
resources := []string{resource}
|
|
if resource == "endpoint" {
|
|
resources = []string{"inbound", "outbound"}
|
|
}
|
|
err = db.Model(model.Stats{}).Where("resource in ? AND tag = ? AND date_time > ?", resources, tag, timeDiff).Scan(&result).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *StatsService) GetOnlines() (onlines, error) {
|
|
return *onlineResources, nil
|
|
}
|
|
func (s *StatsService) DelOldStats(days int) error {
|
|
oldTime := time.Now().AddDate(0, 0, -(days)).Unix()
|
|
db := database.GetDB()
|
|
return db.Where("date_time < ?", oldTime).Delete(model.Stats{}).Error
|
|
}
|