/
distributionsbylayer.go
125 lines (112 loc) · 3.19 KB
/
distributionsbylayer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package postgres
import (
"context"
"errors"
"fmt"
"strconv"
"time"
"github.com/jackc/pgx/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
)
var (
distributionByLayerCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "claircore",
Subsystem: "indexer",
Name: "distributionbylayer_total",
Help: "The count of all queries issued in the DistributionsByLayer method",
},
[]string{"query"},
)
distributionByLayerDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "claircore",
Subsystem: "indexer",
Name: "distributionbylayer_duration_seconds",
Help: "The duration of all queries issued in the DistributionByLayer method",
},
[]string{"query"},
)
)
func (s *IndexerStore) DistributionsByLayer(ctx context.Context, hash claircore.Digest, scnrs indexer.VersionedScanners) ([]*claircore.Distribution, error) {
const (
selectScanner = `
SELECT id
FROM scanner
WHERE name = $1
AND version = $2
AND kind = $3;
`
query = `
SELECT dist.id,
dist.name,
dist.did,
dist.version,
dist.version_code_name,
dist.version_id,
dist.arch,
dist.cpe,
dist.pretty_name
FROM dist_scanartifact
LEFT JOIN dist ON dist_scanartifact.dist_id = dist.id
JOIN layer ON layer.hash = $1
WHERE dist_scanartifact.layer_id = layer.id
AND dist_scanartifact.scanner_id = ANY($2);
`
)
if len(scnrs) == 0 {
return []*claircore.Distribution{}, nil
}
// get scanner ids
scannerIDs := make([]int64, len(scnrs))
for i, scnr := range scnrs {
start := time.Now()
err := s.pool.QueryRow(ctx, selectScanner, scnr.Name(), scnr.Version(), scnr.Kind()).
Scan(&scannerIDs[i])
if err != nil {
return nil, fmt.Errorf("failed to retrieve distribution ids for scanner %q: %w", scnr, err)
}
distributionByLayerCounter.WithLabelValues("selectScanner").Add(1)
distributionByLayerDuration.WithLabelValues("selectScanner").Observe(time.Since(start).Seconds())
}
start := time.Now()
rows, err := s.pool.Query(ctx, query, hash, scannerIDs)
switch {
case errors.Is(err, nil):
case errors.Is(err, pgx.ErrNoRows):
return nil, fmt.Errorf("store:distributionsByLayer no distribution found for hash %v and scanners %v", hash, scnrs)
default:
return nil, fmt.Errorf("store:distributionsByLayer failed to retrieve package rows for hash %v and scanners %v: %w", hash, scnrs, err)
}
distributionByLayerCounter.WithLabelValues("query").Add(1)
distributionByLayerDuration.WithLabelValues("query").Observe(time.Since(start).Seconds())
defer rows.Close()
res := []*claircore.Distribution{}
for rows.Next() {
var dist claircore.Distribution
var id int64
err := rows.Scan(
&id,
&dist.Name,
&dist.DID,
&dist.Version,
&dist.VersionCodeName,
&dist.VersionID,
&dist.Arch,
&dist.CPE,
&dist.PrettyName,
)
dist.ID = strconv.FormatInt(id, 10)
if err != nil {
return nil, fmt.Errorf("failed to scan distribution: %w", err)
}
res = append(res, &dist)
}
if err := rows.Err(); err != nil {
return nil, err
}
return res, nil
}