@mdgs/pivotjet 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +628 -3
- package/bin/pivotjet.mjs +122 -0
- package/dist/core.cjs +4926 -0
- package/dist/core.cjs.map +7 -0
- package/dist/index.cjs +7277 -0
- package/dist/index.cjs.map +7 -0
- package/dist/pivotjet.min.js +334 -0
- package/dist/pivotjet.min.js.map +7 -0
- package/docs/images/cell-formats.png +0 -0
- package/docs/images/chart.png +0 -0
- package/docs/images/configure-fields.png +0 -0
- package/docs/images/designer.png +0 -0
- package/docs/images/large-dataset.png +0 -0
- package/package.json +108 -4
- package/src/cellfmt.js +218 -0
- package/src/chart.js +595 -0
- package/src/core.d.ts +424 -0
- package/src/core.js +18 -0
- package/src/csv.js +198 -0
- package/src/dataset.js +404 -0
- package/src/designer.js +995 -0
- package/src/elements.js +50 -0
- package/src/engine.js +778 -0
- package/src/errors.js +2 -0
- package/src/export.js +775 -0
- package/src/expr.js +180 -0
- package/src/format.js +42 -0
- package/src/holistic.js +237 -0
- package/src/index.d.ts +155 -0
- package/src/index.js +6 -0
- package/src/layouts.js +105 -0
- package/src/node.d.ts +10 -0
- package/src/node.js +29 -0
- package/src/pdffont.js +361 -0
- package/src/remote.js +164 -0
- package/src/report-render.js +31 -0
- package/src/report.js +103 -0
- package/src/reportstore.js +135 -0
- package/src/server.d.ts +20 -0
- package/src/server.js +272 -0
- package/src/stream.js +285 -0
- package/src/styles.js +162 -0
- package/src/util.js +73 -0
- package/src/viewer.js +632 -0
package/bin/pivotjet.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// pivotjet CLI
|
|
3
|
+
// pivotjet serve data.csv [--port 8080] [--stream] [--token T] [--layouts-dir ./layouts]
|
|
4
|
+
// pivotjet pivot data.csv --rows Region,Date@year --cols Channel --values sum:Revenue,count --out report.xlsx
|
|
5
|
+
import { writeFile, readFile } from 'node:fs/promises';
|
|
6
|
+
import { stat } from 'node:fs/promises';
|
|
7
|
+
import { loadCSVFile, csvFileStream, reportToHTMLFile } from '../src/node.js';
|
|
8
|
+
import { createReport, parseReport } from '../src/report.js';
|
|
9
|
+
import { createPivotServer } from '../src/server.js';
|
|
10
|
+
import { toXLSX, toPDF } from '../src/export.js';
|
|
11
|
+
|
|
12
|
+
const HELP = `PivotJet - pivot tables for very large data
|
|
13
|
+
|
|
14
|
+
Usage
|
|
15
|
+
pivotjet serve <file.csv> [options] serve a pivot designer + API for the file
|
|
16
|
+
pivotjet pivot <file.csv> [options] compute one pivot and write it to a file / stdout
|
|
17
|
+
pivotjet report <report.json> [--out f.html] render a saved report to a standalone page
|
|
18
|
+
|
|
19
|
+
Common options
|
|
20
|
+
--stream never hold the rows in memory (any file size; each pivot re-reads the file)
|
|
21
|
+
--delimiter <c> CSV delimiter (default ,)
|
|
22
|
+
--field Name=type:role override a column, e.g. --field Year=number:both --field Id=string:skip
|
|
23
|
+
type: string|number|date|boolean role: dimension|measure|both|skip
|
|
24
|
+
|
|
25
|
+
serve
|
|
26
|
+
--port <n> default 8080 --host <h> default 127.0.0.1
|
|
27
|
+
--name <n> dataset name (default: file name)
|
|
28
|
+
--token <t> require "Authorization: Bearer <t>" (also accepted as ?token=<t> on the page)
|
|
29
|
+
--layouts-dir <dir> persist saved layouts as JSON files (default: in memory)
|
|
30
|
+
--cors allow cross-origin browsers
|
|
31
|
+
--reports keep saved reports on the server (in memory)
|
|
32
|
+
--reports-dir <dir> persist saved reports (versioned) as JSON files
|
|
33
|
+
--pdf-font Name=file.ttf server-side PDF font; repeat; Name:bold=file.ttf / :italic= / :boldItalic= add styles
|
|
34
|
+
|
|
35
|
+
pivot
|
|
36
|
+
--rows A,B row dimensions; date buckets as Date@year|quarter|month|day
|
|
37
|
+
--cols C column dimensions
|
|
38
|
+
--values sum:Revenue,count,avg:Price aggregate:field (sum count avg min max distinct median)
|
|
39
|
+
--out <file> .csv (default: stdout) | .xlsx | .pdf | .json (report) | .html (standalone report page)
|
|
40
|
+
--title <t> report title (for .json / .html)
|
|
41
|
+
--rules <file.json> per-cell format rules (array) applied to .xlsx / .pdf / .json / .html output
|
|
42
|
+
|
|
43
|
+
report
|
|
44
|
+
pivotjet report <report.json> --out page.html turn a saved report into a standalone page (offline, no server)
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
function parseArgs(argv) {
|
|
48
|
+
const a = { _: [], field: [], 'pdf-font': [] };
|
|
49
|
+
for (let i = 0; i < argv.length; i++) {
|
|
50
|
+
const t = argv[i];
|
|
51
|
+
if (!t.startsWith('--')) { a._.push(t); continue; }
|
|
52
|
+
const k = t.slice(2);
|
|
53
|
+
if (['stream', 'cors', 'help', 'reports'].includes(k)) a[k] = true;
|
|
54
|
+
else if (k === 'field') a.field.push(argv[++i]);
|
|
55
|
+
else if (k === 'pdf-font') a['pdf-font'].push(argv[++i]);
|
|
56
|
+
else a[k] = argv[++i];
|
|
57
|
+
}
|
|
58
|
+
return a;
|
|
59
|
+
}
|
|
60
|
+
const dimSpec = (s) => (s || '').split(',').filter(Boolean).map((x) => { const [field, bucket] = x.split('@'); return bucket ? { field, bucket } : { field }; });
|
|
61
|
+
const valSpec = (s) => (s || 'count').split(',').filter(Boolean).map((x) => { const [agg, field] = x.includes(':') ? x.split(':') : [x, '*']; return { agg, field }; });
|
|
62
|
+
function fieldOverrides(list) {
|
|
63
|
+
return list.map((f) => { const [name, rest = ''] = f.split('='); const [type, role] = rest.split(':'); return { name, type: type || undefined, role: role || undefined }; });
|
|
64
|
+
}
|
|
65
|
+
const mb = (n) => (n / 1048576).toFixed(0) + ' MB';
|
|
66
|
+
|
|
67
|
+
async function openSource(file, args) {
|
|
68
|
+
const opts = { delimiter: args.delimiter, fields: fieldOverrides(args.field) };
|
|
69
|
+
if (args.stream) return csvFileStream(file, opts);
|
|
70
|
+
const size = (await stat(file)).size;
|
|
71
|
+
process.stderr.write(`Loading ${file} (${mb(size)}) into memory…\n`);
|
|
72
|
+
let last = 0;
|
|
73
|
+
const ds = await loadCSVFile(file, { ...opts, onProgress: (p) => { if (Date.now() - last > 500) { last = Date.now(); process.stderr.write(`\r ${(p.rows / 1e6).toFixed(1)} M rows (${Math.round((p.fraction || 0) * 100)}%) `); } } });
|
|
74
|
+
process.stderr.write(`\r ${ds.rowCount.toLocaleString()} rows loaded, ~${mb(ds.memoryUsage())} in memory\n`);
|
|
75
|
+
for (const w of ds.warnings) process.stderr.write(` warning: ${w}\n`);
|
|
76
|
+
return ds;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async function main() {
|
|
80
|
+
const args = parseArgs(process.argv.slice(2));
|
|
81
|
+
const [cmd, file] = args._;
|
|
82
|
+
if (!cmd || args.help || !['serve', 'pivot', 'report'].includes(cmd) || !file) { process.stdout.write(HELP); process.exit(cmd && !args.help ? 1 : 0); }
|
|
83
|
+
|
|
84
|
+
if (cmd === 'report') {
|
|
85
|
+
const report = parseReport(await readFile(file, 'utf8'));
|
|
86
|
+
const out = args.out || file.replace(/\.json$/i, '') + '.html';
|
|
87
|
+
await writeFile(out, await reportToHTMLFile(report));
|
|
88
|
+
process.stderr.write(`wrote ${out}\n`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const src = await openSource(file, args);
|
|
92
|
+
if (cmd === 'serve') {
|
|
93
|
+
const name = args.name || file.replace(/^.*[\\/]/, '').replace(/\.[^.]+$/, '');
|
|
94
|
+
const port = Number(args.port || 8080), host = args.host || '127.0.0.1';
|
|
95
|
+
const fonts = {};
|
|
96
|
+
for (const spec of args['pdf-font']) {
|
|
97
|
+
const m = /^([^=:]+)(?::(regular|bold|italic|boldItalic))?=(.+)$/.exec(spec || '');
|
|
98
|
+
if (!m) { process.stderr.write(`Error: --pdf-font expects Name=file.ttf or Name:bold=file.ttf, got "${spec}"
|
|
99
|
+
`); process.exit(1); }
|
|
100
|
+
(fonts[m[1]] ||= {})[m[2] || 'regular'] = m[3];
|
|
101
|
+
}
|
|
102
|
+
const reports = args['reports-dir'] ? { dir: args['reports-dir'] } : args.reports ? true : false;
|
|
103
|
+
const srv = createPivotServer({ sources: { [name]: src }, token: args.token, cors: !!args.cors, layouts: args['layouts-dir'] ? { dir: args['layouts-dir'] } : true, reports, fonts });
|
|
104
|
+
srv.listen(port, host, () => process.stderr.write(`PivotJet serving "${name}" at http://${host}:${port}/${args.token ? `?token=${args.token}` : ''}\n`));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const rules = args.rules ? JSON.parse(await readFile(args.rules, 'utf8')) : undefined;
|
|
108
|
+
const spec = { rows: dimSpec(args.rows), cols: dimSpec(args.cols), values: valSpec(args.values), ...(rules ? { options: { cellRules: rules } } : {}) };
|
|
109
|
+
const res = await src.compute(spec, {});
|
|
110
|
+
process.stderr.write(`${res.stats.matchedRows.toLocaleString()} of ${res.stats.rows.toLocaleString()} rows, ${res.stats.cells.toLocaleString()} cells, ${(res.stats.ms / 1000).toFixed(1)}s\n`);
|
|
111
|
+
const out = args.out || '';
|
|
112
|
+
if (/\.xlsx$/i.test(out)) await writeFile(out, toXLSX(res, { rules }));
|
|
113
|
+
else if (/\.pdf$/i.test(out)) await writeFile(out, toPDF(res, { locale: 'en-US', rules, title: args.title }));
|
|
114
|
+
else if (/\.(json|html)$/i.test(out)) {
|
|
115
|
+
const base = file.replace(/^.*[\\/]/, '');
|
|
116
|
+
const report = createReport({ title: args.title || base, spec, result: res, source: { name: base } });
|
|
117
|
+
await writeFile(out, /\.html$/i.test(out) ? await reportToHTMLFile(report) : JSON.stringify(report));
|
|
118
|
+
}
|
|
119
|
+
else if (out) await writeFile(out, res.toCSV());
|
|
120
|
+
else process.stdout.write(res.toCSV() + '\n');
|
|
121
|
+
}
|
|
122
|
+
main().catch((e) => { process.stderr.write(`Error: ${e.message}\n`); process.exit(1); });
|