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)
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
todo/check: rename to GithubRepoStats or RepoStats - why? why not?.
Instance Method Summary collapse
- #created_at ⇒ Object
-
#fetch(gh) ⇒ Object
fetch / read / write methods.
- #full_name ⇒ Object
- #history ⇒ Object
-
#initialize(full_name) ⇒ Stats
constructor
A new instance of Stats.
- #pushed_at ⇒ Object
-
#read(data_dir: './data') ⇒ Object
note: use read instead of load (load is kind of keyword for loading code).
- #size ⇒ Object
- #stars ⇒ Object
- #updated_at ⇒ Object
- #write(data_dir: './data') ⇒ Object
Constructor Details
#initialize(full_name) ⇒ Stats
Returns a new instance of Stats.
12 13 14 15 |
# File 'lib/hubba/stats.rb', line 12 def initialize( full_name ) @data = {} @data['full_name'] = full_name # e.g. poole/hyde etc. end |
Instance Attribute Details
#data ⇒ Object (readonly)
todo/check: rename to GithubRepoStats or RepoStats - why? why not?
10 11 12 |
# File 'lib/hubba/stats.rb', line 10 def data @data end |
Instance Method Details
#created_at ⇒ Object
19 |
# File 'lib/hubba/stats.rb', line 19 def created_at() @data['created_at']; end |
#fetch(gh) ⇒ Object
fetch / read / write methods
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 |
# File 'lib/hubba/stats.rb', line 53 def fetch( gh ) ## update stats / fetch data from github via api puts "fetching #{full_name}..." repo = gh.repo( full_name ) ## 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'] # size in kb (kilobyte) rec = {} puts "stargazers_count" puts repo.data['stargazers_count'] rec['stargazers_count'] = repo.data['stargazers_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) @data[ 'history' ] ||= {} @data[ 'history' ][ today ] = rec ########################## ## also check / keep track of (latest) commit commits = gh.repo_commits( full_name ) puts "last commit/update:" ## pp commits commit = { 'committer' => { 'date' => commits.data[0]['commit']['committer']['date'], 'name' => commits.data[0]['commit']['committer']['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] pp @data end |
#full_name ⇒ Object
17 |
# File 'lib/hubba/stats.rb', line 17 def full_name() @data['full_name']; end |
#history ⇒ Object
23 |
# File 'lib/hubba/stats.rb', line 23 def history() @data['history']; end |
#pushed_at ⇒ Object
21 |
# File 'lib/hubba/stats.rb', line 21 def pushed_at() @data['pushed_at']; end |
#read(data_dir: './data') ⇒ Object
note: use read instead of load (load is kind of keyword for loading code)
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/hubba/stats.rb', line 107 def read( data_dir: './data' ) ## note: use read instead of load (load is kind of keyword for loading code) ## note: skip reading if file not present basename = full_name.gsub( '/', '~' ) ## e.g. poole/hyde become poole~hyde filename = "#{data_dir}/#{basename}.json" if File.exist?( filename ) puts "reading (loading) from #{basename}..." json = File.open( filename, 'r:utf-8' ) { |file| file.read } ## todo/fix: use read_utf8 @data = JSON.parse( json ) else puts "skipping reading (loading) from #{basename} -- file not found" end end |
#size ⇒ Object
26 27 28 29 |
# File 'lib/hubba/stats.rb', line 26 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hubba/stats.rb', line 31 def stars ## return last stargazers_count entry (as number; 0 if not found) t1 = 0 if history history_keys = history.keys.sort.reverse ## todo/fix: for now assumes one entry per week ## simple case [0] and [1] for a week later ## check actual date - why? why not? stats_t1 = history_keys[0] ? history[ history_keys[0] ] : nil if stats_t1 t1 = stats_t1['stargazers_count'] || 0 end end t1 end |
#updated_at ⇒ Object
20 |
# File 'lib/hubba/stats.rb', line 20 def updated_at() @data['updated_at']; end |
#write(data_dir: './data') ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/hubba/stats.rb', line 99 def write( data_dir: './data' ) basename = full_name.gsub( '/', '~' ) ## e.g. poole/hyde become poole~hyde puts "writing (saving) to #{basename}..." File.open( "#{data_dir}/#{basename}.json", 'w:utf-8' ) do |f| f.write JSON.pretty_generate( data ) end end |