@finopsbricks/tally-sync 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/.next/BUILD_ID +1 -0
- package/.next/app-build-manifest.json +66 -0
- package/.next/app-path-routes-manifest.json +7 -0
- package/.next/build-manifest.json +33 -0
- package/.next/export-marker.json +6 -0
- package/.next/images-manifest.json +58 -0
- package/.next/next-minimal-server.js.nft.json +1 -0
- package/.next/next-server.js.nft.json +1 -0
- package/.next/package.json +1 -0
- package/.next/prerender-manifest.json +11 -0
- package/.next/react-loadable-manifest.json +1 -0
- package/.next/required-server-files.json +322 -0
- package/.next/routes-manifest.json +80 -0
- package/.next/server/app/_not-found/page.js +2 -0
- package/.next/server/app/_not-found/page.js.nft.json +1 -0
- package/.next/server/app/_not-found/page_client-reference-manifest.js +1 -0
- package/.next/server/app/companies/[guid]/page.js +2 -0
- package/.next/server/app/companies/[guid]/page.js.nft.json +1 -0
- package/.next/server/app/companies/[guid]/page_client-reference-manifest.js +1 -0
- package/.next/server/app/companies/[guid]/tables/[table]/page.js +2 -0
- package/.next/server/app/companies/[guid]/tables/[table]/page.js.nft.json +1 -0
- package/.next/server/app/companies/[guid]/tables/[table]/page_client-reference-manifest.js +1 -0
- package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page.js +2 -0
- package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page.js.nft.json +1 -0
- package/.next/server/app/companies/[guid]/vouchers/[voucher_guid]/page_client-reference-manifest.js +1 -0
- package/.next/server/app/page.js +2 -0
- package/.next/server/app/page.js.nft.json +1 -0
- package/.next/server/app/page_client-reference-manifest.js +1 -0
- package/.next/server/app-paths-manifest.json +7 -0
- package/.next/server/chunks/230.js +32 -0
- package/.next/server/chunks/611.js +6 -0
- package/.next/server/chunks/706.js +1 -0
- package/.next/server/chunks/748.js +1 -0
- package/.next/server/functions-config-manifest.json +4 -0
- package/.next/server/interception-route-rewrite-manifest.js +1 -0
- package/.next/server/middleware-build-manifest.js +1 -0
- package/.next/server/middleware-manifest.json +6 -0
- package/.next/server/middleware-react-loadable-manifest.js +1 -0
- package/.next/server/next-font-manifest.js +1 -0
- package/.next/server/next-font-manifest.json +1 -0
- package/.next/server/pages/500.html +1 -0
- package/.next/server/pages/_app.js +1 -0
- package/.next/server/pages/_app.js.nft.json +1 -0
- package/.next/server/pages/_document.js +1 -0
- package/.next/server/pages/_document.js.nft.json +1 -0
- package/.next/server/pages/_error.js +19 -0
- package/.next/server/pages/_error.js.nft.json +1 -0
- package/.next/server/pages-manifest.json +5 -0
- package/.next/server/server-reference-manifest.js +1 -0
- package/.next/server/server-reference-manifest.json +1 -0
- package/.next/server/webpack-runtime.js +1 -0
- package/.next/static/V0rxN1KuyEM0zJoOJVDoX/_buildManifest.js +1 -0
- package/.next/static/V0rxN1KuyEM0zJoOJVDoX/_ssgManifest.js +1 -0
- package/.next/static/chunks/122-df645619bf29fc6e.js +1 -0
- package/.next/static/chunks/255-73938ce8a8eac1ad.js +1 -0
- package/.next/static/chunks/4bd1b696-c023c6e3521b1417.js +1 -0
- package/.next/static/chunks/510-5993a923674db2de.js +1 -0
- package/.next/static/chunks/658-dfe2290e75772c42.js +1 -0
- package/.next/static/chunks/788-e10fcb5f53c6428d.js +1 -0
- package/.next/static/chunks/9ad0e60a-ad37bddef782d7c2.js +1 -0
- package/.next/static/chunks/app/_not-found/page-f187ad254cc1b0eb.js +1 -0
- package/.next/static/chunks/app/companies/[guid]/page-042071b4790e1a86.js +1 -0
- package/.next/static/chunks/app/companies/[guid]/tables/[table]/page-a46008185adae0f8.js +1 -0
- package/.next/static/chunks/app/companies/[guid]/vouchers/[voucher_guid]/page-1b17cd59db180e1f.js +1 -0
- package/.next/static/chunks/app/layout-d46ad692723a97e0.js +1 -0
- package/.next/static/chunks/app/not-found-61f58c9ef91de921.js +1 -0
- package/.next/static/chunks/app/page-042071b4790e1a86.js +1 -0
- package/.next/static/chunks/framework-acd67e14855de5a2.js +1 -0
- package/.next/static/chunks/main-6f56cad8fb2c15f2.js +1 -0
- package/.next/static/chunks/main-app-f136a4b0e7f8f69b.js +1 -0
- package/.next/static/chunks/pages/_app-7d307437aca18ad4.js +1 -0
- package/.next/static/chunks/pages/_error-cb2a52f75f2162e2.js +1 -0
- package/.next/static/chunks/polyfills-42372ed130431b0a.js +1 -0
- package/.next/static/chunks/webpack-4354c1f6ffe64d9d.js +1 -0
- package/.next/static/css/a8e56a10001bee9f.css +1 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +201 -0
- package/NOTICE +40 -0
- package/README.md +109 -2
- package/bin/tally-sync.js +12 -0
- package/ecosystem.config.cjs +12 -0
- package/next.config.mjs +7 -0
- package/package.json +58 -4
- package/src/app/companies/[guid]/page.jsx +261 -0
- package/src/app/companies/[guid]/tables/[table]/page.jsx +116 -0
- package/src/app/companies/[guid]/vouchers/[voucher_guid]/page.jsx +156 -0
- package/src/app/globals.css +3 -0
- package/src/app/layout.jsx +29 -0
- package/src/app/not-found.jsx +22 -0
- package/src/app/page.jsx +172 -0
- package/src/cli/_helpers.js +71 -0
- package/src/cli/index.js +56 -0
- package/src/cli/paths.js +26 -0
- package/src/cli/probe.js +61 -0
- package/src/cli/sync.js +84 -0
- package/src/cli/web.js +32 -0
- package/src/components/AppShell.jsx +17 -0
- package/src/components/AppSidebar.jsx +81 -0
- package/src/components/MainContainer.jsx +12 -0
- package/src/components/RowsTable.jsx +111 -0
- package/src/components/Section.jsx +16 -0
- package/src/mirror/agentDb.js +131 -0
- package/src/mirror/companyDb.js +54 -0
- package/src/mirror/schema.js +49 -0
- package/src/mirror/sqlite.js +41 -0
- package/src/probe/probeFile.js +54 -0
- package/src/probe/runProbe.js +118 -0
- package/src/sync/checkPull.js +61 -0
- package/src/sync/pullCompany.js +54 -0
- package/src/sync/runSyncPass.js +50 -0
- package/src/sync/syncCompany.js +75 -0
- package/src/sync/tieOut.js +93 -0
- package/src/tally/dates.js +23 -0
- package/src/tally/definitions.js +22 -0
- package/src/tally/definitions.yaml +370 -0
- package/src/tally/gateway.js +77 -0
- package/src/tally/http.js +92 -0
- package/src/tally/requests.js +133 -0
- package/src/tally/tallyErrors.js +25 -0
- package/src/tally/xml.js +329 -0
- package/src/utils/config.js +46 -0
- package/src/utils/debugLog.js +23 -0
- package/src/web/format.js +160 -0
- package/src/web/mirror.js +132 -0
- package/src/web/status.js +80 -0
package/src/app/page.jsx
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import Link from 'next/link';
|
|
2
|
+
import AlertBox from '@fob/lib-ui/components/AlertBox';
|
|
3
|
+
import AppBreadcrumbs from '@fob/lib-ui/components/AppBreadcrumbs';
|
|
4
|
+
import PageHeader from '@fob/lib-ui/components/PageHeader';
|
|
5
|
+
import { Badge } from '@fob/lib-ui/primitives/badge';
|
|
6
|
+
import MainContainer from '../components/MainContainer.jsx';
|
|
7
|
+
import RowsTable from '../components/RowsTable.jsx';
|
|
8
|
+
import { readAgentStatus } from '../web/status.js';
|
|
9
|
+
import { readCompanyMirror } from '../web/mirror.js';
|
|
10
|
+
import { formatAgo, formatDateTime, runDetail, runStatus, tieOutVerdict } from '../web/format.js';
|
|
11
|
+
|
|
12
|
+
export const dynamic = 'force-dynamic';
|
|
13
|
+
|
|
14
|
+
const COUNT = new Intl.NumberFormat('en-IN');
|
|
15
|
+
|
|
16
|
+
const COLUMNS = [
|
|
17
|
+
{ key: 'company', label: 'Company' },
|
|
18
|
+
{ key: 'sync', label: 'Last sync' },
|
|
19
|
+
{ key: 'tie_out', label: 'Tie-out', nowrap: true },
|
|
20
|
+
{ key: 'vouchers', label: 'Vouchers', align: 'right' },
|
|
21
|
+
{ key: 'ledgers', label: 'Ledgers', align: 'right' },
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Companies: is Tally there, then each company: loaded or not, its last sync, whether it
|
|
26
|
+
* ties out to Tally, and how much it holds.
|
|
27
|
+
*/
|
|
28
|
+
export default function CompaniesPage() {
|
|
29
|
+
const data_dir = process.env.TALLY_SYNC_DATA_DIR ?? '';
|
|
30
|
+
const status = readAgentStatus(data_dir);
|
|
31
|
+
const companies = status.companies.map((company) => {
|
|
32
|
+
const mirror = readCompanyMirror(data_dir, company.guid);
|
|
33
|
+
const rows_of = (/** @type {string} */ table) => mirror?.tables.find((t) => t.name === table)?.rows ?? null;
|
|
34
|
+
return { ...company, tie_out: mirror?.tie_out ?? null, vouchers: rows_of('trn_voucher'), ledgers: rows_of('mst_ledger') };
|
|
35
|
+
});
|
|
36
|
+
const failed = companies.filter((c) => c.last_run?.status === 'failed');
|
|
37
|
+
|
|
38
|
+
const rows = companies.map((c) => {
|
|
39
|
+
const run = runStatus(c.last_run?.status);
|
|
40
|
+
const verdict = tieOutVerdict(c.tie_out);
|
|
41
|
+
const when = formatAgo(c.last_run?.finished_at ?? c.last_run?.started_at);
|
|
42
|
+
const href = `/companies/${encodeURIComponent(c.guid)}`;
|
|
43
|
+
const in_tally = c.loaded ? 'Loaded' : 'Not loaded';
|
|
44
|
+
const vouchers = c.vouchers === null ? '' : COUNT.format(c.vouchers);
|
|
45
|
+
const ledgers = c.ledgers === null ? '' : COUNT.format(c.ledgers);
|
|
46
|
+
return {
|
|
47
|
+
key: c.guid,
|
|
48
|
+
cells: {
|
|
49
|
+
company: (
|
|
50
|
+
<span className="flex min-w-40 flex-col">
|
|
51
|
+
<Link href={href} className="font-medium text-primary hover:underline">
|
|
52
|
+
{c.name}
|
|
53
|
+
</Link>
|
|
54
|
+
<span className="text-muted-foreground">{in_tally} in Tally</span>
|
|
55
|
+
</span>
|
|
56
|
+
),
|
|
57
|
+
sync: (
|
|
58
|
+
<span className="flex flex-wrap items-center gap-x-2 gap-y-1">
|
|
59
|
+
<Badge variant={run.variant}>{run.label}</Badge>
|
|
60
|
+
{when && <span className="whitespace-nowrap text-muted-foreground">{when}</span>}
|
|
61
|
+
</span>
|
|
62
|
+
),
|
|
63
|
+
tie_out: verdict ? <Badge variant={verdict.variant}>{verdict.label}</Badge> : <span className="text-muted-foreground">Not tied out yet</span>,
|
|
64
|
+
vouchers,
|
|
65
|
+
ledgers,
|
|
66
|
+
},
|
|
67
|
+
card: {
|
|
68
|
+
title: c.name,
|
|
69
|
+
value: <Badge variant={run.variant}>{run.label}</Badge>,
|
|
70
|
+
href,
|
|
71
|
+
meta: [
|
|
72
|
+
`${in_tally} in Tally`,
|
|
73
|
+
when && `Last sync ${when}`,
|
|
74
|
+
verdict?.label,
|
|
75
|
+
vouchers && `${vouchers} vouchers`,
|
|
76
|
+
ledgers && `${ledgers} ledgers`,
|
|
77
|
+
],
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<>
|
|
84
|
+
<AppBreadcrumbs breadcrumbs={[{ text: 'Companies' }]} />
|
|
85
|
+
<MainContainer>
|
|
86
|
+
<PageHeader header="Companies" />
|
|
87
|
+
<TallyStatus status={status} />
|
|
88
|
+
{status.tally_reachable && failed.length > 0 && <FailedRuns companies={failed} />}
|
|
89
|
+
{companies.length === 0 ? (
|
|
90
|
+
status.last_pass_at !== null && (
|
|
91
|
+
<AlertBox color="info" title="No companies yet" message="Open a company in Tally. The next sync mirrors every company loaded in Tally." />
|
|
92
|
+
)
|
|
93
|
+
) : (
|
|
94
|
+
<RowsTable columns={COLUMNS} rows={rows} label="Companies" cards />
|
|
95
|
+
)}
|
|
96
|
+
</MainContainer>
|
|
97
|
+
</>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The first layer of health: has the sync run, and did Tally answer it.
|
|
103
|
+
* @param {{status: import('../web/status.js').AgentStatus}} props
|
|
104
|
+
*/
|
|
105
|
+
function TallyStatus({ status }) {
|
|
106
|
+
if (status.last_pass_at === null) {
|
|
107
|
+
return (
|
|
108
|
+
<AlertBox
|
|
109
|
+
color="info"
|
|
110
|
+
title="No sync has run yet"
|
|
111
|
+
message="Start the sync (tally-sync sync, or with pm2). Each company loaded in Tally shows here after its first pass."
|
|
112
|
+
/>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (!status.tally_reachable) {
|
|
116
|
+
return (
|
|
117
|
+
<AlertBox
|
|
118
|
+
color="danger"
|
|
119
|
+
title="Can't reach Tally"
|
|
120
|
+
message={
|
|
121
|
+
<div className="flex flex-col gap-1">
|
|
122
|
+
<p>{status.error ?? 'Tally did not answer.'}</p>
|
|
123
|
+
<p>
|
|
124
|
+
Last tried {formatAgo(status.last_pass_at)}. The companies below show what was last synced.
|
|
125
|
+
</p>
|
|
126
|
+
</div>
|
|
127
|
+
}
|
|
128
|
+
/>
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
const loaded = status.companies.filter((c) => c.loaded).length;
|
|
132
|
+
return (
|
|
133
|
+
<div className="flex flex-wrap items-center gap-x-2 gap-y-1 text-sm text-muted-foreground">
|
|
134
|
+
<Badge variant="success">Tally running</Badge>
|
|
135
|
+
<span>
|
|
136
|
+
Checked <time dateTime={status.last_pass_at} title={formatDateTime(status.last_pass_at)}>{formatAgo(status.last_pass_at)}</time>
|
|
137
|
+
{' · '}
|
|
138
|
+
{loaded} {loaded === 1 ? 'company' : 'companies'} loaded
|
|
139
|
+
</span>
|
|
140
|
+
</div>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Companies whose latest sync failed, with what went wrong.
|
|
146
|
+
* @param {{companies: {guid: string, name: string, last_run: Record<string, any>|null}[]}} props
|
|
147
|
+
*/
|
|
148
|
+
function FailedRuns({ companies }) {
|
|
149
|
+
return (
|
|
150
|
+
<AlertBox
|
|
151
|
+
color="danger"
|
|
152
|
+
title={companies.length === 1 ? `The last sync of ${companies[0].name} failed` : `The last sync of ${companies.length} companies failed`}
|
|
153
|
+
message={
|
|
154
|
+
<div className="flex flex-col gap-1">
|
|
155
|
+
<ul className="list-inside list-disc">
|
|
156
|
+
{companies.map((c) => {
|
|
157
|
+
const { problems } = runDetail(c.last_run);
|
|
158
|
+
const reason = problems.length > 0 ? problems.join('; ') : c.last_run?.error;
|
|
159
|
+
return (
|
|
160
|
+
<li key={c.guid}>
|
|
161
|
+
{companies.length > 1 && `${c.name}: `}
|
|
162
|
+
{reason}
|
|
163
|
+
</li>
|
|
164
|
+
);
|
|
165
|
+
})}
|
|
166
|
+
</ul>
|
|
167
|
+
<p>The mirror keeps the last sync that worked.</p>
|
|
168
|
+
</div>
|
|
169
|
+
}
|
|
170
|
+
/>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// Shared pieces of the CLI shell.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { loadConfig, resolveConfigDir } from '../utils/config.js';
|
|
9
|
+
import { openDebugLog } from '../utils/debugLog.js';
|
|
10
|
+
import { createGateway } from '../tally/gateway.js';
|
|
11
|
+
|
|
12
|
+
/** The package's own directory: where the built web app and the pm2 file live. */
|
|
13
|
+
export const PACKAGE_DIR = fileURLToPath(new URL('../../', import.meta.url));
|
|
14
|
+
|
|
15
|
+
/** The package version, from package.json. */
|
|
16
|
+
export const VERSION = JSON.parse(fs.readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Wraps a handler so a thrown error prints `Error: <message>` and exits 1.
|
|
20
|
+
* @param {(argv: any) => Promise<void>} handler
|
|
21
|
+
*/
|
|
22
|
+
export function safe(handler) {
|
|
23
|
+
return async (argv) => {
|
|
24
|
+
try {
|
|
25
|
+
await handler(argv);
|
|
26
|
+
} catch (err) {
|
|
27
|
+
console.error(`Error: ${err.message}`);
|
|
28
|
+
if (argv.debug) console.error(err.stack);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Where Tally is: --url (or tally.url) when given, else --host/--port over the config.
|
|
36
|
+
* @param {{url?: string, host?: string, port?: number}} argv
|
|
37
|
+
* @param {import('../utils/config.js').Config} config
|
|
38
|
+
* @returns {import('../tally/http.js').TallyConnection}
|
|
39
|
+
*/
|
|
40
|
+
export function resolveConnection(argv, config) {
|
|
41
|
+
const timeout_ms = config.tally.timeout_seconds * 1000;
|
|
42
|
+
const url = argv.url ?? (argv.host || argv.port ? undefined : config.tally.url);
|
|
43
|
+
if (url) {
|
|
44
|
+
const parsed = new URL(url);
|
|
45
|
+
const protocol = parsed.protocol === 'https:' ? 'https' : 'http';
|
|
46
|
+
return { protocol, host: parsed.hostname, port: Number(parsed.port) || (protocol === 'https' ? 443 : 80), timeout_ms };
|
|
47
|
+
}
|
|
48
|
+
return { protocol: 'http', host: argv.host ?? config.tally.host, port: argv.port ?? config.tally.port, timeout_ms };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Everything a command needs, resolved once at the edge: config dir, config, and a gateway
|
|
53
|
+
* that reports each exchange to the given collector and, with --debug, to the debug log.
|
|
54
|
+
* @param {{'config-dir'?: string, host?: string, port?: number, debug?: boolean}} argv
|
|
55
|
+
* @param {(exchange: import('../tally/gateway.js').Exchange) => void} [collect]
|
|
56
|
+
*/
|
|
57
|
+
export function buildContext(argv, collect = () => {}) {
|
|
58
|
+
const config_dir = argv['config-dir'] ?? resolveConfigDir({ env: process.env, homedir: os.homedir() });
|
|
59
|
+
const config = loadConfig(config_dir);
|
|
60
|
+
const connection = resolveConnection(argv, config);
|
|
61
|
+
const debug_log = argv.debug ? openDebugLog(path.join(config_dir, 'logs'), new Date()) : null;
|
|
62
|
+
const gateway = createGateway({
|
|
63
|
+
connection,
|
|
64
|
+
onExchange(exchange) {
|
|
65
|
+
collect(exchange);
|
|
66
|
+
debug_log?.write({ type: 'tally_exchange', ...exchange });
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
const data_dir = path.join(config_dir, 'data');
|
|
70
|
+
return { config_dir, data_dir, config, connection, gateway, debug_log };
|
|
71
|
+
}
|
package/src/cli/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The yargs root: global options and the command tree.
|
|
3
|
+
|
|
4
|
+
import yargs from 'yargs';
|
|
5
|
+
import { probeHandler } from './probe.js';
|
|
6
|
+
import { syncHandler } from './sync.js';
|
|
7
|
+
import { webHandler } from './web.js';
|
|
8
|
+
import { pathsHandler } from './paths.js';
|
|
9
|
+
import { safe, VERSION } from './_helpers.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string[]} args - process.argv without node and the script
|
|
13
|
+
*/
|
|
14
|
+
export function run(args) {
|
|
15
|
+
return yargs(args)
|
|
16
|
+
.scriptName('tally-sync')
|
|
17
|
+
.usage('$0 <command> [options]')
|
|
18
|
+
.option('config-dir', { describe: 'Config, data and logs directory (default ~/.fob/tally-sync)', type: 'string', global: true })
|
|
19
|
+
.option('host', { describe: "Tally's host (default from config, else localhost)", type: 'string', global: true })
|
|
20
|
+
.option('port', { describe: "Tally's port (default from config, else 9000)", type: 'number', global: true })
|
|
21
|
+
.option('url', { describe: 'Reach Tally at this URL instead of host and port (e.g. an HTTPS tunnel)', type: 'string', global: true })
|
|
22
|
+
.option('debug', { describe: 'Log every exchange with Tally to <config dir>/logs', type: 'boolean', global: true })
|
|
23
|
+
.group(['config-dir', 'host', 'port', 'url', 'debug', 'help', 'version'], 'Global Options:')
|
|
24
|
+
.command(
|
|
25
|
+
'probe',
|
|
26
|
+
'Ask Tally a fixed set of read-only questions and save the answers to one file',
|
|
27
|
+
(y) =>
|
|
28
|
+
y
|
|
29
|
+
.option('company', { describe: 'Probe only this company (exact name as in Tally)', type: 'string' })
|
|
30
|
+
.option('out', { describe: 'Directory for the probe file (default <config dir>/probes)', type: 'string' }),
|
|
31
|
+
safe(probeHandler),
|
|
32
|
+
)
|
|
33
|
+
.command(
|
|
34
|
+
'sync',
|
|
35
|
+
'Mirror every loaded company into SQLite, every few minutes (or once)',
|
|
36
|
+
(y) =>
|
|
37
|
+
y
|
|
38
|
+
.option('once', { describe: 'Run one pass and exit', type: 'boolean' })
|
|
39
|
+
.option('company', { describe: 'Sync only this company (exact name as in Tally)', type: 'string' })
|
|
40
|
+
.option('force', { describe: 'Sync even when Tally reports no change', type: 'boolean' }),
|
|
41
|
+
safe(syncHandler),
|
|
42
|
+
)
|
|
43
|
+
.command(
|
|
44
|
+
'web',
|
|
45
|
+
'Serve the local UI on 127.0.0.1 (reads the mirror, never calls Tally)',
|
|
46
|
+
(y) => y.option('web-port', { describe: "The UI's port (default from config, else 7780)", type: 'number' }),
|
|
47
|
+
safe(webHandler),
|
|
48
|
+
)
|
|
49
|
+
.command('paths', 'Show where tally-sync keeps its config, data and logs, and its pm2 file', (y) => y.option('json', { describe: 'Output JSON', type: 'boolean' }), safe(pathsHandler))
|
|
50
|
+
.demandCommand(1, 'Specify a command: probe, sync, web, paths')
|
|
51
|
+
.strict()
|
|
52
|
+
.help()
|
|
53
|
+
.alias('h', 'help')
|
|
54
|
+
.version(VERSION)
|
|
55
|
+
.parse();
|
|
56
|
+
}
|
package/src/cli/paths.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `tally-sync paths`: where tally-sync keeps things, and the pm2 file to start it with.
|
|
3
|
+
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { buildContext, PACKAGE_DIR } from './_helpers.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {{'config-dir'?: string, json?: boolean}} argv
|
|
9
|
+
*/
|
|
10
|
+
export async function pathsHandler(argv) {
|
|
11
|
+
const { config_dir, data_dir } = buildContext(argv);
|
|
12
|
+
const paths = {
|
|
13
|
+
config_dir,
|
|
14
|
+
config_file: path.join(config_dir, 'config.yml'),
|
|
15
|
+
data_dir,
|
|
16
|
+
logs_dir: path.join(config_dir, 'logs'),
|
|
17
|
+
probes_dir: path.join(config_dir, 'probes'),
|
|
18
|
+
pm2_file: path.join(PACKAGE_DIR, 'ecosystem.config.cjs'),
|
|
19
|
+
};
|
|
20
|
+
if (argv.json) {
|
|
21
|
+
console.log(JSON.stringify(paths, null, 2));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
for (const [name, value] of Object.entries(paths)) console.log(`${name.padEnd(12)} ${value}`);
|
|
25
|
+
console.log(`\nStart with pm2: pm2 start "${paths.pm2_file}"`);
|
|
26
|
+
}
|
package/src/cli/probe.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `tally-sync probe`: run the probe and write the probe file.
|
|
3
|
+
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { runProbe } from '../probe/runProbe.js';
|
|
7
|
+
import { buildProbeFile, writeProbeFile } from '../probe/probeFile.js';
|
|
8
|
+
import { loadDefinitions } from '../tally/definitions.js';
|
|
9
|
+
import { buildContext, VERSION } from './_helpers.js';
|
|
10
|
+
|
|
11
|
+
/** @param {string[]} names */
|
|
12
|
+
function describeCompanies(names) {
|
|
13
|
+
if (names.length === 0) return ': no company is loaded in Tally. Load one and probe again';
|
|
14
|
+
return `: ${names.length} loaded (${names.join(', ')})`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* One line per probe step, for the terminal.
|
|
19
|
+
* @param {import('../probe/runProbe.js').ProbeStep[]} steps
|
|
20
|
+
* @returns {string[]}
|
|
21
|
+
*/
|
|
22
|
+
export function formatProbeSteps(steps) {
|
|
23
|
+
return steps.map((s) => {
|
|
24
|
+
const where = s.company ? `[${s.company}] ` : '';
|
|
25
|
+
const seconds = `${(s.ms / 1000).toFixed(1)}s`;
|
|
26
|
+
if (!s.ok) return ` FAIL ${where}${s.name} (${seconds}): ${s.error?.message}`;
|
|
27
|
+
const count = s.result?.rows;
|
|
28
|
+
const rows = typeof count === 'number' ? `, ${count} ${count === 1 ? 'row' : 'rows'}` : '';
|
|
29
|
+
const companies = Array.isArray(s.result?.companies) ? describeCompanies(s.result.companies) : '';
|
|
30
|
+
const not_loaded = s.result?.loaded === false ? ': not loaded in Tally' : '';
|
|
31
|
+
return ` ok ${where}${s.name} (${seconds}${rows})${companies}${not_loaded}`;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {{'config-dir'?: string, host?: string, port?: number, debug?: boolean, company?: string, out?: string}} argv
|
|
37
|
+
*/
|
|
38
|
+
export async function probeHandler(argv) {
|
|
39
|
+
/** @type {import('../tally/gateway.js').Exchange[]} */
|
|
40
|
+
const exchanges = [];
|
|
41
|
+
const { config_dir, connection, gateway, debug_log } = buildContext(argv, (exchange) => exchanges.push(exchange));
|
|
42
|
+
|
|
43
|
+
console.error(`Probing Tally at ${connection.host}:${connection.port} (read-only)…`);
|
|
44
|
+
const steps = await runProbe({ gateway, definitions: loadDefinitions(), company: argv.company });
|
|
45
|
+
|
|
46
|
+
const probe_file = buildProbeFile({
|
|
47
|
+
version: VERSION,
|
|
48
|
+
now: new Date(),
|
|
49
|
+
system: { node: process.version, platform: os.platform(), arch: os.arch() },
|
|
50
|
+
tally: { host: connection.host, port: connection.port },
|
|
51
|
+
steps,
|
|
52
|
+
exchanges,
|
|
53
|
+
});
|
|
54
|
+
const file = writeProbeFile(argv.out ?? path.join(config_dir, 'probes'), probe_file);
|
|
55
|
+
|
|
56
|
+
console.log(formatProbeSteps(steps).join('\n'));
|
|
57
|
+
console.log(`\n${probe_file.summary.steps} steps, ${probe_file.summary.failed} failed.`);
|
|
58
|
+
console.log(`Probe file: ${file}`);
|
|
59
|
+
if (debug_log) console.log(`Debug log: ${debug_log.file}`);
|
|
60
|
+
console.log('The file contains data from your Tally books. Read it before sharing it.');
|
|
61
|
+
}
|
package/src/cli/sync.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `tally-sync sync`: mirror every loaded company into SQLite, once or on an interval.
|
|
3
|
+
|
|
4
|
+
import { runSyncPass } from '../sync/runSyncPass.js';
|
|
5
|
+
import { loadDefinitions } from '../tally/definitions.js';
|
|
6
|
+
import { markAbandonedRuns, openAgentDb } from '../mirror/agentDb.js';
|
|
7
|
+
import { buildContext } from './_helpers.js';
|
|
8
|
+
|
|
9
|
+
const rupees = (paise) => (paise / 100).toLocaleString('en-IN', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Lines describing one pass, for the terminal.
|
|
13
|
+
* @param {import('../sync/runSyncPass.js').PassResult} pass
|
|
14
|
+
* @returns {string[]}
|
|
15
|
+
*/
|
|
16
|
+
export function formatPass(pass) {
|
|
17
|
+
if (!pass.tally_reachable) return [`Tally not reachable: ${pass.error}`];
|
|
18
|
+
if (pass.loaded_companies.length === 0) return ['Tally is running but no company is loaded.'];
|
|
19
|
+
return pass.results.flatMap((result) => {
|
|
20
|
+
if (result.status === 'not_loaded') return [` ${result.company}: not loaded in Tally`];
|
|
21
|
+
if (result.status === 'unchanged') return [` ${result.company}: no change since the last sync`];
|
|
22
|
+
if (result.status === 'failed') {
|
|
23
|
+
const reasons = result.problems?.length ? result.problems.map((p) => ` - ${p.message}`) : [` - ${result.error}`];
|
|
24
|
+
return [` ${result.company}: sync failed (run ${result.run_id}); the mirror was not replaced`, ...reasons];
|
|
25
|
+
}
|
|
26
|
+
const vouchers = result.counts?.trn_voucher ?? 0;
|
|
27
|
+
const ledgers = result.counts?.mst_ledger ?? 0;
|
|
28
|
+
const tie = result.tie_out;
|
|
29
|
+
const verdict = tie?.matched ? `all ${tie.ledgers} ledgers match Tally` : `${tie?.differences.length} of ${tie?.ledgers} ledgers differ from Tally`;
|
|
30
|
+
const lines = [` ${result.company}: synced ${vouchers} vouchers, ${ledgers} ledgers (run ${result.run_id}); ${verdict}`];
|
|
31
|
+
for (const d of tie?.differences.slice(0, 5) ?? []) lines.push(` - ${d.ledger}: ours ${rupees(d.ours_paise)}, Tally ${rupees(d.tally_paise)}`);
|
|
32
|
+
if (tie?.profit_and_loss && !tie.profit_and_loss.matched) {
|
|
33
|
+
lines.push(` - Profit & Loss A/c: ours ${rupees(tie.profit_and_loss.ours_paise)}, Tally ${rupees(tie.profit_and_loss.tally_paise)} (Tally's figure includes the stock change, which isn't synced)`);
|
|
34
|
+
}
|
|
35
|
+
return lines;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {{'config-dir'?: string, host?: string, port?: number, debug?: boolean, once?: boolean, company?: string, force?: boolean}} argv
|
|
41
|
+
*/
|
|
42
|
+
export async function syncHandler(argv) {
|
|
43
|
+
const { data_dir, config, connection, gateway, debug_log } = buildContext(argv);
|
|
44
|
+
const agent_db = openAgentDb(data_dir);
|
|
45
|
+
const now = () => new Date().toISOString();
|
|
46
|
+
const abandoned = markAbandonedRuns(agent_db, now());
|
|
47
|
+
if (abandoned > 0) console.error(`Marked ${abandoned} unfinished sync run(s) from before as abandoned.`);
|
|
48
|
+
|
|
49
|
+
const definitions = loadDefinitions();
|
|
50
|
+
const interval_ms = config.sync.interval_minutes * 60 * 1000;
|
|
51
|
+
let stopping = false;
|
|
52
|
+
/** @type {(() => void)|null} */
|
|
53
|
+
let wake = null;
|
|
54
|
+
const stop = () => {
|
|
55
|
+
stopping = true;
|
|
56
|
+
wake?.();
|
|
57
|
+
};
|
|
58
|
+
process.once('SIGINT', stop);
|
|
59
|
+
process.once('SIGTERM', stop);
|
|
60
|
+
|
|
61
|
+
console.error(`Syncing from Tally at ${connection.host}:${connection.port} into ${data_dir}`);
|
|
62
|
+
if (debug_log) console.error(`Debug log: ${debug_log.file}`);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
do {
|
|
66
|
+
const started = now();
|
|
67
|
+
const pass = await runSyncPass({ gateway, definitions, agent_db, data_dir, now, company: argv.company, force: argv.force, batch_size: config.sync.batch_size });
|
|
68
|
+
debug_log?.write({ type: 'sync_pass', started_at: started, ...pass });
|
|
69
|
+
console.log(`[${started}] ${formatPass(pass).join('\n')}`);
|
|
70
|
+
if (argv.once || stopping) break;
|
|
71
|
+
await new Promise((resolve) => {
|
|
72
|
+
const timer = setTimeout(resolve, interval_ms);
|
|
73
|
+
wake = () => {
|
|
74
|
+
clearTimeout(timer);
|
|
75
|
+
resolve(undefined);
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
} while (!stopping);
|
|
79
|
+
} finally {
|
|
80
|
+
agent_db.close();
|
|
81
|
+
process.removeListener('SIGINT', stop);
|
|
82
|
+
process.removeListener('SIGTERM', stop);
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/cli/web.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// `tally-sync web`: serve the local UI on 127.0.0.1. It only reads the mirror.
|
|
3
|
+
|
|
4
|
+
import http from 'node:http';
|
|
5
|
+
import next from 'next';
|
|
6
|
+
import { buildContext, PACKAGE_DIR } from './_helpers.js';
|
|
7
|
+
|
|
8
|
+
/** Where the UI listens: this PC only. Tally's own port has no auth; this one isn't opened to the network either. */
|
|
9
|
+
export const WEB_HOST = '127.0.0.1';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {{'config-dir'?: string, port?: number}} argv - --port here is the UI's port
|
|
13
|
+
*/
|
|
14
|
+
export async function webHandler(argv) {
|
|
15
|
+
const { data_dir, config } = buildContext({ ...argv, port: undefined });
|
|
16
|
+
const port = argv['web-port'] ?? config.web.port;
|
|
17
|
+
|
|
18
|
+
// The Next.js app reads these when it renders; set before it starts.
|
|
19
|
+
process.env.TALLY_SYNC_DATA_DIR = data_dir;
|
|
20
|
+
process.env.NEXT_TELEMETRY_DISABLED = '1';
|
|
21
|
+
|
|
22
|
+
const app = next({ dev: false, dir: PACKAGE_DIR, hostname: WEB_HOST, port });
|
|
23
|
+
await app.prepare();
|
|
24
|
+
const handle = app.getRequestHandler();
|
|
25
|
+
const server = http.createServer((req, res) => handle(req, res));
|
|
26
|
+
await new Promise((resolve) => server.listen(port, WEB_HOST, () => resolve(undefined)));
|
|
27
|
+
console.log(`Tally Sync UI: http://${WEB_HOST}:${port} (reading ${data_dir})`);
|
|
28
|
+
|
|
29
|
+
const stop = () => server.close(() => process.exit(0));
|
|
30
|
+
process.once('SIGINT', stop);
|
|
31
|
+
process.once('SIGTERM', stop);
|
|
32
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
// @ts-check
|
|
3
|
+
|
|
4
|
+
import { AppLayout } from '@fob/lib-ui';
|
|
5
|
+
import AppSidebar from './AppSidebar.jsx';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* lib-ui's app shell with this app's sidebar.
|
|
9
|
+
* @param {{companies: {guid: string, name: string}[], version: string, children: React.ReactNode}} props
|
|
10
|
+
*/
|
|
11
|
+
export default function AppShell({ companies, version, children }) {
|
|
12
|
+
return (
|
|
13
|
+
<AppLayout sidebar={AppSidebar} sidebarProps={{ companies, version }}>
|
|
14
|
+
{children}
|
|
15
|
+
</AppLayout>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
// @ts-check
|
|
3
|
+
|
|
4
|
+
import Link from 'next/link';
|
|
5
|
+
import { usePathname } from 'next/navigation';
|
|
6
|
+
import { Building2, LayoutList, RefreshCw } from 'lucide-react';
|
|
7
|
+
import {
|
|
8
|
+
Sidebar,
|
|
9
|
+
SidebarContent,
|
|
10
|
+
SidebarFooter,
|
|
11
|
+
SidebarGroup,
|
|
12
|
+
SidebarGroupContent,
|
|
13
|
+
SidebarGroupLabel,
|
|
14
|
+
SidebarHeader,
|
|
15
|
+
SidebarMenu,
|
|
16
|
+
SidebarMenuButton,
|
|
17
|
+
SidebarMenuItem,
|
|
18
|
+
SidebarRail,
|
|
19
|
+
useSidebar,
|
|
20
|
+
} from '@fob/lib-ui/primitives/sidebar';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The app's navigation: every company the agent has seen, under "All companies". No org
|
|
24
|
+
* switcher and no user: the UI runs on one PC with no sign-in.
|
|
25
|
+
* @param {{companies: {guid: string, name: string}[], version: string}} props
|
|
26
|
+
*/
|
|
27
|
+
export default function AppSidebar({ companies, version }) {
|
|
28
|
+
const pathname = usePathname();
|
|
29
|
+
const { state, isMobile, setOpenMobile } = useSidebar();
|
|
30
|
+
const is_collapsed = state === 'collapsed' && !isMobile;
|
|
31
|
+
const closeSheet = () => {
|
|
32
|
+
if (isMobile) setOpenMobile(false);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const items = [
|
|
36
|
+
{ title: 'All companies', url: '/', icon: LayoutList, active: pathname === '/' },
|
|
37
|
+
...companies.map((c) => ({
|
|
38
|
+
title: c.name,
|
|
39
|
+
url: `/companies/${encodeURIComponent(c.guid)}`,
|
|
40
|
+
icon: Building2,
|
|
41
|
+
active: pathname === `/companies/${encodeURIComponent(c.guid)}` || pathname.startsWith(`/companies/${encodeURIComponent(c.guid)}/`),
|
|
42
|
+
})),
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<Sidebar collapsible="icon">
|
|
47
|
+
<SidebarHeader>
|
|
48
|
+
<Link href="/" onClick={closeSheet} className="flex h-8 items-center gap-2 px-2 font-semibold">
|
|
49
|
+
<RefreshCw className="size-4 shrink-0" aria-hidden="true" />
|
|
50
|
+
{!is_collapsed && <span className="truncate">Tally Sync</span>}
|
|
51
|
+
{is_collapsed && <span className="sr-only">Tally Sync</span>}
|
|
52
|
+
</Link>
|
|
53
|
+
</SidebarHeader>
|
|
54
|
+
<SidebarContent>
|
|
55
|
+
<nav aria-label="Main">
|
|
56
|
+
<SidebarGroup>
|
|
57
|
+
<SidebarGroupLabel>Companies</SidebarGroupLabel>
|
|
58
|
+
<SidebarGroupContent>
|
|
59
|
+
<SidebarMenu>
|
|
60
|
+
{items.map((item) => (
|
|
61
|
+
<SidebarMenuItem key={item.url}>
|
|
62
|
+
<SidebarMenuButton asChild tooltip={item.title} isActive={item.active}>
|
|
63
|
+
<Link href={item.url} onClick={closeSheet} aria-current={item.active ? 'page' : undefined}>
|
|
64
|
+
<item.icon size={16} aria-hidden="true" />
|
|
65
|
+
<span>{item.title}</span>
|
|
66
|
+
</Link>
|
|
67
|
+
</SidebarMenuButton>
|
|
68
|
+
</SidebarMenuItem>
|
|
69
|
+
))}
|
|
70
|
+
</SidebarMenu>
|
|
71
|
+
</SidebarGroupContent>
|
|
72
|
+
</SidebarGroup>
|
|
73
|
+
</nav>
|
|
74
|
+
</SidebarContent>
|
|
75
|
+
<SidebarFooter>
|
|
76
|
+
{!is_collapsed && <p className="px-2 text-xs text-muted-foreground">v{version}</p>}
|
|
77
|
+
</SidebarFooter>
|
|
78
|
+
<SidebarRail />
|
|
79
|
+
</Sidebar>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { cn } from '@fob/lib-ui/lib/utils';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The content area under the breadcrumb bar (design-standards PageLayout). Blocks inside it
|
|
6
|
+
* are spaced by its gap, not by their own margins. min-w-0 lets a wide table scroll inside
|
|
7
|
+
* itself instead of widening the page.
|
|
8
|
+
* @param {{children: React.ReactNode, className?: string}} props
|
|
9
|
+
*/
|
|
10
|
+
export default function MainContainer({ children, className }) {
|
|
11
|
+
return <div className={cn('m-3 my-2 flex min-w-0 flex-col gap-4 pb-6', className)}>{children}</div>;
|
|
12
|
+
}
|