lib/github_issue_stats.rb
#
# Generates tables for collected statistics, for easy copy-pasting
#
Generates tables for collected statistics, for easy copy-pasting
Generates tables for collected statistics for easy copy - pasting
def generate_history_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
      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]}"
         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" ? generate_markdown_table_string(data) : generate_text_table_string(data)
    end
    return tables
  end
def generate_history_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 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]}" 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" ? generate_markdown_table_string ( data ) : generate_text_table_string ( data ) end return tables end
Generates tables for collected statistics, for easy copy-pasting
Generates tables for collected statistics for easy copy - pasting
# TODO throw error # 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
stats
options