Skip to content

Commit a7671cd

Browse files
committed
v2.24.1: Add --json to "pg-microsharding list"
1 parent ab8d2d6 commit a7671cd

File tree

8 files changed

+109
-85
lines changed

8 files changed

+109
-85
lines changed

docs/functions/calcIslandWeights.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **calcIslandWeights**(`__namedParameters`): `Promise`\<\{ `islandNosToDsn`: `Map`\<`number`, `string`\>; `islands`: `Map`\<`number`, `NonNullable`\<`Awaited`\<`ReturnType`\<*typeof* [`weights`](weights.md)\>\>\>\>; \}\>
1010
11-
Defined in: [src/actions/actionList.ts:193](https://github.com/clickup/pg-microsharding/blob/master/src/actions/actionList.ts#L193)
11+
Defined in: [src/actions/actionList.ts:216](https://github.com/clickup/pg-microsharding/blob/master/src/actions/actionList.ts#L216)
1212

1313
## Parameters
1414

docs/functions/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **cli**(): `void`
1010
11-
Defined in: [src/cli.ts:240](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L240)
11+
Defined in: [src/cli.ts:241](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L241)
1212

1313
A wrapper around main() to call it from a bin script.
1414

docs/functions/main.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
> **main**(`argsIn`): `Promise`\<`boolean`\>
1010
11-
Defined in: [src/cli.ts:168](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L168)
11+
Defined in: [src/cli.ts:169](https://github.com/clickup/pg-microsharding/blob/master/src/cli.ts#L169)
1212

1313
Tool main function.
1414

package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@clickup/pg-microsharding",
33
"description": "Microshards support for PostgreSQL",
4-
"version": "2.23.4",
4+
"version": "2.24.1",
55
"license": "MIT",
66
"keywords": [
77
"postgresql",

src/actions/actionList.ts

Lines changed: 88 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const DEBUG_RENDER_MUL = parseInt(process.env["DEBUG_RENDER_MUL"] ?? "0") || 0;
2929
export async function actionList(args: Args): Promise<boolean> {
3030
const weightSql = args["weight-sql"] || undefined;
3131
const verbose = !!args["verbose"];
32+
const json = !!args["json"];
3233

3334
const dsns = await normalizeDsns(args["dsns"] || args["dsn"]);
3435
if (dsns.length === 0) {
@@ -105,86 +106,108 @@ export async function actionList(args: Args): Promise<boolean> {
105106
((stdout.columns || 80) - 10) / tableWidth,
106107
);
107108

108-
for (const [islandNo, shards] of islands) {
109-
const dsn = islandNosToDsn.get(islandNo)!;
110-
print.section(
111-
`${dsnShort(dsn)} — ` + `${pluralize(shards.length, "microshard")}`,
112-
);
113-
if (shards.length === 0) {
114-
print(indent(table([["No microshards"]])));
115-
continue;
116-
}
117-
118-
const totalWeight: number = sumBy(shards, (shard) => shard.weight);
119-
const totalUnit: string | undefined = shards[0].unit;
120-
const totalOther: Record<string, string | undefined> = mapValues(
121-
shards[0].other,
122-
(_, k) => {
109+
const toPrint = {
110+
islands: [...islands].map(([islandNo, shards]) => {
111+
const dsn = islandNosToDsn.get(islandNo)!;
112+
const totalWeight = sumBy(shards, (shard) => shard.weight);
113+
const totalUnit = first(shards)?.unit;
114+
const totalOther = mapValues(first(shards)?.other, (_, k) => {
123115
const values = shards
124116
.map(({ other }) => parseFloat(other[k]))
125117
.filter((v) => !isNaN(v));
126118
return values.length > 0 ? sum(values).toString() : undefined;
127-
},
128-
);
119+
});
120+
return {
121+
no: islandNo,
122+
caption:
123+
`${dsnShort(dsn)} — ` + `${pluralize(shards.length, "microshard")}`,
124+
dsn: dsnShort(dsn),
125+
header,
126+
shards: shards.map(
127+
({ no, weight, appliedFactor, unit, otherNormalized }) => [
128+
no.toString(),
129+
round(weight / appliedFactor, 5).toString() +
130+
(unit ? ` ${unit}` : "") +
131+
(appliedFactor !== 1 ? ` ✖${appliedFactor}` : ""),
132+
totalWeight !== 0
133+
? `${((weight / totalWeight) * 100).toFixed(1)}%`
134+
: "-",
135+
...Object.values(otherNormalized),
136+
],
137+
),
138+
total:
139+
shards.length > 0
140+
? [
141+
"total",
142+
totalWeight + (totalUnit ? ` ${totalUnit}` : ""),
143+
"100%",
144+
...Object.values(totalOther).map((v) => v ?? ""),
145+
]
146+
: undefined,
147+
};
148+
}),
149+
total:
150+
`TOTAL ${grandTotalWeightColName.toUpperCase()}: ${grandTotalWeight}` +
151+
(grandTotalUnit ? ` ${grandTotalUnit}` : "") +
152+
`, ${pluralize(islands.size, "island")}, ${pluralize(grandTotalShards, "microshard")}`,
153+
};
154+
155+
if (json) {
156+
print(JSON.stringify(toPrint, null, 2) + "\n");
157+
return true;
158+
}
159+
160+
for (const island of toPrint.islands) {
161+
print.section(island.caption);
162+
163+
if (island.shards.length === 0) {
164+
print(indent(table([["No microshards"]])));
165+
continue;
166+
}
129167

130-
const shardTables =
168+
const tables =
169+
json ||
131170
islands.size === 1 ||
132171
maxTablesSideBySide === 0 ||
133172
grandTotalShards + islands.size * 8 < (stdout.rows || 25) ||
134-
shards.length <= 4
135-
? [shards]
136-
: shards.length / maxTablesSideBySide < 2
137-
? chunk(shards, 2)
138-
: chunkIntoN(shards, maxTablesSideBySide);
139-
const tables = shardTables.map((shards, tableNo) =>
140-
indent(
141-
table(
142-
compact([
143-
header,
144-
...shards.map(
145-
({ no, weight, appliedFactor, unit, otherNormalized }) =>
146-
[
147-
no.toString(),
148-
round(weight / appliedFactor, 5).toString() +
149-
(unit ? ` ${unit}` : "") +
150-
(appliedFactor !== 1 ? ` ✖${appliedFactor}` : ""),
151-
totalWeight !== 0
152-
? `${((weight / totalWeight) * 100).toFixed(1)}%`
153-
: "-",
154-
...Object.values(otherNormalized),
155-
].map((cell) => (no === 0 ? chalk.yellow(cell) : cell)),
173+
island.shards.length <= 4
174+
? [island.shards]
175+
: island.shards.length / maxTablesSideBySide < 2
176+
? chunk(island.shards, 2)
177+
: chunkIntoN(island.shards, maxTablesSideBySide);
178+
print(
179+
glueHorizontally(
180+
tables.map((tbl, tblNo) =>
181+
indent(
182+
table(
183+
compact([
184+
island.header.map((cell) => chalk.bold(cell)),
185+
...tbl.map((row) =>
186+
row[0].trim() === "0"
187+
? row.map((cell) => chalk.yellow(cell))
188+
: row,
189+
),
190+
tblNo === 0 &&
191+
island.total &&
192+
island.total.map((cell) => chalk.bold(cell)),
193+
]),
194+
{
195+
drawHorizontalLine: (i, rowCount) =>
196+
i === 0 ||
197+
i === 1 ||
198+
(tblNo === 0 && i === rowCount - 1) ||
199+
i === rowCount,
200+
},
156201
),
157-
tableNo === 0 &&
158-
[
159-
"total",
160-
totalWeight + (totalUnit ? ` ${totalUnit}` : ""),
161-
"100%",
162-
...Object.values(totalOther).map((v) => v ?? ""),
163-
].map((cell) => chalk.bold(cell)),
164-
]),
165-
{
166-
drawHorizontalLine: (i, rowCount) =>
167-
i === 0 ||
168-
i === 1 ||
169-
(tableNo === 0 && i === rowCount - 1) ||
170-
i === rowCount,
171-
},
202+
),
172203
),
173204
),
174205
);
175-
print(glueHorizontally(tables));
176206
}
177207

178208
print(
179-
chalk.bgBlue(
180-
chalk.whiteBright(
181-
" " +
182-
`TOTAL ${grandTotalWeightColName.toUpperCase()}: ${grandTotalWeight}` +
183-
(grandTotalUnit ? ` ${grandTotalUnit}` : "") +
184-
`, ${pluralize(islands.size, "island")}, ${pluralize(grandTotalShards, "microshard")}` +
185-
" ",
186-
),
187-
) + (verbose && !weightSql ? "" : " (try --verbose)"),
209+
chalk.bgBlue(" " + chalk.whiteBright(toPrint.total) + " ") +
210+
(verbose && !weightSql ? "" : " (try --verbose)"),
188211
);
189212

190213
return true;

src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const HELP = unindent(`
4646
[--weight-sql='SELECT returning weight with optional unit']
4747
[--verbose]
4848
[--dsn=DSN | --dsns=DNS1,DSN2,...]
49+
[--json]
4950
5051
pg-microsharding allocate
5152
--shard=N | --shards=N-M

src/internal/parseArgs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const ARGS = {
2222
"migrate-cmd",
2323
"schema-name-fmt",
2424
],
25-
boolean: ["verbose", "wait", "skip-config"],
25+
boolean: ["verbose", "wait", "skip-config", "json"],
2626
} as const;
2727

2828
export type Args = Record<"_", string[]> &

0 commit comments

Comments
 (0)