Skip to content

Commit

Permalink
Improve messaging when bulk deleting or uploading KV Pairs (#1080)
Browse files Browse the repository at this point in the history
Closes #555
  • Loading branch information
Cass Fridkin authored May 23, 2022
1 parent 8070763 commit 4a09c1b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .changeset/twelve-goats-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

Improve messaging when bulk deleting or uploading KV Pairs

Closes #555
16 changes: 8 additions & 8 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,10 +1205,10 @@ describe("wrangler", () => {
);
expect(requests.count).toEqual(3);
expect(std.out).toMatchInlineSnapshot(`
"Uploaded 0 of 12000.
Uploaded 5000 of 12000.
Uploaded 10000 of 12000.
Uploaded 12000 of 12000.
"Uploaded 0% (0 out of 12,000)
Uploaded 41% (5,000 out of 12,000)
Uploaded 83% (10,000 out of 12,000)
Uploaded 100% (12,000 out of 12,000)
Success!"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand Down Expand Up @@ -1358,10 +1358,10 @@ describe("wrangler", () => {
);
expect(requests.count).toEqual(3);
expect(std.out).toMatchInlineSnapshot(`
"Deleted 0 of 12000.
Deleted 5000 of 12000.
Deleted 10000 of 12000.
Deleted 12000 of 12000.
"Deleted 0% (0 out of 12,000)
Deleted 41% (5,000 out of 12,000)
Deleted 83% (10,000 out of 12,000)
Deleted 100% (12,000 out of 12,000)
Success!"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand Down
18 changes: 2 additions & 16 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2397,14 +2397,7 @@ function createCLIParser(argv: string[]) {
}

const accountId = await requireAuth(config);
await putKVBulkKeyValue(
accountId,
namespaceId,
content,
(index, total) => {
logger.log(`Uploaded ${index} of ${total}.`);
}
);
await putKVBulkKeyValue(accountId, namespaceId, content);

logger.log("Success!");
}
Expand Down Expand Up @@ -2494,14 +2487,7 @@ function createCLIParser(argv: string[]) {

const accountId = await requireAuth(config);

await deleteKVBulkKeyValue(
accountId,
namespaceId,
content,
(index, total) => {
logger.log(`Deleted ${index} of ${total}.`);
}
);
await deleteKVBulkKeyValue(accountId, namespaceId, content);

logger.log("Success!");
}
Expand Down
44 changes: 34 additions & 10 deletions packages/wrangler/src/kv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URLSearchParams } from "node:url";
import { fetchListResult, fetchResult, fetchKVGetValue } from "./cfetch";
import { logger } from "./logger";
import type { Config } from "./config";

/** The largest number of kv items we can pass to the API in a single request. */
Expand Down Expand Up @@ -205,15 +206,37 @@ export async function deleteKVKeyValue(
);
}

/**
* Formatter for converting e.g. 5328 --> 5,328
*/
const formatNumber = new Intl.NumberFormat("en-US", {
notation: "standard",
}).format;

/**
* Helper function for bulk requests, logs ongoing output to console.
*/
function logBulkProgress(
operation: "put" | "delete",
index: number,
total: number
) {
logger.log(
`${operation === "put" ? "Uploaded" : "Deleted"} ${Math.floor(
(100 * index) / total
)}% (${formatNumber(index)} out of ${formatNumber(total)})`
);
}

export async function putKVBulkKeyValue(
accountId: string,
namespaceId: string,
keyValues: KeyValue[],
progressCallback: (index: number, total: number) => void
quiet = false
) {
for (let index = 0; index < keyValues.length; index += BATCH_KEY_MAX) {
if (progressCallback && keyValues.length > BATCH_KEY_MAX) {
progressCallback(index, keyValues.length);
if (!quiet && keyValues.length > BATCH_KEY_MAX) {
logBulkProgress("put", index, keyValues.length);
}

await fetchResult(
Expand All @@ -225,20 +248,21 @@ export async function putKVBulkKeyValue(
}
);
}
if (progressCallback && keyValues.length > BATCH_KEY_MAX) {
progressCallback(keyValues.length, keyValues.length);

if (!quiet && keyValues.length > BATCH_KEY_MAX) {
logBulkProgress("put", keyValues.length, keyValues.length);
}
}

export async function deleteKVBulkKeyValue(
accountId: string,
namespaceId: string,
keys: string[],
progressCallback: (index: number, total: number) => void
quiet = false
) {
for (let index = 0; index < keys.length; index += BATCH_KEY_MAX) {
if (progressCallback && keys.length > BATCH_KEY_MAX) {
progressCallback(index, keys.length);
if (!quiet && keys.length > BATCH_KEY_MAX) {
logBulkProgress("delete", index, keys.length);
}

await fetchResult(
Expand All @@ -250,8 +274,8 @@ export async function deleteKVBulkKeyValue(
}
);
}
if (progressCallback && keys.length > BATCH_KEY_MAX) {
progressCallback(keys.length, keys.length);
if (!quiet && keys.length > BATCH_KEY_MAX) {
logBulkProgress("delete", keys.length, keys.length);
}
}

Expand Down
9 changes: 2 additions & 7 deletions packages/wrangler/src/sites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,9 @@ export async function syncAssets(

await Promise.all([
// upload all the new assets
putKVBulkKeyValue(accountId, namespace, toUpload, () => {}),
putKVBulkKeyValue(accountId, namespace, toUpload),
// delete all the unused assets
deleteKVBulkKeyValue(
accountId,
namespace,
Array.from(namespaceKeys),
() => {}
),
deleteKVBulkKeyValue(accountId, namespace, Array.from(namespaceKeys)),
]);

logger.log("↗️ Done syncing assets");
Expand Down

0 comments on commit 4a09c1b

Please sign in to comment.