@ -44,31 +44,31 @@ class CollegesController < ApplicationController
defstatistics
defstatistics
logger.info("#########################{params}")
logger.info("#########################{params}")
@teachers_count=User.find_by_sql("SELECT COUNT(users.`id`) AS teacher_count FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE
@teachers_count=User.find_by_sql("SELECT COUNT(users.`id`) AS teacher_count FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE
user_extensions.`school_id` = #{@department.school_id} AND user_extensions.`identity` = 0").first.try(:teacher_count)
user_extensions.`school_id` = #{@school.id} AND user_extensions.`identity` = 0").first.try(:teacher_count)
@students_count=User.find_by_sql("SELECT COUNT(users.`id`) AS student_count FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE
@students_count=User.find_by_sql("SELECT COUNT(users.`id`) AS student_count FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE
user_extensions.`school_id` = #{@department.school_id} AND user_extensions.`identity` = 1").first.try(:student_count)
user_extensions.`school_id` = #{@school.id} AND user_extensions.`identity` = 1").first.try(:student_count)
# Redo:这样做内存会卡死的
# Redo:这样做内存会卡死的
# user_ids = User.find_by_sql("SELECT users.id FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE user_extensions.`school_id` = #{@department.school_id}").map(&:id)
# user_ids = User.find_by_sql("SELECT users.id FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE user_extensions.`school_id` = #{@school.id}").map(&:id)
# Redo:是否直接使用count会更好
# Redo:是否直接使用count会更好
all_course_ids=Course.joins("join users u on courses.tea_id = u.id").joins("join user_extensions ue on u.id = ue.user_id").where("courses.id != 1309 and ue.school_id = #{@department.school_id}")
all_course_ids=Course.joins("join users u on courses.tea_id = u.id").joins("join user_extensions ue on u.id = ue.user_id").where("courses.id != 1309 and ue.school_id = #{@school.id}")
@shixuns_count=Shixun.find_by_sql("select count(s.id) as shixun_count from users u right join shixuns s on u.id=s.user_id and s.status in (2, 3) inner join user_extensions ue on
@shixuns_count=Shixun.find_by_sql("select count(s.id) as shixun_count from users u right join shixuns s on u.id=s.user_id and s.status in (2, 3) inner join user_extensions ue on
@shixun_time_sum=(Game.find_by_sql("select sum(g.cost_time) cost_time from users u RIGHT join games g on u.id=g.user_id inner join user_extensions ue on
@shixun_time_sum=(Game.find_by_sql("select sum(g.cost_time) cost_time from users u RIGHT join games g on u.id=g.user_id inner join user_extensions ue on
# select count(sw.id) from users u left join student_works sw on u.id=sw.user_id and sw.myshixun_id is not null and sw.work_status !=0 inner join user_extensions ue on u.id=ue.user_id and ue.school_id=117 ;
# select count(sw.id) from users u left join student_works sw on u.id=sw.user_id and sw.myshixun_id is not null and sw.work_status !=0 inner join user_extensions ue on u.id=ue.user_id and ue.school_id=117 ;
# @shixun_report_count = StudentWork.where("work_status != 0 and user_id in (#{user_ids.join(',').strip == "" ? -1 : user_ids.join(',')}) and myshixun_id is not null").count
# @shixun_report_count = StudentWork.where("work_status != 0 and user_id in (#{user_ids.join(',').strip == "" ? -1 : user_ids.join(',')}) and myshixun_id is not null").count
@shixun_report_count=StudentWork.find_by_sql("SELECT count(*) as sw_count FROM `student_works` where user_id in (SELECT users.id FROM users RIGHT JOIN user_extensions ON users.id=user_extensions.user_id WHERE
@shixun_report_count=StudentWork.find_by_sql("SELECT count(*) as sw_count FROM `student_works` where user_id in (SELECT users.id FROM users RIGHT JOIN user_extensions ON users.id=user_extensions.user_id WHERE
user_extensions.`school_id`=#{@department.school_id}) and work_status between 1 and 2 and myshixun_id !=0").first.try(:sw_count)
user_extensions.`school_id`=#{@school.id}) and work_status between 1 and 2 and myshixun_id !=0").first.try(:sw_count)
@teachers=User.find_by_sql("SELECT users.id, users.login, users.lastname, users.firstname, users.nickname, IFNULL((SELECT count(shixuns.id) FROM shixuns where shixuns.user_id =users.id group by shixuns.user_id), 0) AS publish_shixun_count,
@teachers=User.find_by_sql("SELECT users.id, users.login, users.lastname, users.firstname, users.nickname, IFNULL((SELECT count(shixuns.id) FROM shixuns where shixuns.user_id =users.id group by shixuns.user_id), 0) AS publish_shixun_count,
FROM`users`,user_extensionsuewhereusers.id=ue.user_idandue.identity=0andue.school_id=#{@department.school_id} ORDER BY publish_shixun_count desc, course_count desc, id desc LIMIT 10")
FROM`users`,user_extensionsuewhereusers.id=ue.user_idandue.identity=0andue.school_id=#{@school.id} ORDER BY publish_shixun_count desc, course_count desc, id desc LIMIT 10")
FROM`courses`cWHERE(c.school_id=#{@department.school_id} and c.is_delete = 0)")
FROM`courses`cWHERE(c.school_id=#{@school.id} and c.is_delete = 0)")
@courses.eachdo|course|
@courses.eachdo|course|
course[:evaluating_count]=Output.find_by_sql("select sum(g.evaluate_count) as evaluating_count from games g inner join
course[:evaluating_count]=Output.find_by_sql("select sum(g.evaluate_count) as evaluating_count from games g inner join
@ -157,11 +157,11 @@ class CollegesController < ApplicationController
# 学生实训
# 学生实训
defstudent_shixun
defstudent_shixun
user_ids=User.find_by_sql("SELECT users.id FROM users, user_extensions WHERE users.id=user_extensions.user_id AND user_extensions.`school_id` = #{@department.school_id}").map(&:id)
user_ids=User.find_by_sql("SELECT users.id FROM users, user_extensions WHERE users.id=user_extensions.user_id AND user_extensions.`school_id` = #{@school.id}").map(&:id)
users.id=ue.user_idwhereue.school_id=#{@department.school_id} AND ue.identity = 1 AND `users`.`type` IN ('User', 'AnonymousUser') ORDER BY experience DESC, myshixun_count DESC LIMIT 10")
users.id=ue.user_idwhereue.school_id=#{@school.id} AND ue.identity = 1 AND `users`.`type` IN ('User', 'AnonymousUser') ORDER BY experience DESC, myshixun_count DESC LIMIT 10")
## outputs基数过大,用inner join有奇效
## outputs基数过大,用inner join有奇效
@shixun_tags=TagRepertoire.find_by_sql(%Q{
@shixun_tags=TagRepertoire.find_by_sql(%Q{
@ -172,7 +172,7 @@ class CollegesController < ApplicationController
)aona.id=outputs.game_idandoutputs.`test_set_position` = 1 group by trid
)aona.id=outputs.game_idandoutputs.`test_set_position` = 1 group by trid
ORDERBYtest_countDESC
ORDERBYtest_countDESC
@ -197,50 +197,50 @@ class CollegesController < ApplicationController
@eval_data=[]
@eval_data=[]
case@index
case@index
when"day"
when"day"
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(end_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(end_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
last_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
last_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(end_day-1.day).beginning_of_day}' AND outputs.created_at < '#{(end_day-1.day).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(end_day-1.day).beginning_of_day}' AND outputs.created_at < '#{(end_day-1.day).end_of_day}'").first.try(:count).to_i
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{end_day.beginning_of_day+(i*2).hour}' AND outputs.created_at < '#{end_day.beginning_of_day+((i+1)*2).hour}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{end_day.beginning_of_day+(i*2).hour}' AND outputs.created_at < '#{end_day.beginning_of_day+((i+1)*2).hour}'").first.try(:count).to_i
end
end
when"week"
when"week"
start_day=end_day-6.days
start_day=end_day-6.days
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
last_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
last_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-7.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-7.days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-7.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-7.days).end_of_day}'").first.try(:count).to_i
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day+i.days).beginning_of_day}' AND outputs.created_at < '#{(start_day+(i+1).days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day+i.days).beginning_of_day}' AND outputs.created_at < '#{(start_day+(i+1).days).end_of_day}'").first.try(:count).to_i
end
end
when"mon"
when"mon"
start_day=end_day-30.days
start_day=end_day-30.days
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
last_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
last_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-30.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-30.days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-30.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-30.days).end_of_day}'").first.try(:count).to_i
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day+(3*i).days).beginning_of_day}' AND outputs.created_at < '#{(start_day+(3*(i+1)).days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day+(3*i).days).beginning_of_day}' AND outputs.created_at < '#{(start_day+(3*(i+1)).days).end_of_day}'").first.try(:count).to_i
end
end
when"year"
when"year"
start_day=end_day-365.days
start_day=end_day-365.days
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
same_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day-365.days).beginning_of_day}' AND outputs.created_at < '#{(end_day-365.days).end_of_day}'").first.try(:count).to_i
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
@eval_data<<Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day+i.days).beginning_of_day}' AND outputs.created_at < '#{(start_day+(i+1).days).end_of_day}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{(start_day+i.days).beginning_of_day}' AND outputs.created_at < '#{(start_day+(i+1).days).end_of_day}'").first.try(:count).to_i
end
end
end
end
@ -248,9 +248,9 @@ class CollegesController < ApplicationController
@total_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
@total_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 ").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 ").first.try(:count).to_i
@trend_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@department.school_id}) b
@trend_count=Output.find_by_sql("select count(outputs.id) as count from outputs RIGHT JOIN (SELECT games.id FROM games RIGHT JOIN user_extensions ue ON games.`user_id` = ue.`user_id` AND ue.`school_id`=#{@school.id}) b
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{Time.now-7.days}'").first.try(:count).to_i
ONoutputs.`game_id` = b.id WHERE outputs.test_set_position = 1 AND outputs.created_at > '#{Time.now-7.days}'").first.try(:count).to_i
end
end
@ -259,9 +259,9 @@ class CollegesController < ApplicationController