History GUI upd

This commit is contained in:
aceberg 2025-07-26 02:27:56 +07:00
parent e6f3b150ab
commit 595f04540b
6 changed files with 20 additions and 14 deletions

View file

@ -62,7 +62,7 @@ export const apiPortScan = async (ip:string, port:number) => {
};
export const apiGetHistory = async (mac:string) => {
const url = apiPath+'/api/history/'+mac;
const url = apiPath+'/api/history/'+mac+'/?num=200';
const hosts = await (await fetch(url)).json();
return hosts;

View file

@ -3,6 +3,7 @@ package api
import (
"log/slog"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
@ -41,8 +42,10 @@ func getHistory(c *gin.Context) {
func getHistoryByMAC(c *gin.Context) {
mac := c.Param("mac")
numStr := c.Query("num")
num, _ := strconv.Atoi(numStr)
hosts := gdb.SelectLatest(mac, 200)
hosts := gdb.SelectLatest(mac, num)
c.IndentedJSON(http.StatusOK, hosts)
}

View file

@ -25,7 +25,7 @@ func Delete(table string, id int) {
func DeleteOldHistory(date string) int64 {
tab := db.Table("history")
result := tab.Where("DATE < ?", date).Delete(&models.Host{})
result := tab.Where("\"DATE\" < ?", date).Delete(&models.Host{})
check.IfError(result.Error)
return result.RowsAffected

View file

@ -26,7 +26,7 @@ func SelectByID(id int) (host models.Host) {
func SelectByMAC(mac string) (hosts []models.Host) {
tab := db.Table("history")
tab.Where("MAC = ?", mac).Find(&hosts)
tab.Where("\"MAC\" = ?", mac).Find(&hosts)
return hosts
}
@ -36,8 +36,8 @@ func SelectByDate(mac, date string) (hosts []models.Host) {
tab := db.Table("history")
tab.
Where("MAC = ?", mac).
Where("DATE LIKE ?", date+"%").
Where("\"MAC\" = ?", mac).
Where("\"DATE\" LIKE ?", date+"%").
Find(&hosts)
return hosts
@ -48,8 +48,8 @@ func SelectLatest(mac string, number int) (hosts []models.Host) {
tab := db.Table("history")
tab.
Where("MAC = ?", mac).
Order("DATE DESC").
Where("\"MAC\" = ?", mac).
Order("\"DATE\" DESC").
Limit(number).
Find(&hosts)

View file

@ -11,6 +11,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"github.com/aceberg/WatchYourLAN/internal/check"
"github.com/aceberg/WatchYourLAN/internal/conf"
@ -34,7 +35,13 @@ func Start() {
Colorful: true,
},
)
gormConf := &gorm.Config{Logger: newLogger}
gormConf := &gorm.Config{
Logger: newLogger,
NamingStrategy: schema.NamingStrategy{
NoLowerCase: true,
// So upper case Columns could work in both PostgreSQL and SQLite
},
}
// Choose DB and connect
if conf.AppConfig.UseDB == "postgres" {
@ -55,10 +62,6 @@ func Start() {
db.Exec("PRAGMA journal_mode = wal;")
db.Exec("PRAGMA busy_timeout = 5000;")
// sqlDB, _ := db.DB()
// sqlDB.SetMaxOpenConns(1) // only one writer at a time
// sqlDB.SetMaxIdleConns(1)
// sqlDB.SetConnMaxLifetime(time.Minute * 1)
}
// Migrate the schema

File diff suppressed because one or more lines are too long