Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INSIGHTS-578 - Update scoring logic to take into stars count into account if the threshold is satisfied #355

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/helm/artifacthub.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type ArtifactHubHelmPackage struct {
AvailableVersions []AvailableVersion `json:"available_versions"`
Maintainers []Maintainer `json:"maintainers"`
Links []Link `json:"links"`
Stars int `json:"stars"`
}

// AvailableVersion is a sub struct of ArtifactHubHelmPackage and provides a version that is available for a given helm chart.
Expand Down
2 changes: 2 additions & 0 deletions pkg/helm/artifacthub_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type ArtifactHubCachedPackage struct {
Links []Link `json:"links"`
Maintainers []Maintainer `json:"maintainers"`
Deprecated bool `json:"deprecated"`
Stars int `json:"stars"`
}

// ArtifactHubCachedRepository is a sub-struct of the Package struct, and represents the repository containing the package.
Expand Down Expand Up @@ -126,6 +127,7 @@ func (ac *ArtifactHubCachedPackageClient) List() ([]ArtifactHubHelmPackage, erro
HomeURL: cachedPackage.HomeURL,
Links: cachedPackage.Links,
Official: cachedPackage.Official,
Stars: cachedPackage.Stars,
Repository: ArtifactHubRepository{
Name: cachedPackage.Repository.Name,
URL: cachedPackage.Repository.URL,
Expand Down
46 changes: 40 additions & 6 deletions pkg/helm/findscore.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,53 @@ import (
"k8s.io/klog/v2"
)

const useStarCountThreshold = 10

type packageKey struct {
Name string
Repository string
}

// FindBestArtifactHubMatch takes the helm releases found in the cluster and attempts to match those to a package in artifacthub
func FindBestArtifactHubMatch(clusterRelease *release.Release, ahubPackages []ArtifactHubHelmPackage) *output.ReleaseOutput {
var highScore float32
var highScorePackage ArtifactHubHelmPackage
packagesByName := map[packageKey]ArtifactHubHelmPackage{}
packageScores := map[packageKey]float32{}
packageStars := map[packageKey]int{}
var useStars bool
for _, p := range ahubPackages {
var score float32
if p.Name != clusterRelease.Chart.Metadata.Name {
continue
}
score = scoreChartSimilarity(clusterRelease, p)

key := packageKey{Name: p.Name, Repository: p.Repository.Name}
packageScores[key] = scoreChartSimilarity(clusterRelease, p)
packagesByName[key] = p
packageStars[key] = p.Stars

if p.Stars >= useStarCountThreshold {
useStars = true // If any package has more than 10 stars, we add a point to the highest star package
}
}

var highestStarPackageName *packageKey
var highStars int
for p, stars := range packageStars {
if stars > highStars {
highStars = stars
highestStarPackageName = &p
}
}

var highScore float32
var highScorePackage ArtifactHubHelmPackage
for k, score := range packageScores {
if useStars && highestStarPackageName != nil && k == *highestStarPackageName {
klog.V(10).Infof("adding a point to the highest star package: %s:%s", k.Repository, k.Name)
score++ // Add a point to the highest star package
}
if score > highScore {
highScore = score
highScorePackage = p
highScorePackage = packagesByName[k]
}
}
klog.V(10).Infof("highScore for '%s': %f, highScorePackage Repo: %s", clusterRelease.Chart.Metadata.Name, highScore, highScorePackage.Repository.Name)
Expand Down Expand Up @@ -122,7 +156,7 @@ func scoreChartSimilarity(release *release.Release, pkg ArtifactHubHelmPackage)
klog.V(10).Infof("+1.5 score for %s, preferred repo (ahub package repo %s)", release.Chart.Metadata.Name, pkg.Repository.Name)
ret += 1.5
}
klog.V(10).Infof("calculated score repo: %s, release: %s, score: %f\n\n", pkg.Repository.Name, release.Name, ret)
klog.V(10).Infof("calculated score repo: %s, release: %s, stars: %d, score: %f\n\n", pkg.Repository.Name, release.Name, pkg.Stars, ret)
return ret
}

Expand Down