| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package dockerhub |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func (c *Collector) collect() (map[string]int64, error) { |
| 11 | var ( |
| 12 | reposNum = len(c.Repositories) |
| 13 | ch = make(chan *repository, reposNum) |
| 14 | mx = make(map[string]int64) |
| 15 | ) |
| 16 | |
| 17 | for _, name := range c.Repositories { |
| 18 | go c.collectRepo(name, ch) |
| 19 | } |
| 20 | |
| 21 | var ( |
| 22 | parsed int |
| 23 | pullSum int |
| 24 | ) |
| 25 | |
| 26 | for range reposNum { |
| 27 | repo := <-ch |
| 28 | if repo == nil { |
| 29 | continue |
| 30 | } |
| 31 | if err := parseRepoTo(repo, mx); err != nil { |
| 32 | c.Errorf("error on parsing %s/%s : %v", repo.User, repo.Name, err) |
| 33 | continue |
| 34 | } |
| 35 | pullSum += repo.PullCount |
| 36 | parsed++ |
| 37 | } |
| 38 | close(ch) |
| 39 | |
| 40 | if parsed == reposNum { |
| 41 | mx["pull_sum"] = int64(pullSum) |
| 42 | } |
| 43 | |
| 44 | return mx, nil |
| 45 | } |
| 46 | |
| 47 | func (c *Collector) collectRepo(repoName string, ch chan *repository) { |
| 48 | repo, err := c.client.getRepository(repoName) |
| 49 | if err != nil { |
| 50 | c.Error(err) |
| 51 | } |
| 52 | ch <- repo |
| 53 | } |
| 54 | |
| 55 | func parseRepoTo(repo *repository, mx map[string]int64) error { |
| 56 | t, err := time.Parse(time.RFC3339Nano, repo.LastUpdated) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | mx[fmt.Sprintf("last_updated_%s/%s", repo.User, repo.Name)] = int64(time.Since(t).Seconds()) |
| 61 | mx[fmt.Sprintf("star_count_%s/%s", repo.User, repo.Name)] = int64(repo.StarCount) |
| 62 | mx[fmt.Sprintf("pull_count_%s/%s", repo.User, repo.Name)] = int64(repo.PullCount) |
| 63 | mx[fmt.Sprintf("status_%s/%s", repo.User, repo.Name)] = int64(repo.Status) |
| 64 | return nil |
| 65 | } |