@nitpicker/report-google-sheets 0.18.1 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/data/create-discrepancies.d.ts +10 -12
- package/lib/data/create-discrepancies.js +27 -31
- package/lib/data/create-image-list.d.ts +26 -6
- package/lib/data/create-image-list.js +65 -39
- package/lib/data/create-links.d.ts +18 -1
- package/lib/data/create-links.js +91 -55
- package/lib/data/create-page-list.d.ts +24 -24
- package/lib/data/create-page-list.js +178 -224
- package/lib/data/create-referrers-relational-table.d.ts +13 -7
- package/lib/data/create-referrers-relational-table.js +57 -47
- package/lib/data/create-resources-relational-table.d.ts +7 -0
- package/lib/data/create-resources-relational-table.js +41 -33
- package/lib/data/create-resources.d.ts +24 -124
- package/lib/data/create-resources.js +100 -264
- package/lib/data/create-violations.d.ts +7 -6
- package/lib/data/create-violations.js +34 -19
- package/lib/dedupe-progress-message.d.ts +17 -0
- package/lib/dedupe-progress-message.js +26 -0
- package/lib/format-progress-count.d.ts +19 -0
- package/lib/format-progress-count.js +22 -0
- package/lib/open-report-archive.d.ts +54 -0
- package/lib/open-report-archive.js +76 -0
- package/lib/report.d.ts +23 -10
- package/lib/report.js +146 -149
- package/lib/reports/get-plugin-reports.d.ts +14 -6
- package/lib/reports/get-plugin-reports.js +14 -34
- package/lib/sheets/create-sheets.d.ts +66 -36
- package/lib/sheets/create-sheets.js +228 -362
- package/lib/sheets/estimate-cell-budget.d.ts +55 -0
- package/lib/sheets/estimate-cell-budget.js +56 -0
- package/lib/sheets/types.d.ts +111 -91
- package/lib/test-helpers/assert-no-lazy-cells.d.ts +16 -0
- package/lib/test-helpers/assert-no-lazy-cells.js +23 -0
- package/lib/test-helpers/cell-inspection.d.ts +20 -0
- package/lib/test-helpers/cell-inspection.js +21 -0
- package/lib/test-helpers/create-mock-sheet.d.ts +30 -0
- package/lib/test-helpers/create-mock-sheet.js +71 -0
- package/lib/test-helpers/one-chunk.d.ts +8 -0
- package/lib/test-helpers/one-chunk.js +11 -0
- package/lib/utils/join-urls-for-note.d.ts +27 -0
- package/lib/utils/join-urls-for-note.js +48 -0
- package/lib/utils/truncate-note-text.d.ts +15 -0
- package/lib/utils/truncate-note-text.js +23 -0
- package/package.json +13 -12
- package/lib/archive.d.ts +0 -20
- package/lib/archive.js +0 -34
- package/lib/sheets/run-finalize-resources.d.ts +0 -40
- package/lib/sheets/run-finalize-resources.js +0 -44
- package/lib/types.d.ts +0 -11
- package/lib/types.js +0 -1
- package/lib/utils/canonicalize-url.d.ts +0 -53
- package/lib/utils/canonicalize-url.js +0 -105
- package/lib/utils/has-prop-filter.d.ts +0 -18
- package/lib/utils/has-prop-filter.js +0 -19
- package/lib/utils/sort-resources-by-url.d.ts +0 -69
- package/lib/utils/sort-resources-by-url.js +0 -223
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Effective cell budget for one spreadsheet, kept below Google Sheets'
|
|
3
|
+
* documented 10,000,000-cell-per-spreadsheet limit. The margin absorbs
|
|
4
|
+
* header rows, formatting metadata, and any rounding in row-count
|
|
5
|
+
* estimates, so a report that lands close to the true limit still leaves
|
|
6
|
+
* headroom rather than failing the final API call.
|
|
7
|
+
*/
|
|
8
|
+
export declare const CELL_BUDGET_LIMIT = 9800000;
|
|
9
|
+
/** One sheet's estimated size, before budget allocation. */
|
|
10
|
+
export interface SheetCellEstimate {
|
|
11
|
+
/** Sheet display name (matches `CreateSheetSetting.name`). */
|
|
12
|
+
readonly name: string;
|
|
13
|
+
/** Column count (header row length). */
|
|
14
|
+
readonly columns: number;
|
|
15
|
+
/** Estimated data row count, from `CreateSheetSetting.estimateRowCount()`. */
|
|
16
|
+
readonly estimatedRows: number;
|
|
17
|
+
}
|
|
18
|
+
/** One sheet's estimated size plus its budget allocation. */
|
|
19
|
+
export interface SheetCellAllocation extends SheetCellEstimate {
|
|
20
|
+
/** Estimated maximum data rows this sheet can send within the shared budget. */
|
|
21
|
+
readonly maxRows: number;
|
|
22
|
+
/** `true` iff `estimatedRows` exceeds `maxRows` — this sheet is expected to be truncated. */
|
|
23
|
+
readonly truncated: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Computes an advisory cell-budget allocation across every selected sheet,
|
|
27
|
+
* in priority order, for the pre-generation warning shown to the user.
|
|
28
|
+
*
|
|
29
|
+
* This is an *estimate* for display purposes only — the actual budget
|
|
30
|
+
* enforcement during generation (`create-sheets.ts`'s Phase 2) decrements a
|
|
31
|
+
* live remaining-cell counter by each sheet's real sent-row count as it
|
|
32
|
+
* completes, not by this function's estimates. The two can disagree (a
|
|
33
|
+
* sheet's `estimateRowCount()` is a `COUNT(*)`-style approximation, not a
|
|
34
|
+
* guarantee), but that is fine: this function exists only to tell the user
|
|
35
|
+
* up front which sheets are *likely* to be truncated and roughly by how
|
|
36
|
+
* much, before any API calls are made.
|
|
37
|
+
*
|
|
38
|
+
* Every sheet's header row is assumed to be created unconditionally
|
|
39
|
+
* (Phase 1 creates every selected sheet and sets its header before any
|
|
40
|
+
* truncation decision is made), so the header cost for every sheet is
|
|
41
|
+
* subtracted from the budget up front, before allocating the remainder to
|
|
42
|
+
* data rows in priority order.
|
|
43
|
+
* @param estimates - Per-sheet estimates, already ordered by priority
|
|
44
|
+
* (`Page List` first, etc. — see `report.ts`'s `SHEET_PRIORITY_ORDER`).
|
|
45
|
+
* @param budget - Total usable cells across the spreadsheet. Defaults to
|
|
46
|
+
* {@link CELL_BUDGET_LIMIT}; overridable for tests.
|
|
47
|
+
* @returns One allocation per input estimate, in the same order.
|
|
48
|
+
* @example
|
|
49
|
+
* const allocations = estimateCellBudget([
|
|
50
|
+
* { name: 'Page List', columns: 65, estimatedRows: 200_000 },
|
|
51
|
+
* { name: 'Links', columns: 9, estimatedRows: 200_000 },
|
|
52
|
+
* ]);
|
|
53
|
+
* const truncatedNames = allocations.filter((a) => a.truncated).map((a) => a.name);
|
|
54
|
+
*/
|
|
55
|
+
export declare function estimateCellBudget(estimates: readonly SheetCellEstimate[], budget?: number): SheetCellAllocation[];
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Effective cell budget for one spreadsheet, kept below Google Sheets'
|
|
3
|
+
* documented 10,000,000-cell-per-spreadsheet limit. The margin absorbs
|
|
4
|
+
* header rows, formatting metadata, and any rounding in row-count
|
|
5
|
+
* estimates, so a report that lands close to the true limit still leaves
|
|
6
|
+
* headroom rather than failing the final API call.
|
|
7
|
+
*/
|
|
8
|
+
export const CELL_BUDGET_LIMIT = 9_800_000;
|
|
9
|
+
/**
|
|
10
|
+
* Computes an advisory cell-budget allocation across every selected sheet,
|
|
11
|
+
* in priority order, for the pre-generation warning shown to the user.
|
|
12
|
+
*
|
|
13
|
+
* This is an *estimate* for display purposes only — the actual budget
|
|
14
|
+
* enforcement during generation (`create-sheets.ts`'s Phase 2) decrements a
|
|
15
|
+
* live remaining-cell counter by each sheet's real sent-row count as it
|
|
16
|
+
* completes, not by this function's estimates. The two can disagree (a
|
|
17
|
+
* sheet's `estimateRowCount()` is a `COUNT(*)`-style approximation, not a
|
|
18
|
+
* guarantee), but that is fine: this function exists only to tell the user
|
|
19
|
+
* up front which sheets are *likely* to be truncated and roughly by how
|
|
20
|
+
* much, before any API calls are made.
|
|
21
|
+
*
|
|
22
|
+
* Every sheet's header row is assumed to be created unconditionally
|
|
23
|
+
* (Phase 1 creates every selected sheet and sets its header before any
|
|
24
|
+
* truncation decision is made), so the header cost for every sheet is
|
|
25
|
+
* subtracted from the budget up front, before allocating the remainder to
|
|
26
|
+
* data rows in priority order.
|
|
27
|
+
* @param estimates - Per-sheet estimates, already ordered by priority
|
|
28
|
+
* (`Page List` first, etc. — see `report.ts`'s `SHEET_PRIORITY_ORDER`).
|
|
29
|
+
* @param budget - Total usable cells across the spreadsheet. Defaults to
|
|
30
|
+
* {@link CELL_BUDGET_LIMIT}; overridable for tests.
|
|
31
|
+
* @returns One allocation per input estimate, in the same order.
|
|
32
|
+
* @example
|
|
33
|
+
* const allocations = estimateCellBudget([
|
|
34
|
+
* { name: 'Page List', columns: 65, estimatedRows: 200_000 },
|
|
35
|
+
* { name: 'Links', columns: 9, estimatedRows: 200_000 },
|
|
36
|
+
* ]);
|
|
37
|
+
* const truncatedNames = allocations.filter((a) => a.truncated).map((a) => a.name);
|
|
38
|
+
*/
|
|
39
|
+
export function estimateCellBudget(estimates, budget = CELL_BUDGET_LIMIT) {
|
|
40
|
+
const headerCost = estimates.reduce((sum, estimate) => sum + estimate.columns, 0);
|
|
41
|
+
let remaining = Math.max(0, budget - headerCost);
|
|
42
|
+
const allocations = [];
|
|
43
|
+
for (const estimate of estimates) {
|
|
44
|
+
const maxRowsForBudget = estimate.columns > 0
|
|
45
|
+
? Math.floor(remaining / estimate.columns)
|
|
46
|
+
: estimate.estimatedRows;
|
|
47
|
+
const maxRows = Math.min(maxRowsForBudget, estimate.estimatedRows);
|
|
48
|
+
allocations.push({
|
|
49
|
+
...estimate,
|
|
50
|
+
maxRows,
|
|
51
|
+
truncated: estimate.estimatedRows > maxRows,
|
|
52
|
+
});
|
|
53
|
+
remaining = Math.max(0, remaining - maxRows * estimate.columns);
|
|
54
|
+
}
|
|
55
|
+
return allocations;
|
|
56
|
+
}
|
package/lib/sheets/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { Sheet } from '@d-zero/google-sheets';
|
|
2
|
+
import type { ArchiveAccessor } from '@nitpicker/crawler';
|
|
3
3
|
import type { Report } from '@nitpicker/types';
|
|
4
4
|
import type { sheets_v4 } from 'googleapis';
|
|
5
5
|
/** A value that may be synchronous or wrapped in a Promise. */
|
|
@@ -9,112 +9,132 @@ export type HeaderCell = string;
|
|
|
9
9
|
/** Google Sheets text format options (font, color, link, etc.). */
|
|
10
10
|
export type createCellTextFormat = sheets_v4.Schema$TextFormat;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* Receives the analyze plugin reports so that sheets like "Violations" or
|
|
15
|
-
* "Discrepancies" can incorporate plugin data alongside crawler data.
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const createMySheet: CreateSheet = (reports) => ({
|
|
19
|
-
* name: 'My Sheet',
|
|
20
|
-
* createHeaders: () => ['URL', 'Title'],
|
|
21
|
-
* eachPage: (page) => [[
|
|
22
|
-
* createCellData({ value: page.url.href }, defaultCellFormat),
|
|
23
|
-
* createCellData({ value: page.title }, defaultCellFormat),
|
|
24
|
-
* ]],
|
|
25
|
-
* });
|
|
26
|
-
* ```
|
|
12
|
+
* Per-sheet context passed to {@link CreateSheetSetting.run}.
|
|
27
13
|
*/
|
|
28
|
-
export
|
|
14
|
+
export interface RunSheetContext {
|
|
15
|
+
/** The Google Sheets API wrapper for this tab. `appendRow`/`flush` drive the streaming send. */
|
|
16
|
+
readonly sheet: Sheet;
|
|
17
|
+
/**
|
|
18
|
+
* The maximum number of data rows this sheet may send, decided by the
|
|
19
|
+
* shared cell-budget allocation (`estimate-cell-budget.ts`) — see
|
|
20
|
+
* `create-sheets.ts`'s Phase 2 docs. `run()` must stop generating rows
|
|
21
|
+
* once it has sent this many (a `break` out of its own loop), not rely
|
|
22
|
+
* on the caller to truncate for it — the row source is a streaming
|
|
23
|
+
* generator the caller cannot safely interrupt mid-row.
|
|
24
|
+
*/
|
|
25
|
+
readonly maxRows: number;
|
|
26
|
+
/**
|
|
27
|
+
* This sheet's own `estimateRowCount()` result, captured once in Phase
|
|
28
|
+
* 1.5 — the intended denominator for `onProgress`'s progress line.
|
|
29
|
+
* **Not the same as `maxRows`**: `maxRows` is the cell-budget cap this
|
|
30
|
+
* sheet may actually send (can be far smaller *or* far larger than the
|
|
31
|
+
* true row count), so passing it to `onProgress` instead of this value
|
|
32
|
+
* renders a misleading "X / cell-budget-cap" line that looks like a
|
|
33
|
+
* real total. A sheet with no cheap live total available (a streaming
|
|
34
|
+
* `report-export/` source with no per-chunk count) should use this
|
|
35
|
+
* value; a sheet whose own paginated query already returns a live
|
|
36
|
+
* total per page (e.g. `listViewerPages`) may use that instead — it is
|
|
37
|
+
* at least as accurate.
|
|
38
|
+
*/
|
|
39
|
+
readonly estimatedTotal: number;
|
|
40
|
+
/**
|
|
41
|
+
* Reports rows sent so far, for the Lanes progress line. Call this
|
|
42
|
+
* periodically while iterating, not just once at the end.
|
|
43
|
+
* @param sent - Rows sent so far.
|
|
44
|
+
* @param total - This sheet's total row count, normally `ctx.estimatedTotal`.
|
|
45
|
+
*/
|
|
46
|
+
readonly onProgress: (sent: number, total: number) => void;
|
|
47
|
+
}
|
|
29
48
|
/**
|
|
30
49
|
* Configuration for a single Google Sheet tab within the report.
|
|
31
50
|
*
|
|
32
|
-
* The `createSheets` pipeline calls these
|
|
51
|
+
* The `createSheets` pipeline calls these in order:
|
|
52
|
+
*
|
|
53
|
+
* 1. `createHeaders()` - Called once to set the header row (Phase 1).
|
|
54
|
+
* 2. `estimateRowCount()` - Called once, before generation, for the
|
|
55
|
+
* cell-budget warning and the `run()` progress denominator.
|
|
56
|
+
* 3. `run()` - Called once to generate and send every row (Phase 2, run in
|
|
57
|
+
* priority order — see `report.ts`'s `SHEET_PRIORITY_ORDER`).
|
|
58
|
+
* 4. `updateSheet()` - Called once after every sheet's `run()` has
|
|
59
|
+
* completed, for formatting (frozen rows, conditional formatting, etc.).
|
|
33
60
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* Same return semantics as `eachPage`.
|
|
42
|
-
* 5. `addRows()` - (Phase 4) Called once to add plugin-derived data
|
|
43
|
-
* that does not come from page/resource iteration (e.g. Violations).
|
|
44
|
-
* Runs in parallel with Phases 2-3.
|
|
45
|
-
* 6. `updateSheet()` - (Phase 5) Called once after all data is written,
|
|
46
|
-
* for formatting (frozen rows, conditional formatting, etc.).
|
|
61
|
+
* `run()` is expected to stream: generate a row, `sheet.appendRow(...)` it,
|
|
62
|
+
* repeat. It must never accumulate the full row set in memory before
|
|
63
|
+
* sending — that reintroduces the OOM class this contract exists to close.
|
|
64
|
+
* Lazy (`createCellData(() => ...)`) cells are forbidden for the same
|
|
65
|
+
* reason (see `create-cell-data.ts`'s docs): a single lazy cell disables
|
|
66
|
+
* `@d-zero/google-sheets`' automatic 2500-row flush for the rest of the
|
|
67
|
+
* sheet's rows.
|
|
47
68
|
*/
|
|
48
69
|
export interface CreateSheetSetting {
|
|
49
70
|
/** Display name of the sheet tab in Google Sheets. */
|
|
50
71
|
name: string;
|
|
51
|
-
/** Returns the header row cell values. */
|
|
52
|
-
createHeaders: () => Promiseable<HeaderCell[]>;
|
|
53
72
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
73
|
+
* `true` iff `run()` reads from the viewer read model (`viewer_pages`,
|
|
74
|
+
* `viewer_anchor_facts`, `viewer_images`, etc.) — `report.ts` only calls
|
|
75
|
+
* `requireViewerReadModel` once, up front, when at least one selected
|
|
76
|
+
* sheet sets this. Sheets that read only write-model tables (Links,
|
|
77
|
+
* Resources, Violations) must leave this `false`/omitted so a report
|
|
78
|
+
* limited to those sheets never requires a `viewer-build` run. Defaults
|
|
79
|
+
* to `false`.
|
|
61
80
|
*/
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
|
|
65
|
-
* @param page - The current page object.
|
|
66
|
-
* @param num - 1-based page number within the total.
|
|
67
|
-
* @param total - Total number of pages.
|
|
68
|
-
* @param prevPage - The previous page, or `null` for the first.
|
|
69
|
-
* @returns Row data (one or more rows), `null`/`void` to skip.
|
|
70
|
-
*/
|
|
71
|
-
eachPage?: (page: Page, num: number, total: number, prevPage: Page | null) => Promiseable<Cell[][] | null | void>;
|
|
72
|
-
/**
|
|
73
|
-
* Generates row data for a single network resource.
|
|
74
|
-
*
|
|
75
|
-
* To accumulate state across all resources and emit aggregated rows
|
|
76
|
-
* once at the end, use {@link CreateSheetSetting.finalizeResources}
|
|
77
|
-
* instead of trying to detect the final iteration here.
|
|
78
|
-
* @param resource - The resource record from the archive.
|
|
79
|
-
* @returns Row data, `null`/`void` to skip.
|
|
80
|
-
*/
|
|
81
|
-
eachResource?: (resource: Resource) => Promiseable<Cell[][] | null | void>;
|
|
82
|
-
/**
|
|
83
|
-
* When `true`, the Phase 3 dispatcher does **not** sort the
|
|
84
|
-
* resources array before calling `eachResource`; the setting takes
|
|
85
|
-
* full responsibility for ordering its own output (typically by
|
|
86
|
-
* sorting the aggregated rows inside `finalizeResources`).
|
|
87
|
-
*
|
|
88
|
-
* Useful for dedupe-style settings where sorting the raw 1M+
|
|
89
|
-
* resource list up front is wasted work — the aggregated output
|
|
90
|
-
* is orders of magnitude smaller, so sorting *after* aggregation
|
|
91
|
-
* is far cheaper. Defaults to `false` (sorted insertion order).
|
|
92
|
-
*/
|
|
93
|
-
skipSortResources?: boolean;
|
|
81
|
+
requiresReadModel?: boolean;
|
|
82
|
+
/** Returns the header row cell values. */
|
|
83
|
+
createHeaders: () => Promiseable<HeaderCell[]>;
|
|
94
84
|
/**
|
|
95
|
-
*
|
|
96
|
-
* `
|
|
97
|
-
*
|
|
98
|
-
* `
|
|
99
|
-
* e.g. dedupe-by-canonical-URL aggregation.
|
|
100
|
-
*
|
|
101
|
-
* Decoupled from `eachResource` so the contract does not depend on
|
|
102
|
-
* the Phase 3 implementation (sequential vs. parallel iteration,
|
|
103
|
-
* exact `num/total` semantics, etc.).
|
|
104
|
-
* @returns Row data to append, `null`/`void` to emit nothing.
|
|
85
|
+
* Estimates this sheet's total data row count, for the cell-budget
|
|
86
|
+
* warning (`estimate-cell-budget.ts`) and the `run()` progress
|
|
87
|
+
* denominator. An approximation is fine (a `COUNT(*)`-style query) —
|
|
88
|
+
* `run()` is the source of truth for what actually gets sent.
|
|
105
89
|
*/
|
|
106
|
-
|
|
90
|
+
estimateRowCount: () => Promiseable<number>;
|
|
107
91
|
/**
|
|
108
|
-
* Generates
|
|
109
|
-
*
|
|
110
|
-
*
|
|
92
|
+
* Generates and sends every row for this sheet, via `ctx.sheet.appendRow(...)`.
|
|
93
|
+
* Must stream (never build a full in-memory row array) and must stop
|
|
94
|
+
* once it has sent `ctx.maxRows` rows.
|
|
95
|
+
* @param ctx - See {@link RunSheetContext}.
|
|
111
96
|
*/
|
|
112
|
-
|
|
97
|
+
run: (ctx: RunSheetContext) => Promise<void>;
|
|
113
98
|
/**
|
|
114
|
-
* Post-data formatting hook. Called after
|
|
115
|
-
* Typically used for freezing rows/columns, conditional
|
|
116
|
-
* and hiding unused columns.
|
|
99
|
+
* Post-data formatting hook. Called after every sheet's `run()` has
|
|
100
|
+
* completed. Typically used for freezing rows/columns, conditional
|
|
101
|
+
* formatting, and hiding unused columns.
|
|
117
102
|
* @param sheet - The Google Sheets API wrapper for this tab.
|
|
118
103
|
*/
|
|
119
104
|
updateSheet?: (sheet: Sheet) => Promiseable<void>;
|
|
120
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Factory function that produces a {@link CreateSheetSetting} for one sheet.
|
|
108
|
+
*
|
|
109
|
+
* Receives the analyze plugin reports (for sheets like "Violations" or
|
|
110
|
+
* "Discrepancies" that incorporate plugin data) and the archive accessor
|
|
111
|
+
* (for `run()`/`estimateRowCount()` to query against).
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* const createMySheet: CreateSheet = (reports, accessor) => ({
|
|
115
|
+
* name: 'My Sheet',
|
|
116
|
+
* createHeaders: () => ['URL', 'Title'],
|
|
117
|
+
* estimateRowCount: async () => (await listViewerPages(accessor, { limit: 1 })).total,
|
|
118
|
+
* async run({ sheet, maxRows, onProgress }) {
|
|
119
|
+
* let sent = 0;
|
|
120
|
+
* let cursor: string | undefined;
|
|
121
|
+
* for (;;) {
|
|
122
|
+
* const page = await listViewerPages(accessor, { limit: 500, cursor });
|
|
123
|
+
* for (const item of page.items) {
|
|
124
|
+
* if (sent >= maxRows) return;
|
|
125
|
+
* await sheet.appendRow([
|
|
126
|
+
* createCellData({ value: item.url }, defaultCellFormat),
|
|
127
|
+
* createCellData({ value: item.title }, defaultCellFormat),
|
|
128
|
+
* ]);
|
|
129
|
+
* sent++;
|
|
130
|
+
* onProgress(sent, page.total);
|
|
131
|
+
* }
|
|
132
|
+
* if (!page.nextCursor) break;
|
|
133
|
+
* cursor = page.nextCursor;
|
|
134
|
+
* }
|
|
135
|
+
* await sheet.flush();
|
|
136
|
+
* },
|
|
137
|
+
* });
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export type CreateSheet = (reports: Report[], accessor: ArchiveAccessor) => Promiseable<CreateSheetSetting>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Cell as CellType } from '@d-zero/google-sheets';
|
|
2
|
+
/**
|
|
3
|
+
* Asserts that no cell in the given rows is a lazy (`createCellData(() => ...)`)
|
|
4
|
+
* cell — i.e. every cell's `provide` is the base `Cell.prototype.provide`,
|
|
5
|
+
* not an overridden `LazyCell.prototype.provide`.
|
|
6
|
+
*
|
|
7
|
+
* `@d-zero/google-sheets` detects a lazy cell via this exact identity check
|
|
8
|
+
* (`containsLazyCell`) and, once one appears, disables `Sheet#appendRow`'s
|
|
9
|
+
* automatic 2500-row flush for every subsequent row until an explicit
|
|
10
|
+
* `flush()` — the mechanism that let the pre-rewrite Page List's
|
|
11
|
+
* "Internal Referrers" thunk buffer an entire batch with no flush and OOM.
|
|
12
|
+
* This assertion pins every rewritten sheet against reintroducing that
|
|
13
|
+
* pattern.
|
|
14
|
+
* @param rows - Rows to check, as produced by `CreateSheetSetting.run()`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function assertNoLazyCells(rows: readonly CellType[][]): void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Cell } from '@d-zero/google-sheets';
|
|
2
|
+
import { expect } from 'vitest';
|
|
3
|
+
/**
|
|
4
|
+
* Asserts that no cell in the given rows is a lazy (`createCellData(() => ...)`)
|
|
5
|
+
* cell — i.e. every cell's `provide` is the base `Cell.prototype.provide`,
|
|
6
|
+
* not an overridden `LazyCell.prototype.provide`.
|
|
7
|
+
*
|
|
8
|
+
* `@d-zero/google-sheets` detects a lazy cell via this exact identity check
|
|
9
|
+
* (`containsLazyCell`) and, once one appears, disables `Sheet#appendRow`'s
|
|
10
|
+
* automatic 2500-row flush for every subsequent row until an explicit
|
|
11
|
+
* `flush()` — the mechanism that let the pre-rewrite Page List's
|
|
12
|
+
* "Internal Referrers" thunk buffer an entire batch with no flush and OOM.
|
|
13
|
+
* This assertion pins every rewritten sheet against reintroducing that
|
|
14
|
+
* pattern.
|
|
15
|
+
* @param rows - Rows to check, as produced by `CreateSheetSetting.run()`.
|
|
16
|
+
*/
|
|
17
|
+
export function assertNoLazyCells(rows) {
|
|
18
|
+
for (const row of rows) {
|
|
19
|
+
for (const cell of row) {
|
|
20
|
+
expect(cell.provide).toBe(Cell.prototype.provide);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** A `Cell`-shaped value with a callable, no-argument-required `provide()`. */
|
|
2
|
+
interface InspectableCell {
|
|
3
|
+
provide: (n?: number) => {
|
|
4
|
+
userEnteredValue: Record<string, unknown>;
|
|
5
|
+
note?: string;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Extracts the primitive value from a Cell by calling `provide()` and reading `userEnteredValue`.
|
|
10
|
+
* @param cell - A Cell object with a `provide` method.
|
|
11
|
+
* @returns The string, number, boolean, or formula value held by the cell.
|
|
12
|
+
*/
|
|
13
|
+
export declare function cellValue(cell: InspectableCell): unknown;
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the note string from a Cell by calling `provide()`.
|
|
16
|
+
* @param cell - A Cell object with a `provide` method.
|
|
17
|
+
* @returns The note attached to the cell, or `undefined`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function cellNote(cell: InspectableCell): string | undefined;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extracts the primitive value from a Cell by calling `provide()` and reading `userEnteredValue`.
|
|
3
|
+
* @param cell - A Cell object with a `provide` method.
|
|
4
|
+
* @returns The string, number, boolean, or formula value held by the cell.
|
|
5
|
+
*/
|
|
6
|
+
export function cellValue(cell) {
|
|
7
|
+
const provided = cell.provide();
|
|
8
|
+
return (provided.userEnteredValue.stringValue ??
|
|
9
|
+
provided.userEnteredValue.numberValue ??
|
|
10
|
+
provided.userEnteredValue.boolValue ??
|
|
11
|
+
provided.userEnteredValue.formulaValue ??
|
|
12
|
+
'');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Extracts the note string from a Cell by calling `provide()`.
|
|
16
|
+
* @param cell - A Cell object with a `provide` method.
|
|
17
|
+
* @returns The note attached to the cell, or `undefined`.
|
|
18
|
+
*/
|
|
19
|
+
export function cellNote(cell) {
|
|
20
|
+
return cell.provide().note;
|
|
21
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Cell, Sheet } from '@d-zero/google-sheets';
|
|
2
|
+
/** A fake `Sheet` that records every `appendRow`/`flush`/formatting call instead of hitting the API. */
|
|
3
|
+
export interface MockSheet {
|
|
4
|
+
/** The fake `Sheet`, cast to the real type for passing to `run()`/`updateSheet()`. */
|
|
5
|
+
readonly sheet: Sheet;
|
|
6
|
+
/** Every row appended so far, in call order. */
|
|
7
|
+
readonly rows: Cell[][];
|
|
8
|
+
/** Number of times `flush()` was called. */
|
|
9
|
+
readonly flushCount: number;
|
|
10
|
+
/** Headers set via `setHeaders()`, or `[]` if never called. */
|
|
11
|
+
readonly headers: string[];
|
|
12
|
+
/** Every `conditionalFormat()` call's arguments, in call order. */
|
|
13
|
+
readonly conditionalFormatCalls: unknown[][];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a fake `Sheet` for testing `CreateSheetSetting.run()`/`updateSheet()`
|
|
17
|
+
* without a real Google Sheets API connection.
|
|
18
|
+
*
|
|
19
|
+
* `appendRow` mirrors the real `@d-zero/google-sheets` `Sheet`'s rest-param
|
|
20
|
+
* signature (`appendRow(...rows: Cell[][])`) — each call's arguments are
|
|
21
|
+
* flattened into {@link MockSheet.rows} in order, so a test calling
|
|
22
|
+
* `sheet.appendRow(row)` (one row per call, the new `run()` contract's
|
|
23
|
+
* convention) sees `rows` grow by exactly one entry per call.
|
|
24
|
+
* @returns A {@link MockSheet}.
|
|
25
|
+
* @example
|
|
26
|
+
* const mock = createMockSheet();
|
|
27
|
+
* await setting.run({ sheet: mock.sheet, maxRows: 10, onProgress: () => {} });
|
|
28
|
+
* expect(mock.rows).toHaveLength(3);
|
|
29
|
+
*/
|
|
30
|
+
export declare function createMockSheet(): MockSheet;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a fake `Sheet` for testing `CreateSheetSetting.run()`/`updateSheet()`
|
|
3
|
+
* without a real Google Sheets API connection.
|
|
4
|
+
*
|
|
5
|
+
* `appendRow` mirrors the real `@d-zero/google-sheets` `Sheet`'s rest-param
|
|
6
|
+
* signature (`appendRow(...rows: Cell[][])`) — each call's arguments are
|
|
7
|
+
* flattened into {@link MockSheet.rows} in order, so a test calling
|
|
8
|
+
* `sheet.appendRow(row)` (one row per call, the new `run()` contract's
|
|
9
|
+
* convention) sees `rows` grow by exactly one entry per call.
|
|
10
|
+
* @returns A {@link MockSheet}.
|
|
11
|
+
* @example
|
|
12
|
+
* const mock = createMockSheet();
|
|
13
|
+
* await setting.run({ sheet: mock.sheet, maxRows: 10, onProgress: () => {} });
|
|
14
|
+
* expect(mock.rows).toHaveLength(3);
|
|
15
|
+
*/
|
|
16
|
+
export function createMockSheet() {
|
|
17
|
+
const rows = [];
|
|
18
|
+
const conditionalFormatCalls = [];
|
|
19
|
+
let flushCount = 0;
|
|
20
|
+
let headers = [];
|
|
21
|
+
const sheet = {
|
|
22
|
+
setHeaders(newHeaders) {
|
|
23
|
+
headers = newHeaders;
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
},
|
|
26
|
+
appendRow(...appendedRows) {
|
|
27
|
+
for (const row of appendedRows) {
|
|
28
|
+
rows.push(row);
|
|
29
|
+
}
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
},
|
|
32
|
+
flush() {
|
|
33
|
+
flushCount++;
|
|
34
|
+
return Promise.resolve();
|
|
35
|
+
},
|
|
36
|
+
get sentCount() {
|
|
37
|
+
return rows.length;
|
|
38
|
+
},
|
|
39
|
+
frozen() {
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
},
|
|
42
|
+
conditionalFormat(...args) {
|
|
43
|
+
conditionalFormatCalls.push(args);
|
|
44
|
+
return Promise.resolve();
|
|
45
|
+
},
|
|
46
|
+
getColNumByHeaderName(name) {
|
|
47
|
+
const index = headers.indexOf(name);
|
|
48
|
+
if (index === -1) {
|
|
49
|
+
throw new Error(`createMockSheet: unknown header "${name}"`);
|
|
50
|
+
}
|
|
51
|
+
return index + 1;
|
|
52
|
+
},
|
|
53
|
+
hideCol() {
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
},
|
|
56
|
+
overwriteHeaderFormat() {
|
|
57
|
+
return Promise.resolve();
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
sheet,
|
|
62
|
+
rows,
|
|
63
|
+
get flushCount() {
|
|
64
|
+
return flushCount;
|
|
65
|
+
},
|
|
66
|
+
get headers() {
|
|
67
|
+
return headers;
|
|
68
|
+
},
|
|
69
|
+
conditionalFormatCalls,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a fixed chunk array as the single yield of an async generator, for
|
|
3
|
+
* mocking a chunked `stream*` read function from `@nitpicker/query`
|
|
4
|
+
* (e.g. `streamAllContentItems`, `streamAnchorFactEdges`).
|
|
5
|
+
* @param chunk - The one chunk to yield.
|
|
6
|
+
* @yields The given chunk, once.
|
|
7
|
+
*/
|
|
8
|
+
export declare function oneChunk<T>(chunk: T[]): AsyncGenerator<T[]>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps a fixed chunk array as the single yield of an async generator, for
|
|
3
|
+
* mocking a chunked `stream*` read function from `@nitpicker/query`
|
|
4
|
+
* (e.g. `streamAllContentItems`, `streamAnchorFactEdges`).
|
|
5
|
+
* @param chunk - The one chunk to yield.
|
|
6
|
+
* @yields The given chunk, once.
|
|
7
|
+
*/
|
|
8
|
+
export async function* oneChunk(chunk) {
|
|
9
|
+
await Promise.resolve();
|
|
10
|
+
yield chunk;
|
|
11
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safety cap for a cell note produced by this package. `@d-zero/google-sheets`'s
|
|
3
|
+
* `Cell.provide()` — which every `sheet.appendRow()` call goes through — hard-slices
|
|
4
|
+
* any note longer than 5,000 characters (its `noteMaxLength` default) and appends
|
|
5
|
+
* its own `"\n\n...\nToo Large Text"` marker. There is currently no call path from
|
|
6
|
+
* this package (no `CellData`/`Sheet` option) that overrides that default, so any
|
|
7
|
+
* note built above ~5,000 characters is silently re-cut by that layer regardless of
|
|
8
|
+
* what this module does. Staying comfortably under that value ensures the
|
|
9
|
+
* informative suffix this module builds (`"... and N more"` / a truncation notice)
|
|
10
|
+
* is what actually reaches the spreadsheet, instead of being itself chopped
|
|
11
|
+
* mid-string by `Cell.provide()`'s blunter default cut.
|
|
12
|
+
*/
|
|
13
|
+
export declare const NOTE_MAX_LENGTH = 4800;
|
|
14
|
+
/**
|
|
15
|
+
* Joins URLs into a single newline-separated string, truncating at
|
|
16
|
+
* `maxLength` to stay within Google Sheets' note size cap. When truncated,
|
|
17
|
+
* appends a `... and N more` line where `N` counts every URL that did not
|
|
18
|
+
* fit, including the one whose insertion would have crossed the limit.
|
|
19
|
+
*
|
|
20
|
+
* Shared by every sheet that shows a referrer/redirect-source URL list in a
|
|
21
|
+
* cell note (Page List, Links, Resources) — previously duplicated per sheet
|
|
22
|
+
* as `joinReferrersForNote`.
|
|
23
|
+
* @param urls - The URLs to join, in display order. A `Set` (for
|
|
24
|
+
* already-deduplicated callers) or a plain array both work.
|
|
25
|
+
* @param maxLength - Optional override for the character cap (defaults to {@link NOTE_MAX_LENGTH}).
|
|
26
|
+
*/
|
|
27
|
+
export declare function joinUrlsForNote(urls: Iterable<string>, maxLength?: number): string;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safety cap for a cell note produced by this package. `@d-zero/google-sheets`'s
|
|
3
|
+
* `Cell.provide()` — which every `sheet.appendRow()` call goes through — hard-slices
|
|
4
|
+
* any note longer than 5,000 characters (its `noteMaxLength` default) and appends
|
|
5
|
+
* its own `"\n\n...\nToo Large Text"` marker. There is currently no call path from
|
|
6
|
+
* this package (no `CellData`/`Sheet` option) that overrides that default, so any
|
|
7
|
+
* note built above ~5,000 characters is silently re-cut by that layer regardless of
|
|
8
|
+
* what this module does. Staying comfortably under that value ensures the
|
|
9
|
+
* informative suffix this module builds (`"... and N more"` / a truncation notice)
|
|
10
|
+
* is what actually reaches the spreadsheet, instead of being itself chopped
|
|
11
|
+
* mid-string by `Cell.provide()`'s blunter default cut.
|
|
12
|
+
*/
|
|
13
|
+
export const NOTE_MAX_LENGTH = 4800;
|
|
14
|
+
/**
|
|
15
|
+
* Joins URLs into a single newline-separated string, truncating at
|
|
16
|
+
* `maxLength` to stay within Google Sheets' note size cap. When truncated,
|
|
17
|
+
* appends a `... and N more` line where `N` counts every URL that did not
|
|
18
|
+
* fit, including the one whose insertion would have crossed the limit.
|
|
19
|
+
*
|
|
20
|
+
* Shared by every sheet that shows a referrer/redirect-source URL list in a
|
|
21
|
+
* cell note (Page List, Links, Resources) — previously duplicated per sheet
|
|
22
|
+
* as `joinReferrersForNote`.
|
|
23
|
+
* @param urls - The URLs to join, in display order. A `Set` (for
|
|
24
|
+
* already-deduplicated callers) or a plain array both work.
|
|
25
|
+
* @param maxLength - Optional override for the character cap (defaults to {@link NOTE_MAX_LENGTH}).
|
|
26
|
+
*/
|
|
27
|
+
export function joinUrlsForNote(urls, maxLength = NOTE_MAX_LENGTH) {
|
|
28
|
+
const all = [...urls];
|
|
29
|
+
const total = all.length;
|
|
30
|
+
if (total === 0) {
|
|
31
|
+
return '';
|
|
32
|
+
}
|
|
33
|
+
const kept = [];
|
|
34
|
+
let used = 0;
|
|
35
|
+
let seen = 0;
|
|
36
|
+
for (const url of all) {
|
|
37
|
+
seen++;
|
|
38
|
+
const next = used + url.length + (kept.length > 0 ? 1 : 0);
|
|
39
|
+
if (next > maxLength) {
|
|
40
|
+
const remaining = total - seen + 1;
|
|
41
|
+
kept.push(`... and ${remaining} more`);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
kept.push(url);
|
|
45
|
+
used = next;
|
|
46
|
+
}
|
|
47
|
+
return kept.join('\n');
|
|
48
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncates an arbitrary, already-built note string to `maxLength`,
|
|
3
|
+
* appending a truncation marker when truncation actually happens. Sibling
|
|
4
|
+
* to {@link joinUrlsForNote} for note content that isn't a list of URLs (a
|
|
5
|
+
* page title, a JSON blob, a plugin-supplied free-text note) — both share
|
|
6
|
+
* the same {@link NOTE_MAX_LENGTH} ceiling so every note this package
|
|
7
|
+
* writes stays under `Cell.provide()`'s hard cut (see `NOTE_MAX_LENGTH`'s
|
|
8
|
+
* docs).
|
|
9
|
+
* @param text - The full note text.
|
|
10
|
+
* @param maxLength - Optional override (defaults to {@link NOTE_MAX_LENGTH}).
|
|
11
|
+
* @example
|
|
12
|
+
* truncateNoteText('a'.repeat(10_000), 100);
|
|
13
|
+
* // 'aaaa...aaaa\n\n...(truncated: showing first 100 of 10000 characters)'
|
|
14
|
+
*/
|
|
15
|
+
export declare function truncateNoteText(text: string, maxLength?: number): string;
|