github.com/quay/claircore@v1.5.28/datastore/postgres/setindexreport.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  
    11  	"github.com/quay/claircore"
    12  )
    13  
    14  var (
    15  	setIndexReportCounter = promauto.NewCounterVec(
    16  		prometheus.CounterOpts{
    17  			Namespace: "claircore",
    18  			Subsystem: "indexer",
    19  			Name:      "setindexreport_total",
    20  			Help:      "Total number of database queries issued in the SetIndexReport method.",
    21  		},
    22  		[]string{"query"},
    23  	)
    24  
    25  	setIndexReportDuration = promauto.NewHistogramVec(
    26  		prometheus.HistogramOpts{
    27  			Namespace: "claircore",
    28  			Subsystem: "indexer",
    29  			Name:      "setindexreport_duration_seconds",
    30  			Help:      "The duration of all queries issued in the SetIndexReport method",
    31  		},
    32  		[]string{"query"},
    33  	)
    34  )
    35  
    36  func (s *IndexerStore) SetIndexReport(ctx context.Context, ir *claircore.IndexReport) error {
    37  	const query = `
    38  WITH
    39  	manifests
    40  		AS (
    41  			SELECT
    42  				id AS manifest_id
    43  			FROM
    44  				manifest
    45  			WHERE
    46  				hash = $1
    47  		)
    48  INSERT
    49  INTO
    50  	indexreport (manifest_id, scan_result)
    51  VALUES
    52  	((SELECT manifest_id FROM manifests), $2)
    53  ON CONFLICT
    54  	(manifest_id)
    55  DO
    56  	UPDATE SET scan_result = excluded.scan_result;
    57  `
    58  	// we cast scanner.IndexReport to jsonbIndexReport in order to obtain the value/scan
    59  	// implementations
    60  
    61  	start := time.Now()
    62  	_, err := s.pool.Exec(ctx, query, ir.Hash, jsonbIndexReport(*ir))
    63  	if err != nil {
    64  		return fmt.Errorf("failed to upsert index report: %w", err)
    65  	}
    66  	setIndexReportCounter.WithLabelValues("query").Add(1)
    67  	setIndexReportDuration.WithLabelValues("query").Observe(time.Since(start).Seconds())
    68  
    69  	return nil
    70  }