fix: resolve empty configs logging
This commit is contained in:
@@ -21,36 +21,12 @@ func NewActivityLogController(as *service.ActivityLogService, routeGroups *commo
|
||||
errorHandler: error_handler.NewControllerErrorHandler(),
|
||||
}
|
||||
|
||||
// Per-server activity log: GET /server/:id/activity-log
|
||||
routeGroups.ActivityLog.Use(auth.Authenticate)
|
||||
routeGroups.ActivityLog.Get("/", ac.GetServerLogs)
|
||||
|
||||
// Global activity log: GET /activity-log (admin use)
|
||||
routeGroups.Groups.Group("/activity-log").Use(auth.Authenticate).Get("/", ac.GetAllLogs)
|
||||
|
||||
return ac
|
||||
}
|
||||
|
||||
// GetServerLogs returns activity logs for a specific server.
|
||||
//
|
||||
// @Summary Return activity logs for a server
|
||||
// @Tags ActivityLog
|
||||
// @Success 200 {array} model.ActivityLog
|
||||
// @Router /server/{id}/activity-log [get]
|
||||
func (ac *ActivityLogController) GetServerLogs(c *fiber.Ctx) error {
|
||||
var filter model.ActivityLogFilter
|
||||
if err := common.ParseQueryFilter(c, &filter); err != nil {
|
||||
return ac.errorHandler.HandleValidationError(c, err, "query_filter")
|
||||
}
|
||||
|
||||
result, err := ac.service.GetAll(c.UserContext(), &filter)
|
||||
if err != nil {
|
||||
return ac.errorHandler.HandleServiceError(c, err)
|
||||
}
|
||||
|
||||
return c.JSON(result)
|
||||
}
|
||||
|
||||
// GetAllLogs returns activity logs across all servers (admin).
|
||||
//
|
||||
// @Summary Return all activity logs
|
||||
|
||||
@@ -29,6 +29,7 @@ func NewServiceControlController(as *service.ServiceControlService, routeGroups
|
||||
}
|
||||
|
||||
serviceRoutes := routeGroups.Server.Group("/:id/service")
|
||||
serviceRoutes.Use(auth.Authenticate)
|
||||
serviceRoutes.Get("/:service", ac.getStatus)
|
||||
serviceRoutes.Post("/start", ac.startServer)
|
||||
serviceRoutes.Post("/stop", ac.stopServer)
|
||||
|
||||
@@ -20,6 +20,7 @@ const (
|
||||
type ActivityLog struct {
|
||||
ID uuid.UUID `json:"id" gorm:"type:uuid;primary_key;"`
|
||||
ServerID uuid.UUID `json:"serverId" gorm:"not null;type:uuid;index"`
|
||||
Server *Server `json:"server,omitempty" gorm:"foreignKey:ServerID"`
|
||||
UserID string `json:"userId" gorm:"not null"`
|
||||
Username string `json:"username" gorm:"not null"`
|
||||
Action ActionType `json:"action" gorm:"not null"`
|
||||
@@ -39,10 +40,10 @@ func (a *ActivityLog) BeforeCreate(tx *gorm.DB) error {
|
||||
|
||||
type ActivityLogFilter struct {
|
||||
BaseFilter
|
||||
ServerBasedFilter
|
||||
DateRangeFilter
|
||||
UserID string `query:"user_id"`
|
||||
Action ActionType `query:"action"`
|
||||
UserID string `query:"user_id"`
|
||||
Action ActionType `query:"action"`
|
||||
ServerID string `query:"server_id"`
|
||||
}
|
||||
|
||||
func (f *ActivityLogFilter) ApplyFilter(query *gorm.DB) *gorm.DB {
|
||||
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -24,7 +25,27 @@ func (r *ActivityLogRepository) Insert(ctx context.Context, entry *model.Activit
|
||||
}
|
||||
|
||||
func (r *ActivityLogRepository) GetAll(ctx context.Context, filter *model.ActivityLogFilter) (*[]model.ActivityLog, error) {
|
||||
return r.BaseRepository.GetAll(ctx, filter)
|
||||
result := &[]model.ActivityLog{}
|
||||
query := r.BaseRepository.db.WithContext(ctx).
|
||||
Model(&model.ActivityLog{}).
|
||||
Preload("Server")
|
||||
|
||||
query = filter.ApplyFilter(query)
|
||||
|
||||
offset, limit := filter.Pagination()
|
||||
query = query.Offset(offset).Limit(limit)
|
||||
|
||||
field, desc := filter.GetSorting()
|
||||
if desc {
|
||||
query = query.Order(field + " DESC")
|
||||
} else {
|
||||
query = query.Order(field)
|
||||
}
|
||||
|
||||
if err := query.Find(result).Error; err != nil {
|
||||
return nil, fmt.Errorf("error getting activity logs: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type lastActivityRow struct {
|
||||
|
||||
@@ -57,6 +57,9 @@ func BuildConfigDetails(file string, oldJSON, newJSON []byte) string {
|
||||
}
|
||||
}
|
||||
|
||||
if len(changedKeys) == 0 {
|
||||
return ""
|
||||
}
|
||||
keysJSON, _ := json.Marshal(changedKeys)
|
||||
return fmt.Sprintf(`{"file":%q,"changed_keys":%s}`, file, string(keysJSON))
|
||||
}
|
||||
|
||||
@@ -157,16 +157,17 @@ func (as *ConfigService) UpdateAllConfigs(ctx *fiber.Ctx, req *model.Configurati
|
||||
results = append(results, result)
|
||||
|
||||
if as.activityLog != nil {
|
||||
details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData)
|
||||
go func(details string) {
|
||||
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{
|
||||
ServerID: serverUUID,
|
||||
UserID: actorID,
|
||||
Username: actorUsername,
|
||||
Action: model.ActionConfigUpdate,
|
||||
Details: details,
|
||||
})
|
||||
}(details)
|
||||
if details := BuildConfigDetails(entry.fileName, oldDataUTF8, newData); details != "" {
|
||||
go func(details string) {
|
||||
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{
|
||||
ServerID: serverUUID,
|
||||
UserID: actorID,
|
||||
Username: actorUsername,
|
||||
Action: model.ActionConfigUpdate,
|
||||
Details: details,
|
||||
})
|
||||
}(details)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,16 +282,17 @@ func (as *ConfigService) updateConfigInternal(ctx context.Context, serverID stri
|
||||
as.serverService.StartAccServerRuntime(server)
|
||||
|
||||
if as.activityLog != nil {
|
||||
details := BuildConfigDetails(configFile, oldDataUTF8, newData)
|
||||
go func() {
|
||||
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{
|
||||
ServerID: serverUUID,
|
||||
UserID: actorID,
|
||||
Username: actorUsername,
|
||||
Action: model.ActionConfigUpdate,
|
||||
Details: details,
|
||||
})
|
||||
}()
|
||||
if details := BuildConfigDetails(configFile, oldDataUTF8, newData); details != "" {
|
||||
go func() {
|
||||
_ = as.activityLog.Log(context.Background(), &model.ActivityLog{
|
||||
ServerID: serverUUID,
|
||||
UserID: actorID,
|
||||
Username: actorUsername,
|
||||
Action: model.ActionConfigUpdate,
|
||||
Details: details,
|
||||
})
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
return as.repository.UpdateConfig(ctx, &model.Config{
|
||||
|
||||
Reference in New Issue
Block a user