<commit_before># see http://datatables.net/usage/server-side for details on how datatables work, params, etc
=begin
HOW TO EDIT THE DATATABLE

1. Update index.html.haml
2. Update PetitionsDataTable.data
  a. This may require updating PetitionStatistics
3. Update PetitionsDataTable.totals
4. Update PetitionsDataTable.sort_column.columns
  a. This may require updating PetitionStatistics
5. Add/remove a sorting column from petitions.js.coffee if required
=end

class PetitionsDatatable
  delegate :params, :h, :float_to_percentage, :format_date_time, :link_to, :petition_path, to: :@view
    
  def initialize(view, statistics_builder)
    @view = view
    @statistics_builder = statistics_builder
  end
  
  def petitions
    @petitions ||= @statistics_builder.all_since_and_ordered(time_span, sort_column, sort_direction)
  end
  
  def as_json(options = {})
    formatted_data = Kaminari.paginate_array(data).page(page).per(per_page)
    count = petitions.count
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: count,
      iTotalDisplayRecords: count,
      aaData: formatted_data + [totals]
    }
  end
  
  def dpct(numerator, denominator, percentify=true)
    fn = lambda {|x| percentify ? float_to_percentage(x) : x.to_s[0..4]}
    n = denominator.nonzero? ? numerator / denominator.to_f : 0.0
    "<span title='#{numerator}'>" + fn.call(n) + "</span>"
  end
  
  def data
    petitions.map do |petition|
      [
        link_to(petition.petition_title, petition_path(petition.petition_id)),
        h(petition.sent_emails_count).to_i,
        dpct(petition.opened_emails_count, petition.sent_emails_count),
        dpct(petition.clicked_emails_count, petition.sent_emails_count),
        dpct(petition.signed_from_emails_count, petition.sent_emails_count),
        dpct(petition.like_count, petition.sent_emails_count),
        dpct(petition.hit_count, petition.sent_emails_count, false),
        dpct(petition.new_members_count, petition.sent_emails_count),
        dpct(petition.unsubscribes_count, petition.sent_emails_count),
        h(format_date_time(petition.petition_created_at)),
      ]
    end
  end
  
  def totals
    petition = PetitionStatisticsTotals.new(petitions)
    [
      'All petitions',
      h(petition.sent_emails_count).to_i,
      dpct(petition.opened_emails_count, petition.sent_emails_count),<commit_msg>No longer calculating rates on PetitionDatatable. Using the pre-calculated data from the reports table instead.<commit_after># see http://datatables.net/usage/server-side for details on how datatables work, params, etc
=begin
HOW TO EDIT THE DATATABLE

1. Update index.html.haml
2. Update PetitionsDataTable.data
  a. This may require updating PetitionStatistics
3. Update PetitionsDataTable.totals
4. Update PetitionsDataTable.sort_column.columns
  a. This may require updating PetitionStatistics
5. Add/remove a sorting column from petitions.js.coffee if required
=end

class PetitionsDatatable
  delegate :params, :h, :float_to_percentage, :format_date_time, :link_to, :petition_path, to: :@view
    
  def initialize(view, statistics_builder)
    @view = view
    @statistics_builder = statistics_builder
  end
  
  def petitions
    @petitions ||= @statistics_builder.all_since_and_ordered(time_span, sort_column, sort_direction)
  end
  
  def as_json(options = {})
    formatted_data = Kaminari.paginate_array(data).page(page).per(per_page)
    count = petitions.count
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: count,
      iTotalDisplayRecords: count,
      aaData: formatted_data + [totals]
    }
  end
  
  def dpct(numerator, denominator, percentify=true)
    fn = lambda {|x| percentify ? float_to_percentage(x) : x.to_s[0..4]}
    n = denominator.nonzero? ? numerator / denominator.to_f : 0.0
    "<span title='#{numerator}'>" + fn.call(n) + "</span>"
  end

  def format_rate(petition_report, property, percentify=true)
    count = petition_report.send(:"#{property}_count")
    rate  = petition_report.send(:"#{property}_rate")
    display_rate = percentify ? float_to_percentage(rate) : rate.round(3).to_s
    "<span title='#{count}'>#{display_rate}</span>"
  end

  def data
    petitions.map do |petition|
      [
        link_to(petition.petition_title, petition_path(petition.petition_id)),
        h(petition.sent_emails_count).to_i,
        format_rate(petition, :opened_emails),
        format_rate(petition, :clicked_emails),
        format_rate(petition, :signed_from_emails),
        format_rate(petition, :like),
        format_rate(petition, :hit, false),
        format_rate(petition, :new_members),
        format_rate(petition, :unsubscribes),
        h(format_date_time(petition.petition_created_at)),
      ]
    end
  end
  
  def totals
    petition = PetitionStatisticsTotals.new(petitions)
    [
      'All petitions',
      h(petition.sent_emails_count).to_i,
      dpct(petition.opened_emails_count, petition.sent_emails_count),
{'fraction_non_alphanumeric': 0.10198300283286119, 'fraction_numerical': 0.0038445973290165926, 'mean_word_length': 3.9778449144008055, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730711', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1599, 'n_words': 324}