Class: Hubba::ReportTrafficReferrers
- Defined in:
- lib/hubba/reports/reports/traffic_referrers.rb
Instance Method Summary collapse
Methods inherited from Report
Constructor Details
This class inherits a constructor from Hubba::Report
Instance Method Details
#build ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/hubba/reports/reports/traffic_referrers.rb', line 6 def build ## note: orgs is orgs+users e.g. geraldb, yorobot etc buf = String.new('') buf << "# Traffic Referrers" buf << " - #{@stats.repos.size} Repos @ #{@stats.orgs.size} Orgs" buf << "\n\n" buf << "popular referrer sources over the last 14 days - page views / unique\n" buf << "\n" ### step 1: filter all repos w/o traffic summary repos = @stats.repos.select do |repo| traffic = repo.stats.traffic || {} summary = traffic['summary'] || {} referrers = summary['referrers'] res = referrers && referrers.size > 0 ## return true if present and non-empty array too puts " no traffic/summary/referrers - skipping >#{repo.full_name}<..." unless res res end ## collect all referrers entries lines = [] repos.each do |repo| summary = repo.stats.traffic['summary'] # e.g. # "referrers" => # [{"referrer"=>"github.com", "count"=>327, "uniques"=>198}, # {"referrer"=>"openfootball.github.io", "count"=>71, "uniques"=>54}, # {"referrer"=>"Google", "count"=>5, "uniques"=>5}, # {"referrer"=>"reddit.com", "count"=>4, "uniques"=>4}] referrers = summary['referrers'] if referrers lines += referrers.map do |referrer| # note: return a new copy with (repo) path added referrer.merge( 'path' => repo.full_name ) end end end ## sort by 1) count ## 2) uniques ## 3) a-z referrer ## 4) a-z path lines = lines.sort do |l,r| res = r['count'] <=> l['count'] res = r['uniques'] <=> l['uniques'] if res == 0 res = l['referrer'] <=> r['referrer'] if res == 0 res = l['path'] <=> r['path'] if res == 0 res end lines_by_referrer = lines.group_by { |line| line['referrer'] } .sort { |(lreferrer,llines), (rreferrer,rlines)| lcount = llines.reduce(0) {|sum,line| sum+line['count'] } rcount = rlines.reduce(0) {|sum,line| sum+line['count'] } res = rcount <=> lcount res = llines.size <=> rlines.size if res == 0 res = lreferrer <=> rreferrer if res == 0 res } .to_h ## convert back to hash ### start with summary ## limit to top 10 or top 20 - why? why not? lines_by_referrer.each_with_index do |(referrer, lines),i| count = lines.reduce(0) {|sum,line| sum+line['count']} buf << "#{i+1}. **#{referrer}** #{count} _(#{lines.size})_" buf << "\n" end buf << "<!-- break -->\n" ## markdown hack: add a list end marker buf << "\n\n" buf << "Details:" buf << "\n\n" lines_by_referrer.each_with_index do |(referrer, lines),i| count = lines.reduce(0) {|sum,line| sum+line['count']} buf << "#{i+1}. **#{referrer}** #{count} _(#{lines.size})_" buf << "\n" ### todo - sort by count / uniques !! lines.each do |line| ## note: sublist indent four (4) spaces buf << " - #{line['count']} / #{line['uniques']} -- #{line['path']}" buf << "\n" end end buf << "<!-- break -->\n" ## markdown hack: add a list end marker buf << "\n\n" ### all referrer sources / records by page views buf << "All referrers:" buf << "\n\n" lines.each_with_index do |line,i| buf << "- #{line['referrer']} -- #{line['count']} / #{line['uniques']} -- #{line['path']}" buf << "\n" end buf << "<!-- break -->\n" ## markdown hack: add a list end marker buf << "\n\n" buf end |