updater: extract deduplicate function
This commit is contained in:
parent
25078ac838
commit
7084a226ae
33
updater.go
33
updater.go
@ -200,20 +200,7 @@ func update(ctx context.Context, datastore database.Datastore, firstUpdate bool)
|
|||||||
// Fetch updates.
|
// Fetch updates.
|
||||||
success, vulnerabilities, flags, notes := fetchUpdates(ctx, datastore)
|
success, vulnerabilities, flags, notes := fetchUpdates(ctx, datastore)
|
||||||
|
|
||||||
// do vulnerability namespacing again to merge potentially duplicated
|
namespaces, vulnerabilities := deduplicate(vulnerabilities)
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
|
if err := database.PersistNamespacesAndCommit(datastore, namespaces); err != nil {
|
||||||
log.WithError(err).Error("Unable to insert namespaces")
|
log.WithError(err).Error("Unable to insert namespaces")
|
||||||
@ -264,6 +251,24 @@ func update(ctx context.Context, datastore database.Datastore, firstUpdate bool)
|
|||||||
return nil
|
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) {
|
func setUpdaterDuration(start time.Time) {
|
||||||
promUpdaterDurationSeconds.Set(time.Since(start).Seconds())
|
promUpdaterDurationSeconds.Set(time.Since(start).Seconds())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user