github.com/quay/claircore@v1.5.28/datastore/postgres/setlayerscanned.go (about)

     1  package postgres
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/prometheus/client_golang/prometheus"
     9  	"github.com/prometheus/client_golang/prometheus/promauto"
    10  	"github.com/quay/zlog"
    11  
    12  	"github.com/quay/claircore"
    13  	"github.com/quay/claircore/indexer"
    14  )
    15  
    16  var (
    17  	setLayerScannedCounter = promauto.NewCounterVec(
    18  		prometheus.CounterOpts{
    19  			Namespace: "claircore",
    20  			Subsystem: "indexer",
    21  			Name:      "setlayerscanned_total",
    22  			Help:      "Total number of database queries issued in the SetLayerScanned method.",
    23  		},
    24  		[]string{"query"},
    25  	)
    26  
    27  	setLayerScannedDuration = promauto.NewHistogramVec(
    28  		prometheus.HistogramOpts{
    29  			Namespace: "claircore",
    30  			Subsystem: "indexer",
    31  			Name:      "setlayerscanned_duration_seconds",
    32  			Help:      "The duration of all queries issued in the SetLayerScanned method",
    33  		},
    34  		[]string{"query"},
    35  	)
    36  )
    37  
    38  func (s *IndexerStore) SetLayerScanned(ctx context.Context, hash claircore.Digest, vs indexer.VersionedScanner) error {
    39  	ctx = zlog.ContextWithValues(ctx, "scanner", vs.Name())
    40  	const query = `
    41  WITH
    42  	scanner
    43  		AS (
    44  			SELECT
    45  				id
    46  			FROM
    47  				scanner
    48  			WHERE
    49  				name = $2 AND version = $3 AND kind = $4
    50  		),
    51  	layer AS (SELECT id FROM layer WHERE hash = $1)
    52  INSERT
    53  INTO
    54  	scanned_layer (layer_id, scanner_id)
    55  VALUES
    56  	(
    57  		(SELECT id AS layer_id FROM layer),
    58  		(SELECT id AS scanner_id FROM scanner)
    59  	)
    60  ON CONFLICT
    61  	(layer_id, scanner_id)
    62  DO
    63  	NOTHING;
    64  `
    65  
    66  	start := time.Now()
    67  	_, err := s.pool.Exec(ctx, query, hash, vs.Name(), vs.Version(), vs.Kind())
    68  	if err != nil {
    69  		return fmt.Errorf("error setting layer scanned: %w", err)
    70  	}
    71  	setLayerScannedCounter.WithLabelValues("query").Add(1)
    72  	setLayerScannedDuration.WithLabelValues("query").Observe(time.Since(start).Seconds())
    73  
    74  	return nil
    75  }