show changes feature

This commit is contained in:
Alireza Ahmadi
2024-06-08 17:49:03 +02:00
parent 1b9d5e9378
commit 12fe21906e
12 changed files with 254 additions and 6 deletions
+6
View File
@@ -157,6 +157,12 @@ func (a *APIHandler) getHandler(c *gin.Context) {
level := c.Query("l")
logs := a.ServerService.GetLogs(service, count, level)
jsonObj(c, logs, nil)
case "changes":
actor := c.Query("a")
chngKey := c.Query("k")
count := c.Query("c")
changes := a.ConfigService.GetChanges(actor, chngKey, count)
jsonObj(c, changes, nil)
default:
jsonMsg(c, "API call", nil)
}
+12
View File
@@ -28,6 +28,13 @@ func migrateDb() {
}()
fmt.Println("Start migrating database...")
err = migrateClientSchema(tx)
if err != nil {
log.Fatal(err)
}
err = changesObj(tx)
if err != nil {
log.Fatal(err)
}
fmt.Println("Migration done!")
}
@@ -81,5 +88,10 @@ func migrateClientSchema(db *gorm.DB) error {
}
}
}
db.AutoMigrate(model.Client{})
return nil
}
func changesObj(db *gorm.DB) error {
return db.Exec("UPDATE changes SET obj = CAST('\"' || CAST(obj AS TEXT) || '\"' AS BLOB) WHERE actor = ? and obj not like ?", "DepleteJob", "\"%\"").Error
}
+1 -1
View File
@@ -70,7 +70,7 @@ func (s *ClientService) DepleteClients() ([]string, []string, error) {
Actor: "DepleteJob",
Key: "clients",
Action: "disable",
Obj: json.RawMessage(client.Name),
Obj: json.RawMessage("\"" + client.Name + "\""),
})
}
+19
View File
@@ -6,6 +6,7 @@ import (
"s-ui/config"
"s-ui/database"
"s-ui/database/model"
"s-ui/logger"
"s-ui/singbox"
"strconv"
"time"
@@ -342,3 +343,21 @@ func (s *ConfigService) contains(slice []string, item string) bool {
}
return false
}
func (s *ConfigService) GetChanges(actor string, chngKey string, count string) []model.Changes {
c, _ := strconv.Atoi(count)
whereString := "`id`>0"
if len(actor) > 0 {
whereString += " and `actor`='" + actor + "'"
}
if len(chngKey) > 0 {
whereString += " and `key`='" + chngKey + "'"
}
db := database.GetDB()
var chngs []model.Changes
err := db.Model(model.Changes{}).Where(whereString).Order("`id` desc").Limit(c).Scan(&chngs).Error
if err != nil {
logger.Warning(err)
}
return chngs
}