var queries = require('./queries');
var insertPlayer = queries.insertPlayer;
var utility = require('./utility');
var getData = utility.getData;
var async = require('async');
var db = require('./db');
var max;
start();
doMax();

//get the max account_id and save for 10 minutes
function doMax()
{
    db.raw("select max(account_id) from player_matches").asCallback(function(err, result)
    {
        if (err)
        {
            throw err;
        }
        max = Number(result.rows[0].max);
        console.log("recomputed max: %s", max);
        return setTimeout(doMax, 60 * 10 * 1000);
    });
}

function start()
{
    getSummaries(function(err)
    {
        if (err)
        {
            throw err;
        }
        return setTimeout(start, 1000);
    });
}

function getSummaries(cb)
{
    if (!max)
    {
        console.log('waiting for max');
        return cb();
    }
    var random = Math.floor((Math.random()*max)); 
    db.raw("select distinct(account_id) from player_matches where account_id > ? limit 100", [random]).asCallback(function(err, results)
    {
        if (err)
        {
            return cb(err);
        }
        var container = utility.generateJob("api_summaries",
        {
            players: results.rows
        });
        getData(container.url, function(err, body)
        {
            if (err)
            {
                //couldn't get data from api, non-retryable
                return cb(JSON.stringify(err));
            }
            //player summaries response
            async.each(body.response.players, function(player, cb)
            {
                insertPlayer(db, player, cb);
            }, cb);
        });
    });
}
@@ -189,24 +189,64 @@
 re('./db');%0A
+var constants = require('./constants');%0A
 var max;%0Asta
@@ -260,17 +260,16 @@
 oMax();%0A
-%0A
 //get th
@@ -298,22 +298,10 @@
 ave 
-for 10 minutes
+it
 %0Afun
@@ -370,17 +370,72 @@
 _matches
-%22
+ where account_id %3C ?%22, %5Bconstants.anonymous_account_id%5D
 ).asCall
@@ -655,17 +655,17 @@
 x, 60 * 
-1
+6
 0 * 1000
@@ -1006,16 +1006,17 @@
 om()
-*
+ * 
 max));
- 
 %0A   

fix profiler