Skip to content

Commit

Permalink
updater: extract deduplicate function
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Jan 10, 2019
1 parent 25078ac commit 7084a22
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,7 @@ func update(ctx context.Context, datastore database.Datastore, firstUpdate bool)
// Fetch updates.
success, vulnerabilities, flags, notes := fetchUpdates(ctx, datastore)

// do vulnerability namespacing again to merge potentially duplicated
// vulnerabilities from each updater.
vulnerabilities = doVulnerabilitiesNamespacing(vulnerabilities)

// deduplicate fetched namespaces and store them into database.
nsMap := map[database.Namespace]struct{}{}
for _, vuln := range vulnerabilities {
nsMap[vuln.Namespace] = struct{}{}
}

namespaces := make([]database.Namespace, 0, len(nsMap))
for ns := range nsMap {
namespaces = append(namespaces, ns)
}
namespaces, vulnerabilities := deduplicate(vulnerabilities)

if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
log.WithError(err).Error("Unable to insert namespaces")
Expand Down Expand Up @@ -264,6 +251,24 @@ func update(ctx context.Context, datastore database.Datastore, firstUpdate bool)
return nil
}

func deduplicate(vulns []database.VulnerabilityWithAffected) ([]database.Namespace, []database.VulnerabilityWithAffected) {
// do vulnerability namespacing again to merge potentially duplicated
// vulnerabilities from each updater.
vulnerabilities := doVulnerabilitiesNamespacing(vulns)

nsMap := map[database.Namespace]struct{}{}
for _, vuln := range vulnerabilities {
nsMap[vuln.Namespace] = struct{}{}
}

namespaces := make([]database.Namespace, 0, len(nsMap))
for ns := range nsMap {
namespaces = append(namespaces, ns)
}

return namespaces, vulnerabilities
}

func setUpdaterDuration(start time.Time) {
promUpdaterDurationSeconds.Set(time.Since(start).Seconds())
}
Expand Down

0 comments on commit 7084a22

Please sign in to comment.