Amir,
This is one of those cases that's tough because App Engine's datastore
has no on-the-fly SUM ability. You'd have to write an updating
process that makes periodic calls to your server app to process recent
results and update the entities used for your 24-hour scoring table.
Here's how I'd do it:
- Record each event affecting scoring in an entity (call the model
ScoreEvent) with a timestamp. The key_name of each ScoreEvent would
be the timestamp + user info. On write of this entity, update a 24-hr
score entity (call the model Score24) for this particular user.
- Periodically do maintenance calls to your server to update the
Score24 entities. Each maintenance call would sort ScoreEvents on the
key and then fetch all ScoreEvents since the last Score24 maintenance
call (use a key to mark your place in the ScoreEvents stream).
Querying on keys was added to datastore in Nov:
http://code.google.com/appengine/docs/datastore/queriesandindexes.html#Queries_on_Keys
So for each Score24 maintenance call, you'd fetch some # of ScoreEvent
entities, make a first pass so you combine all events into user
deltas, then update the associated user Score24 entities. Then save
the last ScoreEvent key that you processed in this maintenance call.
On the next maintenance call, use that "last ScoreEvent key" in the
query just as if you were paging:
query = ScoreEvent.gql('WHERE __key__ > :1 ORDER BY __key__',
last_key)
So your Score24 entities will reflect each user's score over the last
24 hours with some error due to the frequency of your maintenance
calls.
Now getting the top X scoring users over the last 24 hours is a query
on Score24 entities where you sort on score and fetch the top X
entities.
Best,
Bill