var conf = require('../loop/config').conf;
var redis = require("redis");

var storage = conf.get("storage");

function main(callback) {
  if (storage.engine === "redis") {
    var options = storage.settings;
    var client = redis.createClient(
      options.port,
      options.host,
      options.options
    );
    if (options.db) client.select(options.db);

    client.keys('hawkuser.*', function(err, keys) {
      if (err) throw err;
      var multi = client.multi();
      
      keys.forEach(function(key) {
        multi.get(key);
      });
      
      multi.exec(function(err, results) {
        var users = {};

        results.forEach(function(result, index) {
          if(!users.hasOwnProperty(result)) {
            users[result] = 1;
          } else {
            users[result]++;
          }
        });

        var total = Object.keys(users).length;
        var max = 0;
        var sum = 0;
        for(var key in users) {
          sum += users[key];
          if (max < users[key]) {
            max = users[key];
          }
        }

        
        callback({
          users: total,
          average: sum/total,
          max: max
        });
      });      
    });
  }
}


if (require.main === module) {
  main(function(results) {
    process.stdout.write(results.users + " FxA users with " +
                         results.average + " devices in average and a maximum of " + results.max + ".\n");
    process.exit(0);
  });
}

module.exports = main;

@@ -467,22 +467,16 @@
 ulti();%0A
-      
 %0A      k
@@ -537,22 +537,16 @@
     %7D);%0A
-      
 %0A      m
@@ -648,15 +648,8 @@
 sult
-, index
 ) %7B%0A
@@ -883,24 +883,51 @@
 ar sum = 0;%0A
+        var moreThan1 = 0;%0A
         for(
@@ -967,32 +967,101 @@
  += users%5Bkey%5D;%0A
+          if (users%5Bkey%5D %3E 1) %7B%0A            moreThan1++;%0A          %7D%0A
           if (ma
@@ -1131,24 +1131,16 @@
      %7D%0A%0A
-        
 %0A       
@@ -1223,16 +1223,48 @@
 max: max
+,%0A          moreThan1: moreThan1
 %0A       
@@ -1277,22 +1277,16 @@
      %7D);
-      
 %0A    %7D);
@@ -1467,17 +1467,17 @@
 devices 
-i
+o
 n averag
@@ -1514,16 +1514,113 @@
 .max + %22
+, %22);%0A    process.stdout.write(%22and %22 + results.moreThan1 + %22 FxA users with more than one device
 .%5Cn%22);%0A 

Extend the fxa devices tool to include a count of FxA users with more than one device.