History Optimization

This commit is contained in:
aceberg 2025-07-24 16:03:15 +07:00
parent 1f924034b0
commit 5c449f76ba
5 changed files with 15 additions and 19 deletions

2
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/prometheus/client_golang v1.22.0
github.com/spf13/viper v1.20.1
gorm.io/driver/postgres v1.6.0
gorm.io/gorm v1.30.0
gorm.io/gorm v1.30.1
)
require (

4
go.sum
View file

@ -192,8 +192,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
gorm.io/gorm v1.30.1 h1:lSHg33jJTBxs2mgJRfRZeLDG+WZaHYCk3Wtfl6Ngzo4=
gorm.io/gorm v1.30.1/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
modernc.org/cc/v4 v4.26.1 h1:+X5NtzVBn0KgsBCBe+xkDC7twLb/jNVj9FPgiwSQO3s=
modernc.org/cc/v4 v4.26.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=

View file

@ -33,15 +33,14 @@ func apiGetConfig(c *gin.Context) {
func apiHistory(c *gin.Context) {
var hosts []models.Host
histHosts := gdb.Select("history")
mac := c.Param("mac")
if mac != "/" {
mac = mac[1:]
hosts = getHostsByMAC(mac, histHosts)
hosts = gdb.SelectByMAC(mac)
} else {
hosts = histHosts
hosts = gdb.Select("history")
}
c.IndentedJSON(http.StatusOK, hosts)

View file

@ -14,15 +14,3 @@ func getHostByID(idStr string) (oneHost models.Host) {
return oneHost
}
func getHostsByMAC(mac string, hosts []models.Host) (foundHosts []models.Host) {
for _, host := range hosts {
if host.Mac == mac {
foundHosts = append(foundHosts, host)
}
}
return foundHosts
}

View file

@ -14,7 +14,7 @@ func Select(table string) (dbHosts []models.Host) {
return dbHosts
}
// SelectByID - get all hosts
// SelectByID - get host by ID
func SelectByID(id int) (host models.Host) {
tab := db.Table("now")
@ -23,6 +23,15 @@ func SelectByID(id int) (host models.Host) {
return host
}
// SelectByMAC - get all hosts by MAC
func SelectByMAC(mac string) (hosts []models.Host) {
tab := db.Table("history")
tab.Where("MAC = ?", mac).Find(&hosts)
return hosts
}
// Update - update or create host
func Update(table string, oneHost models.Host) {