Class: GitHubIssueStats

Inherits:
Object
  • Object
show all
Defined in:
lib/github_issue_stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, verbose = false) ⇒ GitHubIssueStats

Returns a new instance of GitHubIssueStats.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/github_issue_stats.rb', line 40

def initialize(token, verbose=false)
  @logger = Logger.new(STDERR)
  @logger.sev_threshold = verbose ? Logger::DEBUG : Logger::WARN

  @logger.debug "Creating new GitHubIssueStats instance."

  @logger.debug "Creating a new Octokit client with token #{token[0..5]}"

  begin
    @client = Octokit::Client.new(:access_token => token)
    @client.rate_limit
  rescue Octokit::Unauthorized => exception
    @logger.error "Token #{token[0..5]} is not valid"
    raise ArgumentError.new("Token #{token[0..5]} is not valid")
  end

  @logger.debug "Token #{token[0..5]} is valid"
end

Instance Attribute Details

#clientObject

Octokit client for acesing the API



36
37
38
# File 'lib/github_issue_stats.rb', line 36

def client
  @client
end

#loggerObject

Octokit client for acesing the API



36
37
38
# File 'lib/github_issue_stats.rb', line 36

def logger
  @logger
end

#sleep_periodObject

Octokit client for acesing the API



36
37
38
# File 'lib/github_issue_stats.rb', line 36

def sleep_period
  @sleep_period
end

Instance Method Details

#compute_previous_time(current_time, period) ⇒ Object

Computes the the beginning of the period based on the end of a period



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/github_issue_stats.rb', line 232

def compute_previous_time(current_time, period)
  period_number, period_type = period.chars
  period_number = Integer(period_number)

  if period_type == "h"
    return current_time - period_number * 3600
  elsif period_type == "d"
    return current_time - period_number * 3600 * 24
  elsif period_type == "w"
    return current_time - 7 * 3600 * 24
  elsif period_type == "m"
    current_date = Date.new(current_time.year, current_time.month, current_time.day)
    previous_date = current_date.prev_month
    previous_time = Time.new(previous_date.year, previous_date.month, previous_date.day, current_time.hour, current_time.min, current_time.sec, "+00:00")
  elsif period_type == "y"
    return Time.new(current_time.year - 1, current_time.month, current_time.day, current_time.hour, current_time.min, current_time.sec, "+00:00")
  else
    # TODO throw error
  end
end

#generate_tables(stats, options) ⇒ Object

Generates tables for collected statistics, for easy copy-pasting



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
414
415
416
417
418
419
420
# File 'lib/github_issue_stats.rb', line 329

def generate_tables(stats, options)
  def get_headers(labels, scope, output_format)
    if output_format == "markdown"
      return labels.map do |label|
        query_string = get_search_query_string({:scope => scope, :label => label, :state => "open"})
        "[#{label}](#{get_search_url(query_string)})"
      end
    else
      return labels
    end
  end

  def get_period_humanized_name(slice, period_type, index)
    names = {
      "h" => ["Now", "1 hour ago", "hours"],
      "d" => ["Today", "Yesterday", "days"],
      "w" => ["This week", "Last week", "weeks"],
      "m" => ["This month", "Last month", "months"],
      "y" => ["This year", "Last year", "years"]
    }

    if index < 2
      return names[period_type][index]
    else
      return "#{index} #{names[period_type][2]} ago"
    end
  end

  def get_period_date(slice, period_type)
    if period_type == "h"
      return slice[:previous_timestamp].strftime "%Y-%m-%d %H:00"
    elsif period_type == "d"
      return slice[:previous_timestamp].strftime "%Y-%m-%d"
    elsif period_type == "w"
      return slice[:previous_timestamp].strftime "%Y-%m-%d"
    elsif period_type == "m"
      return slice[:previous_timestamp].strftime "%Y-%m"
    elsif period_type == "y"
      return slice[:previous_timestamp].strftime "%Y"
    else
      # TODO throw error
    end
  end

  def get_period_name(slice, interval, index, type)
    period_number, period_type = interval.chars
    if type == "markdown"
      return "**#{get_period_humanized_name(slice, period_type, index)}** <br>(#{get_period_date(slice, period_type)})"
    else
      return "#{get_period_humanized_name(slice, period_type, index)} (#{get_period_date(slice, period_type)})"
    end
  end

  def get_period_stats(slice, labels, scope, type)
    def get_difference_string(stats)
      difference_string = "+#{stats[:interval_new_total]}, -#{stats[:interval_closed_total]}"

      # TODO: maybe something like this in the future
      # difference = stats[:interval_new_total] - stats[:interval_closed_total]
      # difference_string = "#{difference}, +#{stats[:interval_new_total]}, -#{stats[:interval_closed_total]}"
      #
      # return "▲" + difference_string if difference > 0
      # return "▼" + difference_string if difference < 0
      # return "▶" + difference_string
    end

    if type == "markdown"
      return labels.map do |label|
        "**#{slice[scope][label][:interval_end_total]}** <br>(#{get_difference_string(slice[scope][label])})"
      end
    else
      return labels.map do |label|
        "#{slice[scope][label][:interval_end_total]} (#{get_difference_string(slice[scope][label])})"
      end
    end
  end

  tables = {}

  for scope in options[:scopes]
    data = []

    data << ["period"] + get_headers(options[:labels], scope, options[:output_format])
    stats.each_with_index do |slice, index|
      data << [get_period_name(slice, options[:interval_length], index, options[:output_format])] + get_period_stats(slice, options[:labels], scope, options[:output_format])
    end

    tables[scope] = options[:output_format] == "markdown" ? data.to_markdown_table : data.to_table(:first_row_is_head => true).to_s
  end

  return tables
end

#get_beginning_of_current_period(current_time, period) ⇒ Object

Returns the timestamps for the beginning of the current period



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/github_issue_stats.rb', line 209

def get_beginning_of_current_period(current_time, period)
  period_type = period[1]

  if period_type == "h"
    return Time.new(current_time.year, current_time.month, current_time.day, current_time.hour, 0, 0, "+00:00")
  elsif period_type == "d"
    return Time.new(current_time.year, current_time.month, current_time.day, 0, 0, 0, "+00:00")
  elsif period_type == "w"
    current_date = Date.new(current_time.year, current_time.month, current_time.day)
    previous_date = current_date - (current_date.cwday - 1)
    previous_time = Time.new(previous_date.year, previous_date.month, previous_date.day, 0, 0, 0, "+00:00")
  elsif period_type == "m"
    return Time.new(current_time.year, current_time.month, 1, 0, 0, 0, "+00:00")
  elsif period_type == "y"
    return Time.new(current_time.year, 1, 1, 0, 0, 0, "+00:00")
  else
    # TODO throw error
  end
end

#get_difference_string(stats) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
# File 'lib/github_issue_stats.rb', line 383

def get_difference_string(stats)
  difference_string = "+#{stats[:interval_new_total]}, -#{stats[:interval_closed_total]}"

  # TODO: maybe something like this in the future
  # difference = stats[:interval_new_total] - stats[:interval_closed_total]
  # difference_string = "#{difference}, +#{stats[:interval_new_total]}, -#{stats[:interval_closed_total]}"
  #
  # return "▲" + difference_string if difference > 0
  # return "▼" + difference_string if difference < 0
  # return "▶" + difference_string
end

#get_headers(labels, scope, output_format) ⇒ Object



330
331
332
333
334
335
336
337
338
339
# File 'lib/github_issue_stats.rb', line 330

def get_headers(labels, scope, output_format)
  if output_format == "markdown"
    return labels.map do |label|
      query_string = get_search_query_string({:scope => scope, :label => label, :state => "open"})
      "[#{label}](#{get_search_url(query_string)})"
    end
  else
    return labels
  end
end

#get_period_date(slice, period_type) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/github_issue_stats.rb', line 357

def get_period_date(slice, period_type)
  if period_type == "h"
    return slice[:previous_timestamp].strftime "%Y-%m-%d %H:00"
  elsif period_type == "d"
    return slice[:previous_timestamp].strftime "%Y-%m-%d"
  elsif period_type == "w"
    return slice[:previous_timestamp].strftime "%Y-%m-%d"
  elsif period_type == "m"
    return slice[:previous_timestamp].strftime "%Y-%m"
  elsif period_type == "y"
    return slice[:previous_timestamp].strftime "%Y"
  else
    # TODO throw error
  end
end

#get_period_humanized_name(slice, period_type, index) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/github_issue_stats.rb', line 341

def get_period_humanized_name(slice, period_type, index)
  names = {
    "h" => ["Now", "1 hour ago", "hours"],
    "d" => ["Today", "Yesterday", "days"],
    "w" => ["This week", "Last week", "weeks"],
    "m" => ["This month", "Last month", "months"],
    "y" => ["This year", "Last year", "years"]
  }

  if index < 2
    return names[period_type][index]
  else
    return "#{index} #{names[period_type][2]} ago"
  end
end

#get_period_name(slice, interval, index, type) ⇒ Object



373
374
375
376
377
378
379
380
# File 'lib/github_issue_stats.rb', line 373

def get_period_name(slice, interval, index, type)
  period_number, period_type = interval.chars
  if type == "markdown"
    return "**#{get_period_humanized_name(slice, period_type, index)}** <br>(#{get_period_date(slice, period_type)})"
  else
    return "#{get_period_humanized_name(slice, period_type, index)} (#{get_period_date(slice, period_type)})"
  end
end

#get_period_stats(slice, labels, scope, type) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/github_issue_stats.rb', line 382

def get_period_stats(slice, labels, scope, type)
  def get_difference_string(stats)
    difference_string = "+#{stats[:interval_new_total]}, -#{stats[:interval_closed_total]}"

    # TODO: maybe something like this in the future
    # difference = stats[:interval_new_total] - stats[:interval_closed_total]
    # difference_string = "#{difference}, +#{stats[:interval_new_total]}, -#{stats[:interval_closed_total]}"
    #
    # return "▲" + difference_string if difference > 0
    # return "▼" + difference_string if difference < 0
    # return "▶" + difference_string
  end

  if type == "markdown"
    return labels.map do |label|
      "**#{slice[scope][label][:interval_end_total]}** <br>(#{get_difference_string(slice[scope][label])})"
    end
  else
    return labels.map do |label|
      "#{slice[scope][label][:interval_end_total]} (#{get_difference_string(slice[scope][label])})"
    end
  end
end

#get_required_number_of_api_calls(options) ⇒ Object

Computes the number of search API calls to collect all the data



256
257
258
# File 'lib/github_issue_stats.rb', line 256

def get_required_number_of_api_calls(options)
  return options[:scopes].size * options[:labels].size * (2 * options[:interval_count] + 1)
end

#get_search_query_string(options) ⇒ Object

Construct the search query string based on different options.



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
# File 'lib/github_issue_stats.rb', line 286

def get_search_query_string(options)
  query = ""

  if options[:scope].include?("/")
    query += "repo:#{options[:scope]} "
  else
    query += "user:#{options[:scope]} "
  end

  if options[:label] == "issues"
    query += "is:issue "
  elsif options[:label] == "pulls"
    query += "is:pr "
  else
    query += "label:#{options[:label]} "
  end

  if !options[:state].nil?
    query += "is:#{options[:state]} "
  end

  if !options[:created_at].nil?
    query += "created:#{options[:created_at][:from].iso8601()}..#{options[:created_at][:until].iso8601()} "
  end

  if !options[:closed_at].nil?
    query += "closed:#{options[:closed_at][:from].iso8601()}..#{options[:closed_at][:until].iso8601()} "
  end

  return query.strip
end

#get_search_total_results(query_string) ⇒ Object

Call Search API for a query and return total number of results



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/github_issue_stats.rb', line 185

def get_search_total_results(query_string)
  sleep_before_api_call()

  @logger.debug "Getting search results for query: #{query_string}"

  # Print something just so the user know something is going on
  if @logger.sev_threshold != Logger::DEBUG
    STDERR.print(".")
    STDERR.flush
  end

  result = @client.search_issues(query_string)
  @logger.debug "Total count: #{result.total_count}"

  if result.incomplete_results
    @logger.error "Incomplete search API results for query #{query_string}"
  end

  return result.total_count
end

#get_search_url(query_string) ⇒ Object

Returns the github.com URL for viewing the list of issues which match the given query string



322
323
324
# File 'lib/github_issue_stats.rb', line 322

def get_search_url(query_string)
  return "https://github.com/issues?q=#{query_string}"
end

#get_statistics(options) ⇒ Object

Collect and return statistics

Input:

options =

:interval_length => "1w",             # 1 week interval
:interval_count => 2,                 # 2 intervals to collect data for
:scopes => ["atom", "atom/atom"],     # atom user and atom/atom repo
:labels => ["issues", "pulls", "bug"] # issues, pulls, and bug label

Output:

[

{                                     # each interval will be represented as hash
  :interval_end_timestamp => Time,    # end of interval
  :interval_start_timestamp => Time,  # beginning of interval
  "atom" => {                         # each scope will have a key and hash value
     "issues" => {                    # each label will have a key and hash value
       :interval_end_total => 1,      # number of items at end of period
       :interval_beginning_total => 2,# number of items at beginning of period
       :interval_new_total => 3,        # number of new items during period
       :interval_closed_total => 4      # number of closed items during period
     }
   }
}

]



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/github_issue_stats.rb', line 88

def get_statistics(options)
  # number_of_calls = get_required_number_of_api_calls(options)
  # @sleep_period = get_api_calls_sleep(number_of_calls)

  stats = []
  for i in 1..options[:interval_count]
    stats << get_stats_for_interval(stats[-1], options)
  end

  return stats
end

#get_stats_for_interval(previous_slice, options) ⇒ Object

Collects statistics for a single interval



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
129
130
131
132
133
134
135
136
137
138
139
140
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/github_issue_stats.rb', line 103

def get_stats_for_interval(previous_slice, options)
  slice = {}

  # set timestamps

  if previous_slice.nil? # initial
    slice[:current_timestamp] = Time.now.utc
    slice[:previous_timestamp] = get_beginning_of_current_period(slice[:current_timestamp], options[:interval_length])
  else # not initial
    slice[:current_timestamp] = previous_slice[:previous_timestamp]
    slice[:previous_timestamp] = compute_previous_time(slice[:current_timestamp], options[:interval_length])
  end

  for scope in options[:scopes]
    scope_stats = {}
    slice[scope] = scope_stats

    for label in options[:labels]
      label_stats = {}
      scope_stats[label] = label_stats

      # current state

      search_options = {
        :scope => scope,
        :label => label,
        :state => "open"
      }

      if previous_slice.nil?
        query_string = get_search_query_string(search_options)
        label_stats[:interval_end_total_url] = get_search_url(query_string)
        label_stats[:interval_end_total] = get_search_total_results(query_string)
      else
        label_stats[:interval_end_total] = previous_slice[scope][label][:interval_beginning_total]
      end

      # number of new issues in period

      search_options = {
        :scope => scope,
        :label => label,
        :created_at => {
          :from => slice[:previous_timestamp],
          :until => slice[:current_timestamp]
        }
      }

      query_string = get_search_query_string(search_options)
      label_stats[:interval_new_total_url] = get_search_url(query_string)
      label_stats[:interval_new_total] = get_search_total_results(query_string)

      # number of closed issues in period

      search_options = {
        :scope => scope,
        :label => label,
        :state => "closed",
        :closed_at => {
          :from => slice[:previous_timestamp],
          :until => slice[:current_timestamp]
        }
      }

      query_string = get_search_query_string(search_options)
      label_stats[:interval_closed_total_url] = get_search_url(query_string)
      label_stats[:interval_closed_total] = get_search_total_results(query_string)

      # number of issues in previous period

      label_stats[:interval_beginning_total] = label_stats[:interval_end_total] + label_stats[:interval_closed_total] - label_stats[:interval_new_total]

      @logger.debug "Computed total at beginning of interval: #{label_stats[:interval_beginning_total]}"
    end
  end

  return slice
end

#sleep_before_api_callObject

Computes the required sleep period to avoid hitting the API rate limits



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/github_issue_stats.rb', line 263

def sleep_before_api_call()
  @logger.debug "Calculating sleep period for next search API call"

  rate_limit_data = @client.get("https://api.github.com/rate_limit")

  if rate_limit_data[:resources][:core][:remaining] == 0
    reset_timestamp = rate_limit_data[:resources][:core][:reset]
    sleep_seconds = reset_timestamp - Time.now.to_i
    @logger.warn "Remaining regular API rate limit is 0, sleeping for #{sleep_seconds} seconds."
    sleep(sleep_seconds)
  elsif rate_limit_data[:resources][:search][:remaining] == 0
    reset_timestamp = rate_limit_data[:resources][:search][:reset]
    sleep_seconds = reset_timestamp - Time.now.to_i
    @logger.warn "Remaining search API rate limit is 0, sleeping for #{sleep_seconds} seconds."
    sleep(sleep_seconds)
  elsif
    sleep(1)
  end
end