load distributions from api
[opensuse:software-o-o.git] / app / helpers / search_helper.rb
1 module SearchHelper
2   def shorten_description(text, chars)
3     text.sub /^(.{0,#{chars}}\b).*/m, '\1'
4   end
5
6   def prepare_desc(desc)
7     desc.gsub(/([\w.]+)@([\w.]+)/,'\1 [at] xxx').gsub(/\n/, "<br/>")
8   end
9
10   # returns an array of tokens: :prev, :next, :dots or 
11   # actual page
12   def paginator_list(page_count, current_page)
13     range = 3 #show 4 pages around current page
14     list = Array.new
15     return list if page_count == 1
16     
17     list << :prev unless current_page == 1
18
19     # add start block unless midrange includes first page
20     if current_page > range+1
21       1.upto current_page-range-1 do |page|
22         if page == range+2
23           list << :dots
24           break
25         end
26         list << page
27       end
28     end
29
30     midstart = current_page-range < 1 ? 1 : current_page-range
31     midend = page_count-range < current_page ? page_count : current_page+range
32     midstart.upto midend do |page|
33       list << page
34     end
35
36     list << :dots if midend < page_count-range-1
37
38     if page_count > current_page+range
39       (page_count-range).upto page_count do |page|
40         next if page <= midend
41         list << page
42       end
43     end
44
45     list << :next if current_page != page_count
46     return list
47   end
48
49   def default_baseproject
50     'openSUSE:11.2'
51   end
52
53
54   def top_downloads
55     Rails.cache.fetch('top_downloads', :expires_in => 12.hours) do
56       list=ActiveRecord::Base.connection.execute('select query, count(*) as c from download_histories where query is NOT NULL group by query order by c desc limit 200;')
57       queries=Hash.new
58       list.each do |entry|
59         s = entry[0].strip.downcase
60         queries[s] ||= 0
61         queries[s] += entry[1].to_i
62       end
63
64       queries = queries.to_a.sort { |x,y| y[1] <=> x[1] }
65       tops = Array.new
66       count = 0
67       queries.each do |q,c|
68         tops << { :query => q, :count => c}
69         break if count > 15
70         count += 1
71       end
72       tops
73     end
74   end
75 end