1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
class AdminsController < Admin::AdminController
use_bootstrap_for :user_search, :weekly_user_stats, :stats, :correlations
def user_search
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
@users ||= []
end
def admin_inviter
inviter = InvitationCode.default_inviter_or(current_user)
email = params[:identifier]
user = User.find_by_email(email)
unless user
EmailInviter.new(email, inviter).send!
flash[:notice] = "invitation sent to #{email}"
else
flash[:notice]= "error sending invite to #{email}"
end
redirect_to user_search_path, :notice => flash[:notice]
end
def add_invites
InvitationCode.find_by_token(params[:invite_code_id]).add_invites!
redirect_to user_search_path
end
def weekly_user_stats
@created_users_by_week = Hash.new{ |h,k| h[k] = [] }
@created_users = User.where("username IS NOT NULL and created_at IS NOT NULL")
@created_users.find_each do |u|
week = u.created_at.beginning_of_week.strftime("%Y-%m-%d")
@created_users_by_week[week] << u.username
end
@selected_week = params[:week] || @created_users_by_week.keys.last
@counter = @created_users_by_week[@selected_week].count
end
def stats
@popular_tags = ActsAsTaggableOn::Tagging.joins(:tag).limit(50).order('count(taggings.id) DESC').group(:tag).count
case params[:range]
when "week"
range = 1.week
@segment = t('admins.stats.week')
when "2weeks"
range = 2.weeks
@segment = t('admins.stats.2weeks')
when "month"
range = 1.month
@segment = t('admins.stats.month')
else
range = 1.day
@segment = t('admins.stats.daily')
end
[Post, Comment, AspectMembership, User].each do |model|
create_hash(model, :range => range)
end
@posts_per_day = Post.where("created_at >= ?", Date.today - 21.days).group("DATE(created_at)").order("DATE(created_at) ASC").count
@most_posts_within = @posts_per_day.values.max.to_f
@user_count = User.count
#@posts[:new_public] = Post.where(:type => ['StatusMessage','ActivityStreams::Photo'],
# :public => true).order('created_at DESC').limit(15).all
end
def correlations
@correlations_hash = Statistics.new.generate_correlations
end
private
def percent_change(today, yesterday)
sprintf( "%0.02f", ((today-yesterday) / yesterday.to_f)*100).to_f
end
def create_hash(model, opts={})
opts[:range] ||= 1.day
plural = model.to_s.underscore.pluralize
eval(<<DATA
@#{plural} = {
: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
}
@#{plural}[:change] = percent_change(@#{plural}[:yesterday], @#{plural}[:day_before])
DATA
)
end
class UserSearch
include ActiveModel::Model
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
def assign_attributes(values)
values.each do |k, v|
public_send("#{k}=", v)
end
end
def any_searchfield_present?
if %w(username email guid under13).all? { |attr| public_send(attr).blank? }
errors.add :base, "no fields for search set"
end
end
def perform
return User.none unless valid?
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
end
|