Skip to content

Commit

Permalink
use latest data timestamp for DB timestamp (#251)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman authored Mar 7, 2024
1 parent b4e2769 commit 001623b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 17 deletions.
51 changes: 34 additions & 17 deletions cmd/grype-db/cli/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,55 @@ func runBuild(cfg buildConfig) error {
pvdrs = pvdrs.Filter(cfg.Provider.IncludeFilter...)
}

states, err := providerStates(cfg.SkipValidation, pvdrs)
if err != nil {
return fmt.Errorf("unable to get provider states: %w", err)
}

return process.Build(process.BuildConfig{
SchemaVersion: cfg.SchemaVersion,
Directory: cfg.Directory,
States: states,
Timestamp: latestTimestamp(states),
})
}

func providerStates(skipValidation bool, providers []provider.Provider) ([]provider.State, error) {
var states []provider.State
stateTimestamp := time.Now()
log.Debug("reading all provider state")
for _, p := range pvdrs {

if len(providers) == 0 {
return nil, fmt.Errorf("no providers configured")
}

for _, p := range providers {
log.WithFields("provider", p.ID().Name).Debug("reading state")

sd, err := p.State()
if err != nil {
return fmt.Errorf("unable to read provider state: %w", err)
return nil, fmt.Errorf("unable to read provider state: %w", err)
}

if !cfg.SkipValidation {
if !skipValidation {
log.WithFields("provider", p.ID().Name).Trace("validating state")
if err := sd.Verify(); err != nil {
return fmt.Errorf("invalid provider state: %w", err)
return nil, fmt.Errorf("invalid provider state: %w", err)
}
}

if sd.Timestamp.Before(stateTimestamp) {
stateTimestamp = sd.Timestamp
}
states = append(states, *sd)
}

if !cfg.SkipValidation {
if !skipValidation {
log.Debugf("state validated for all providers")
}
return states, nil
}

return process.Build(process.BuildConfig{
SchemaVersion: cfg.SchemaVersion,
Directory: cfg.Directory,
States: states,
Timestamp: stateTimestamp,
})
func latestTimestamp(states []provider.State) time.Time {
var latest time.Time
for _, s := range states {
if s.Timestamp.After(latest) {
latest = s.Timestamp
}
}
return latest
}
40 changes: 40 additions & 0 deletions cmd/grype-db/cli/commands/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package commands

import (
"reflect"
"testing"
"time"

"github.com/anchore/grype-db/pkg/provider"
)

func Test_latestTimestamp(t *testing.T) {
tests := []struct {
name string
states []provider.State
want time.Time
}{
{
name: "happy path",
states: []provider.State{
{
Timestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
},
{
Timestamp: time.Date(2021, 1, 3, 0, 0, 0, 0, time.UTC),
},
{
Timestamp: time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC),
},
},
want: time.Date(2021, 1, 3, 0, 0, 0, 0, time.UTC),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := latestTimestamp(tt.states); !reflect.DeepEqual(got, tt.want) {
t.Errorf("latestTimestamp() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 001623b

Please sign in to comment.