Class: Hubba::ReportTraffic

Inherits:
Report
  • Object
show all
Defined in:
lib/hubba/reports/reports/traffic.rb

Instance Method Summary collapse

Methods inherited from Report

#initialize, #save

Constructor Details

This class inherits a constructor from Hubba::Report

Instance Method Details

#buildObject



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
# File 'lib/hubba/reports/reports/traffic.rb', line 6

def build


## note: orgs is orgs+users e.g. geraldb, yorobot etc
buf = String.new('')
buf << "# Traffic"
buf << " - #{@stats.repos.size} Repos @ #{@stats.orgs.size} Orgs"
buf << "\n\n"

buf << "traffic over the last 14 days - page views / unique, clones / 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'] || {}

  res = summary['views'] && summary['clones']  ## return true if present
  puts "    no traffic/summary/{views,clones} - skipping >#{repo.full_name}<..."   unless res
  res
end


repos = repos.sort do |l,r|
  lsummary = l.stats.traffic['summary']
  rsummary = r.stats.traffic['summary']

  ## note: use reverse sort (right,left) - e.g. most page views first
  res = rsummary['views']['count']    <=> lsummary['views']['count']
  res = rsummary['views']['uniques']  <=> lsummary['views']['uniques']    if res == 0
  res = rsummary['clones']['count']   <=> lsummary['clones']['count']     if res == 0
  res = rsummary['clones']['uniques'] <=> lsummary['clones']['uniques']   if res == 0
  res = l.full_name                   <=> r.full_name                     if res == 0
  res
end


repos_by_org = repos.group_by { |repo|
                                 #  csvreader/csvreader" =>
                                 #   csvreader
                                 repo.owner   # user username / org | login
                               }
                         .sort { |(lowner,lrepos), (rowner,rrepos)|
                                 lviews  = lrepos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['views']['count'] }
                                 rviews  = rrepos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['views']['count'] }
                                 lclones = lrepos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['clones']['count'] }
                                 rclones = rrepos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['clones']['count'] }
                                 res = rviews      <=> lviews
                                 res = rclones     <=> lclones     if res == 0
                                 res = lrepos.size <=> rrepos.size if res == 0
                                 res = lowner      <=> rowner      if res == 0
                                 res
                              }
                        .to_h  ## convert back to hash


### start with summary
##  limit to top 10 or top 20 - why? why not?
repos_by_org.each_with_index do |(owner, repos),i|
  views  = repos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['views']['count'] }
  clones = repos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['clones']['count'] }

  buf << "#{i+1}. **#{owner}** views: #{views}, clones: #{clones}  _(#{repos.size})_"
  buf << "\n"
end

buf << "<!-- break -->\n"   ## markdown hack: add a list end marker
buf << "\n\n"



buf << "Details:"   ## markdown hack: add a list end marker
buf << "\n\n"

repos_by_org.each_with_index do |(owner, repos),i|
  views  = repos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['views']['count'] }
  clones = repos.reduce(0) {|sum,repo| sum+repo.stats.traffic['summary']['clones']['count'] }

  buf << "#{i+1}. **#{owner}** views: #{views}, clones: #{clones}  _(#{repos.size})_"
  buf << "\n"

  ### todo - sort by count / uniques !!
  repos.each do |repo|

    summary = repo.stats.traffic['summary']

    ## note: sublist indent four (4) spaces
    buf << "    - #{repo.name} -- "
    buf << " views: #{summary['views']['count']} / #{summary['views']['uniques']} - "
    buf << " clones: #{summary['clones']['count']} / #{summary['clones']['uniques']}"
    buf << "\n"
  end
end

buf << "<!-- break -->\n"   ## markdown hack: add a list end marker
buf << "\n\n"



### all page paths
buf << "All repos:"
buf << "\n\n"


## pp repos

repos.each_with_index do |repo,i|
  summary = repo.stats.traffic['summary']

  buf << "#{i+1}.  **#{repo.full_name}** -- "
  buf << " views: #{summary['views']['count']} / #{summary['views']['uniques']} - "
  buf << " clones: #{summary['clones']['count']} / #{summary['clones']['uniques']}"
  buf << "\n"
end
buf << "<!-- break -->\n"   ## markdown hack: add a list end marker
buf << "\n\n"


buf
end