2020-09-16 11:40:03 +03:00
|
|
|
import configs from "~/knexfile";
|
|
|
|
import knex from "knex";
|
|
|
|
|
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Strings from "~/common/strings";
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
2020-09-16 11:40:03 +03:00
|
|
|
|
|
|
|
const envConfig = configs["development"];
|
|
|
|
const db = knex(envConfig);
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`RUNNING: worker-analytics.js`);
|
2020-09-16 11:40:03 +03:00
|
|
|
|
|
|
|
function sortObject(obj) {
|
|
|
|
var arr = [];
|
|
|
|
for (var prop in obj) {
|
|
|
|
if (obj.hasOwnProperty(prop)) {
|
|
|
|
arr.push({
|
|
|
|
key: prop,
|
|
|
|
value: obj[prop],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-03-07 23:53:54 +03:00
|
|
|
arr.sort(function (a, b) {
|
2020-09-16 11:40:03 +03:00
|
|
|
return b.value - a.value;
|
|
|
|
});
|
|
|
|
//arr.sort(function(a, b) { a.value.toLowerCase().localeCompare(b.value.toLowerCase()); }); //use this to sort as strings
|
|
|
|
return arr; // returns array
|
|
|
|
}
|
|
|
|
|
|
|
|
const run = async () => {
|
2021-03-07 23:53:54 +03:00
|
|
|
const response = await Data.getEveryUser();
|
2020-09-16 11:40:03 +03:00
|
|
|
|
|
|
|
let count = 0;
|
|
|
|
let bytes = 0;
|
|
|
|
let userMap = {};
|
|
|
|
|
|
|
|
response.forEach((user) => {
|
|
|
|
count = count + 1;
|
|
|
|
|
|
|
|
let userBytes = 0;
|
2021-09-02 01:26:25 +03:00
|
|
|
user.library.forEach((each) => {
|
2020-09-16 11:40:03 +03:00
|
|
|
userBytes = each.size + userBytes;
|
|
|
|
bytes = each.size + bytes;
|
|
|
|
});
|
|
|
|
|
|
|
|
userMap[user.username] = userBytes;
|
|
|
|
});
|
|
|
|
|
|
|
|
userMap = sortObject(userMap);
|
|
|
|
userMap = userMap.map((each, index) => {
|
|
|
|
return { ...each, index, value: Strings.bytesToSize(each.value) };
|
|
|
|
});
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(userMap);
|
|
|
|
Logging.log("TOTAL USER COUNT", count);
|
|
|
|
Logging.log("TOTAL BYTES", bytes);
|
|
|
|
Logging.log("TOTAL BYTES (CONVERTED)", Strings.bytesToSize(bytes));
|
2020-09-16 11:40:03 +03:00
|
|
|
};
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
// run();
|
2020-09-16 11:40:03 +03:00
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`FINISHED: worker-analytics.js`);
|
|
|
|
Logging.log(` CTRL +C to return to terminal.`);
|