dbmaster-cli
Version:
Tool for converting tables between Fifa Soccer Games
99 lines (98 loc) • 4.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DistributionAction = void 0;
const chalk_1 = __importDefault(require("chalk"));
const actions_1 = require("../src/actions");
const fifa_config_1 = require("../src/fifa-config");
const utils_1 = require("../src/utils");
const abstract_action_1 = require("./abstract.action");
class DistributionAction extends abstract_action_1.AbstractAction {
async handle(inputs, options) {
const inputFolder = options.find((option) => option.name === 'input').value;
const table = options.find((option) => option.name === 'table').value;
const fifa = options.find((option) => option.name === 'fifa').value;
const column = options.find((option) => option.name === 'column').value;
const cfg = fifa_config_1.fifaConfigFactory(fifa);
if (column && !cfg[table].find((f) => f.name === column)) {
console.error('Invalid argument "column".');
return;
}
if (column) {
await this.printColumnDistribution(cfg, inputFolder, fifa, table, column);
}
else {
await this.printTableDistribution(cfg, inputFolder, fifa, table);
}
}
async printColumnDistribution(cfg, path, fifa, table, column) {
console.info(chalk_1.default.green(`[${fifa}]`), chalk_1.default.green(`[${table}]`), chalk_1.default.red(`[${column}]`), chalk_1.default.yellow('How is the value distributed?'));
console.info(chalk_1.default.white('Default value is:'), chalk_1.default.yellow(cfg[table].find((c) => c.name === column).default));
const agg = [];
await actions_1.actionFactory({
input: {
fields: cfg[table],
folder: path
},
table,
actions: [
{
type: actions_1.ActionType.ActionOnData,
onDataFn: (data) => {
utils_1.aggregateFn(agg, data, column);
}
}
]
});
const sortedAggs = agg.sort(utils_1.sortAggregateFn);
console.info(chalk_1.default.white('Most frequent value is:'), chalk_1.default.yellow(sortedAggs.length ? sortedAggs[0].value : undefined));
console.table(sortedAggs);
}
async printTableDistribution(cfg, path, fifa, table) {
console.info(chalk_1.default.green(`[${fifa}]`), chalk_1.default.red(`[${table}]`), chalk_1.default.yellow('How are the values distributed?'));
const aggregatedData = new Object();
await actions_1.actionFactory({
input: {
fields: cfg[table],
folder: path
},
table,
actions: [
{
type: actions_1.ActionType.ActionOnData,
onDataFn: (data) => {
for (const f of cfg[table].sort(utils_1.sortByOrder)) {
const agg = aggregatedData[f.name] || [];
utils_1.aggregateFn(agg, data, f.name);
aggregatedData[f.name] = agg.sort(utils_1.sortAggregateFn);
}
}
}
]
});
let showedData = new Object();
for (const key of Object.keys(aggregatedData)) {
const firstFrequent = aggregatedData[key][0] || undefined;
const secondFrequent = aggregatedData[key][1] || undefined;
const thirdFrequent = aggregatedData[key][2] || undefined;
const defaultValue = cfg[table].find((f) => f.name === key).default;
showedData = {
...showedData,
[key]: {
mostFreqIsDefault: defaultValue === (firstFrequent ? firstFrequent.value : undefined),
defaultValue,
firstValue: firstFrequent ? firstFrequent.value : undefined,
firstCount: firstFrequent ? firstFrequent.count : undefined,
secondValue: secondFrequent ? secondFrequent.value : undefined,
secondCount: secondFrequent ? secondFrequent.count : undefined,
thirdValue: thirdFrequent ? thirdFrequent.value : undefined,
thirdCount: thirdFrequent ? thirdFrequent.count : undefined
}
};
}
console.table(showedData);
}
}
exports.DistributionAction = DistributionAction;