admins_controller.rb 4.3 KB
Newer Older
1
class AdminsController < Admin::AdminController
2 3 4 5 6
  include ApplicationHelper

  def dashboard
    gon.push(pod_version: pod_version)
  end
7

8
  def user_search
9 10 11 12 13 14 15 16
    if params[:admins_controller_user_search]
      search_params = params.require(:admins_controller_user_search)
                            .permit(:username, :email, :guid, :under13)
      @search = UserSearch.new(search_params)
      @users = @search.perform
    end

    @search ||= UserSearch.new
17
    @users ||= []
18 19
  end

20
  def admin_inviter
21 22
    inviter = InvitationCode.default_inviter_or(current_user)
    email = params[:identifier]
23
    user = User.find_by_email(email)
24

25
    unless user
26
      EmailInviter.new(email, inviter).send!
27
      flash[:notice] = "invitation sent to #{email}"
Maxwell Salzberg's avatar
Maxwell Salzberg committed
28
    else
29
      flash[:notice]= "error sending invite to #{email}"
30
    end
Maxwell Salzberg's avatar
Maxwell Salzberg committed
31
    redirect_to user_search_path, :notice => flash[:notice]
32
  end
33

34 35 36 37 38
  def add_invites
    InvitationCode.find_by_token(params[:invite_code_id]).add_invites!
    redirect_to user_search_path
  end

39
  def weekly_user_stats
Fabian Rodriguez's avatar
Fabian Rodriguez committed
40
    @created_users_by_week = Hash.new{ |h,k| h[k] = [] }
41
    @created_users = User.where("username IS NOT NULL and created_at IS NOT NULL")
42
    @created_users.find_each do |u|
Fabian Rodriguez's avatar
Fabian Rodriguez committed
43 44
      week = u.created_at.beginning_of_week.strftime("%Y-%m-%d")
      @created_users_by_week[week] << u.username
45
    end
Fabian Rodriguez's avatar
Fabian Rodriguez committed
46

47
    @selected_week = params[:week] || @created_users_by_week.keys.last
Fabian Rodriguez's avatar
Fabian Rodriguez committed
48
    @counter = @created_users_by_week[@selected_week].count
49 50
  end

51
  def stats
52
    @popular_tags = ActsAsTaggableOn::Tagging.joins(:tag).limit(50).order('count(taggings.id) DESC').group(:tag).count
53

danielgrippi's avatar
danielgrippi committed
54 55 56
    case params[:range]
    when "week"
      range = 1.week
57
      @segment = t('admins.stats.week')
danielgrippi's avatar
danielgrippi committed
58 59
    when "2weeks"
      range = 2.weeks
60
      @segment = t('admins.stats.2weeks')
danielgrippi's avatar
danielgrippi committed
61 62
    when "month"
      range = 1.month
63
      @segment = t('admins.stats.month')
danielgrippi's avatar
danielgrippi committed
64 65
    else
      range = 1.day
66
      @segment = t('admins.stats.daily')
danielgrippi's avatar
danielgrippi committed
67 68
    end

69
    [Post, Comment, AspectMembership, User].each do |model|
danielgrippi's avatar
danielgrippi committed
70
      create_hash(model, :range => range)
71 72
    end

73
    @posts_per_day = Post.where("created_at >= ?", Date.today - 21.days).group("DATE(created_at)").order("DATE(created_at) ASC").count
74 75
    @most_posts_within = @posts_per_day.values.max.to_f

76 77
    @user_count = User.count

78 79 80
    #@posts[:new_public] = Post.where(:type => ['StatusMessage','ActivityStreams::Photo'],
    #                                 :public => true).order('created_at DESC').limit(15).all

danielgrippi's avatar
danielgrippi committed
81 82 83
  end

  def correlations
84
    @correlations_hash = Statistics.new.generate_correlations
85 86 87
  end

  private
88

89 90 91 92
  def percent_change(today, yesterday)
    sprintf( "%0.02f", ((today-yesterday) / yesterday.to_f)*100).to_f
  end

danielgrippi's avatar
danielgrippi committed
93 94
  def create_hash(model, opts={})
    opts[:range] ||= 1.day
95 96 97
    plural = model.to_s.underscore.pluralize
    eval(<<DATA
      @#{plural} = {
danielgrippi's avatar
danielgrippi committed
98 99
        :day_before => #{model}.where(:created_at => ((Time.now.midnight - #{opts[:range]*2})..Time.now.midnight - #{opts[:range]})).count,
        :yesterday => #{model}.where(:created_at => ((Time.now.midnight - #{opts[:range]})..Time.now.midnight)).count
100
      }
101
      @#{plural}[:change] = percent_change(@#{plural}[:yesterday], @#{plural}[:day_before])
102 103
DATA
    )
104
  end
105 106 107


  class UserSearch
108
    include ActiveModel::Model
109 110 111 112 113 114 115 116 117 118 119 120
    include ActiveModel::Conversion
    include ActiveModel::Validations

    attr_accessor :username, :email, :guid, :under13

    validate :any_searchfield_present?

    def initialize(attributes={})
      assign_attributes(attributes)
      yield(self) if block_given?
    end

121 122 123
    def assign_attributes(values)
      values.each do |k, v|
        public_send("#{k}=", v)
124 125 126 127
      end
    end

    def any_searchfield_present?
128
      if %w(username email guid under13).all? { |attr| public_send(attr).blank? }
129 130 131 132 133
        errors.add :base, "no fields for search set"
      end
    end

    def perform
134
      return User.none unless valid?
135 136 137 138 139 140 141 142 143 144 145 146

      users = User.arel_table
      people = Person.arel_table
      profiles = Profile.arel_table
      res = User.joins(person: :profile)
      res = res.where(users[:username].matches("%#{username}%")) unless username.blank?
      res = res.where(users[:email].matches("%#{email}%")) unless email.blank?
      res = res.where(people[:guid].matches("%#{guid}%")) unless guid.blank?
      res = res.where(profiles[:birthday].gt(Date.today-13.years)) if under13 == '1'
      res
    end
  end
147
end