|
| 1 | +package snapshot |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/openbootdotdev/openboot/internal/config" |
| 5 | +) |
| 6 | + |
| 7 | +// MatchPackages compares the snapshot's installed packages against the catalog. |
| 8 | +// Returns a CatalogMatch with matched/unmatched packages and match rate. |
| 9 | +func MatchPackages(snap *Snapshot) *CatalogMatch { |
| 10 | + catalogSet := make(map[string]bool) |
| 11 | + for _, cat := range config.Categories { |
| 12 | + for _, pkg := range cat.Packages { |
| 13 | + catalogSet[pkg.Name] = true |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + allPkgs := append([]string{}, snap.Packages.Formulae...) |
| 18 | + allPkgs = append(allPkgs, snap.Packages.Casks...) |
| 19 | + |
| 20 | + matched := []string{} |
| 21 | + unmatched := []string{} |
| 22 | + |
| 23 | + for _, pkg := range allPkgs { |
| 24 | + if catalogSet[pkg] { |
| 25 | + matched = append(matched, pkg) |
| 26 | + } else { |
| 27 | + unmatched = append(unmatched, pkg) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + matchRate := 0.0 |
| 32 | + if len(allPkgs) > 0 { |
| 33 | + matchRate = float64(len(matched)) / float64(len(allPkgs)) |
| 34 | + } |
| 35 | + |
| 36 | + return &CatalogMatch{ |
| 37 | + Matched: matched, |
| 38 | + Unmatched: unmatched, |
| 39 | + MatchRate: matchRate, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// DetectBestPreset finds the preset with highest Jaccard similarity to the snapshot. |
| 44 | +// Returns preset name if similarity >= 0.3, otherwise returns empty string. |
| 45 | +func DetectBestPreset(snap *Snapshot) string { |
| 46 | + snapshotSet := make(map[string]bool) |
| 47 | + for _, pkg := range snap.Packages.Formulae { |
| 48 | + snapshotSet[pkg] = true |
| 49 | + } |
| 50 | + for _, pkg := range snap.Packages.Casks { |
| 51 | + snapshotSet[pkg] = true |
| 52 | + } |
| 53 | + |
| 54 | + snapshotPkgs := make([]string, 0, len(snapshotSet)) |
| 55 | + for pkg := range snapshotSet { |
| 56 | + snapshotPkgs = append(snapshotPkgs, pkg) |
| 57 | + } |
| 58 | + |
| 59 | + bestPreset := "" |
| 60 | + bestSimilarity := 0.0 |
| 61 | + |
| 62 | + for presetName, preset := range config.Presets { |
| 63 | + presetPkgs := append([]string{}, preset.CLI...) |
| 64 | + presetPkgs = append(presetPkgs, preset.Cask...) |
| 65 | + |
| 66 | + similarity := jaccardSimilarity(snapshotPkgs, presetPkgs) |
| 67 | + |
| 68 | + if similarity > bestSimilarity { |
| 69 | + bestSimilarity = similarity |
| 70 | + bestPreset = presetName |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if bestSimilarity >= 0.3 { |
| 75 | + return bestPreset |
| 76 | + } |
| 77 | + |
| 78 | + return "" |
| 79 | +} |
| 80 | + |
| 81 | +// jaccardSimilarity computes |A ∩ B| / |A ∪ B| for two string slices. |
| 82 | +func jaccardSimilarity(setA, setB []string) float64 { |
| 83 | + mapA := make(map[string]bool) |
| 84 | + for _, item := range setA { |
| 85 | + mapA[item] = true |
| 86 | + } |
| 87 | + |
| 88 | + mapB := make(map[string]bool) |
| 89 | + for _, item := range setB { |
| 90 | + mapB[item] = true |
| 91 | + } |
| 92 | + |
| 93 | + intersection := 0 |
| 94 | + for item := range mapA { |
| 95 | + if mapB[item] { |
| 96 | + intersection++ |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + union := len(mapA) + len(mapB) - intersection |
| 101 | + |
| 102 | + if union == 0 { |
| 103 | + return 0.0 |
| 104 | + } |
| 105 | + |
| 106 | + return float64(intersection) / float64(union) |
| 107 | +} |
0 commit comments