Skip to content

Commit 992fecb

Browse files
feat(common): Optimize commonRepo Invocation (#7621)
1 parent b1fbdcc commit 992fecb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+420
-416
lines changed

agent/app/repo/common.go

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,34 @@ import (
1212

1313
type DBOption func(*gorm.DB) *gorm.DB
1414

15-
type ICommonRepo interface {
16-
WithByID(id uint) DBOption
17-
WithByIDs(ids []uint) DBOption
18-
WithByName(name string) DBOption
19-
WithByNames(names []string) DBOption
20-
WithByLikeName(name string) DBOption
21-
WithByDetailName(detailName string) DBOption
22-
23-
WithByFrom(from string) DBOption
24-
WithByType(tp string) DBOption
25-
WithTypes(types []string) DBOption
26-
WithByStatus(status string) DBOption
27-
28-
WithOrderBy(orderStr string) DBOption
29-
WithOrderRuleBy(orderBy, order string) DBOption
30-
31-
WithByDate(startTime, endTime time.Time) DBOption
32-
WithByCreatedAt(startTime, endTime time.Time) DBOption
33-
}
34-
35-
type CommonRepo struct{}
36-
37-
func NewCommonRepo() ICommonRepo {
38-
return &CommonRepo{}
39-
}
40-
41-
func (c *CommonRepo) WithByID(id uint) DBOption {
15+
func WithByID(id uint) DBOption {
4216
return func(g *gorm.DB) *gorm.DB {
4317
return g.Where("id = ?", id)
4418
}
4519
}
46-
func (c *CommonRepo) WithByIDs(ids []uint) DBOption {
20+
21+
func WithByIDs(ids []uint) DBOption {
4722
return func(g *gorm.DB) *gorm.DB {
4823
return g.Where("id in (?)", ids)
4924
}
5025
}
5126

52-
func (c *CommonRepo) WithByName(name string) DBOption {
27+
func WithByName(name string) DBOption {
5328
return func(g *gorm.DB) *gorm.DB {
5429
return g.Where("name = ?", name)
5530
}
5631
}
57-
func (c *CommonRepo) WithByNames(names []string) DBOption {
58-
return func(g *gorm.DB) *gorm.DB {
59-
return g.Where("name in (?)", names)
60-
}
61-
}
62-
func (c *CommonRepo) WithByLikeName(name string) DBOption {
32+
33+
func WithByLikeName(name string) DBOption {
6334
return func(g *gorm.DB) *gorm.DB {
6435
if len(name) == 0 {
6536
return g
6637
}
6738
return g.Where("name like ?", "%"+name+"%")
6839
}
6940
}
70-
func (c *CommonRepo) WithByDetailName(detailName string) DBOption {
41+
42+
func WithByDetailName(detailName string) DBOption {
7143
return func(g *gorm.DB) *gorm.DB {
7244
if len(detailName) == 0 {
7345
return g
@@ -76,51 +48,53 @@ func (c *CommonRepo) WithByDetailName(detailName string) DBOption {
7648
}
7749
}
7850

79-
func (c *CommonRepo) WithByType(tp string) DBOption {
51+
func WithByType(tp string) DBOption {
8052
return func(g *gorm.DB) *gorm.DB {
8153
return g.Where("type = ?", tp)
8254
}
8355
}
84-
func (c *CommonRepo) WithTypes(types []string) DBOption {
56+
57+
func WithTypes(types []string) DBOption {
8558
return func(db *gorm.DB) *gorm.DB {
8659
return db.Where("type in (?)", types)
8760
}
8861
}
89-
func (c *CommonRepo) WithByStatus(status string) DBOption {
62+
63+
func WithByStatus(status string) DBOption {
9064
return func(g *gorm.DB) *gorm.DB {
9165
if len(status) == 0 {
9266
return g
9367
}
9468
return g.Where("status = ?", status)
9569
}
9670
}
97-
func (c *CommonRepo) WithByFrom(from string) DBOption {
71+
func WithByFrom(from string) DBOption {
9872
return func(g *gorm.DB) *gorm.DB {
9973
return g.Where("`from` = ?", from)
10074
}
10175
}
10276

103-
func (c *CommonRepo) WithByDate(startTime, endTime time.Time) DBOption {
77+
func WithByDate(startTime, endTime time.Time) DBOption {
10478
return func(g *gorm.DB) *gorm.DB {
10579
return g.Where("start_time > ? AND start_time < ?", startTime, endTime)
10680
}
10781
}
10882

109-
func (c *CommonRepo) WithByCreatedAt(startTime, endTime time.Time) DBOption {
83+
func WithByCreatedAt(startTime, endTime time.Time) DBOption {
11084
return func(g *gorm.DB) *gorm.DB {
11185
return g.Where("created_at > ? AND created_at < ?", startTime, endTime)
11286
}
11387
}
11488

115-
func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
89+
func WithOrderBy(orderStr string) DBOption {
11690
if orderStr == "createdAt" {
11791
orderStr = "created_at"
11892
}
11993
return func(g *gorm.DB) *gorm.DB {
12094
return g.Order(orderStr)
12195
}
12296
}
123-
func (c *CommonRepo) WithOrderRuleBy(orderBy, order string) DBOption {
97+
func WithOrderRuleBy(orderBy, order string) DBOption {
12498
if orderBy == "createdAt" {
12599
orderBy = "created_at"
126100
}

agent/app/repo/entry.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

agent/app/repo/task.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package repo
22

33
import (
44
"context"
5-
65
"github.com/1Panel-dev/1Panel/agent/constant"
76
"github.com/1Panel-dev/1Panel/agent/global"
87

@@ -24,6 +23,7 @@ type ITaskRepo interface {
2423
WithByID(id string) DBOption
2524
WithResourceID(id uint) DBOption
2625
WithOperate(taskOperate string) DBOption
26+
WithByStatus(status string) DBOption
2727
}
2828

2929
func NewITaskRepo() ITaskRepo {
@@ -67,6 +67,12 @@ func (t TaskRepo) WithResourceID(id uint) DBOption {
6767
}
6868
}
6969

70+
func (t TaskRepo) WithByStatus(status string) DBOption {
71+
return func(g *gorm.DB) *gorm.DB {
72+
return g.Where("status = ?", status)
73+
}
74+
}
75+
7076
func (t TaskRepo) Save(ctx context.Context, task *model.Task) error {
7177
return getTaskTx(ctx).Save(&task).Error
7278
}
@@ -94,11 +100,11 @@ func (t TaskRepo) Update(ctx context.Context, task *model.Task) error {
94100
}
95101

96102
func (t TaskRepo) UpdateRunningTaskToFailed() error {
97-
return getTaskDb(commonRepo.WithByStatus(constant.StatusExecuting)).Model(&model.Task{}).Updates(map[string]interface{}{"status": constant.StatusFailed, "error_msg": "1Panel restart causes failure"}).Error
103+
return getTaskDb(t.WithByStatus(constant.StatusExecuting)).Model(&model.Task{}).Updates(map[string]interface{}{"status": constant.StatusFailed, "error_msg": "1Panel restart causes failure"}).Error
98104
}
99105

100106
func (t TaskRepo) CountExecutingTask() (int64, error) {
101107
var count int64
102-
err := getTaskDb(commonRepo.WithByStatus(constant.StatusExecuting)).Model(&model.Task{}).Count(&count).Error
108+
err := getTaskDb(t.WithByStatus(constant.StatusExecuting)).Model(&model.Task{}).Count(&count).Error
103109
return count, err
104110
}

agent/app/service/app.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (a AppService) PageApp(req request.AppSearch) (interface{}, error) {
9595
for _, t := range appTags {
9696
appIds = append(appIds, t.AppId)
9797
}
98-
opts = append(opts, commonRepo.WithByIDs(appIds))
98+
opts = append(opts, repo.WithByIDs(appIds))
9999
}
100100
var res response.AppRes
101101

@@ -203,7 +203,7 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response.
203203
appDetailDTO.Enable = true
204204

205205
if appType == "runtime" {
206-
app, err := appRepo.GetFirst(commonRepo.WithByID(appID))
206+
app, err := appRepo.GetFirst(repo.WithByID(appID))
207207
if err != nil {
208208
return appDetailDTO, err
209209
}
@@ -274,7 +274,7 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response.
274274

275275
appDetailDTO.HostMode = isHostModel(appDetailDTO.DockerCompose)
276276

277-
app, err := appRepo.GetFirst(commonRepo.WithByID(detail.AppId))
277+
app, err := appRepo.GetFirst(repo.WithByID(detail.AppId))
278278
if err != nil {
279279
return appDetailDTO, err
280280
}
@@ -288,7 +288,7 @@ func (a AppService) GetAppDetail(appID uint, version, appType string) (response.
288288
}
289289
func (a AppService) GetAppDetailByID(id uint) (*response.AppDetailDTO, error) {
290290
res := &response.AppDetailDTO{}
291-
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(id))
291+
appDetail, err := appDetailRepo.GetFirst(repo.WithByID(id))
292292
if err != nil {
293293
return nil, err
294294
}
@@ -309,7 +309,7 @@ func (a AppService) GetIgnoredApp() ([]response.IgnoredApp, error) {
309309
return res, nil
310310
}
311311
for _, detail := range details {
312-
app, err := appRepo.GetFirst(commonRepo.WithByID(detail.AppId))
312+
app, err := appRepo.GetFirst(repo.WithByID(detail.AppId))
313313
if err != nil {
314314
return nil, err
315315
}
@@ -328,7 +328,7 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
328328
err = buserr.WithDetail(constant.Err1PanelNetworkFailed, err.Error(), nil)
329329
return
330330
}
331-
if list, _ := appInstallRepo.ListBy(commonRepo.WithByName(req.Name)); len(list) > 0 {
331+
if list, _ := appInstallRepo.ListBy(repo.WithByName(req.Name)); len(list) > 0 {
332332
err = buserr.New(constant.ErrAppNameExist)
333333
return
334334
}
@@ -338,16 +338,16 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
338338
appDetail model.AppDetail
339339
app model.App
340340
)
341-
appDetail, err = appDetailRepo.GetFirst(commonRepo.WithByID(req.AppDetailId))
341+
appDetail, err = appDetailRepo.GetFirst(repo.WithByID(req.AppDetailId))
342342
if err != nil {
343343
return
344344
}
345-
app, err = appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
345+
app, err = appRepo.GetFirst(repo.WithByID(appDetail.AppId))
346346
if err != nil {
347347
return
348348
}
349349
if DatabaseKeys[app.Key] > 0 {
350-
if existDatabases, _ := databaseRepo.GetList(commonRepo.WithByName(req.Name)); len(existDatabases) > 0 {
350+
if existDatabases, _ := databaseRepo.GetList(repo.WithByName(req.Name)); len(existDatabases) > 0 {
351351
err = buserr.New(constant.ErrRemoteExist)
352352
return
353353
}
@@ -462,7 +462,7 @@ func (a AppService) Install(req request.AppInstallCreate) (appInstall *model.App
462462
appInstall.DockerCompose = string(composeByte)
463463

464464
if hostName, ok := req.Params["PANEL_DB_HOST"]; ok {
465-
database, _ := databaseRepo.Get(commonRepo.WithByName(hostName.(string)))
465+
database, _ := databaseRepo.Get(repo.WithByName(hostName.(string)))
466466
if !reflect.DeepEqual(database, model.Database{}) {
467467
req.Params["PANEL_DB_HOST"] = database.Address
468468
req.Params["PANEL_DB_PORT"] = database.Port

0 commit comments

Comments
 (0)