From c47d81e4ebcd3c58383d6ac184711c5744de0e31 Mon Sep 17 00:00:00 2001 From: Alice Harris Date: Mon, 1 Sep 2014 19:33:23 +1000 Subject: [PATCH] sort output alphabetically by item name; improve readable data so it is actually readable --- .../count_users_who_own_specified_gear.js | 178 +++++++++--------- 1 file changed, 90 insertions(+), 88 deletions(-) diff --git a/database_reports/count_users_who_own_specified_gear.js b/database_reports/count_users_who_own_specified_gear.js index ecacaf16c9..c5fe8709b9 100644 --- a/database_reports/count_users_who_own_specified_gear.js +++ b/database_reports/count_users_who_own_specified_gear.js @@ -7,31 +7,31 @@ var authorUuid = 'd904bd62-da08-416b-a816-ba797c9ee265'; //... own data is done */ var thingsOfInterest = { - 'Unconventional Armor ownership': { - 'data_path': 'items.gear.owned', - 'identifyOwnershipWith': 'exists', - 'items': [ - 'headAccessory_special_wondercon_red', - 'headAccessory_special_wondercon_black', - 'back_special_wondercon_black', - 'back_special_wondercon_red', - 'body_special_wondercon_red', - 'body_special_wondercon_black', - 'body_special_wondercon_gold' - ], - }, - 'Spooky Skins purchases': { - 'data_path': 'purchased.skin', - 'identifyOwnershipWith': 'true', - 'items': [ - 'monster', - 'pumpkin', - 'skeleton', - 'zombie', - 'ghost', - 'shadow' - ] - } + 'Unconventional Armor ownership': { + 'data_path': 'items.gear.owned', + 'identifyOwnershipWith': 'exists', + 'items': [ + 'headAccessory_special_wondercon_red', + 'headAccessory_special_wondercon_black', + 'back_special_wondercon_black', + 'back_special_wondercon_red', + 'body_special_wondercon_red', + 'body_special_wondercon_black', + 'body_special_wondercon_gold' + ], + }, + 'Spooky Skins purchases': { + 'data_path': 'purchased.skin', + 'identifyOwnershipWith': 'true', + 'items': [ + 'monster', + 'pumpkin', + 'skeleton', + 'zombie', + 'ghost', + 'shadow' + ] + } }; var mongo = require('mongoskin'); @@ -63,21 +63,21 @@ dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) { count++; _.each(thingsOfInterest,function(obj,label){ - var data_path = obj['data_path']; - var items = obj['items']; - var identifyOwnershipWith = obj['identifyOwnershipWith']; - var userOwns = path(user, data_path); + var data_path = obj['data_path']; + var items = obj['items']; + var identifyOwnershipWith = obj['identifyOwnershipWith']; + var userOwns = path(user, data_path); - _.each(items,function(item){ - if ( (identifyOwnershipWith == 'exists' && item in userOwns) || - (identifyOwnershipWith == 'true' && userOwns[item]) - ) { - if (! thingsFound[label]) { thingsFound[label] = {}; } - thingsFound[label][item] = (thingsFound[label][item] || 0) + 1; - // console.warn(user.auth.local.username + ": " + label + ": " + item); // good for testing, bad for privacy - } - }); - }); + _.each(items,function(item){ + if ( (identifyOwnershipWith == 'exists' && item in userOwns) || + (identifyOwnershipWith == 'true' && userOwns[item]) + ) { + if (! thingsFound[label]) { thingsFound[label] = {}; } + thingsFound[label][item] = (thingsFound[label][item] || 0) + 1; + // console.warn(user.auth.local.username + ": " + label + ": " + item); // good for testing, bad for privacy + } + }); + }); if (count%progressCount == 0) console.warn(count + ' ' + user._id); if (user._id == authorUuid) console.warn(authorName + ' processed'); @@ -86,28 +86,31 @@ dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) { function displayData() { - var today = yyyymmdd(new Date()); - var report = ''; - _.each(thingsFound,function(obj,label){ - report += '"' + label + '"' + '\n'; - var header = '"date"'; // heading row in CSV data - var data = '"' + today + '"'; // data row in CSV data - _.each(obj,function(value,key){ - header += ',"' + key + '"'; - data += ',"' + (value || 0) + '"'; - }); - report += header + '\n' + data + '\n\n'; - }); + var today = yyyymmdd(new Date()); + var csvReport = ''; + var textReport = today + '\n'; - console.log('\nCSV DATA:\n\n' + - report + - '\nREADABLE DATA:\n\n' + - today + '\n' + - JSON.stringify(thingsFound, null, ' ') + - '\n'); + _.each(thingsFound,function(obj,label){ + csvReport += '\n"' + label + '"' + '\n'; + textReport += '\n' + label + ':\n'; + var csvHeader = '"date"'; // heading row in CSV data + var csvData = '"' + today + '"'; // data row in CSV data - console.warn('\n' + count + ' users searched (should be >400k)\n'); - // NB: "should be" assumes that no query filter was applied to findEach + var sortedKeys = _.sortBy(_.keys(obj), function(key){ return key; }); + _.each(sortedKeys,function(key){ + var value = obj[key]; + csvHeader += ',"' + key + '"'; + csvData += ',"' + (value || 0) + '"'; + textReport += '\t' + key + ': ' + value + '\n'; + }); + csvReport += csvHeader + '\n' + csvData + '\n'; + }); + + console.log('\nCSV DATA:\n' + csvReport + '\n\n' + + 'READABLE DATA:\n\n' + textReport + '\n\n'); + + console.warn(count + ' users searched (should be >400k)\n'); + // NB: "should be" assumes that no query filter was applied to findEach return exiting(0); } @@ -123,12 +126,12 @@ function path(obj, path, def) { * http://stackoverflow.com/a/16190716 * Usage: console.log(path(someObject, pathname)); */ - for(var i = 0,path = path.split('.'),len = path.length; i < len; i++){ - if(!obj || typeof obj !== 'object') return def; - obj = obj[path[i]]; - } - if(obj === 'undefined') return def; - return obj; + for(var i = 0,path = path.split('.'),len = path.length; i < len; i++){ + if(!obj || typeof obj !== 'object') return def; + obj = obj[path[i]]; + } + if(obj === 'undefined') return def; + return obj; } @@ -162,36 +165,35 @@ All users found. CSV DATA: "Unconventional Armor ownership" -"date","headAccessory_special_wondercon_red","headAccessory_special_wondercon_black","back_special_wondercon_black","back_special_wondercon_red","body_special_wondercon_red","body_special_wondercon_black","body_special_wondercon_gold" -"2014-09-01","9","7","7","7","7","7","7" +"date","back_special_wondercon_black","back_special_wondercon_red","body_special_wondercon_black","body_special_wondercon_gold","body_special_wondercon_red","headAccessory_special_wondercon_black","headAccessory_special_wondercon_red" +"2014-09-01","7","7","7","7","7","7","9" "Spooky Skins purchases" -"date","monster","pumpkin","skeleton","zombie","ghost","shadow" -"2014-09-01","3","3","4","3","2","6" +"date","ghost","monster","pumpkin","shadow","skeleton","zombie" +"2014-09-01","2","3","3","6","4","3" READABLE DATA: 2014-09-01 -{ - "Unconventional Armor ownership": { - "headAccessory_special_wondercon_red": 9, - "headAccessory_special_wondercon_black": 7, - "back_special_wondercon_black": 7, - "back_special_wondercon_red": 7, - "body_special_wondercon_red": 7, - "body_special_wondercon_black": 7, - "body_special_wondercon_gold": 7 - }, - "Spooky Skins purchases": { - "monster": 3, - "pumpkin": 3, - "skeleton": 4, - "zombie": 3, - "ghost": 2, - "shadow": 6 - } -} + +Unconventional Armor ownership: + back_special_wondercon_black: 7 + back_special_wondercon_red: 7 + body_special_wondercon_black: 7 + body_special_wondercon_gold: 7 + body_special_wondercon_red: 7 + headAccessory_special_wondercon_black: 7 + headAccessory_special_wondercon_red: 9 + +Spooky Skins purchases: + ghost: 2 + monster: 3 + pumpkin: 3 + shadow: 6 + skeleton: 4 + zombie: 3 + 400100 users searched (should be >400k)