class Admin::StatsController

Controlador para las estadísticas en tiempo real de últimas IPs, últimas búsquedas en Google, últimos referers y páginas más visitadas

Public Instance Methods

contents() click to toggle source
# File app/controllers/admin/stats_controller.rb, line 99
def contents
  @days = params[:days] ? params[:days].to_i : 100
  
  if @days == 100
    conditions = nil
    created_at_conditions = nil
    cover_photo_conditions = nil
  elsif @days == 0
    begin
      @start_date = Date.parse("#{params[:start][:year]}-#{params[:start][:month]}-#{params[:start][:day]}")
      @end_date = Date.parse("#{params[:end][:year]}-#{params[:end][:month]}-#{params[:end][:day]}")
      conditions = ["published_at BETWEEN ? AND ?", @start_date, @end_date]
      created_at_conditions = ["created_at BETWEEN ? AND ?", @start_date, @end_date]
      cover_photo_conditions = ["cover_photo_updated_at BETWEEN ? AND ?", @start_date, @end_date]
      @periodo = "#{I18n.localize(@start_date, :format => :long)} - #{I18n.localize(@end_date, :format => :long)}"
    rescue ArgumentError => err
      flash[:error] = "Las fechas elegidas no son correctas"
      redirect_to contents_admin_stats_path and return
    end
  elsif @days > 30
    flash[:error] = "El intervalo elegido no es correcto"
    redirect_to contents_admin_stats_path and return
  else
    conditions = ["published_at BETWEEN now()-?::interval AND now()", "#{@days}days"]
    created_at_conditions = ["created_at BETWEEN now()-?::interval AND now()", "#{@days}days"]
    cover_photo_conditions = ["cover_photo_updated_at BETWEEN now()-?::interval AND now()", "#{@days}days"]
    @periodo = "#{I18n.localize(@days.days.ago.to_date, :format => :long)} - #{I18n.localize(Date.today, :format => :long)}"
  end
  
  @tab = 3
  @news_counter = News.count(:conditions => conditions)
  @exported_to_enet_counter = News.exported_to_enet.count(:conditions => conditions)
  
  @shared_events_counter = Event.count(:conditions => conditions)
  @events_with_streaming = Event.with_streaming.count(:conditions => conditions)
      
  sede_conditions = "send_alerts = 't'" 
  itinerante_conditions = "send_alerts = 'f'"
  conditions_with_sede = conditions.is_a?(Array) ? [conditions[0] + " AND #{sede_conditions}"] + conditions[1..-1] : sede_conditions
  conditions_itinerante = conditions.is_a?(Array) ? [conditions[0] + " AND #{itinerante_conditions}"] + conditions[1..-1] : itinerante_conditions
  
  @events_with_streaming_in_sedes = Event.with_streaming.count(:conditions => conditions_with_sede, :joins => :stream_flow)
  @events_with_streaming_itinerante = Event.with_streaming.count(:conditions => conditions_itinerante, :joins => :stream_flow)
  
  @private_events_counter = ScheduleEvent.count(:conditions => created_at_conditions)
  @pages_counter = Page.count(:conditions => conditions)
  @proposals_counter = Proposal.approved.count(:conditions => conditions)
  # HQ
  # @questions_counter = Question.approved.count(:conditions => conditions)    
  @comments_counter = Comment.count(:conditions => created_at_conditions)
  @photos_counter = Photo.count(:conditions => created_at_conditions) + Document.count(:conditions => cover_photo_conditions)
  @webtv_video_counter = Video.count(:conditions => conditions)
  @attachment_counter = Attachment.count(:conditions => created_at_conditions)
  @escucha_counter = {:total => Headline.count(:conditions => created_at_conditions), :published => Headline.count(:conditions => conditions)}
  
  # FS
  @videos_mpg = Stats::FS.mpg
  @audios_mp3 = Stats::FS.mp3
  
  # Valoracion de relacionados
  @top_voters = RecommendationRating.find :all, 
    :select => "count(recommendation_ratings.id) AS counter, user_id", 
    :group => "user_id", 
    :joins => :user, 
    :limit => 5,
    :order => "counter desc"
end
index() click to toggle source
# File app/controllers/admin/stats_controller.rb, line 25
def index
  @tab = 1
  @last_ips_counter = Stats::CouchDB.last_ips_counter
  @streaming_watchers = Stats::CouchDB.streaming_watchers
  @dep_id = 0
  @hours = params[:hours] ? params[:hours].to_i : 2

  @referers = Stats::CouchDB.last_referers(@hours)
  @googles = Stats::CouchDB.last_googles
  @top_pages = Stats::CouchDB.top_pages(@hours)
  @comments = Comment.find :all, 
    :select => "commentable_id, commentable_type, count(id)", 
    :conditions => ["created_at between now()-?::interval and now()", "#{@hours} hours"],
    :group => "commentable_id, commentable_type",
    :order => "count desc",
    :limit => 10
end
midterm() click to toggle source
# File app/controllers/admin/stats_controller.rb, line 43
def midterm
  @tab = 2
  @days = params[:days] ? params[:days].to_i : 1
  @actual_days = @days
  
  if @days == 0
    begin
      @start_date = Date.parse("#{params[:start][:year]}-#{params[:start][:month]}-#{params[:start][:day]}")
      @end_date = Date.parse("#{params[:end][:year]}-#{params[:end][:month]}-#{params[:end][:day]}")
      @actual_days = (@end_date-@start_date).to_i
      if @actual_days > 275
        flash[:notice] = "Sólo se puede consultar un máximo de 9 meses atrás"
        redirect_to contents_admin_stats_path and return
      end
      @periodo = "#{I18n.localize(@start_date, :format => :long)} - #{I18n.localize(@end_date, :format => :long)}"
    rescue ArgumentError => err
      flash[:error] = "Las fechas elegidas no son correctas"
      redirect_to contents_admin_stats_path and return
    end
  elsif @days > 275
    flash[:error] = "El intervalo elegido no es correcto"
    redirect_to contents_admin_stats_path and return
  else
    @periodo = "#{I18n.localize(@days.days.ago.to_date, :format => :long)} - #{I18n.localize(Date.today, :format => :long)}"
  end
  
  
  
  
  
  # @last_ips_counter = Stats::CouchDB.last_ips_counter
  top_videos = Stats::CouchDB.top_videos(@actual_days)
  @top_videos = top_videos[:videos]
  @videos_sum = top_videos[:sum]        
  
  top_rsss = Stats::CouchDB.top_rsss(@actual_days)
  @top_rsss = top_rsss[:videos]
  @rsss_sum = top_rsss[:sum]
  
  @top_rated_comments = Comment.find :all, 
    :select => "comments.id, substring(comments.body from 0 for 50) as body, commentable_id, count(rating)",
    :joins => :ratings,
    :conditions => ["rating=1 AND created_at BETWEEN now()-?::interval AND now()", "#{@actual_days}days"],
    :group => "comments.id, comments.body, commentable_id",
    :order => "count DESC",
    :limit => 10
    
  @bottom_rated_comments = Comment.find :all, 
    :select => "comments.id, substring(comments.body from 0 for 50) as body, commentable_id, count(rating)",
    :joins => :ratings,
    :conditions => ["rating=0 AND created_at BETWEEN now()-?::interval AND now()", "#{@actual_days}days"],
    :group => "comments.id, comments.body, commentable_id",
    :order => "count DESC",
    :limit => 10    
end