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 constants = require('./constants');
var max;
start();
doMax();
//get the max account_id and save it
function doMax()
{
    db.raw("select max(account_id) from players").asCallback(function(err, result)
    {
        if (err)
        {
            throw err;
        }
        max = Number(result.rows[0].max);
        console.log("recomputed max: %s", max);
        return setTimeout(doMax, 60 * 60 * 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 account_id from players 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);
        });
    });
}
@@ -255,69 +255,200 @@
 ();%0A
-doMax();%0A//get the max account_id and save it%0Afunction doMax(
+%0Afunction start()%0A%7B%0A    getSummaries(function(err)%0A    %7B%0A        if (err)%0A        %7B%0A            throw err;%0A        %7D%0A        return setTimeout(start, 1000);%0A    %7D);%0A%7D%0A%0Afunction getSummaries(cb
 )%0A%7B%0A
@@ -454,25 +454,25 @@
 %0A    db.raw(
-%22
+%60
 select max(a
@@ -470,23 +470,21 @@
 ect max(
-account
+match
 _id) fro
@@ -489,16 +489,16 @@
 rom 
-players%22
+matches%60
 ).as
@@ -569,33 +569,38 @@
             
-throw 
+return cb(
 err
+)
 ;%0A        %7D%0A
@@ -653,173 +653,263 @@
     
-console.log(%22recomputed max: %25s%22, max);%0A        return setTimeout(doMax, 60 * 60 * 1000);%0A    %7D);%0A%7D%0A%0Afunction start()%0A%7B%0A    getSummaries(function(err)%0A
+var min = max - 10000000;%0A        db.raw(%60%0A        select distinct account_id %0A        from player_matches %0A        where match_id in (SELECT (?::bigint + random()*(?::bigint - ?::bigint))::bigint as rand                                       
     
-%7B%0A
         
 if (
@@ -904,35 +904,17 @@
         
-if (err)%0A        %7B%0A
+ 
         
@@ -917,35 +917,33 @@
         
-throw err;%0A
+         
         
 %7D%0A      
@@ -930,34 +930,32 @@
             
-%7D%0A
         
 return setTi
@@ -946,107 +946,48 @@
     
-return setTimeout(start, 1000);%0A    %7D);%0A%7D%0A%0Afunction getSummaries(cb)%0A%7B%0A    if (!max)%0A
+                            
     
-%7B%0A
         
 cons
@@ -986,167 +986,80 @@
     
-console.log('waiting for max');%0A        return cb();%0A
+           
     
-%7D%0A
     
-var random = Math.floor((Math.random() * max));%0A    db.raw(%22select account_id from players where
+     %0A        from generate_series(1,50))%0A        and
  acc
@@ -1070,9 +1070,9 @@
 _id 
-%3E
+%3C
  ? l
@@ -1083,18 +1083,66 @@
  100
-%22, %5Brandom
+%0A        %60, %5Bmin, max, min, constants.anonymous_account_id
 %5D).a
@@ -1174,26 +1174,34 @@
 esults)%0A    
+  
-%7B%0A
+  %7B%0A    
         if (
@@ -1205,34 +1205,42 @@
 f (err)%0A        
+  
-%7B%0A
+  %7B%0A    
             retu
@@ -1251,34 +1251,111 @@
 b(err);%0A        
+ 
+   
 %7D%0A
+            console.log('players sampled: %25s', results.rows.length);%0A    
         var cont
@@ -1399,34 +1399,42 @@
 aries%22,%0A        
-%7B%0A
+    %7B%0A    
             play
@@ -1451,32 +1451,36 @@
 ts.rows%0A        
+    
 %7D);%0A        getD
@@ -1475,16 +1475,20 @@
         
+   
+ 
 getData(
@@ -1522,34 +1522,41 @@
 , body)%0A        
-%7B%0A
+    %7B%0A   
             if (
@@ -1543,32 +1543,33 @@
 %0A               
+ 
 if (err)%0A       
@@ -1565,34 +1565,41 @@
 rr)%0A            
-%7B%0A
+    %7B%0A   
                 
@@ -1598,16 +1598,17 @@
         
+ 
 //couldn
@@ -1651,32 +1651,36 @@
                 
+    
 return cb(JSON.s
@@ -1699,34 +1699,42 @@
 ));%0A            
+   
+ 
 %7D%0A
+    
             //pl
@@ -1769,16 +1769,20 @@
         
+  
+  
 async.ea
@@ -1832,34 +1832,40 @@
 cb)%0A            
-%7B%0A
+    %7B%0A  
                 
@@ -1864,16 +1864,18 @@
         
+  
 insertPl
@@ -1912,16 +1912,20 @@
     
+    
 %7D, cb);%0A
     
@@ -1916,24 +1916,40 @@
     %7D, cb);%0A
+            %7D);%0A
         %7D);%0A

optimize profiler sampling