const async = require('async');
const redis = require('../store/redis');
const db = require('../store/db');
const utility = require('../util/utility');

db.transaction((trx) => {
  redis.keys('mmr_estimates:*', (err, keys) => {
    async.eachLimit(keys, 1000, (key, cb) => {
      console.log(key);
      redis.lrange(key, 0, -1, (err, result) => {
        const accountId = key.split(':')[1];
        const data = result
          .filter(d => d)
          .map(d => Number(d));
        const estimate = utility.average(data);
        db.raw('INSERT INTO mmr_estimates VALUES (?, ?) ON CONFLICT(account_id) DO UPDATE SET estimate = ?', [accountId, estimate, estimate]).asCallback(cb);
      });
    }, (err) => {
      if (err) {
        return trx.rollback(err);
      }
      return trx.commit();
    });
  });
});

@@ -521,16 +521,55 @@
 (data);%0A
+        if (accountId && estimate) %7B%0A  
         
@@ -718,16 +718,59 @@
 ck(cb);%0A
+        %7D else %7B%0A          cb();%0A        %7D%0A
       %7D)

edit movemmrestimate