-
Notifications
You must be signed in to change notification settings - Fork 80
/
layerscanned.go
93 lines (83 loc) · 2.13 KB
/
layerscanned.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
package postgres
import (
"context"
"errors"
"fmt"
"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 (
layerScannedCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "claircore",
Subsystem: "indexer",
Name: "layerscanned_total",
Help: "Total number of database queries issued in the LayerScanned method.",
},
[]string{"query"},
)
layerScannedDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "claircore",
Subsystem: "indexer",
Name: "layerscanned_duration_seconds",
Help: "The duration of all queries issued in the LayerScanned method",
},
[]string{"query"},
)
)
func (s *IndexerStore) LayerScanned(ctx context.Context, hash claircore.Digest, scnr indexer.VersionedScanner) (bool, error) {
// TODO(hank) Could this be written as a single query that reports NULL if
// the scanner isn't present?
const (
selectScanner = `
SELECT
id
FROM
scanner
WHERE
name = $1 AND version = $2 AND kind = $3;
`
selectScanned = `
SELECT
EXISTS(
SELECT
1
FROM
layer
JOIN scanned_layer ON
scanned_layer.layer_id = layer.id
WHERE
layer.hash = $1
AND scanned_layer.scanner_id = $2
);
`
)
start := time.Now()
var scannerID int64
err := s.pool.QueryRow(ctx, selectScanner, scnr.Name(), scnr.Version(), scnr.Kind()).
Scan(&scannerID)
switch {
case errors.Is(err, nil):
case errors.Is(err, pgx.ErrNoRows):
return false, fmt.Errorf("scanner %s not found", scnr.Name())
default:
return false, err
}
layerScannedCounter.WithLabelValues("selectScanner").Add(1)
layerScannedDuration.WithLabelValues("selectScanner").Observe(time.Since(start).Seconds())
var ok bool
start = time.Now()
err = s.pool.QueryRow(ctx, selectScanned, hash.String(), scannerID).
Scan(&ok)
if err != nil {
return false, err
}
layerScannedCounter.WithLabelValues("selectScanned").Add(1)
layerScannedDuration.WithLabelValues("selectScanned").Observe(time.Since(start).Seconds())
return ok, nil
}