<commit_before>require_relative '../statistics.rb'

namespace :statistics do
  desc '月次のイベント履歴を集計します'
  task :aggregation, [:from_yyyymm, :to_yyyymm] => :environment do |tasks, args|
    date_from_str = -> (str) {
      d = %w(%Y%m %Y/%m %Y-%m).map { |fmt|
        begin
          Time.zone.strptime(str, fmt)
        rescue ArgumentError
        end
      }.compact.first
      raise ArgumentError, "Invalid format: `#{str}`" if d.nil?
      d
    }

    from = (args[:from_yyyymm] ? date_from_str.call(args[:from_yyyymm]) : Time.current.prev_month).beginning_of_month
    to   = (args[:to_yyyymm]   ? date_from_str.call(args[:to_yyyymm])   : Time.current.prev_month).end_of_month

    EventHistory.where(evented_at: from..to).delete_all

    loop.with_object([from]) { |_, list|
      nm = list.last.next_month
      nm > to ? (break list) : (list << nm)
    }.each { |date|
      Statistics::Aggregation.run(date: date)
    }
  end

  desc 'キーワードからイベント情報を検索します'
  task :search, [:keyword] => :environment do |tasks, args|
    raise ArgumentError, 'Require the keyword' if args[:keyword].nil?

    require 'pp'

    puts 'Searching Connpass'
    pp Statistics::Client::Connpass.new.search(keyword: args[:keyword])

    puts 'Searching Doorkeeper'
    pp Statistics::Client::Doorkeeper.new.search(keyword: args[:keyword])
  end
end<commit_msg>Refactoring for easy reading<commit_after>require_relative '../statistics.rb'

namespace :statistics do
  desc '月次のイベント履歴を集計します'
  task :aggregation, [:from_yyyymm, :to_yyyymm] => :environment do |tasks, args|
    date_from_str = -> (str) {
      d = %w(%Y%m %Y/%m %Y-%m).map { |fmt|
        begin
          Time.zone.strptime(str, fmt)
        rescue ArgumentError
        end
      }.compact.first
      raise ArgumentError, "Invalid format: `#{str}`" if d.nil?
      d
    }

    from = (args[:from_yyyymm] ? date_from_str.call(args[:from_yyyymm]) : Time.current.prev_month).beginning_of_month
    to   = (args[:to_yyyymm]   ? date_from_str.call(args[:to_yyyymm])   : Time.current.prev_month).end_of_month

    EventHistory.where(evented_at: from..to).delete_all

    loop.with_object([from]) { |_, list|
      nm = list.last.next_month
      raise StopIteration if nm > to
      list << nm
    }.each { |date|
      Statistics::Aggregation.run(date: date)
    }
  end

  desc 'キーワードからイベント情報を検索します'
  task :search, [:keyword] => :environment do |tasks, args|
    raise ArgumentError, 'Require the keyword' if args[:keyword].nil?

    require 'pp'

    puts 'Searching Connpass'
    pp Statistics::Client::Connpass.new.search(keyword: args[:keyword])

    puts 'Searching Doorkeeper'
    pp Statistics::Client::Doorkeeper.new.search(keyword: args[:keyword])
  end
end
{'fraction_non_alphanumeric': 0.13932915591596018, 'fraction_numerical': 0.0, 'mean_word_length': 3.328548644338118, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1174104', 'n_tokens_mistral': 1013, 'n_tokens_neox': 986, 'n_words': 191}