UNPKG

@clickup/pg-microsharding

Version:
90 lines 3.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.actionFactor = actionFactor; const flatten_1 = __importDefault(require("lodash/flatten")); const groupBy_1 = __importDefault(require("lodash/groupBy")); const round_1 = __importDefault(require("lodash/round")); const prompts_1 = __importDefault(require("prompts")); const table_1 = require("table"); const factor_1 = require("../api/factor"); const getSchemaWeights_1 = require("../internal/getSchemaWeights"); const logging_1 = require("../internal/logging"); const names_1 = require("../internal/names"); const promiseAllMap_1 = require("../internal/promiseAllMap"); const actionList_1 = require("./actionList"); const parseMatchingShards_1 = require("./internal/parseMatchingShards"); /** * Sets the weight factor for some shards. */ async function actionFactor(args) { var _a; const { matchingShards } = await (0, parseMatchingShards_1.parseMatchingShards)(args); let factorSpec; if ((_a = args["factor"]) === null || _a === void 0 ? void 0 : _a.match(/^([-+*])?(\d+(\.\d+)?)$/)) { factorSpec = { op: RegExp.$1, num: parseFloat(RegExp.$2), }; } else { await (0, actionList_1.actionList)(args).catch(() => { }); throw 'Please provide --factor=P|+P.Q|-P.Q|"*P.Q", factor to set for the microshard(s)'; } const plan = (0, flatten_1.default)(await (0, promiseAllMap_1.promiseAllMap)(Object.entries((0, groupBy_1.default)(matchingShards, "dsn")), async ([dsn, shards]) => { const weights = await (0, getSchemaWeights_1.getSchemaWeights)({ dsn, schemas: shards.map(({ schema }) => schema), weightSql: undefined, }); return shards.map(({ schema }) => { const info = weights.get(schema); const oldFactor = (info === null || info === void 0 ? void 0 : info.appliedFactor) || 1; return { dsn, schema, oldFactor, newFactor: (0, round_1.default)(factorSpec.op === "" ? factorSpec.num : factorSpec.op === "+" ? oldFactor + factorSpec.num : factorSpec.op === "-" ? Math.max(oldFactor - factorSpec.num, 1) : factorSpec.op === "*" ? oldFactor * factorSpec.num : oldFactor, 3), }; }); })); (0, logging_1.print)((0, table_1.table)([ ["DSN", "Microshard", "Current Factor", "New Factor"], ...plan.map(({ dsn, schema, oldFactor, newFactor }) => [ (0, names_1.dsnShort)(dsn), schema, oldFactor, newFactor, ]), ], { drawHorizontalLine: (i, rowCount) => i === 0 || i === 1 || i === rowCount, }).trim()); const validResponse = "apply"; const response = await (0, prompts_1.default)({ type: "text", name: "value", message: `Type "${validResponse}" to apply the changes in weight factors:`, validate: (value) => value !== validResponse ? `Enter "${validResponse}" to confirm, ^C to skip` : true, }); if (response.value !== validResponse) { return false; } for (const { dsn, schema, newFactor } of plan) { (0, logging_1.print)(`- ${(0, names_1.dsnShort)(dsn)}, ${schema}: setting factor=${newFactor}`); await (0, factor_1.factor)({ dsn, schema, factor: newFactor }); } return true; } //# sourceMappingURL=actionFactor.js.map