Skip to main content

Architecture: hbf-reports

C4 Component Diagram

Key Flows

Scheduled Report Delivery (weekly or monthly cron)

  1. SchedulerService fires the cron job (weekly: Monday 00:00, monthly: 1st 00:05).
  2. ReportsService.sendWeeklyReports() (or monthly) queries MySQL for all ScheduledReport rows with enabled=true and weekly=true (or monthly=true).
  3. For each matching schedule: a. Creates a ScheduledReportExport row with status=STARTED. b. Calls GenerateGraphsService.generateGraphs() which:
    • Fetches org and tenant metadata from hbf-core.
    • Draws the index page and deployment page (if filtered by deployment).
    • Iterates the configured sections and draws each one: fetches analytics data from hbf-core, transforms it, generates an SVG chart via echarts, embeds via svg-to-pdfkit, draws the data table.
    • Adds page-number footers to every page.
    • Returns the finished PDF as a base64 string. c. Calls nodemailer.sendMail() with the PDF attached and a branded HTML email body. d. Updates the ScheduledReportExport row with status=COMPLETED and the base64 PDF.

On-Demand Export

  1. HTTP caller sends POST /exports with ExportDto (org, tenants or tenantsFilter, date range, optional sections).
  2. ExportsController passes the request through auth guards, then delegates to ExportsService.exportReport().
  3. ExportsService calls GenerateGraphsService.generateGraphs() (same path as above).
  4. The resulting base64 string is decoded into a Buffer, wrapped in a Readable stream, and returned in the HTTP response.

PDF Section Rendering

Each section follows the same pattern inside GenerateGraphsService:

  1. Call the relevant HbfCoreService method to fetch section data.
  2. Pass the response through a coreDataToChartData transformer to get chart-ready and table-ready structures.
  3. Add a new page to the pdfkit document.
  4. Call the matching DrawPdfService.draw*Template() to paint the section header/background.
  5. Generate the chart SVG via GenerateChartsService and embed with SVGtoPDF.
  6. Call DrawPdfService.drawTable() to render the data table below the chart.