-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class UserRank < ActiveRecord::Base | ||
belongs_to :user | ||
attr_accessible :flag_type, :score, :session_id | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class UserScores < ActiveRecord::Base | ||
belongs_to :user | ||
attr_accessible :flag_type, :score, :user_id | ||
|
||
def self.rank_for_user_task(user_id, task) | ||
score = UserScores.where("user_id = ? AND flag_type = ?", user_id, task).first | ||
return User.count if score == nil # LAST! | ||
count = UserScores.where("score > ? AND flag_type = ?", score, task).count | ||
return count + 1 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateUserScores < ActiveRecord::Migration | ||
def change | ||
create_table :user_scores do |t| | ||
t.integer :user_id | ||
t.string :flag_type | ||
t.integer :score | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class AddUserscoreTaskIndex < ActiveRecord::Migration | ||
def up | ||
add_index(:user_scores,[:user_id, :flag_type],{unique: true, name: "user_task_index"}) | ||
end | ||
|
||
def down | ||
remove_index(:user_scores,name: "user_task_index") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace :user do | ||
|
||
desc "Calculate user scores for ranking/leaderboard" | ||
task :calculate_scores => :environment do | ||
# truncate the table | ||
UserScores.connection.execute("TRUNCATE table user_scores") | ||
UserScores.connection.execute(build_score_query("geometry")) | ||
UserScores.connection.execute(build_score_query("color")) | ||
UserScores.connection.execute(build_score_query("address")) | ||
UserScores.connection.execute(build_score_query("polygonfix")) | ||
UserScores.connection.execute(build_score_query("toponym")) | ||
end | ||
|
||
def build_score_query(task) | ||
"INSERT INTO user_scores (user_id, flag_type, score, created_at, updated_at) | ||
SELECT U.id AS user_id, C.flag_type AS flag_type, COUNT(*) AS score, current_timestamp AS created_at, current_timestamp AS updated_at | ||
FROM users U | ||
INNER JOIN usersessions S ON S.user_id=U.id | ||
INNER JOIN flags C ON C.session_id=S.session_id | ||
WHERE C.flag_type = '#{task}' | ||
GROUP BY U.id, C.flag_type | ||
" | ||
end | ||
|
||
end |