class Hubba::Stats

keep track of repo stats over time (with history hash)

Public Instance Methods

build_history( timeseries ) click to toggle source
# File lib/hubba/reports/stats.rb, line 145
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,
             forks: h['forks_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 ) click to toggle source
# File lib/hubba/reports/stats.rb, line 174
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() click to toggle source

commits

# File lib/hubba/reports/stats.rb, line 57
def commits() @data['commits']; end
committed() click to toggle source
# File lib/hubba/reports/stats.rb, line 68
def committed   ## last commit date (from author NOT committer)
  @cache['committed'] ||= parse_date( last_commit_author_date )
end
committed_at() click to toggle source
# File lib/hubba/reports/stats.rb, line 72
def committed_at()   ## last commit date (from author NOT committer)
  @cache['committed_at'] ||= parse_datetime( last_commit_author_date )
end
created() click to toggle source

date (only) versions

# File lib/hubba/reports/stats.rb, line 24
def created() @cache['created'] ||= parse_date( @data['created_at'] ); end
created_at() click to toggle source

note: return datetime objects (NOT strings); if not present/available return nil/null

# File lib/hubba/reports/stats.rb, line 19
def created_at() @cache['created_at'] ||= parse_datetime( @data['created_at'] ); end
desc()
Alias for: description
descr()
Alias for: description
description() click to toggle source
# File lib/hubba/reports/stats.rb, line 11
def description()  @data['description'] || ''; end
Also aliased as: descr, desc
full_name() click to toggle source

attr_reader :data - needed - why? why not?

# File lib/hubba/reports/stats.rb, line 10
def full_name()    @data['full_name']; end
history() click to toggle source
# File lib/hubba/reports/stats.rb, line 34
def history
  @cache['history'] ||= begin
                          if @data['history']
                            build_history( @data['history'] )
                          else
                            nil
                          end
                        end
end
history_str() click to toggle source
# File lib/hubba/reports/stats.rb, line 203
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
languages() click to toggle source
# File lib/hubba/reports/stats.rb, line 16
def languages()   @data['languages'] || {}; end
last_commit() click to toggle source
# File lib/hubba/reports/stats.rb, line 59
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() click to toggle source
# File lib/hubba/reports/stats.rb, line 76
def last_commit_author_date
  h = last_commit
  h ? h['author']['date'] : nil
end
last_commit_message() click to toggle source
# File lib/hubba/reports/stats.rb, line 82
def last_commit_message    ## convenience shortcut; last commit message
  h = last_commit

  committer_name = h['committer']['name']
  author_name    = h['author']['name']
  message        = h['message']

  buf = ""
  buf << message
  buf << " by #{author_name}"

  if committer_name != author_name
    buf << " w/ #{committer_name}"
  end
end
parse_date( str ) click to toggle source
# File lib/hubba/reports/stats.rb, line 103
def parse_date( str )     str ? Date.strptime( str, '%Y-%m-%d') : nil; end
parse_datetime( str ) click to toggle source

helpers

# File lib/hubba/reports/stats.rb, line 102
def parse_datetime( str ) str ? DateTime.strptime( str, '%Y-%m-%dT%H:%M:%S') : nil; end
pushed() click to toggle source
# File lib/hubba/reports/stats.rb, line 26
def pushed()  @cache['pushed']  ||= parse_date( @data['pushed_at'] ); end
pushed_at() click to toggle source
# File lib/hubba/reports/stats.rb, line 21
def pushed_at()  @cache['pushed_at']  ||= parse_datetime( @data['pushed_at'] );  end
size() click to toggle source
# File lib/hubba/reports/stats.rb, line 28
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() click to toggle source
# File lib/hubba/reports/stats.rb, line 45
def stars
  ## return last stargazers_count entry (as number; 0 if not found)
  @cache['stars'] ||= history ? history[0].stars : 0
end
topics() click to toggle source
# File lib/hubba/reports/stats.rb, line 15
def topics()      @data['topics'] || []; end
traffic() click to toggle source

traffic

# File lib/hubba/reports/stats.rb, line 52
def traffic() @data['traffic']; end
updated() click to toggle source
# File lib/hubba/reports/stats.rb, line 25
def updated() @cache['updated'] ||= parse_date( @data['updated_at'] ); end
updated_at() click to toggle source
# File lib/hubba/reports/stats.rb, line 20
def updated_at() @cache['updated_at'] ||= parse_datetime( @data['updated_at'] ); end