admins_controller.rb 4.24 KB
Newer Older
1 2
# frozen_string_literal: true

3
class AdminsController < Admin::AdminController
4 5 6 7 8
  include ApplicationHelper

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

10
  def user_search
11 12 13 14 15 16 17 18
    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
19
    @users ||= []
20 21
  end

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

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

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

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

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

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

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

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

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

78 79
    @user_count = User.count

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

danielgrippi's avatar
danielgrippi committed
83 84
  end

85
  private
86

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

danielgrippi's avatar
danielgrippi committed
91 92
  def create_hash(model, opts={})
    opts[:range] ||= 1.day
93 94 95
    plural = model.to_s.underscore.pluralize
    eval(<<DATA
      @#{plural} = {
danielgrippi's avatar
danielgrippi committed
96 97
        :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
98
      }
99
      @#{plural}[:change] = percent_change(@#{plural}[:yesterday], @#{plural}[:day_before])
100 101
DATA
    )
102
  end
103 104 105


  class UserSearch
106
    include ActiveModel::Model
107 108 109 110 111 112 113 114 115 116 117 118
    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

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

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

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

      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
145
end