mixed_sources.go 6.5 KB

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