<commit_before>class StatsController < ApplicationController

  before_filter :set_time_zone_to_utc
  before_filter :load_params, except: [:year, :generate_year]
  before_filter :authenticate_user!, only: [:cnc2017_taxa, :cnc2017_stats, :generate_year]
  before_filter :allow_external_iframes, only: [:wed_bioblitz]

  caches_action :summary, expires_in: 1.hour
  caches_action :observation_weeks_json, expires_in: 1.day
  caches_action :nps_bioblitz, expires_in: 5.minutes
  caches_action :cnc2016, expires_in: 5.minutes
  caches_action :cnc2017, expires_in: 5.minutes

  def index
    respond_to do |format|
      format.json {
        fetch_statistics
        render json: @stats, except: :id, callback: params[:callback]
      }
      format.html {
        if params[:start_date].nil?
          @start_date = @end_date - 1.year
        end
        fetch_statistics
        render layout: 'bootstrap'
      }
    end
  end

  def summary
    params = { verifiable: true, per_page: 1 }
    params[:place_id] = @site.place_id if @site.place_id
    observations = INatAPIService.observations(params)
    observers = INatAPIService.observations_observers(params)
    species_counts = INatAPIService.observations_species_counts(params)
    user_count_scope = User.where("suspended_at IS NULL")
    if @site.name != "iNaturalist.org"
      user_count_scope = user_count_scope.where(site_id: @site.id)
    end
    @stats = {
      total_users: user_count_scope.count,
      total_leaf_taxa: (species_counts && species_counts.total_results) || 0,
      total_observations: (observations && observations.total_results) || 0,
      total_observed_taxa: (species_counts && species_counts.total_results) || 0,
      total_observers: (observers && observers.total_results) || 0,
      updated_at: Time.now
    }
    respond_to do |format|
      format.json { render json: @stats}
    end
  end

  def year
    @year = params[:year].to_i
    if @year > Date.today.year || @year < 1950
      return render_404
    end
    @display_user = User.find_by_login( params[:login] )
    if !params[:login].blank? && !@display_user
      return render_404
    end
    @year_statistic = if @display_user
      YearStatistic.where( "user_id = ? AND year = ?", @display_user, @year ).first
    elsif Site.default.try(:id) == @site.id
      YearStatistic.where( "user_id IS NULL and site_id IS NULL and year = ?", @year ).first
    else
      YearStatistic.where( site_id: @site, year: @year ).where( "user_id IS NULL" ).first
    end
    unless @year_statistic
      return render_404
    end
    @headless = @footless = true
    @shareable_image_url = if @year_statistic && @year_statistic.shareable_image?
      @year_statistic.shareable_image
    elsif @display_user && @display_user.icon?
      @display_user.icon.url(:large)
    else
      @site.logo_square.url
    end<commit_msg>People should be able to render their own year stats. Oi.<commit_after>class StatsController < ApplicationController

  before_filter :set_time_zone_to_utc
  before_filter :load_params, except: [:year, :generate_year]
  before_filter :authenticate_user!, only: [:cnc2017_taxa, :cnc2017_stats, :generate_year]
  before_filter :allow_external_iframes, only: [:wed_bioblitz]

  caches_action :summary, expires_in: 1.hour
  caches_action :observation_weeks_json, expires_in: 1.day
  caches_action :nps_bioblitz, expires_in: 5.minutes
  caches_action :cnc2016, expires_in: 5.minutes
  caches_action :cnc2017, expires_in: 5.minutes

  def index
    respond_to do |format|
      format.json {
        fetch_statistics
        render json: @stats, except: :id, callback: params[:callback]
      }
      format.html {
        if params[:start_date].nil?
          @start_date = @end_date - 1.year
        end
        fetch_statistics
        render layout: 'bootstrap'
      }
    end
  end

  def summary
    params = { verifiable: true, per_page: 1 }
    params[:place_id] = @site.place_id if @site.place_id
    observations = INatAPIService.observations(params)
    observers = INatAPIService.observations_observers(params)
    species_counts = INatAPIService.observations_species_counts(params)
    user_count_scope = User.where("suspended_at IS NULL")
    if @site.name != "iNaturalist.org"
      user_count_scope = user_count_scope.where(site_id: @site.id)
    end
    @stats = {
      total_users: user_count_scope.count,
      total_leaf_taxa: (species_counts && species_counts.total_results) || 0,
      total_observations: (observations && observations.total_results) || 0,
      total_observed_taxa: (species_counts && species_counts.total_results) || 0,
      total_observers: (observers && observers.total_results) || 0,
      updated_at: Time.now
    }
    respond_to do |format|
      format.json { render json: @stats}
    end
  end

  def year
    @year = params[:year].to_i
    if @year > Date.today.year || @year < 1950
      return render_404
    end
    @display_user = User.find_by_login( params[:login] )
    if !params[:login].blank? && !@display_user
      return render_404
    end
    @year_statistic = if @display_user
      YearStatistic.where( "user_id = ? AND year = ?", @display_user, @year ).first
    elsif Site.default.try(:id) == @site.id
      YearStatistic.where( "user_id IS NULL and site_id IS NULL and year = ?", @year ).first
    else
      YearStatistic.where( site_id: @site, year: @year ).where( "user_id IS NULL" ).first
    end
    @headless = @footless = true
    @shareable_image_url = if @year_statistic && @year_statistic.shareable_image?
      @year_statistic.shareable_image
    elsif @display_user && @display_user.icon?
      @display_user.icon.url(:large)
    else
      @site.logo_square.url
    end
{'fraction_non_alphanumeric': 0.10211946050096339, 'fraction_numerical': 0.01348747591522158, 'mean_word_length': 3.4749216300940438, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2537296', 'n_tokens_mistral': 2002, 'n_tokens_neox': 1895, 'n_words': 410}