mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-07-13 17:52:22 +00:00
modify report to use better code
This commit is contained in:
parent
3c971b630e
commit
34e2f23361
1 changed files with 118 additions and 26 deletions
|
|
@ -1,4 +1,42 @@
|
||||||
var itemsOfInterest = [
|
var migrationName = '20140829_change_headAccessory_to_eyewear';
|
||||||
|
var authorName = 'Alys'; // in case script author needs to know when their ...
|
||||||
|
var authorUuid = 'd904bd62-da08-416b-a816-ba797c9ee265'; //... own data is done
|
||||||
|
|
||||||
|
/**
|
||||||
|
* count_users_who_own_specified_gear.js
|
||||||
|
* https://github.com/HabitRPG/habitrpg/pull/3884
|
||||||
|
*/
|
||||||
|
|
||||||
|
var mongo = require('mongoskin');
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
|
/////////////////// UNCOMMENT *ONE* OF THESE LINES: ///////////////////
|
||||||
|
// var dbUsers = mongo.db('lefnire:mAdn3s5s@charlotte.mongohq.com:10015/habitrpg_large?auto_reconnect').collection('users'); // @lefnire production?
|
||||||
|
// var dbUsers = mongo.db('localhost:27017/habitrpg_old?auto_reconnect').collection('users'); // @lefnire habitrpg_old
|
||||||
|
var dbUsers = mongo.db('localhost:27017/habitrpg?auto_reconnect').collection('users'); // for local testing by script author (e.g., vagrant install)
|
||||||
|
if (typeof dbUsers == 'undefined') { exiting(1, 'Uncomment one of the "var dbUsers" lines!'); }
|
||||||
|
|
||||||
|
var thingsOfInterest = {
|
||||||
|
'items.gear.owned': [
|
||||||
|
'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'
|
||||||
|
],
|
||||||
|
'purchased.skin': [
|
||||||
|
'monster',
|
||||||
|
'pumpkin',
|
||||||
|
'skeleton',
|
||||||
|
'zombie',
|
||||||
|
'ghost',
|
||||||
|
'shadow'
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var thingsOfInterest = [ // TST
|
||||||
'headAccessory_special_wondercon_red',
|
'headAccessory_special_wondercon_red',
|
||||||
'headAccessory_special_wondercon_black',
|
'headAccessory_special_wondercon_black',
|
||||||
'back_special_wondercon_black',
|
'back_special_wondercon_black',
|
||||||
|
|
@ -8,47 +46,100 @@ var itemsOfInterest = [
|
||||||
'body_special_wondercon_gold'
|
'body_special_wondercon_gold'
|
||||||
];
|
];
|
||||||
|
|
||||||
var itemsFound = {}; // each key is one item from itemsOfInterest, and
|
var query = {}; // Not worth limiting search data with query and fields since
|
||||||
// the value for that key is the number of users who own it
|
var fields = {}; // this will be run over a local copy of the database?
|
||||||
// ('owned' values of both true and false are counted to
|
|
||||||
// include items lost on death)
|
var thingsFound = {}; // each key is one "thing" from thingsOfInterest,
|
||||||
|
// and the value for that key is the number of users who own it
|
||||||
|
// (for items, 'owned' values of both true and false are counted
|
||||||
|
// to include items lost on death)
|
||||||
|
|
||||||
|
console.warn('Finding data...');
|
||||||
|
var progressCount = 1000;
|
||||||
|
var count = 0;
|
||||||
|
// db.users.find().forEach(function(user) { ... });
|
||||||
|
dbUsers.findEach(query, fields, {batchSize:250}, function(err, user) {
|
||||||
|
if (err) { return exiting(1, 'ERROR! ' + err); }
|
||||||
|
if (!user) {
|
||||||
|
console.warn('All users found.');
|
||||||
|
return displayData();
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
_.each(['costume','equipped'],function(type){
|
||||||
|
unset['items.gear.'+type+'.headAccessory'] = "";
|
||||||
|
set['items.gear.'+type+'.eyewear'] = newName;
|
||||||
|
});
|
||||||
|
_.each(oldToNew,function(newName,oldName){
|
||||||
|
unset['items.gear.owned.'+oldName] = "";
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
db.users.find().forEach(function(user) {
|
|
||||||
var owned = user.items.gear.owned;
|
var owned = user.items.gear.owned;
|
||||||
for (var i=0, ic=itemsOfInterest.length; i<ic; i++) {
|
for (var i=0, ic=thingsOfInterest.length; i<ic; i++) {
|
||||||
var itemKey = itemsOfInterest[i];
|
var thingsKey = thingsOfInterest[i];
|
||||||
if (itemKey in owned) {
|
if (thingsKey in owned) {
|
||||||
itemsFound[itemKey] = (itemsFound[itemKey] || 0) + 1;
|
thingsFound[thingsKey] = (thingsFound[thingsKey] || 0) + 1;
|
||||||
// print(user.auth.local.username + ": " + itemKey); // good for testing, bad for privacy
|
// console.warn(user.auth.local.username + ": " + thingsKey); // good for testing, bad for privacy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// console.warn(JSON.stringify(thingsFound, null, " "));
|
||||||
|
|
||||||
|
if (count%progressCount == 0) console.warn(count + ' ' + user._id);
|
||||||
|
if (user._id == '9') console.warn('lefnire processed');
|
||||||
|
if (user._id == authorUuid) console.warn(authorName + ' processed');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function displayData() {
|
||||||
|
var today = yyyymmdd(new Date());
|
||||||
|
|
||||||
|
var header = '"date"'; // heading row in CSV data
|
||||||
|
var data = '"' + today + '"'; // data row in CSV data
|
||||||
|
for (var i=0, ic=thingsOfInterest.length; i<ic; i++) {
|
||||||
|
var thingsKey = thingsOfInterest[i];
|
||||||
|
header += ',"' + thingsKey + '"';
|
||||||
|
data += ',"' + (thingsFound[thingsKey] || 0) + '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
//_.each(userResults, function(text, uuid) {
|
||||||
|
//console.log(text); // text contains uuid
|
||||||
|
//});
|
||||||
|
|
||||||
|
console.log("\nCSV DATA:\n");
|
||||||
|
console.log(header);
|
||||||
|
console.log(data);
|
||||||
|
console.log("\nREADABLE DATA:\n");
|
||||||
|
console.log(today);
|
||||||
|
console.log(JSON.stringify(thingsFound, null, " "));
|
||||||
|
console.log("\n");
|
||||||
|
|
||||||
|
console.warn('\n' + count + ' users searched\n');
|
||||||
|
return exiting(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function yyyymmdd(date) {
|
function yyyymmdd(date) {
|
||||||
var yyyy = date.getFullYear().toString();
|
var yyyy = date.getFullYear().toString();
|
||||||
var mm = (date.getMonth()+1).toString();
|
var mm = (date.getMonth()+1).toString();
|
||||||
var dd = date.getDate().toString();
|
var dd = date.getDate().toString();
|
||||||
return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]);
|
return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]);
|
||||||
}
|
}
|
||||||
var today = yyyymmdd(new Date());
|
|
||||||
|
|
||||||
var header = '"date"'; // heading row in CSV data
|
|
||||||
var data = '"' + today + '"'; // data row in CSV data
|
function exiting(code, msg) {
|
||||||
for (var i=0, ic=itemsOfInterest.length; i<ic; i++) {
|
code = code || 0; // 0 = success
|
||||||
var itemKey = itemsOfInterest[i];
|
if (code && !msg) { msg = 'ERROR!'; }
|
||||||
header += ',"' + itemKey + '"';
|
if (msg) {
|
||||||
data += ',"' + (itemsFound[itemKey] || 0) + '"';
|
if (code) { console.error(msg); }
|
||||||
|
else { console.log( msg); }
|
||||||
|
}
|
||||||
|
process.exit(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
print("\nCSV DATA:\n");
|
|
||||||
print(header);
|
|
||||||
print(data);
|
|
||||||
print("\nREADABLE DATA:\n");
|
|
||||||
print(today);
|
|
||||||
print(JSON.stringify(itemsFound, null, " "));
|
|
||||||
print("\n");
|
|
||||||
|
|
||||||
|
|
||||||
/* SAMPLE OUTPUT:
|
/* SAMPLE OUTPUT:
|
||||||
> load("count_users_who_own_specified_gear.js")
|
> load("count_users_who_own_specified_gear.js")
|
||||||
|
|
@ -75,3 +166,4 @@ READABLE DATA:
|
||||||
true
|
true
|
||||||
>
|
>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue