Ivane Gkomarteli
Ivane Gkomarteli

Reputation: 133

Implementing global ranking system based on each user's personal ranking. (Rails project)

Ok, so I have around 20 teams and every person ranks Up and Down each Team in his personal ranking list. Like the following format

    -Barcelona "Move up on the list" "Move Down on the list"
    -Arsenal "Move up" "Move Down"
    -Liverpool "Move up" "Move Down"

So in this example, for this particular user, Barcelona has the rank 1, Arsenal rank 2 etc.

Now I want to create a global ranking system, after taking into consideration each individual ranking list.

How do I, logically, go about that? Smaller rank means higher position, so I can't just take the sum and see which is bigger. Or can I take a sum and see which is smaller?

I thought I could add a "global ranking" integer in each team and see which is smaller, but is this approach right?

Upvotes: 0

Views: 267

Answers (1)

user3334690
user3334690

Reputation: 899

You can use averages to represent the global_rankings which would also make it relatively simple to keep the global_rankings up to date.

If your global_ranking is the average rank of that team (sum of rank indexes from each list for given team/number of lists) then the value of each list change on the global_ranking is 1/number of lists.

This means that moving a team up on a list would need to reduce their global_ranking by 1/number of lists while the team that moved down on the list would need to have its global_ranking increased by 1/number of lists (Assuming rank index grows bigger as move down the list).

As long as each new list is initialized to have the same order as the current global_ranking this should keep accurate.
Even so, you may still want to periodically (maybe nightly) recalculate the averages/global_rankings just in case.
This approach should be relatively quick as getting the total number of lists should be relatively trivial on your database and only needs to be done once per request.

Alternatively,

you can recalculate the averages for each team periodically (think minutes or even every few seconds). You'll probably have an easier time with this approach as it does mean that you don't have to worry about making changes to lists outside of the normal button presses. Performance wise it is probably a little heavier on your server/database but doesn't necessarily need to be seen by your end users. There are several different ways you could go about doing this.

The ways I can think of off the top of my head:

  1. DelayedJob(this is a gem for running tasks at a given time) and/or Clockwork(gem which lets you schedule rb tasks) (I find these are both useful gems though you probably don't need both, these gems do require their own process in addition to the rails server so keep that in mind if you're going to use them).
  2. Schedule a procedure on your database(this largely depends on your database and is purely sql).
  3. Keep track on the application of when the last time these values were calculated and update them using the first request after x amount of time (this affects 1 in n requests potentially slowing it down dramatically).

Upvotes: 1

Related Questions