History GUI upd
This commit is contained in:
parent
e6f3b150ab
commit
595f04540b
6 changed files with 20 additions and 14 deletions
|
|
@ -62,7 +62,7 @@ export const apiPortScan = async (ip:string, port:number) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const apiGetHistory = async (mac:string) => {
|
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();
|
const hosts = await (await fetch(url)).json();
|
||||||
|
|
||||||
return hosts;
|
return hosts;
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
|
@ -41,8 +42,10 @@ func getHistory(c *gin.Context) {
|
||||||
func getHistoryByMAC(c *gin.Context) {
|
func getHistoryByMAC(c *gin.Context) {
|
||||||
|
|
||||||
mac := c.Param("mac")
|
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)
|
c.IndentedJSON(http.StatusOK, hosts)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ func Delete(table string, id int) {
|
||||||
func DeleteOldHistory(date string) int64 {
|
func DeleteOldHistory(date string) int64 {
|
||||||
|
|
||||||
tab := db.Table("history")
|
tab := db.Table("history")
|
||||||
result := tab.Where("DATE < ?", date).Delete(&models.Host{})
|
result := tab.Where("\"DATE\" < ?", date).Delete(&models.Host{})
|
||||||
check.IfError(result.Error)
|
check.IfError(result.Error)
|
||||||
|
|
||||||
return result.RowsAffected
|
return result.RowsAffected
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ func SelectByID(id int) (host models.Host) {
|
||||||
func SelectByMAC(mac string) (hosts []models.Host) {
|
func SelectByMAC(mac string) (hosts []models.Host) {
|
||||||
|
|
||||||
tab := db.Table("history")
|
tab := db.Table("history")
|
||||||
tab.Where("MAC = ?", mac).Find(&hosts)
|
tab.Where("\"MAC\" = ?", mac).Find(&hosts)
|
||||||
|
|
||||||
return hosts
|
return hosts
|
||||||
}
|
}
|
||||||
|
|
@ -36,8 +36,8 @@ func SelectByDate(mac, date string) (hosts []models.Host) {
|
||||||
|
|
||||||
tab := db.Table("history")
|
tab := db.Table("history")
|
||||||
tab.
|
tab.
|
||||||
Where("MAC = ?", mac).
|
Where("\"MAC\" = ?", mac).
|
||||||
Where("DATE LIKE ?", date+"%").
|
Where("\"DATE\" LIKE ?", date+"%").
|
||||||
Find(&hosts)
|
Find(&hosts)
|
||||||
|
|
||||||
return hosts
|
return hosts
|
||||||
|
|
@ -48,8 +48,8 @@ func SelectLatest(mac string, number int) (hosts []models.Host) {
|
||||||
|
|
||||||
tab := db.Table("history")
|
tab := db.Table("history")
|
||||||
tab.
|
tab.
|
||||||
Where("MAC = ?", mac).
|
Where("\"MAC\" = ?", mac).
|
||||||
Order("DATE DESC").
|
Order("\"DATE\" DESC").
|
||||||
Limit(number).
|
Limit(number).
|
||||||
Find(&hosts)
|
Find(&hosts)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
|
|
||||||
"github.com/aceberg/WatchYourLAN/internal/check"
|
"github.com/aceberg/WatchYourLAN/internal/check"
|
||||||
"github.com/aceberg/WatchYourLAN/internal/conf"
|
"github.com/aceberg/WatchYourLAN/internal/conf"
|
||||||
|
|
@ -34,7 +35,13 @@ func Start() {
|
||||||
Colorful: true,
|
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
|
// Choose DB and connect
|
||||||
if conf.AppConfig.UseDB == "postgres" {
|
if conf.AppConfig.UseDB == "postgres" {
|
||||||
|
|
@ -55,10 +62,6 @@ func Start() {
|
||||||
|
|
||||||
db.Exec("PRAGMA journal_mode = wal;")
|
db.Exec("PRAGMA journal_mode = wal;")
|
||||||
db.Exec("PRAGMA busy_timeout = 5000;")
|
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
|
// Migrate the schema
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue