Description:
report ip
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r442:ce576ec8e8c3 - - 2 files changed: 10 inserted, 2 deleted

@@ -1,189 +1,193
1 class ReportController < ApplicationController
1 class ReportController < ApplicationController
2
2
3 before_filter :admin_authorization, only: [:login_stat,:submission_stat]
3 before_filter :admin_authorization, only: [:login_stat,:submission_stat]
4 before_filter(only: [:problem_hof]) { |c|
4 before_filter(only: [:problem_hof]) { |c|
5 return false unless authenticate
5 return false unless authenticate
6
6
7 if GraderConfiguration["right.user_view_submission"]
7 if GraderConfiguration["right.user_view_submission"]
8 return true;
8 return true;
9 end
9 end
10
10
11 admin_authorization
11 admin_authorization
12 }
12 }
13
13
14 def login_stat
14 def login_stat
15 @logins = Array.new
15 @logins = Array.new
16
16
17 date_and_time = '%Y-%m-%d %H:%M'
17 date_and_time = '%Y-%m-%d %H:%M'
18 begin
18 begin
19 @since_time = DateTime.strptime(params[:since_datetime],date_and_time)
19 @since_time = DateTime.strptime(params[:since_datetime],date_and_time)
20 rescue
20 rescue
21 @since_time = DateTime.new(1000,1,1)
21 @since_time = DateTime.new(1000,1,1)
22 end
22 end
23 begin
23 begin
24 @until_time = DateTime.strptime(params[:until_datetime],date_and_time)
24 @until_time = DateTime.strptime(params[:until_datetime],date_and_time)
25 rescue
25 rescue
26 @until_time = DateTime.new(3000,1,1)
26 @until_time = DateTime.new(3000,1,1)
27 end
27 end
28
28
29 User.all.each do |user|
29 User.all.each do |user|
30 @logins << { id: user.id,
30 @logins << { id: user.id,
31 login: user.login,
31 login: user.login,
32 full_name: user.full_name,
32 full_name: user.full_name,
33 count: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
33 count: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
34 user.id,@since_time,@until_time)
34 user.id,@since_time,@until_time)
35 .count(:id),
35 .count(:id),
36 min: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
36 min: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
37 user.id,@since_time,@until_time)
37 user.id,@since_time,@until_time)
38 .minimum(:created_at),
38 .minimum(:created_at),
39 max: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
39 max: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
40 user.id,@since_time,@until_time)
40 user.id,@since_time,@until_time)
41 - .maximum(:created_at)
41 + .maximum(:created_at),
42 + ip: Login.where("user_id = ? AND created_at >= ? AND created_at <= ?",
43 + user.id,@since_time,@until_time)
44 + .select(:ip_address).uniq
45 +
42 }
46 }
43 end
47 end
44 end
48 end
45
49
46 def submission_stat
50 def submission_stat
47
51
48 date_and_time = '%Y-%m-%d %H:%M'
52 date_and_time = '%Y-%m-%d %H:%M'
49 begin
53 begin
50 @since_time = DateTime.strptime(params[:since_datetime],date_and_time)
54 @since_time = DateTime.strptime(params[:since_datetime],date_and_time)
51 rescue
55 rescue
52 @since_time = DateTime.new(1000,1,1)
56 @since_time = DateTime.new(1000,1,1)
53 end
57 end
54 begin
58 begin
55 @until_time = DateTime.strptime(params[:until_datetime],date_and_time)
59 @until_time = DateTime.strptime(params[:until_datetime],date_and_time)
56 rescue
60 rescue
57 @until_time = DateTime.new(3000,1,1)
61 @until_time = DateTime.new(3000,1,1)
58 end
62 end
59
63
60 @submissions = {}
64 @submissions = {}
61
65
62 User.find_each do |user|
66 User.find_each do |user|
63 @submissions[user.id] = { login: user.login, full_name: user.full_name, count: 0, sub: { } }
67 @submissions[user.id] = { login: user.login, full_name: user.full_name, count: 0, sub: { } }
64 end
68 end
65
69
66 Submission.where("submitted_at >= ? AND submitted_at <= ?",@since_time,@until_time).find_each do |s|
70 Submission.where("submitted_at >= ? AND submitted_at <= ?",@since_time,@until_time).find_each do |s|
67 if @submissions[s.user_id]
71 if @submissions[s.user_id]
68 if not @submissions[s.user_id][:sub].has_key?(s.problem_id)
72 if not @submissions[s.user_id][:sub].has_key?(s.problem_id)
69 a = nil
73 a = nil
70 begin
74 begin
71 a = Problem.find(s.problem_id)
75 a = Problem.find(s.problem_id)
72 rescue
76 rescue
73 a = nil
77 a = nil
74 end
78 end
75 @submissions[s.user_id][:sub][s.problem_id] =
79 @submissions[s.user_id][:sub][s.problem_id] =
76 { prob_name: (a ? a.full_name : '(NULL)'),
80 { prob_name: (a ? a.full_name : '(NULL)'),
77 sub_ids: [s.id] }
81 sub_ids: [s.id] }
78 else
82 else
79 @submissions[s.user_id][:sub][s.problem_id][:sub_ids] << s.id
83 @submissions[s.user_id][:sub][s.problem_id][:sub_ids] << s.id
80 end
84 end
81 @submissions[s.user_id][:count] += 1
85 @submissions[s.user_id][:count] += 1
82 end
86 end
83 end
87 end
84 end
88 end
85
89
86 def problem_hof
90 def problem_hof
87 # gen problem list
91 # gen problem list
88 @user = User.find(session[:user_id])
92 @user = User.find(session[:user_id])
89 @problems = @user.available_problems
93 @problems = @user.available_problems
90
94
91 # get selected problems or the default
95 # get selected problems or the default
92 if params[:id]
96 if params[:id]
93 begin
97 begin
94 @problem = Problem.available.find(params[:id])
98 @problem = Problem.available.find(params[:id])
95 rescue
99 rescue
96 redirect_to action: :problem_hof
100 redirect_to action: :problem_hof
97 flash[:notice] = 'Error: submissions for that problem are not viewable.'
101 flash[:notice] = 'Error: submissions for that problem are not viewable.'
98 return
102 return
99 end
103 end
100 end
104 end
101
105
102 if @problem
106 if @problem
103 #aggregrate by language
107 #aggregrate by language
104 @by_lang = {}
108 @by_lang = {}
105 Submission.where(problem_id: @problem.id).find_each do |sub|
109 Submission.where(problem_id: @problem.id).find_each do |sub|
106 lang = Language.find_by_id(sub.language_id)
110 lang = Language.find_by_id(sub.language_id)
107 next unless lang
111 next unless lang
108 next unless sub.points >= @problem.full_score
112 next unless sub.points >= @problem.full_score
109
113
110 #initialize
114 #initialize
111 unless @by_lang.has_key?(lang.pretty_name)
115 unless @by_lang.has_key?(lang.pretty_name)
112 @by_lang[lang.pretty_name] = {
116 @by_lang[lang.pretty_name] = {
113 runtime: { avail: false, value: 2**30-1 },
117 runtime: { avail: false, value: 2**30-1 },
114 memory: { avail: false, value: 2**30-1 },
118 memory: { avail: false, value: 2**30-1 },
115 length: { avail: false, value: 2**30-1 },
119 length: { avail: false, value: 2**30-1 },
116 first: { avail: false, value: DateTime.new(3000,1,1) }
120 first: { avail: false, value: DateTime.new(3000,1,1) }
117 }
121 }
118 end
122 end
119
123
120 if sub.max_runtime and sub.max_runtime < @by_lang[lang.pretty_name][:runtime][:value]
124 if sub.max_runtime and sub.max_runtime < @by_lang[lang.pretty_name][:runtime][:value]
121 @by_lang[lang.pretty_name][:runtime] = {
125 @by_lang[lang.pretty_name][:runtime] = {
122 avail: true,
126 avail: true,
123 user_id: sub.user_id,
127 user_id: sub.user_id,
124 value: sub.max_runtime,
128 value: sub.max_runtime,
125 sub_id: sub.id
129 sub_id: sub.id
126 }
130 }
127 end
131 end
128
132
129 if sub.peak_memory and sub.peak_memory < @by_lang[lang.pretty_name][:memory][:value]
133 if sub.peak_memory and sub.peak_memory < @by_lang[lang.pretty_name][:memory][:value]
130 @by_lang[lang.pretty_name][:memory] = {
134 @by_lang[lang.pretty_name][:memory] = {
131 avail: true,
135 avail: true,
132 user_id: sub.user_id,
136 user_id: sub.user_id,
133 value: sub.peak_memory,
137 value: sub.peak_memory,
134 sub_id: sub.id
138 sub_id: sub.id
135 }
139 }
136 end
140 end
137
141
138 if sub.submitted_at and sub.submitted_at < @by_lang[lang.pretty_name][:first][:value] and
142 if sub.submitted_at and sub.submitted_at < @by_lang[lang.pretty_name][:first][:value] and
139 !sub.user.admin?
143 !sub.user.admin?
140 @by_lang[lang.pretty_name][:first] = {
144 @by_lang[lang.pretty_name][:first] = {
141 avail: true,
145 avail: true,
142 user_id: sub.user_id,
146 user_id: sub.user_id,
143 value: sub.submitted_at,
147 value: sub.submitted_at,
144 sub_id: sub.id
148 sub_id: sub.id
145 }
149 }
146 end
150 end
147
151
148 if @by_lang[lang.pretty_name][:length][:value] > sub.effective_code_length
152 if @by_lang[lang.pretty_name][:length][:value] > sub.effective_code_length
149 @by_lang[lang.pretty_name][:length] = {
153 @by_lang[lang.pretty_name][:length] = {
150 avail: true,
154 avail: true,
151 user_id: sub.user_id,
155 user_id: sub.user_id,
152 value: sub.effective_code_length,
156 value: sub.effective_code_length,
153 sub_id: sub.id
157 sub_id: sub.id
154 }
158 }
155 end
159 end
156 end
160 end
157
161
158 #process user_id
162 #process user_id
159 @by_lang.each do |lang,prop|
163 @by_lang.each do |lang,prop|
160 prop.each do |k,v|
164 prop.each do |k,v|
161 v[:user] = User.exists?(v[:user_id]) ? User.find(v[:user_id]).full_name : "(NULL)"
165 v[:user] = User.exists?(v[:user_id]) ? User.find(v[:user_id]).full_name : "(NULL)"
162 end
166 end
163 end
167 end
164
168
165 #sum into best
169 #sum into best
166 if @by_lang and @by_lang.first
170 if @by_lang and @by_lang.first
167 @best = @by_lang.first[1].clone
171 @best = @by_lang.first[1].clone
168 @by_lang.each do |lang,prop|
172 @by_lang.each do |lang,prop|
169 if @best[:runtime][:value] >= prop[:runtime][:value]
173 if @best[:runtime][:value] >= prop[:runtime][:value]
170 @best[:runtime] = prop[:runtime]
174 @best[:runtime] = prop[:runtime]
171 @best[:runtime][:lang] = lang
175 @best[:runtime][:lang] = lang
172 end
176 end
173 if @best[:memory][:value] >= prop[:memory][:value]
177 if @best[:memory][:value] >= prop[:memory][:value]
174 @best[:memory] = prop[:memory]
178 @best[:memory] = prop[:memory]
175 @best[:memory][:lang] = lang
179 @best[:memory][:lang] = lang
176 end
180 end
177 if @best[:length][:value] >= prop[:length][:value]
181 if @best[:length][:value] >= prop[:length][:value]
178 @best[:length] = prop[:length]
182 @best[:length] = prop[:length]
179 @best[:length][:lang] = lang
183 @best[:length][:lang] = lang
180 end
184 end
181 if @best[:first][:value] >= prop[:first][:value]
185 if @best[:first][:value] >= prop[:first][:value]
182 @best[:first] = prop[:first]
186 @best[:first] = prop[:first]
183 @best[:first][:lang] = lang
187 @best[:first][:lang] = lang
184 end
188 end
185 end
189 end
186 end
190 end
187 end
191 end
188 end
192 end
189 end
193 end
@@ -1,32 +1,36
1 - content_for :header do
1 - content_for :header do
2 = stylesheet_link_tag 'tablesorter-theme.cafe'
2 = stylesheet_link_tag 'tablesorter-theme.cafe'
3 = javascript_include_tag 'new'
3 = javascript_include_tag 'new'
4
4
5 %script{:type=>"text/javascript"}
5 %script{:type=>"text/javascript"}
6 $(function () {
6 $(function () {
7 $('#since_datetime').datetimepicker({ showButtonPanel: true, dateFormat: "yy-mm-dd", controlType: "slider"} );
7 $('#since_datetime').datetimepicker({ showButtonPanel: true, dateFormat: "yy-mm-dd", controlType: "slider"} );
8 $('#until_datetime').datetimepicker({ showButtonPanel: true, dateFormat: "yy-mm-dd", controlType: "slider"} );
8 $('#until_datetime').datetimepicker({ showButtonPanel: true, dateFormat: "yy-mm-dd", controlType: "slider"} );
9 $('#my_table').tablesorter({widthFixed: true, widgets: ['zebra']});
9 $('#my_table').tablesorter({widthFixed: true, widgets: ['zebra']});
10 });
10 });
11
11
12 %h1 Login status
12 %h1 Login status
13
13
14 =render partial: 'report_menu'
14 =render partial: 'report_menu'
15 =render partial: 'date_range', locals: {param_text: 'Login date range:', title: 'Query login stat in the range' }
15 =render partial: 'date_range', locals: {param_text: 'Login date range:', title: 'Query login stat in the range' }
16
16
17 %table.tablesorter-cafe#my_table
17 %table.tablesorter-cafe#my_table
18 %thead
18 %thead
19 %tr
19 %tr
20 %th login
20 %th login
21 %th full name
21 %th full name
22 %th login count
22 %th login count
23 %th earliest
23 %th earliest
24 %th latest
24 %th latest
25 + %th IP
25 %tbody
26 %tbody
26 - @logins.each do |l|
27 - @logins.each do |l|
27 %tr{class: cycle('info-even','info-odd')}
28 %tr{class: cycle('info-even','info-odd')}
28 %td= link_to l[:login], controller: 'users', action: 'profile', id: l[:id]
29 %td= link_to l[:login], controller: 'users', action: 'profile', id: l[:id]
29 %td= l[:full_name]
30 %td= l[:full_name]
30 %td= l[:count]
31 %td= l[:count]
31 %td= l[:min] ? l[:min].in_time_zone.strftime('%Y-%m-%d %H:%M') : ''
32 %td= l[:min] ? l[:min].in_time_zone.strftime('%Y-%m-%d %H:%M') : ''
32 - %td= l[:max] ? "#{l[:max].in_time_zone.strftime('%Y-%m-%d %H:%M')} (#{time_ago_in_words(l[:max].in_time_zone)} ago)" : ''
33 + %td= l[:max] ? "#{l[:max].in_time_zone.strftime('%Y-%m-%d %H:%M.%S')} (#{time_ago_in_words(l[:max].in_time_zone)} ago)" : ''
34 + %td
35 + - l[:ip].each do |ip|
36 + #{ip.ip_address} <br/>
You need to be logged in to leave comments. Login now