优化代码结构,修复查询逻辑,添加构建脚本
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,6 +8,7 @@
|
|||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
dist/
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
此实现目前并不是完全并发安全的:
|
此实现目前并不是完全并发安全的:
|
||||||
|
- 目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题
|
||||||
目前事务等级没有对 cityhash 表的 offset 字段做防丢失,并发操作可能会出问题
|
|
||||||
@@ -2,8 +2,9 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/gorm"
|
|
||||||
"zzman/model"
|
"zzman/model"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindCities(tx *gorm.DB) ([]model.City, error) {
|
func FindCities(tx *gorm.DB) ([]model.City, error) {
|
||||||
@@ -15,15 +16,24 @@ func FindCities(tx *gorm.DB) ([]model.City, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FindCitiesWithEdgesCount(tx *gorm.DB) ([]model.City, error) {
|
func FindCitiesWithEdgesCount(tx *gorm.DB) ([]model.City, error) {
|
||||||
var cities []model.City
|
var datas []struct {
|
||||||
|
model.City
|
||||||
|
EdgesCount int `gorm:"column:edges_count"`
|
||||||
|
}
|
||||||
err := tx.Debug().
|
err := tx.Debug().
|
||||||
Select("cities.*, COUNT(edges.id) AS edges_count").
|
Select("cityhash.*, COUNT(edge.id) AS edges_count").
|
||||||
Joins("LEFT JOIN edges ON edges.city_id = cities.id").
|
Joins("LEFT JOIN edge ON edge.city_id = cityhash.id").
|
||||||
Group("cities.id").
|
Where("cityhash.label IS NOT NULL").
|
||||||
Find(&cities).Error
|
Group("cityhash.id").
|
||||||
|
Find(&datas).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to find cities with edges count: %w", err)
|
return nil, fmt.Errorf("failed to find cities with edges count: %w", err)
|
||||||
}
|
}
|
||||||
|
var cities = make([]model.City, len(datas))
|
||||||
|
for i, data := range datas {
|
||||||
|
cities[i] = data.City
|
||||||
|
cities[i].EdgesCount = data.EdgesCount
|
||||||
|
}
|
||||||
return cities, nil
|
return cities, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package actions
|
package actions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
|
||||||
"zzman/model"
|
"zzman/model"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindConfigsByGateway(tx *gorm.DB, macaddr string) ([]model.Config, error) {
|
func FindConfigsByGateway(tx *gorm.DB, macaddr string) ([]model.Config, error) {
|
||||||
@@ -20,7 +21,7 @@ func CreateConfigs(tx *gorm.DB, configs []model.Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用事务批量插入配置
|
// 使用事务批量插入配置
|
||||||
return tx.Create(&configs).Error
|
return tx.Omit("createtime", "updatetime").Create(&configs).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateConfigs(tx *gorm.DB, configs []model.ConfigUpdate) error {
|
func UpdateConfigs(tx *gorm.DB, configs []model.ConfigUpdate) error {
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package actions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/gorm"
|
|
||||||
"math"
|
"math"
|
||||||
"zzman/model"
|
"zzman/model"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindGateways(tx *gorm.DB) ([]model.Gateway, error) {
|
func FindGateways(tx *gorm.DB) ([]model.Gateway, error) {
|
||||||
var gateways []model.Gateway
|
var gateways []model.Gateway
|
||||||
err := tx.Find(gateways).Error
|
err := tx.Where("enable = 1").Find(&gateways).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
1
build.ps1
Normal file
1
build.ps1
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$env:GOOS="linux"; $env:GOARCH="amd64"; $env:CGO_ENABLED=0; go build -o dist/zz main.go
|
||||||
15
main.go
15
main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"zzman/actions"
|
"zzman/actions"
|
||||||
"zzman/clients"
|
"zzman/clients"
|
||||||
@@ -16,7 +17,14 @@ func main() {
|
|||||||
|
|
||||||
// 初始化环境
|
// 初始化环境
|
||||||
slog.Debug("初始化环境变量")
|
slog.Debug("初始化环境变量")
|
||||||
err := godotenv.Load()
|
ex, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
exPath := filepath.Dir(ex)
|
||||||
|
file := filepath.Join(exPath, ".env")
|
||||||
|
println("加载环境变量文件:", file)
|
||||||
|
err = godotenv.Load(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error(fmt.Errorf("初始化变量失败:%w", err).Error())
|
slog.Error(fmt.Errorf("初始化变量失败:%w", err).Error())
|
||||||
}
|
}
|
||||||
@@ -43,8 +51,9 @@ func main() {
|
|||||||
|
|
||||||
var args actions.UpdateArgs
|
var args actions.UpdateArgs
|
||||||
if len(os.Args) >= 3 {
|
if len(os.Args) >= 3 {
|
||||||
slices.Contains(os.Args, "--mock")
|
if slices.Contains(os.Args, "--mock") {
|
||||||
args.Mock = true
|
args.Mock = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := actions.Update(model.DB, args)
|
err := actions.Update(model.DB, args)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ type Gateway struct {
|
|||||||
TokenTime string `gorm:"column:token_time"`
|
TokenTime string `gorm:"column:token_time"`
|
||||||
PrivateIp string `gorm:"column:inner_ip"`
|
PrivateIp string `gorm:"column:inner_ip"`
|
||||||
ProxyIp string `gorm:"column:l2ip"`
|
ProxyIp string `gorm:"column:l2ip"`
|
||||||
|
Enable bool `gorm:"column:enable"`
|
||||||
CreateTime string `gorm:"column:createtime"`
|
CreateTime string `gorm:"column:createtime"`
|
||||||
UpdateTime string `gorm:"column:updatetime"`
|
UpdateTime string `gorm:"column:updatetime"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user