Class: Hubba::Stats
- Inherits:
-
Object
- Object
- Hubba::Stats
- Defined in:
- lib/hubba/stats.rb
Overview
keep track of repo stats over time (with history hash)
Defined Under Namespace
Classes: HistoryItem
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
todo/check: rename to GithubRepoStats or RepoStats - why? why not?.
Instance Method Summary collapse
- #build_history(timeseries) ⇒ Object
- #calc_diff_stars(samples: 3, days: 30) ⇒ Object
- #commits ⇒ Object
-
#committed ⇒ Object
last commit date (from author NOT committer).
-
#committed_at ⇒ Object
last commit date (from author NOT committer).
-
#created ⇒ Object
date (only) versions.
-
#created_at ⇒ Object
note: return datetime objects (NOT strings); if not present/available return nil/null.
- #full_name ⇒ Object
- #history ⇒ Object
-
#history_str ⇒ Object
todo/check: rename/change to format_history or fmt_history - why? why not?.
-
#initialize(full_name) ⇒ Stats
constructor
A new instance of Stats.
-
#last_commit ⇒ Object
convenience shortcut; get first/last commit (use [0]) or nil.
- #last_commit_author_date ⇒ Object
-
#last_commit_message ⇒ Object
convenience shortcut; last commit message.
- #parse_date(str) ⇒ Object
-
#parse_datetime(str) ⇒ Object
helpers.
- #pushed ⇒ Object
- #pushed_at ⇒ Object
- #read ⇒ Object
- #size ⇒ Object
- #stars ⇒ Object
- #update(repo, commits: nil, topics: nil) ⇒ Object
-
#update_traffic(clones: nil, views: nil, paths: nil, referrers: nil) ⇒ Object
update.
- #updated ⇒ Object
- #updated_at ⇒ Object
-
#write ⇒ Object
read / write methods / helpers.
Constructor Details
#initialize(full_name) ⇒ Stats
Returns a new instance of Stats.
10 11 12 13 14 15 |
# File 'lib/hubba/stats.rb', line 10 def initialize( full_name ) @data = {} @data['full_name'] = full_name # e.g. poole/hyde etc. @cache = {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
todo/check: rename to GithubRepoStats or RepoStats - why? why not?
8 9 10 |
# File 'lib/hubba/stats.rb', line 8 def data @data end |
Instance Method Details
#build_history(timeseries) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/hubba/stats.rb', line 141 def build_history( timeseries ) items = [] keys = timeseries.keys.sort.reverse ## newest (latest) items first keys.each do |key| h = timeseries[ key ] item = HistoryItem.new( date: Date.strptime( key, '%Y-%m-%d' ), stars: h['stargazers_count'] || 0 ) ## link items last_item = items[-1] last_item.append( item ) if last_item ## if not nil? append (note first item has no prev item) items << item end ## todo/check: return [] for empty items array (items.empty?) - why?? why not?? if items.empty? nil else items end end |
#calc_diff_stars(samples: 3, days: 30) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/hubba/stats.rb', line 169 def calc_diff_stars( samples: 3, days: 30 ) ## samples: use n history item samples e.g. 3 samples ## days e.g. 7 days (per week), 30 days (per month) if history.nil? nil ## todo/check: return 0.0 too - why? why not? elsif history.size == 1 ## just one item; CANNOT calc diff; return zero 0.0 else idx = [history.size, samples].min ## calc last index last = history[idx-1] first = history[0] diff_days = first.date.jd - last.date.jd diff_stars = first.stars - last.stars ## note: use factor 1000 for fixed integer division ## converts to float at the end ## todo: check for better way (convert to float upfront - why? why not?) diff = (diff_stars * days * 1000) / diff_days ## puts "diff=#{diff}:#{diff.class.name}" ## check if it's a float (diff.to_f/1000.0) end end |
#commits ⇒ Object
54 |
# File 'lib/hubba/stats.rb', line 54 def commits() @data['commits']; end |
#committed ⇒ Object
last commit date (from author NOT committer)
65 66 67 |
# File 'lib/hubba/stats.rb', line 65 def committed ## last commit date (from author NOT committer) @cache['committed'] ||= parse_date( ) end |
#committed_at ⇒ Object
last commit date (from author NOT committer)
69 70 71 |
# File 'lib/hubba/stats.rb', line 69 def committed_at() ## last commit date (from author NOT committer) @cache['committed_at'] ||= parse_datetime( ) end |
#created ⇒ Object
date (only) versions
27 |
# File 'lib/hubba/stats.rb', line 27 def created() @cache['created'] ||= parse_date( @data['created_at'] ); end |
#created_at ⇒ Object
note: return datetime objects (NOT strings); if not present/available return nil/null
22 |
# File 'lib/hubba/stats.rb', line 22 def created_at() @cache['created_at'] ||= parse_datetime( @data['created_at'] ); end |
#full_name ⇒ Object
18 |
# File 'lib/hubba/stats.rb', line 18 def full_name() @data['full_name']; end |
#history ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/hubba/stats.rb', line 37 def history @cache['history'] ||= begin if @data['history'] build_history( @data['history'] ) else nil end end end |
#history_str ⇒ Object
todo/check: rename/change to format_history or fmt_history - why? why not?
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/hubba/stats.rb', line 198 def history_str ## todo/check: rename/change to format_history or fmt_history - why? why not? ## returns "pretty printed" history as string buffer buf = '' buf << "[#{history.size}]: " history.each do |item| buf << "#{item.stars}" diff_stars = item.diff_stars diff_days = item.diff_days if diff_stars && diff_days ## note: last item has no diffs if diff_stars > 0 || diff_stars < 0 if diff_stars > 0 buf << " (+#{diff_stars}" else buf << " (#{diff_stars}" end buf << " in #{diff_days}d) " else ## diff_stars == 0 buf << " (#{diff_days}d) " end end end buf end |
#last_commit ⇒ Object
convenience shortcut; get first/last commit (use [0]) or nil
56 57 58 59 60 61 62 |
# File 'lib/hubba/stats.rb', line 56 def last_commit ## convenience shortcut; get first/last commit (use [0]) or nil if @data['commits'] && @data['commits'][0] @data['commits'][0] else nil end end |
#last_commit_author_date ⇒ Object
73 74 75 76 |
# File 'lib/hubba/stats.rb', line 73 def h = last_commit h ? h['author']['date'] : nil end |
#last_commit_message ⇒ Object
convenience shortcut; last commit message
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/hubba/stats.rb', line 79 def ## convenience shortcut; last commit message h = last_commit committer_name = h['committer']['name'] = h['author']['name'] = h['message'] buf = "" buf << buf << " by #{}" if committer_name != buf << " w/ #{committer_name}" end end |
#parse_date(str) ⇒ Object
100 |
# File 'lib/hubba/stats.rb', line 100 def parse_date( str ) str ? Date.strptime( str, '%Y-%m-%d') : nil; end |
#parse_datetime(str) ⇒ Object
helpers
99 |
# File 'lib/hubba/stats.rb', line 99 def parse_datetime( str ) str ? DateTime.strptime( str, '%Y-%m-%dT%H:%M:%S') : nil; end |
#pushed ⇒ Object
29 |
# File 'lib/hubba/stats.rb', line 29 def pushed() @cache['pushed'] ||= parse_date( @data['pushed_at'] ); end |
#pushed_at ⇒ Object
24 |
# File 'lib/hubba/stats.rb', line 24 def pushed_at() @cache['pushed_at'] ||= parse_datetime( @data['pushed_at'] ); end |
#read ⇒ Object
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/hubba/stats.rb', line 432 def read ## note: skip reading if file not present basename = full_name.gsub( '/', '~' ) ## e.g. poole/hyde become poole~hyde data_dir = Hubba.config.data_dir path = "#{data_dir}/#{basename}.json" if File.exist?( path ) puts " reading stats from #{basename} (#{data_dir})..." json = File.open( path, 'r:utf-8' ) { |f| f.read } @data = JSON.parse( json ) ## reset (invalidate) cached values from data hash ## use after reading or fetching @cache = {} else puts "!! WARN: - skipping reading stats from #{basename} -- file not found" end self ## return self for (easy chaining) end |
#size ⇒ Object
31 32 33 34 |
# File 'lib/hubba/stats.rb', line 31 def size # size of repo in kb (as reported by github api) @data['size'] || 0 ## return 0 if not found - why? why not? (return nil - why? why not??) end |
#stars ⇒ Object
48 49 50 51 |
# File 'lib/hubba/stats.rb', line 48 def stars ## return last stargazers_count entry (as number; 0 if not found) @cache['stars'] ||= history ? history[0].stars : 0 end |
#update(repo, commits: nil, topics: nil) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/hubba/stats.rb', line 321 def update( repo, commits: nil, topics: nil ) ## update stats / fetch data from github via api raise ArgumentError, "Github::Resource expected; got #{repo.class.name}" unless repo.is_a?( Github::Resource ) ## e.g. 2015-05-11T20:21:43Z ## puts Time.iso8601( repo.data['created_at'] ) @data['created_at'] = repo.data['created_at'] @data['updated_at'] = repo.data['updated_at'] @data['pushed_at'] = repo.data['pushed_at'] @data['size'] = repo.data['size'] # note: size in kb (kilobyte) @data['description'] = repo.data['description'] @data['language'] = repo.data['language'] ## note: might be nil!!! ######################################## #### history / by date record rec = {} rec['stargazers_count'] = repo.data['stargazers_count'] rec['forks_count'] = repo.data['forks_count'] today = Date.today.strftime( '%Y-%m-%d' ) ## e.g. 2016-09-27 puts "add record #{today} to history..." pp rec # check if stargazers_count is a number (NOT a string) history = @data[ 'history' ] ||= {} item = history[ today ] ||= {} ## note: merge "in-place" (overwrite with new - but keep other key/value pairs if any e.g. pageviews, clones, etc.) item.merge!( rec ) ########################## ## also check / keep track of (latest) commit if commits raise ArgumentError, "Github::Resource expected; got #{commits.class.name}" unless commits.is_a?( Github::Resource ) puts "update - last commit:" ## pp commits commit = { 'committer' => { 'date' => commits.data[0]['commit']['committer']['date'], 'name' => commits.data[0]['commit']['committer']['name'] }, 'author' => { 'date' => commits.data[0]['commit']['author']['date'], 'name' => commits.data[0]['commit']['author']['name'] }, 'message' => commits.data[0]['commit']['message'] } ## for now store only the latest commit (e.g. a single commit in an array) @data[ 'commits' ] = [commit] end if topics raise ArgumentError, "Github::Resource expected; got #{topics.class.name}" unless topics.is_a?( Github::Resource ) puts "update - topics:" ## e.g. # {"names"=> # ["opendata", # "football", # "seriea", # "italia", # "italy", # "juve", # "inter", # "napoli", # "roma", # "sqlite"]} # # {"names"=>[]} @data[ 'topics' ] = topics.data['names'] end pp @data ## reset (invalidate) cached values from data hash ## use after reading or fetching @cache = {} self ## return self for (easy chaining) end |
#update_traffic(clones: nil, views: nil, paths: nil, referrers: nil) ⇒ Object
update
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/hubba/stats.rb', line 229 def update_traffic( clones: nil, views: nil, paths: nil, referrers: nil ) traffic = @data[ 'traffic' ] ||= {} summary = traffic['summary'] ||= {} history = traffic['history'] ||= {} if views raise ArgumentError, "Github::Resource expected; got #{views.class.name}" unless views.is_a?( Github::Resource ) =begin {"count"=>1526, "uniques"=>287, "views"=> [{"timestamp"=>"2020-09-27T00:00:00Z", "count"=>52, "uniques"=>13}, {"timestamp"=>"2020-09-28T00:00:00Z", "count"=>108, "uniques"=>28}, ... ]}> =end ## keep lastest (summary) record of last two weeks (14 days) summary['views'] = { 'count' => views.data['count'], 'uniques' => views.data['uniques'] } ## update history / day-by-day items / timeline views.data['views'].each do |view| # e.g. "2020-09-27T00:00:00Z" = DateTime.strptime( view['timestamp'], '%Y-%m-%dT%H:%M:%S%z' ) item = history[ .strftime( '%Y-%m-%d' ) ] ||= {} ## e.g. 2016-09-27 ## note: merge "in-place" item.merge!( { 'views' => { 'count' => view['count'], 'uniques' => view['uniques'] }} ) end end if clones raise ArgumentError, "Github::Resource expected; got #{clones.class.name}" unless clones.is_a?( Github::Resource ) =begin {"count"=>51, "uniques"=>17, "clones"=> [{"timestamp"=>"2020-09-26T00:00:00Z", "count"=>1, "uniques"=>1}, {"timestamp"=>"2020-09-27T00:00:00Z", "count"=>2, "uniques"=>1}, ... ]} =end ## keep lastest (summary) record of last two weeks (14 days) summary['clones'] = { 'count' => clones.data['count'], 'uniques' => clones.data['uniques'] } ## update history / day-by-day items / timeline clones.data['clones'].each do |clone| # e.g. "2020-09-27T00:00:00Z" = DateTime.strptime( clone['timestamp'], '%Y-%m-%dT%H:%M:%S%z' ) item = history[ .strftime( '%Y-%m-%d' ) ] ||= {} ## e.g. 2016-09-27 ## note: merge "in-place" item.merge!( { 'clones' => { 'count' => clone['count'], 'uniques' => clone['uniques'] }} ) end end if paths raise ArgumentError, "Github::Resource expected; got #{paths.class.name}" unless paths.is_a?( Github::Resource ) =begin [{"path"=>"/openfootball/england", "title"=> "openfootball/england: Free open public domain football data for England (and ...", "count"=>394, "uniques"=>227}, =end summary['paths'] = paths.data end if referrers raise ArgumentError, "Github::Resource expected; got #{referrers.class.name}" unless referrers.is_a?( Github::Resource ) =begin [{"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}] =end summary['referrers'] = referrers.data end end |
#updated ⇒ Object
28 |
# File 'lib/hubba/stats.rb', line 28 def updated() @cache['updated'] ||= parse_date( @data['updated_at'] ); end |
#updated_at ⇒ Object
23 |
# File 'lib/hubba/stats.rb', line 23 def updated_at() @cache['updated_at'] ||= parse_datetime( @data['updated_at'] ); end |
#write ⇒ Object
read / write methods / helpers
419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/hubba/stats.rb', line 419 def write basename = full_name.gsub( '/', '~' ) ## e.g. poole/hyde become poole~hyde data_dir = Hubba.config.data_dir puts " writing stats to #{basename} (#{data_dir})..." ## todo/fix: add FileUtils.makepath_r or such!!! File.open( "#{data_dir}/#{basename}.json", 'w:utf-8' ) do |f| f.write JSON.pretty_generate( data ) end self ## return self for (easy chaining) end |