You could probably implement something along the lines of Bill's
suggested solution using the scheduled tasks feature
(http://code.google.com/appengine/docs/python/config/cron.html).
Depending on the frequency of the events for a given player there may
be other solutions as well. For example, if hour resolution is
sufficient and updates are fairly infrequent (probably several times /
hour / player), perhaps you could create 24 child objects for each
player similar to what follows:
class Player(db.Model):
name = db.StringProperty()
last_scores = db.ListProperty(indexed=False)
last_score_times = db.ListProperty(indexed=False)
class HourlyPlayerScore(db.Model):
score_hour = db.DateTimeProperty()
ttl_score = db.IntegerProperty()
The last_scores field of the Player class will hold the scores from
the last 24 hours for a particular player. The last_score_times will
hold the corresponding times of those scores. You will have up to 24
HourlyPlayerScore entities per player, these models are explained more
below. Hour 1 refers to within the last hour, hour 2 refers to within
the last two hours, and so on.
When you get a player's new score you could do the following:
1) Fetch the player entity.
2) Add the new score and time to the last_score and last_score_time
lists, respectively.
3) iterate over your list of scores and their corresponding times (the
zip function is handy here)
a) keep a cumulative total of the scores by hour (i.e. hour 3
includes the scores from hours 1 and 2, on other words each entry
includes things that happen AFTER it!)
b) Create an HourlyPlayerScore entity for each "cumulative total of
the scores by hour" you have.
- HourlyPlayerScore(key_name=str(hour_lag), parent=current_player)
- Set score_hour = the hour you are currently processing
- Set ttl_hour_score to the cumulative total of the scores
up to that hour.
c) Be sure to create an HourlyPlayerScore entry even for hours with no scores.
d) Remove scores and corresponding times older than 24 hours from the lists.
Note 1) It is important that you create an HourlyPlayerScore entity
for any "missing" hours, otherwise they could be missed.
Note 2) Because you will name the HourlyPlayerScore models, fetching
them for a current player (if you ever need to) is quite easy:
hourlyscore_keys = [
db.Key.from_path('HourlyPlayerScore',str(lag),'Player',current_player)
for lag in xrange(1,25) ]
hourlyscores = db.get(hourlyscore_keys)
Now, when you need to query:
top_player_scores = HourlyPlayerScore.all(keys_only=True)
top_player_scores.filter(score_hour = DateTime( now - 24 hours ) )
top_player_scores.order(ttl_score)
top_player_keys = [ tps_key.parent() for tps_key in
top_player_scores.fetch(10) ]
top_players = db.get(top_player_keys)
This is not perfect, but it avoids having to clean up old stuff unless
needed. If you do not have ten players meeting this for a given hour
(note 1 should help with this though) you could run a second query to
fetch the next hour's top players and fill in the current list with
those players. If you batch put the player and hourlyplayerscore
entities, it will be expensive, but probably manageable.
Just a thought.
Robert
On Wed, Dec 16, 2009 at 12:25 AM, Amir Michail <[email protected]> wrote:
> Hello,
>
> I asked this question before but perhaps this is more feasible now
> with the latest version of the GAE?
>
> http://groups.google.com/group/google-appengine/browse_thread/thread/e7f892a2efabed59/2ced4b6790ce1f1b
>
> Amir
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en.