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

     1  package postgres
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"time"
     8  
     9  	"github.com/jackc/pgx/v4"
    10  	"github.com/prometheus/client_golang/prometheus"
    11  	"github.com/prometheus/client_golang/prometheus/promauto"
    12  
    13  	"github.com/quay/claircore"
    14  )
    15  
    16  var (
    17  	indexReportCounter = promauto.NewCounterVec(
    18  		prometheus.CounterOpts{
    19  			Namespace: "claircore",
    20  			Subsystem: "indexer",
    21  			Name:      "indexreport_total",
    22  			Help:      "Total number of database queries issued in the IndexReport method.",
    23  		},
    24  		[]string{"query"},
    25  	)
    26  
    27  	indexReportDuration = promauto.NewHistogramVec(
    28  		prometheus.HistogramOpts{
    29  			Namespace: "claircore",
    30  			Subsystem: "indexer",
    31  			Name:      "indexreport_duration_seconds",
    32  			Help:      "The duration of all queries issued in the IndexReport method",
    33  		},
    34  		[]string{"query"},
    35  	)
    36  )
    37  
    38  func (s *IndexerStore) IndexReport(ctx context.Context, hash claircore.Digest) (*claircore.IndexReport, bool, error) {
    39  	const query = `
    40  	SELECT scan_result
    41  	FROM indexreport
    42  			 JOIN manifest ON manifest.hash = $1
    43  	WHERE indexreport.manifest_id = manifest.id;
    44  	`
    45  	// we scan into a jsonbIndexReport which has value/scan method set
    46  	// then type convert back to scanner.domain object
    47  	var jsr jsonbIndexReport
    48  
    49  	start := time.Now()
    50  	err := s.pool.QueryRow(ctx, query, hash).Scan(&jsr)
    51  	switch {
    52  	case errors.Is(err, nil):
    53  	case errors.Is(err, pgx.ErrNoRows):
    54  		return nil, false, nil
    55  	default:
    56  		return nil, false, fmt.Errorf("failed to retrieve index report: %w", err)
    57  	}
    58  	indexReportCounter.WithLabelValues("query").Add(1)
    59  	indexReportDuration.WithLabelValues("query").Observe(time.Since(start).Seconds())
    60  
    61  	sr := claircore.IndexReport(jsr)
    62  	return &sr, true, nil
    63  }