mixed_sources.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package query
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "github.com/Jguer/aur"
  10. "github.com/Jguer/go-alpm/v2"
  11. "github.com/adrg/strutil"
  12. "github.com/adrg/strutil/metrics"
  13. "github.com/leonelquinteros/gotext"
  14. "github.com/Jguer/yay/v12/pkg/db"
  15. "github.com/Jguer/yay/v12/pkg/intrange"
  16. "github.com/Jguer/yay/v12/pkg/settings/parser"
  17. "github.com/Jguer/yay/v12/pkg/stringset"
  18. "github.com/Jguer/yay/v12/pkg/text"
  19. )
  20. const sourceAUR = "aur"
  21. type Builder interface {
  22. Len() int
  23. Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string)
  24. Results(w io.Writer, dbExecutor db.Executor, verboseSearch SearchVerbosity) error
  25. GetTargets(include, exclude intrange.IntRanges, otherExclude stringset.StringSet) ([]string, error)
  26. }
  27. type MixedSourceQueryBuilder struct {
  28. results []abstractResult
  29. sortBy string
  30. searchBy string
  31. targetMode parser.TargetMode
  32. queryMap map[string]map[string]interface{}
  33. bottomUp bool
  34. singleLineResults bool
  35. aurClient aur.QueryClient
  36. logger *text.Logger
  37. }
  38. func NewMixedSourceQueryBuilder(
  39. aurClient aur.QueryClient,
  40. logger *text.Logger,
  41. sortBy string,
  42. targetMode parser.TargetMode,
  43. searchBy string,
  44. bottomUp,
  45. singleLineResults bool,
  46. ) *MixedSourceQueryBuilder {
  47. return &MixedSourceQueryBuilder{
  48. aurClient: aurClient,
  49. logger: logger,
  50. bottomUp: bottomUp,
  51. sortBy: sortBy,
  52. targetMode: targetMode,
  53. searchBy: searchBy,
  54. singleLineResults: singleLineResults,
  55. queryMap: map[string]map[string]interface{}{},
  56. results: make([]abstractResult, 0, 100),
  57. }
  58. }
  59. type abstractResult struct {
  60. source string
  61. name string
  62. description string
  63. votes int
  64. provides []string
  65. }
  66. type abstractResults struct {
  67. results []abstractResult
  68. search string
  69. distanceCache map[string]float64
  70. bottomUp bool
  71. metric strutil.StringMetric
  72. }
  73. func (a *abstractResults) Len() int { return len(a.results) }
  74. func (a *abstractResults) Swap(i, j int) { a.results[i], a.results[j] = a.results[j], a.results[i] }
  75. func (a *abstractResults) GetMetric(pkg *abstractResult) float64 {
  76. if v, ok := a.distanceCache[pkg.name]; ok {
  77. return v
  78. }
  79. sim := strutil.Similarity(pkg.name, a.search, a.metric)
  80. for _, prov := range pkg.provides {
  81. // If the package provides search, it's a perfect match
  82. // AUR packages don't populate provides
  83. candidate := strutil.Similarity(prov, a.search, a.metric)
  84. if candidate > sim {
  85. sim = candidate
  86. }
  87. }
  88. simDesc := strutil.Similarity(pkg.description, a.search, a.metric)
  89. // slightly overweight sync sources by always giving them max popularity
  90. popularity := 1.0
  91. if pkg.source == sourceAUR {
  92. popularity = float64(pkg.votes) / float64(pkg.votes+60)
  93. }
  94. sim = sim*0.6 + simDesc*0.2 + popularity*0.2
  95. a.distanceCache[pkg.name] = sim
  96. return sim
  97. }
  98. func (a *abstractResults) Less(i, j int) bool {
  99. pkgA := a.results[i]
  100. pkgB := a.results[j]
  101. simA := a.GetMetric(&pkgA)
  102. simB := a.GetMetric(&pkgB)
  103. if a.bottomUp {
  104. return simA < simB
  105. }
  106. return simA > simB
  107. }
  108. func (s *MixedSourceQueryBuilder) Execute(ctx context.Context, dbExecutor db.Executor, pkgS []string) {
  109. var aurErr error
  110. pkgS = RemoveInvalidTargets(pkgS, s.targetMode)
  111. metric := &metrics.JaroWinkler{
  112. CaseSensitive: false,
  113. }
  114. sortableResults := &abstractResults{
  115. results: []abstractResult{},
  116. search: strings.Join(pkgS, ""),
  117. distanceCache: map[string]float64{},
  118. bottomUp: s.bottomUp,
  119. metric: metric,
  120. }
  121. if s.targetMode.AtLeastAUR() {
  122. var aurResults aurQuery
  123. aurResults, aurErr = queryAUR(ctx, s.aurClient, pkgS, s.searchBy)
  124. dbName := sourceAUR
  125. for i := range aurResults {
  126. if s.queryMap[dbName] == nil {
  127. s.queryMap[dbName] = map[string]interface{}{}
  128. }
  129. s.queryMap[dbName][aurResults[i].Name] = aurResults[i]
  130. sortableResults.results = append(sortableResults.results, abstractResult{
  131. source: dbName,
  132. name: aurResults[i].Name,
  133. description: aurResults[i].Description,
  134. provides: aurResults[i].Provides,
  135. votes: aurResults[i].NumVotes,
  136. })
  137. }
  138. }
  139. var repoResults []alpm.IPackage
  140. if s.targetMode.AtLeastRepo() {
  141. repoResults = dbExecutor.SyncPackages(pkgS...)
  142. for i := range repoResults {
  143. dbName := repoResults[i].DB().Name()
  144. if s.queryMap[dbName] == nil {
  145. s.queryMap[dbName] = map[string]interface{}{}
  146. }
  147. s.queryMap[dbName][repoResults[i].Name()] = repoResults[i]
  148. rawProvides := repoResults[i].Provides().Slice()
  149. provides := make([]string, len(rawProvides))
  150. for j := range rawProvides {
  151. provides[j] = rawProvides[j].Name
  152. }
  153. sortableResults.results = append(sortableResults.results, abstractResult{
  154. source: repoResults[i].DB().Name(),
  155. name: repoResults[i].Name(),
  156. description: repoResults[i].Description(),
  157. provides: provides,
  158. votes: -1,
  159. })
  160. }
  161. }
  162. sort.Sort(sortableResults)
  163. s.results = sortableResults.results
  164. if aurErr != nil {
  165. s.logger.Errorln(ErrAURSearch{inner: aurErr})
  166. if len(repoResults) != 0 {
  167. s.logger.Warnln(gotext.Get("Showing repo packages only"))
  168. }
  169. }
  170. }
  171. func (s *MixedSourceQueryBuilder) Results(w io.Writer, dbExecutor db.Executor, verboseSearch SearchVerbosity) error {
  172. for i := range s.results {
  173. if verboseSearch == Minimal {
  174. _, _ = fmt.Fprintln(w, s.results[i].name)
  175. continue
  176. }
  177. var toPrint string
  178. if verboseSearch == NumberMenu {
  179. if s.bottomUp {
  180. toPrint += text.Magenta(strconv.Itoa(len(s.results)-i)) + " "
  181. } else {
  182. toPrint += text.Magenta(strconv.Itoa(i+1)) + " "
  183. }
  184. }
  185. pkg := s.queryMap[s.results[i].source][s.results[i].name]
  186. if s.results[i].source == sourceAUR {
  187. aurPkg := pkg.(aur.Pkg)
  188. toPrint += aurPkgSearchString(&aurPkg, dbExecutor, s.singleLineResults)
  189. } else {
  190. syncPkg := pkg.(alpm.IPackage)
  191. toPrint += syncPkgSearchString(syncPkg, dbExecutor, s.singleLineResults)
  192. }
  193. fmt.Fprintln(w, toPrint)
  194. }
  195. return nil
  196. }
  197. func (s *MixedSourceQueryBuilder) Len() int {
  198. return len(s.results)
  199. }
  200. func (s *MixedSourceQueryBuilder) GetTargets(include, exclude intrange.IntRanges,
  201. otherExclude stringset.StringSet,
  202. ) ([]string, error) {
  203. var (
  204. isInclude = len(exclude) == 0 && len(otherExclude) == 0
  205. targets []string
  206. lenRes = len(s.results)
  207. )
  208. for i := 0; i <= s.Len(); i++ {
  209. // FIXME: this is probably broken
  210. target := i - 1
  211. if s.bottomUp {
  212. target = lenRes - i
  213. }
  214. if (isInclude && include.Get(i)) || (!isInclude && !exclude.Get(i)) {
  215. targets = append(targets, s.results[target].source+"/"+s.results[target].name)
  216. }
  217. }
  218. return targets, nil
  219. }