From 337dad2ca67ac14a26f33892d1924b5dd9299c24 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 21 Mar 2025 17:17:24 +0800 Subject: [PATCH] fix: Operation logs Add node information (#8212) --- core/app/api/v2/command.go | 6 +-- core/app/dto/logs.go | 7 ++-- core/app/model/logs.go | 1 + core/app/repo/common.go | 5 +++ core/app/service/command.go | 14 +++++-- core/app/service/logs.go | 3 ++ core/i18n/i18n.go | 4 +- core/init/migration/migrate.go | 1 + core/init/migration/migrations/init.go | 10 +++++ core/middleware/operation.go | 2 + .../log/custom-hightlight/index.vue | 2 +- .../src/components/system-upgrade/index.vue | 3 +- frontend/src/layout/index.vue | 22 ++++++---- frontend/src/views/cronjob/cronjob/index.vue | 1 + .../views/cronjob/cronjob/record/index.vue | 1 - frontend/src/views/cronjob/library/index.vue | 2 +- .../views/cronjob/library/operate/index.vue | 1 + .../database/postgresql/setting/index.vue | 6 ++- .../src/views/database/redis/remote/index.vue | 2 +- .../views/database/redis/setting/index.vue | 6 ++- frontend/src/views/log/operation/index.vue | 42 +++++++++++++++++-- frontend/src/views/log/system/index.vue | 9 +++- .../src/views/login/components/login-form.vue | 2 + frontend/src/views/setting/panel/index.vue | 2 +- .../src/views/terminal/terminal/index.vue | 6 +-- 25 files changed, 121 insertions(+), 39 deletions(-) diff --git a/core/app/api/v2/command.go b/core/app/api/v2/command.go index 33906d11b..900a29c66 100644 --- a/core/app/api/v2/command.go +++ b/core/app/api/v2/command.go @@ -137,11 +137,7 @@ func (b *BaseApi) UpdateCommand(c *gin.Context) { return } - upMap := make(map[string]interface{}) - upMap["name"] = req.Name - upMap["group_id"] = req.GroupID - upMap["command"] = req.Command - if err := commandService.Update(req.ID, upMap); err != nil { + if err := commandService.Update(req); err != nil { helper.InternalServer(c, err) return } diff --git a/core/app/dto/logs.go b/core/app/dto/logs.go index 8831801db..16a71ef84 100644 --- a/core/app/dto/logs.go +++ b/core/app/dto/logs.go @@ -5,9 +5,9 @@ import ( ) type OperationLog struct { - ID uint `json:"id"` - Source string `json:"source"` - + ID uint `json:"id"` + Source string `json:"source"` + Node string `json:"node"` IP string `json:"ip"` Path string `json:"path"` Method string `json:"method"` @@ -26,6 +26,7 @@ type SearchOpLogWithPage struct { PageInfo Source string `json:"source"` Status string `json:"status"` + Node string `json:"node"` Operation string `json:"operation"` } diff --git a/core/app/model/logs.go b/core/app/model/logs.go index 490b6a48c..99e26c43f 100644 --- a/core/app/model/logs.go +++ b/core/app/model/logs.go @@ -8,6 +8,7 @@ type OperationLog struct { BaseModel Source string `json:"source"` IP string `json:"ip"` + Node string `json:"node"` Path string `json:"path"` Method string `json:"method"` UserAgent string `json:"userAgent"` diff --git a/core/app/repo/common.go b/core/app/repo/common.go index 627da80dd..0b3046609 100644 --- a/core/app/repo/common.go +++ b/core/app/repo/common.go @@ -55,6 +55,11 @@ func WithByStatus(status string) global.DBOption { return g.Where("status = ?", status) } } +func WithByNode(node string) global.DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("node = ?", node) + } +} func WithByGroupBelong(group string) global.DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("group_belong = ?", group) diff --git a/core/app/service/command.go b/core/app/service/command.go index d99d28e5a..09bbaeb4f 100644 --- a/core/app/service/command.go +++ b/core/app/service/command.go @@ -15,7 +15,7 @@ type ICommandService interface { SearchForTree(req dto.OperateByType) ([]dto.CommandTree, error) SearchWithPage(search dto.SearchCommandWithPage) (int64, interface{}, error) Create(req dto.CommandOperate) error - Update(id uint, upMap map[string]interface{}) error + Update(req dto.CommandOperate) error Delete(ids []uint) error } @@ -123,6 +123,14 @@ func (u *CommandService) Delete(ids []uint) error { return commandRepo.Delete(repo.WithByIDs(ids)) } -func (u *CommandService) Update(id uint, upMap map[string]interface{}) error { - return commandRepo.Update(id, upMap) +func (u *CommandService) Update(req dto.CommandOperate) error { + command, _ := commandRepo.Get(repo.WithByName(req.Name), repo.WithByType(req.Type)) + if command.ID != req.ID { + return buserr.New("ErrRecordExist") + } + upMap := make(map[string]interface{}) + upMap["name"] = req.Name + upMap["group_id"] = req.GroupID + upMap["command"] = req.Command + return commandRepo.Update(req.ID, upMap) } diff --git a/core/app/service/logs.go b/core/app/service/logs.go index 48a432424..8e233f6a6 100644 --- a/core/app/service/logs.go +++ b/core/app/service/logs.go @@ -122,6 +122,9 @@ func (u *LogService) PageOperationLog(req dto.SearchOpLogWithPage) (int64, inter if len(req.Status) != 0 { options = append(options, repo.WithByStatus(req.Status)) } + if len(req.Node) != 0 { + options = append(options, repo.WithByNode(req.Node)) + } total, ops, err := logRepo.PageOperationLog( req.Page, diff --git a/core/i18n/i18n.go b/core/i18n/i18n.go index e85019643..dcbff8f38 100644 --- a/core/i18n/i18n.go +++ b/core/i18n/i18n.go @@ -2,7 +2,6 @@ package i18n import ( "embed" - "fmt" "strings" "github.com/1Panel-dev/1Panel/core/global" @@ -65,10 +64,9 @@ func GetErrMsg(key string, maps map[string]interface{}) string { } func GetMsgByKey(key string) string { - content, err := global.I18n.Localize(&i18n.LocalizeConfig{ + content, _ := global.I18n.Localize(&i18n.LocalizeConfig{ MessageID: key, }) - fmt.Println(err) return content } diff --git a/core/init/migration/migrate.go b/core/init/migration/migrate.go index 3d6a98395..d5301f0f2 100644 --- a/core/init/migration/migrate.go +++ b/core/init/migration/migrate.go @@ -24,6 +24,7 @@ func Init() { migrations.UpdateXpackHideMemu, migrations.AddSystemIP, migrations.InitScriptLibrary, + migrations.AddOperationNode, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/core/init/migration/migrations/init.go b/core/init/migration/migrations/init.go index fdbfa05c2..10f3acf24 100644 --- a/core/init/migration/migrations/init.go +++ b/core/init/migration/migrations/init.go @@ -339,3 +339,13 @@ var InitScriptLibrary = &gormigrate.Migration{ return nil }, } + +var AddOperationNode = &gormigrate.Migration{ + ID: "20250321-add-operation-node", + Migrate: func(tx *gorm.DB) error { + if err := tx.AutoMigrate(&model.OperationLog{}); err != nil { + return err + } + return nil + }, +} diff --git a/core/middleware/operation.go b/core/middleware/operation.go index b1fccf507..dabbbaad3 100644 --- a/core/middleware/operation.go +++ b/core/middleware/operation.go @@ -32,8 +32,10 @@ func OperationLog() gin.HandlerFunc { source := loadLogInfo(c.Request.URL.Path) pathItem := strings.TrimPrefix(c.Request.URL.Path, "/api/v2") pathItem = strings.TrimPrefix(pathItem, "/api/v2/core") + currentNode := c.Request.Header.Get("CurrentNode") record := &model.OperationLog{ Source: source, + Node: currentNode, IP: c.ClientIP(), Method: strings.ToLower(c.Request.Method), Path: pathItem, diff --git a/frontend/src/components/log/custom-hightlight/index.vue b/frontend/src/components/log/custom-hightlight/index.vue index 4a90d03a4..f51eb2ec2 100644 --- a/frontend/src/components/log/custom-hightlight/index.vue +++ b/frontend/src/components/log/custom-hightlight/index.vue @@ -89,7 +89,7 @@ const systemRules: TokenRule[] = [ const taskRules: TokenRule[] = [ { type: 'bracket-text', - pattern: /\[([^\]]+)\]/g, + pattern: /\[([^\,]]+)\]/g, color: '#B87A2B', }, ]; diff --git a/frontend/src/components/system-upgrade/index.vue b/frontend/src/components/system-upgrade/index.vue index 8b9793452..4635b1ee8 100644 --- a/frontend/src/components/system-upgrade/index.vue +++ b/frontend/src/components/system-upgrade/index.vue @@ -31,11 +31,12 @@ v-if="version !== 'Waiting' && !globalStore.hasNewVersion" type="primary" :underline="false" + class="ml-2" @click="onLoadUpgradeInfo" > {{ $t('commons.button.update') }} - + {{ $t('setting.upgrading') }} diff --git a/frontend/src/layout/index.vue b/frontend/src/layout/index.vue index 7b65ddd68..655a6f973 100644 --- a/frontend/src/layout/index.vue +++ b/frontend/src/layout/index.vue @@ -7,15 +7,19 @@ @mouseenter="collapseButtonShow = true" @mouseleave="collapseButtonShow = false" > - - + + + + diff --git a/frontend/src/views/cronjob/cronjob/index.vue b/frontend/src/views/cronjob/cronjob/index.vue index 6e494f47f..d8aa073a6 100644 --- a/frontend/src/views/cronjob/cronjob/index.vue +++ b/frontend/src/views/cronjob/cronjob/index.vue @@ -29,6 +29,7 @@ @sort-change="search" @search="search" :data="data" + :heightDiff="300" > { }; const loadRecord = async (row: Cronjob.Record) => { currentRecord.value = row; - console.log(currentRecord.value); if (row.records) { const res = await getRecordLog(row.id); let log = res.data.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, ''); diff --git a/frontend/src/views/cronjob/library/index.vue b/frontend/src/views/cronjob/library/index.vue index 85a1a89d4..0dabdd40e 100644 --- a/frontend/src/views/cronjob/library/index.vue +++ b/frontend/src/views/cronjob/library/index.vue @@ -29,7 +29,7 @@ :pagination-config="paginationConfig" :data="data" @search="search" - :heightDiff="370" + :heightDiff="300" > diff --git a/frontend/src/views/cronjob/library/operate/index.vue b/frontend/src/views/cronjob/library/operate/index.vue index 1f4785906..5bcfb8fab 100644 --- a/frontend/src/views/cronjob/library/operate/index.vue +++ b/frontend/src/views/cronjob/library/operate/index.vue @@ -87,6 +87,7 @@ const onSubmit = async (formEl: FormInstance | undefined) => { formEl.validate(async (valid) => { if (!valid) return; loading.value = true; + dialogData.value.rowData.groupList = dialogData.value.rowData.groupList || []; dialogData.value.rowData.groups = dialogData.value.rowData.groupList.join(','); if (dialogData.value.title === 'create') { await addScript(dialogData.value.rowData) diff --git a/frontend/src/views/database/postgresql/setting/index.vue b/frontend/src/views/database/postgresql/setting/index.vue index 39258d3cd..926234343 100644 --- a/frontend/src/views/database/postgresql/setting/index.vue +++ b/frontend/src/views/database/postgresql/setting/index.vue @@ -1,7 +1,11 @@