From 535c723e61dae919950ced1031291e0b7b8eec2a Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Sun, 27 Jun 2021 15:17:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=97=B6=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E3=80=81=E6=B5=8F=E8=A7=88=E5=99=A8=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=97=B6=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/client_browser_dao.go | 27 ++++++++++++++++++------ internal/db/models/client_system_dao.go | 27 ++++++++++++++++++------ internal/rpc/services/service_server.go | 2 -- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/internal/db/models/client_browser_dao.go b/internal/db/models/client_browser_dao.go index 224191a5..eee8b11c 100644 --- a/internal/db/models/client_browser_dao.go +++ b/internal/db/models/client_browser_dao.go @@ -35,7 +35,7 @@ func init() { }) } -// 启用条目 +// EnableClientBrowser 启用条目 func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error { _, err := this.Query(tx). Pk(id). @@ -44,7 +44,7 @@ func (this *ClientBrowserDAO) EnableClientBrowser(tx *dbs.Tx, id uint32) error { return err } -// 禁用条目 +// DisableClientBrowser 禁用条目 func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error { _, err := this.Query(tx). Pk(id). @@ -53,7 +53,7 @@ func (this *ClientBrowserDAO) DisableClientBrowser(tx *dbs.Tx, id uint32) error return err } -// 查找启用中的条目 +// FindEnabledClientBrowser 查找启用中的条目 func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*ClientBrowser, error) { result, err := this.Query(tx). Pk(id). @@ -65,7 +65,7 @@ func (this *ClientBrowserDAO) FindEnabledClientBrowser(tx *dbs.Tx, id int64) (*C return result.(*ClientBrowser), err } -// 根据主键查找名称 +// FindClientBrowserName 根据主键查找名称 func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (string, error) { return this.Query(tx). Pk(id). @@ -73,7 +73,7 @@ func (this *ClientBrowserDAO) FindClientBrowserName(tx *dbs.Tx, id uint32) (stri FindStringCol("") } -// 根据浏览器名称查找浏览器ID +// FindBrowserIdWithNameCacheable 根据浏览器名称查找浏览器ID func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browserName string) (int64, error) { SharedCacheLocker.RLock() browserId, ok := clientBrowserNameAndIdCacheMap[browserName] @@ -102,8 +102,23 @@ func (this *ClientBrowserDAO) FindBrowserIdWithNameCacheable(tx *dbs.Tx, browser return browserId, nil } -// 创建浏览器 +// CreateBrowser 创建浏览器 func (this *ClientBrowserDAO) CreateBrowser(tx *dbs.Tx, browserName string) (int64, error) { + SharedCacheLocker.Lock() + defer SharedCacheLocker.Unlock() + + // 检查是否已经创建 + browserId, err := this.Query(tx). + Attr("name", browserName). + ResultPk(). + FindInt64Col(0) + if err != nil { + return 0, err + } + if browserId > 0 { + return browserId, nil + } + op := NewClientBrowserOperator() op.Name = browserName codes := []string{browserName} diff --git a/internal/db/models/client_system_dao.go b/internal/db/models/client_system_dao.go index 7ac98e54..c7b7267d 100644 --- a/internal/db/models/client_system_dao.go +++ b/internal/db/models/client_system_dao.go @@ -35,7 +35,7 @@ func init() { }) } -// 启用条目 +// EnableClientSystem 启用条目 func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error { _, err := this.Query(tx). Pk(id). @@ -44,7 +44,7 @@ func (this *ClientSystemDAO) EnableClientSystem(tx *dbs.Tx, id uint32) error { return err } -// 禁用条目 +// DisableClientSystem 禁用条目 func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error { _, err := this.Query(tx). Pk(id). @@ -53,7 +53,7 @@ func (this *ClientSystemDAO) DisableClientSystem(tx *dbs.Tx, id uint32) error { return err } -// 查找启用中的条目 +// FindEnabledClientSystem 查找启用中的条目 func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*ClientSystem, error) { result, err := this.Query(tx). Pk(id). @@ -65,7 +65,7 @@ func (this *ClientSystemDAO) FindEnabledClientSystem(tx *dbs.Tx, id int64) (*Cli return result.(*ClientSystem), err } -// 根据主键查找名称 +// FindClientSystemName 根据主键查找名称 func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string, error) { return this.Query(tx). Pk(id). @@ -73,7 +73,7 @@ func (this *ClientSystemDAO) FindClientSystemName(tx *dbs.Tx, id uint32) (string FindStringCol("") } -// 根据操作系统名称查找系统ID +// FindSystemIdWithNameCacheable 根据操作系统名称查找系统ID func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemName string) (int64, error) { SharedCacheLocker.RLock() systemId, ok := clientSystemNameAndIdCacheMap[systemName] @@ -102,8 +102,23 @@ func (this *ClientSystemDAO) FindSystemIdWithNameCacheable(tx *dbs.Tx, systemNam return systemId, nil } -// 创建浏览器 +// CreateSystem 创建浏览器 func (this *ClientSystemDAO) CreateSystem(tx *dbs.Tx, systemName string) (int64, error) { + SharedCacheLocker.Lock() + defer SharedCacheLocker.Unlock() + + // 检查是否已经创建 + systemId, err := this.Query(tx). + Attr("name", systemName). + ResultPk(). + FindInt64Col(0) + if err != nil { + return 0, err + } + if systemId > 0 { + return systemId, nil + } + op := NewClientSystemOperator() op.Name = systemName diff --git a/internal/rpc/services/service_server.go b/internal/rpc/services/service_server.go index e5ce6ae7..51cce5e7 100644 --- a/internal/rpc/services/service_server.go +++ b/internal/rpc/services/service_server.go @@ -1303,7 +1303,6 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req return err } if systemId == 0 { - // TODO 失败时,需要查询一次确认是否已添加 systemId, err = models.SharedClientSystemDAO.CreateSystem(tx, result.Name) if err != nil { return err @@ -1332,7 +1331,6 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req return err } if browserId == 0 { - // TODO 失败时,需要查询一次确认是否已添加 browserId, err = models.SharedClientBrowserDAO.CreateBrowser(tx, result.Name) if err != nil { return err