From 8ebfda7377ed73e5819c9e992e22da5712ffbbc7 Mon Sep 17 00:00:00 2001 From: Marvin Zhang Date: Sat, 29 Jun 2024 15:29:24 +0800 Subject: [PATCH] feat: added multi-spider git https://github.com/crawlab-team/crawlab/issues/1485 --- core/controllers/spider_v2.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/core/controllers/spider_v2.go b/core/controllers/spider_v2.go index 333c3176..889c6c14 100644 --- a/core/controllers/spider_v2.go +++ b/core/controllers/spider_v2.go @@ -108,8 +108,12 @@ func getSpiderListWithStats(c *gin.Context) { // ids var ids []primitive.ObjectID + var gitIds []primitive.ObjectID for _, s := range spiders { ids = append(ids, s.Id) + if !s.GitId.IsZero() { + gitIds = append(gitIds, s.GitId) + } } // total count @@ -182,6 +186,22 @@ func getSpiderListWithStats(c *gin.Context) { } } + // git list + var gits []models.GitV2 + if len(gitIds) > 0 && utils.IsPro() { + gits, err = service.NewModelServiceV2[models.GitV2]().GetMany(bson.M{"_id": bson.M{"$in": gitIds}}, nil) + if err != nil { + HandleErrorInternalServerError(c, err) + return + } + } + + // cache git list to dict + dictGit := map[primitive.ObjectID]models.GitV2{} + for _, g := range gits { + dictGit[g.Id] = g + } + // iterate list again var data []models.SpiderV2 for _, s := range spiders { @@ -197,6 +217,14 @@ func getSpiderListWithStats(c *gin.Context) { } } + // git + if !s.GitId.IsZero() && utils.IsPro() { + g, ok := dictGit[s.GitId] + if ok { + s.Git = &g + } + } + // add to list data = append(data, s) }