<commit_before>module Hyrax
  class AdminStatsPresenter
    attr_reader :limit, :stats_filters

    def initialize(stats_filters, limit)
      @stats_filters = stats_filters
      @limit = limit
    end

    def start_date
      @start_date ||= Time.zone.parse(stats_filters[:start_date]).beginning_of_day if stats_filters[:start_date].present?
    end

    def end_date
      @end_date ||= Time.zone.parse(stats_filters[:end_date]).end_of_day if stats_filters[:end_date].present?
    end

    def depositors
      @depositors ||= Hyrax::Statistics::Depositors::Summary.new(start_date, end_date).depositors
    end

    def recent_users
      @recent_users ||= stats.recent_users
    end

    def active_users
      @active_users ||= Hyrax::Statistics::Works::ByDepositor.new(limit).query
    end

    def top_formats
      @top_formats ||= Hyrax::Statistics::FileSets::ByFormat.new(limit).query
    end

    def works_count
      @works_count ||= Hyrax::Statistics::Works::Count.new(start_date, end_date).by_permission
    end

    def date_filter_string
      if start_date.blank?
        "unfiltered"
      elsif end_date.blank?
        "#{start_date.to_date.to_formatted_s(:standard)} to #{Date.current.to_formatted_s(:standard)}"
      else<commit_msg>Extracting common method to reduce repetition<commit_after>module Hyrax
  class AdminStatsPresenter
    attr_reader :limit, :stats_filters

    def initialize(stats_filters, limit)
      @stats_filters = stats_filters
      @limit = limit
    end

    def start_date
      @start_date ||= extract_date_from_stats_filters(key: :start_date, as_of: :beginning_of_day)
    end

    def end_date
      @end_date ||= extract_date_from_stats_filters(key: :end_date, as_of: :end_of_day)
    end

    private

      def extract_date_from_stats_filters(key:, as_of:)
        return unless stats_filters[key].present?
        Time.zone.parse(stats_filters[key]).public_send(as_of)
      end

    public

    def depositors
      @depositors ||= Hyrax::Statistics::Depositors::Summary.new(start_date, end_date).depositors
    end

    def recent_users
      @recent_users ||= stats.recent_users
    end

    def active_users
      @active_users ||= Hyrax::Statistics::Works::ByDepositor.new(limit).query
    end

    def top_formats
      @top_formats ||= Hyrax::Statistics::FileSets::ByFormat.new(limit).query
    end

    def works_count
      @works_count ||= Hyrax::Statistics::Works::Count.new(start_date, end_date).by_permission
    end

    def date_filter_string
      if start_date.blank?
        "unfiltered"
      elsif end_date.blank?
        "#{start_date.to_date.to_formatted_s(:standard)} to #{Date.current.to_formatted_s(:standard)}"
      else
{'fraction_non_alphanumeric': 0.10129390018484288, 'fraction_numerical': 0.0, 'mean_word_length': 3.5786802030456855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6192738', 'n_tokens_mistral': 905, 'n_tokens_neox': 863, 'n_words': 150}