From 8214744e92054392cf3c74a893b027a94100c474 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Tue, 15 Nov 2022 18:44:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/logs.go | 17 +++++++++++++ backend/app/repo/logs.go | 10 ++++++++ backend/app/service/logs.go | 9 +++++++ backend/constant/dir.go | 1 + backend/router/ro_log.go | 1 + frontend/src/api/modules/log.ts | 4 ++++ frontend/src/lang/modules/zh.ts | 1 + frontend/src/views/log/login/index.vue | 28 +++++++++++++++++++++- frontend/src/views/log/operation/index.vue | 28 +++++++++++++++++++++- 9 files changed, 97 insertions(+), 2 deletions(-) diff --git a/backend/app/api/v1/logs.go b/backend/app/api/v1/logs.go index 48b0eaeb2..a31ee0ac5 100644 --- a/backend/app/api/v1/logs.go +++ b/backend/app/api/v1/logs.go @@ -1,6 +1,8 @@ package v1 import ( + "errors" + "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/constant" @@ -44,3 +46,18 @@ func (b *BaseApi) GetOperationLogs(c *gin.Context) { Total: total, }) } + +func (b *BaseApi) CleanLogs(c *gin.Context) { + logtype, ok := c.Params.Get("logtype") + if !ok { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, errors.New("error logtype in path")) + return + } + + if err := logService.CleanLogs(logtype); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + + helper.SuccessWithData(c, nil) +} diff --git a/backend/app/repo/logs.go b/backend/app/repo/logs.go index 46ef67ebd..05ffac772 100644 --- a/backend/app/repo/logs.go +++ b/backend/app/repo/logs.go @@ -8,9 +8,11 @@ import ( type LogRepo struct{} type ILogRepo interface { + CleanLogin() error CreateLoginLog(user *model.LoginLog) error PageLoginLog(limit, offset int, opts ...DBOption) (int64, []model.LoginLog, error) + CleanOperation() error CreateOperationLog(user *model.OperationLog) error PageOperationLog(limit, offset int, opts ...DBOption) (int64, []model.OperationLog, error) } @@ -19,6 +21,10 @@ func NewILogRepo() ILogRepo { return &LogRepo{} } +func (u *LogRepo) CleanLogin() error { + return global.DB.Exec("delete from login_logs;").Error +} + func (u *LogRepo) CreateLoginLog(log *model.LoginLog) error { return global.DB.Create(log).Error } @@ -35,6 +41,10 @@ func (u *LogRepo) PageLoginLog(page, size int, opts ...DBOption) (int64, []model return count, ops, err } +func (u *LogRepo) CleanOperation() error { + return global.DB.Exec("delete from operation_logs").Error +} + func (u *LogRepo) CreateOperationLog(log *model.OperationLog) error { return global.DB.Create(log).Error } diff --git a/backend/app/service/logs.go b/backend/app/service/logs.go index 6b19b640e..56fff6582 100644 --- a/backend/app/service/logs.go +++ b/backend/app/service/logs.go @@ -19,6 +19,8 @@ type ILogService interface { CreateOperationLog(operation model.OperationLog) error PageOperationLog(search dto.PageInfo) (int64, interface{}, error) + + CleanLogs(logtype string) error } func NewILogService() ILogService { @@ -70,6 +72,13 @@ func (u *LogService) PageOperationLog(search dto.PageInfo) (int64, interface{}, return total, dtoOps, err } +func (u *LogService) CleanLogs(logtype string) error { + if logtype == "operation" { + return logRepo.CleanOperation() + } + return logRepo.CleanLogin() +} + func filterSensitive(vars string) string { var Sensitives = []string{"password", "Password", "credential", "privateKey"} ops := make(map[string]interface{}) diff --git a/backend/constant/dir.go b/backend/constant/dir.go index a0098a519..532e35fbc 100644 --- a/backend/constant/dir.go +++ b/backend/constant/dir.go @@ -6,6 +6,7 @@ import ( var ( DefaultDataDir = "/opt/1Panel/data" + LogDir = "opt/1Panel/log" ResourceDir = path.Join(DefaultDataDir, "resource") AppResourceDir = path.Join(ResourceDir, "apps") AppInstallDir = path.Join(DefaultDataDir, "apps") diff --git a/backend/router/ro_log.go b/backend/router/ro_log.go index 236b2b68a..41b729c02 100644 --- a/backend/router/ro_log.go +++ b/backend/router/ro_log.go @@ -16,5 +16,6 @@ func (s *LogRouter) InitLogRouter(Router *gin.RouterGroup) { { operationRouter.POST("login", baseApi.GetLoginLogs) operationRouter.POST("operation", baseApi.GetOperationLogs) + operationRouter.POST("clean/:logtype", baseApi.CleanLogs) } } diff --git a/frontend/src/api/modules/log.ts b/frontend/src/api/modules/log.ts index 9167e99d2..4c0bdee35 100644 --- a/frontend/src/api/modules/log.ts +++ b/frontend/src/api/modules/log.ts @@ -9,3 +9,7 @@ export const getOperationLogs = (info: ReqPage) => { export const getLoginLogs = (info: ReqPage) => { return http.post>(`/logs/login`, info); }; + +export const cleanLogs = (logtype: string) => { + return http.post(`/logs/clean/${logtype}`); +}; diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 5ed60b4c9..b733745fc 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -495,6 +495,7 @@ export default { loginAgent: '用户代理', loginStatus: '登录状态', system: '系统日志', + deleteLogs: '清空日志', detail: { users: '用户', hosts: '主机', diff --git a/frontend/src/views/log/login/index.vue b/frontend/src/views/log/login/index.vue index 8925c2905..b78ff67f5 100644 --- a/frontend/src/views/log/login/index.vue +++ b/frontend/src/views/log/login/index.vue @@ -3,6 +3,12 @@ + + @@ -26,17 +32,22 @@ /> +