github.com/quay/claircore@v1.5.28/datastore/postgres/setindexfinished.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  	"github.com/quay/claircore/indexer"
    13  )
    14  
    15  var (
    16  	setIndexedFinishedCounter = promauto.NewCounterVec(
    17  		prometheus.CounterOpts{
    18  			Namespace: "claircore",
    19  			Subsystem: "indexer",
    20  			Name:      "setindexedfinished_total",
    21  			Help:      "Total number of database queries issued in the SetIndexFinished method.",
    22  		},
    23  		[]string{"query"},
    24  	)
    25  
    26  	setIndexedFinishedDuration = promauto.NewHistogramVec(
    27  		prometheus.HistogramOpts{
    28  			Namespace: "claircore",
    29  			Subsystem: "indexer",
    30  			Name:      "setindexfinished_duration_seconds",
    31  			Help:      "The duration of all queries issued in the SetIndexFinished method",
    32  		},
    33  		[]string{"query"},
    34  	)
    35  )
    36  
    37  func (s *IndexerStore) SetIndexFinished(ctx context.Context, ir *claircore.IndexReport, scnrs indexer.VersionedScanners) error {
    38  	const (
    39  		insertManifestScanned = `
    40  WITH
    41  	manifests
    42  		AS (
    43  			SELECT
    44  				id AS manifest_id
    45  			FROM
    46  				manifest
    47  			WHERE
    48  				hash = $1
    49  		)
    50  INSERT
    51  INTO
    52  	scanned_manifest (manifest_id, scanner_id)
    53  VALUES
    54  	((SELECT manifest_id FROM manifests), $2);
    55  `
    56  		upsertIndexReport = `
    57  WITH
    58  	manifests
    59  		AS (
    60  			SELECT
    61  				id AS manifest_id
    62  			FROM
    63  				manifest
    64  			WHERE
    65  				hash = $1
    66  		)
    67  INSERT
    68  INTO
    69  	indexreport (manifest_id, scan_result)
    70  VALUES
    71  	((SELECT manifest_id FROM manifests), $2)
    72  ON CONFLICT
    73  	(manifest_id)
    74  DO
    75  	UPDATE SET scan_result = excluded.scan_result;
    76  `
    77  	)
    78  
    79  	scannerIDs, err := s.selectScanners(ctx, scnrs)
    80  	if err != nil {
    81  		return fmt.Errorf("failed to select package scanner id: %w", err)
    82  	}
    83  
    84  	tx, err := s.pool.Begin(ctx)
    85  	if err != nil {
    86  		return fmt.Errorf("failed to create transaction: %w", err)
    87  	}
    88  	defer tx.Rollback(ctx)
    89  
    90  	// link extracted scanner IDs with incoming manifest
    91  	for _, id := range scannerIDs {
    92  		start := time.Now()
    93  		_, err := tx.Exec(ctx, insertManifestScanned, ir.Hash, id)
    94  		if err != nil {
    95  			return fmt.Errorf("failed to link manifest with scanner list: %w", err)
    96  		}
    97  		setIndexedFinishedCounter.WithLabelValues("insertManifestScanned").Add(1)
    98  		setIndexedFinishedDuration.WithLabelValues("insertManifestScanned").Observe(time.Since(start).Seconds())
    99  	}
   100  
   101  	// push IndexReport to the store
   102  	// we cast claircore.IndexReport to jsonbIndexReport in order to obtain the value/scan
   103  	// implementations
   104  
   105  	start := time.Now()
   106  	_, err = tx.Exec(ctx, upsertIndexReport, ir.Hash, jsonbIndexReport(*ir))
   107  	if err != nil {
   108  		return fmt.Errorf("failed to upsert scan result: %w", err)
   109  	}
   110  	setIndexedFinishedCounter.WithLabelValues("upsertIndexReport").Add(1)
   111  	setIndexedFinishedDuration.WithLabelValues("upsertIndexReport").Observe(time.Since(start).Seconds())
   112  
   113  	err = tx.Commit(ctx)
   114  	if err != nil {
   115  		return fmt.Errorf("failed to commit transaction: %w", err)
   116  	}
   117  	return nil
   118  }