-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreport-service.ts
More file actions
525 lines (473 loc) · 20.3 KB
/
Copy pathreport-service.ts
File metadata and controls
525 lines (473 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import type {
IReportService,
SavedReport,
ReportSchedule,
ReportQuery,
ReportRunResult,
ReportFormat,
SaveReportInput,
ScheduleReportInput,
SharingExecutionContext,
} from '@objectstack/spec/contracts';
import { Cron } from 'croner';
/**
* Narrow engine surface — keeps the service testable without booting
* a real ObjectQL kernel.
*/
export interface ReportEngine {
find(object: string, options?: any): Promise<any[]>;
findOne?(object: string, options?: any): Promise<any>;
insert(object: string, data: any, options?: any): Promise<any>;
update(object: string, idOrData: any, dataOrOptions?: any, options?: any): Promise<any>;
delete(object: string, options?: any): Promise<any>;
}
/**
* Minimum email surface — implementations may pass the full
* `IEmailService` instance straight through.
*/
export interface ReportEmail {
send(input: {
to: string | string[];
subject: string;
text?: string;
html?: string;
attachments?: Array<{ filename: string; content: string; contentType?: string }>;
relatedObject?: string;
relatedId?: string;
}): Promise<{ status: 'sent' | 'queued' | 'failed' }>;
}
/** Stamped only in tests / specialised callers to make `now` deterministic. */
export interface ReportClock { now(): Date }
const SYSTEM_CTX = { isSystem: true, roles: [], permissions: [] } as const;
const DEFAULT_FORMAT: ReportFormat = 'csv';
const DEFAULT_INTERVAL_MIN = 1440;
const DEFAULT_LIMIT = 1000;
function uid(prefix: string): string {
const g: any = globalThis as any;
if (g.crypto?.randomUUID) return `${prefix}_${g.crypto.randomUUID()}`;
return `${prefix}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
}
function parseQuery(raw: unknown): ReportQuery {
if (!raw) return {};
if (typeof raw === 'string') {
try { return JSON.parse(raw) as ReportQuery; }
catch { return {}; }
}
if (typeof raw === 'object') return raw as ReportQuery;
return {};
}
function rowFromSaved(row: any): SavedReport {
return {
id: String(row.id),
name: String(row.name ?? ''),
description: row.description ?? undefined,
object_name: String(row.object_name ?? ''),
query: parseQuery(row.query_json),
format: (row.format as ReportFormat) ?? DEFAULT_FORMAT,
owner_id: row.owner_id ?? undefined,
last_run_at: row.last_run_at ?? undefined,
last_row_count: row.last_row_count ?? undefined,
created_at: row.created_at ?? undefined,
updated_at: row.updated_at ?? undefined,
};
}
function rowFromSchedule(row: any): ReportSchedule {
return {
id: String(row.id),
report_id: String(row.report_id),
name: row.name ?? undefined,
interval_minutes: row.interval_minutes ?? undefined,
cron_expression: row.cron_expression ?? undefined,
timezone: row.timezone ?? undefined,
active: row.active !== false,
recipients: String(row.recipients ?? ''),
format: row.format ?? undefined,
subject_template: row.subject_template ?? undefined,
owner_id: row.owner_id ?? undefined,
next_run_at: row.next_run_at ?? undefined,
last_sent_at: row.last_sent_at ?? undefined,
last_status: row.last_status ?? undefined,
last_error: row.last_error ?? undefined,
};
}
// ─── Rendering ─────────────────────────────────────────────────────
function escapeCsvCell(v: unknown): string {
if (v == null) return '';
const s = typeof v === 'string' ? v : (typeof v === 'object' ? JSON.stringify(v) : String(v));
if (/[",\r\n]/.test(s)) return `"${s.replace(/"/g, '""')}"`;
return s;
}
function pickFields(rows: any[], explicit?: string[]): string[] {
if (explicit && explicit.length > 0) return explicit;
const seen = new Set<string>();
for (const r of rows.slice(0, 50)) {
if (r && typeof r === 'object') for (const k of Object.keys(r)) seen.add(k);
}
return Array.from(seen);
}
function renderCsv(rows: any[], fields?: string[]): string {
const cols = pickFields(rows, fields);
const head = cols.join(',');
const body = rows.map(r => cols.map(c => escapeCsvCell(r?.[c])).join(',')).join('\r\n');
return body.length > 0 ? `${head}\r\n${body}` : head;
}
function renderJson(rows: any[]): string {
return JSON.stringify(rows, null, 2);
}
function escapeHtml(s: string): string {
return s.replace(/[&<>"']/g, c => ({
'&': '&', '<': '<', '>': '>', '"': '"', "'": ''',
} as Record<string, string>)[c]);
}
function renderHtmlTable(rows: any[], fields?: string[]): string {
const cols = pickFields(rows, fields);
const th = cols.map(c => `<th style="text-align:left;padding:4px 8px;border-bottom:1px solid #ccc;">${escapeHtml(c)}</th>`).join('');
const trs = rows.map(r => {
const tds = cols.map(c => {
const v = r?.[c];
const s = v == null ? '' : (typeof v === 'string' ? v : (typeof v === 'object' ? JSON.stringify(v) : String(v)));
return `<td style="padding:4px 8px;border-bottom:1px solid #eee;">${escapeHtml(s)}</td>`;
}).join('');
return `<tr>${tds}</tr>`;
}).join('');
return `<table style="border-collapse:collapse;font-family:system-ui,Arial,sans-serif;font-size:13px;">`
+ `<thead><tr>${th}</tr></thead><tbody>${trs}</tbody></table>`;
}
export function renderReport(rows: any[], format: ReportFormat, fields?: string[]): string {
switch (format) {
case 'json': return renderJson(rows);
case 'html_table': return renderHtmlTable(rows, fields);
case 'csv':
default: return renderCsv(rows, fields);
}
}
// ─── Subject templating (minimal {{var}}) ─────────────────────────
function renderSubject(template: string | undefined, vars: Record<string, string>): string {
const tpl = template ?? '{{name}} — {{date}}';
return tpl.replace(/\{\{\s*(\w+)\s*\}\}/g, (_m, k) => vars[String(k)] ?? '');
}
// ─── Service ──────────────────────────────────────────────────────
export interface ReportServiceOptions {
engine: ReportEngine;
email?: ReportEmail;
clock?: ReportClock;
logger?: { info?: (msg: any, ...rest: any[]) => void; warn?: (msg: any, ...rest: any[]) => void; error?: (msg: any, ...rest: any[]) => void };
/** Cap rows per report to protect both DB and email size. */
maxRows?: number;
}
export class ReportService implements IReportService {
private readonly engine: ReportEngine;
private readonly email?: ReportEmail;
private readonly clock: ReportClock;
private readonly logger: NonNullable<ReportServiceOptions['logger']>;
private readonly maxRows: number;
constructor(opts: ReportServiceOptions) {
this.engine = opts.engine;
this.email = opts.email;
this.clock = opts.clock ?? { now: () => new Date() };
this.logger = opts.logger ?? {};
this.maxRows = Math.max(1, opts.maxRows ?? 5000);
}
// ── Report CRUD ────────────────────────────────────────────────
async saveReport(input: SaveReportInput, context: SharingExecutionContext): Promise<SavedReport> {
if (!input.name) throw new Error('VALIDATION_FAILED: name is required');
if (!input.object) throw new Error('VALIDATION_FAILED: object is required');
if (!input.query) throw new Error('VALIDATION_FAILED: query is required');
const now = this.clock.now().toISOString();
const payload: any = {
name: input.name,
description: input.description ?? null,
object_name: input.object,
query_json: JSON.stringify(input.query ?? {}),
format: input.format ?? DEFAULT_FORMAT,
owner_id: input.ownerId ?? context.userId ?? null,
updated_at: now,
};
if (input.id) {
const existing = await this.engine.find('sys_saved_report', {
filter: { id: input.id }, limit: 1, context: SYSTEM_CTX,
});
if (Array.isArray(existing) && existing[0]) {
await this.engine.update('sys_saved_report', { id: input.id, ...payload }, { context: SYSTEM_CTX });
return rowFromSaved({ ...existing[0], ...payload, id: input.id });
}
}
const id = input.id ?? uid('rpt');
const row = { id, ...payload, created_at: now };
await this.engine.insert('sys_saved_report', row, { context: SYSTEM_CTX });
return rowFromSaved(row);
}
async listReports(
filter: { object?: string; ownerId?: string } | undefined,
_context: SharingExecutionContext,
): Promise<SavedReport[]> {
const f: any = {};
if (filter?.object) f.object_name = filter.object;
if (filter?.ownerId) f.owner_id = filter.ownerId;
const rows = await this.engine.find('sys_saved_report', {
filter: f, limit: 500, orderBy: [{ field: 'updated_at', order: 'desc' }], context: SYSTEM_CTX,
});
return Array.isArray(rows) ? rows.map(rowFromSaved) : [];
}
async getReport(reportId: string, _context: SharingExecutionContext): Promise<SavedReport | null> {
const rows = await this.engine.find('sys_saved_report', {
filter: { id: reportId }, limit: 1, context: SYSTEM_CTX,
});
return Array.isArray(rows) && rows[0] ? rowFromSaved(rows[0]) : null;
}
async deleteReport(reportId: string, _context: SharingExecutionContext): Promise<void> {
if (!reportId) throw new Error('VALIDATION_FAILED: reportId is required');
// Cascade — drop attached schedules first.
const schedules = await this.engine.find('sys_report_schedule', {
filter: { report_id: reportId }, limit: 500, context: SYSTEM_CTX,
});
for (const s of (schedules ?? [])) {
await this.engine.delete('sys_report_schedule', { where: { id: (s as any).id }, context: SYSTEM_CTX });
}
await this.engine.delete('sys_saved_report', { where: { id: reportId }, context: SYSTEM_CTX });
}
// ── Execution ───────────────────────────────────────────────────
async run(reportId: string, context: SharingExecutionContext): Promise<ReportRunResult> {
const report = await this.getReport(reportId, context);
if (!report) throw new Error(`REPORT_NOT_FOUND: ${reportId}`);
return this.executeReport(report, context);
}
async runAdHoc(input: SaveReportInput, context: SharingExecutionContext): Promise<ReportRunResult> {
if (!input.object) throw new Error('VALIDATION_FAILED: object is required');
if (!input.query) throw new Error('VALIDATION_FAILED: query is required');
const adhoc: SavedReport = {
id: '__adhoc__',
name: input.name ?? 'Ad-hoc report',
object_name: input.object,
query: input.query,
format: input.format ?? DEFAULT_FORMAT,
};
return this.executeReport(adhoc, context, /* stamp */ false);
}
private async executeReport(
report: SavedReport,
context: SharingExecutionContext,
stamp = true,
): Promise<ReportRunResult> {
const q = report.query ?? {};
const limit = Math.min(q.limit ?? DEFAULT_LIMIT, this.maxRows);
const rows = await this.engine.find(report.object_name, {
filter: q.filter,
fields: q.fields,
orderBy: q.orderBy,
limit,
// Reports execute with the caller's identity so sharing rules
// (if installed) apply. Falls back to system bypass only when
// the report definition was created by a system writer.
context: {
userId: context.userId,
tenantId: context.tenantId,
roles: context.roles ?? [],
permissions: context.permissions ?? [],
isSystem: context.isSystem ?? false,
},
});
const list = Array.isArray(rows) ? rows : [];
const body = renderReport(list, report.format, q.fields);
const ranAt = this.clock.now().toISOString();
if (stamp && report.id !== '__adhoc__') {
try {
await this.engine.update('sys_saved_report', {
id: report.id,
last_run_at: ranAt,
last_row_count: list.length,
updated_at: ranAt,
}, { context: SYSTEM_CTX });
} catch (err) {
this.logger.warn?.('ReportService: failed to stamp last_run_at', err);
}
}
return {
reportId: report.id,
rowCount: list.length,
format: report.format,
body,
rows: list,
ranAt,
};
}
// ── Schedules ──────────────────────────────────────────────────
async scheduleReport(input: ScheduleReportInput, context: SharingExecutionContext): Promise<ReportSchedule> {
if (!input.reportId) throw new Error('VALIDATION_FAILED: reportId is required');
if (!input.recipients || input.recipients.length === 0) {
throw new Error('VALIDATION_FAILED: recipients must be a non-empty array');
}
const report = await this.getReport(input.reportId, context);
if (!report) throw new Error(`REPORT_NOT_FOUND: ${input.reportId}`);
const now = this.clock.now();
const interval = input.intervalMinutes ?? DEFAULT_INTERVAL_MIN;
const cron = input.cronExpression?.trim() || null;
if (cron) {
// Validate eagerly so an author gets a clear error at schedule time
// instead of a schedule that silently falls back to interval on sweep.
try {
new Cron(cron, { timezone: input.timezone || 'UTC' });
} catch (err) {
throw new Error(`VALIDATION_FAILED: invalid cron_expression '${cron}': ${(err as Error).message}`);
}
}
const nextRun = this.nextRunAt(
{ cron_expression: cron, interval_minutes: interval, timezone: input.timezone ?? 'UTC' },
now,
).toISOString();
const id = uid('rsch');
const row: any = {
id,
report_id: input.reportId,
name: input.name ?? null,
interval_minutes: interval,
cron_expression: cron,
timezone: input.timezone ?? 'UTC',
active: input.active !== false,
recipients: input.recipients.join(','),
format: input.format ?? 'html_table',
subject_template: input.subjectTemplate ?? null,
owner_id: input.ownerId ?? context.userId ?? null,
next_run_at: nextRun,
created_at: now.toISOString(),
updated_at: now.toISOString(),
};
await this.engine.insert('sys_report_schedule', row, { context: SYSTEM_CTX });
return rowFromSchedule(row);
}
async unscheduleReport(scheduleId: string, _context: SharingExecutionContext): Promise<void> {
if (!scheduleId) throw new Error('VALIDATION_FAILED: scheduleId is required');
await this.engine.delete('sys_report_schedule', { where: { id: scheduleId }, context: SYSTEM_CTX });
}
async listSchedules(
filter: { reportId?: string } | undefined,
_context: SharingExecutionContext,
): Promise<ReportSchedule[]> {
const f: any = {};
if (filter?.reportId) f.report_id = filter.reportId;
const rows = await this.engine.find('sys_report_schedule', {
filter: f, limit: 500, orderBy: [{ field: 'next_run_at', order: 'asc' }], context: SYSTEM_CTX,
});
return Array.isArray(rows) ? rows.map(rowFromSchedule) : [];
}
// ── Dispatcher ─────────────────────────────────────────────────
async dispatchDue(now?: Date): Promise<{ fired: number; failed: number; skipped: number }> {
const ts = (now ?? this.clock.now()).toISOString();
const due = await this.engine.find('sys_report_schedule', {
filter: { active: true },
limit: 200,
context: SYSTEM_CTX,
});
const list = (Array.isArray(due) ? due : []).map(rowFromSchedule)
.filter(s => !s.next_run_at || s.next_run_at <= ts);
let fired = 0, failed = 0, skipped = 0;
for (const schedule of list) {
try {
const report = await this.getReport(schedule.report_id, { isSystem: true });
if (!report) {
skipped++;
await this.markSchedule(schedule.id, {
last_status: 'skipped',
last_error: `report ${schedule.report_id} missing`,
});
continue;
}
// Force the schedule's own format so the recipient gets what
// the admin configured (CSV attachment vs inline HTML table).
const fmt: ReportFormat = (schedule.format ?? 'html_table') as ReportFormat;
const result = await this.executeReport({ ...report, format: fmt }, { isSystem: true }, false);
const recipients = schedule.recipients.split(',').map(s => s.trim()).filter(Boolean);
const subject = renderSubject(schedule.subject_template, {
name: schedule.name ?? report.name,
date: ts.slice(0, 10),
rows: String(result.rowCount),
});
if (this.email && recipients.length > 0) {
if (fmt === 'csv') {
await this.email.send({
to: recipients,
subject,
text: `Attached: ${result.rowCount} row(s).`,
attachments: [{
filename: `${(schedule.name ?? report.name).replace(/[^\w.-]+/g, '_')}-${ts.slice(0, 10)}.csv`,
content: result.body,
contentType: 'text/csv',
}],
relatedObject: 'sys_report_schedule',
relatedId: schedule.id,
});
} else {
await this.email.send({
to: recipients,
subject,
html: `<p>${escapeHtml(report.name)} — ${result.rowCount} row(s)</p>${result.body}`,
text: `${report.name} — ${result.rowCount} row(s)`,
relatedObject: 'sys_report_schedule',
relatedId: schedule.id,
});
}
} else if (!this.email) {
this.logger.warn?.('ReportService.dispatchDue: no email service — schedule fired but mail not sent');
}
await this.advanceSchedule(schedule, ts);
fired++;
} catch (err: any) {
failed++;
await this.markSchedule(schedule.id, {
last_status: 'failed',
last_error: String(err?.message ?? err ?? 'unknown').slice(0, 500),
});
this.logger.error?.('ReportService.dispatchDue: schedule failed', err);
}
}
return { fired, failed, skipped };
}
/**
* Compute the next fire time for a schedule. A `cron_expression` wins over
* `interval_minutes` (the documented `sys_report_schedule` contract) and is
* evaluated in the schedule's `timezone` (default UTC) via croner — the same
* library the job scheduler uses. Falls back to `from + interval_minutes` for
* interval schedules, and also if a cron expression is invalid or has no
* future occurrence (logged; never throws into the sweep). `from` is the
* reference instant (the injected clock), so `today()`-style boundaries honor
* the test clock.
*/
private nextRunAt(
schedule: { cron_expression?: string | null; interval_minutes?: number | null; timezone?: string | null },
from: Date,
): Date {
const cron = (schedule.cron_expression ?? '').trim();
if (cron) {
try {
const next = new Cron(cron, { timezone: schedule.timezone || 'UTC' }).nextRun(from);
if (next) return next;
this.logger.warn?.(`ReportService: cron '${cron}' has no next occurrence; falling back to interval`);
} catch (err) {
this.logger.warn?.(`ReportService: invalid cron '${cron}'; falling back to interval`, err);
}
}
const interval = schedule.interval_minutes ?? DEFAULT_INTERVAL_MIN;
return new Date(from.getTime() + interval * 60_000);
}
private async advanceSchedule(schedule: ReportSchedule, ranAt: string): Promise<void> {
const nextRun = this.nextRunAt(schedule, this.clock.now()).toISOString();
await this.engine.update('sys_report_schedule', {
id: schedule.id,
next_run_at: nextRun,
last_sent_at: ranAt,
last_status: 'ok',
last_error: null,
updated_at: ranAt,
}, { context: SYSTEM_CTX });
}
private async markSchedule(id: string, patch: Record<string, unknown>): Promise<void> {
try {
await this.engine.update('sys_report_schedule', {
id, ...patch, updated_at: this.clock.now().toISOString(),
}, { context: SYSTEM_CTX });
} catch (err) {
this.logger.warn?.('ReportService: failed to mark schedule', err);
}
}
}