added logs truncate endpoint

This commit is contained in:
Gani Georgiev
2026-08-19 10:04:35 +03:00
parent 1a5f29818c
commit e71be7194a
15 changed files with 201 additions and 21 deletions
+22 -1
View File
@@ -11,8 +11,13 @@ import (
// bindLogsApi registers the request logs api endpoints.
func bindLogsApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
sub := rg.Group("/logs").Bind(RequireSuperuserAuth(), SkipSuccessActivityLog())
sub := rg.Group("/logs").Bind(
RequireSuperuserAuth(),
SkipSuccessActivityLog(),
)
sub.GET("", logsList)
sub.DELETE("", logsTruncate)
sub.GET("/stats", logsStats)
sub.GET("/{id}", logsView)
}
@@ -71,3 +76,19 @@ func logsView(e *core.RequestEvent) error {
return e.JSON(http.StatusOK, log)
}
func logsTruncate(e *core.RequestEvent) error {
// delete all rows directly (aka. no model hooks will be fired)
_, err := e.App.AuxNonconcurrentDB().Delete((&core.Log{}).TableName(), nil).Execute()
if err != nil {
return e.InternalServerError("Failed to truncate all logs.", err)
}
// try to free the unused disk space
err = e.App.AuxVacuum()
if err != nil {
e.App.Logger().Warn("Failed to VACUUM aux database", "error", err)
}
return e.NoContent(http.StatusNoContent)
}
+56
View File
@@ -210,3 +210,59 @@ func TestLogsStats(t *testing.T) {
scenario.Test(t)
}
}
func TestLogsTruncate(t *testing.T) {
t.Parallel()
scenarios := []tests.ApiScenario{
{
Name: "unauthorized",
Method: http.MethodDelete,
URL: "/api/logs",
ExpectedStatus: 401,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
},
{
Name: "authorized as regular user",
Method: http.MethodDelete,
URL: "/api/logs",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjRxMXhsY2xtZmxva3UzMyIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoiX3BiX3VzZXJzX2F1dGhfIiwiZXhwIjoyNTI0NjA0NDYxLCJyZWZyZXNoYWJsZSI6dHJ1ZX0.ZT3F0Z3iM-xbGgSG3LEKiEzHrPHr8t8IuHLZGGNuxLo",
},
ExpectedStatus: 403,
ExpectedContent: []string{`"data":{}`},
ExpectedEvents: map[string]int{"*": 0},
},
{
Name: "authorized as superuser",
Method: http.MethodDelete,
URL: "/api/logs",
Headers: map[string]string{
"Authorization": "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6InN5d2JoZWNuaDQ2cmhtMCIsInR5cGUiOiJhdXRoIiwiY29sbGVjdGlvbklkIjoicGJjXzMxNDI2MzU4MjMiLCJleHAiOjI1MjQ2MDQ0NjEsInJlZnJlc2hhYmxlIjp0cnVlfQ.UXgO3j-0BumcugrFjbd7j0M4MQvbrLggLlcu_YNGjoY",
},
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
if err := tests.StubLogsData(app); err != nil {
t.Fatal(err)
}
},
AfterTestFunc: func(t testing.TB, app *tests.TestApp, res *http.Response) {
var found []core.Log
if err := app.LogQuery().All(&found); err != nil {
t.Fatal(err)
}
if len(found) > 0 {
t.Fatalf("Expected all logs to be deleted, found: %v", found)
}
},
ExpectedStatus: 204,
ExpectedEvents: map[string]int{"*": 0},
},
}
for _, scenario := range scenarios {
scenario.Test(t)
}
}