/
store.go
70 lines (58 loc) · 1.39 KB
/
store.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
package postgres
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/prometheus/client_golang/prometheus"
"github.com/quay/claircore/internal/indexer"
)
var _ indexer.Store = (*store)(nil)
// Store implements the claircore.Store interface.
//
// All the other exported methods live in their own files.
type store struct {
pool *pgxpool.Pool
}
func NewStore(pool *pgxpool.Pool) *store {
return &store{
pool: pool,
}
}
func (s *store) Close(_ context.Context) error {
s.pool.Close()
return nil
}
const selectScanner = `
SELECT
id
FROM
scanner
WHERE
name = $1 AND version = $2 AND kind = $3;
`
func (s *store) selectScanners(ctx context.Context, vs indexer.VersionedScanners) ([]int64, error) {
ids := make([]int64, len(vs))
for i, v := range vs {
ctx, done := context.WithTimeout(ctx, time.Second)
err := s.pool.QueryRow(ctx, selectScanner, v.Name(), v.Version(), v.Kind()).
Scan(&ids[i])
done()
if err != nil {
return nil, fmt.Errorf("failed to retrieve id for scanner %q: %w", v.Name(), err)
}
}
return ids, nil
}
func promTimer(h *prometheus.HistogramVec, name string, err *error) func() time.Duration {
t := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
h.WithLabelValues(name, success(*err)).Observe(v)
}))
return t.ObserveDuration
}
func success(err error) string {
if err == nil {
return "true"
}
return "false"
}