Server: headless rendering, alerts and schedules

@prismbi/prismbi/server runs on Node (or any runtime) to render visuals and report pages without a browser, watch measures for alerts, and schedule report jobs. Rendering needs a DOM implementation you bring yourself, such as jsdom, linkedom or happy-dom. PrismBI itself adds no dependency.

import { JSDOM } from 'jsdom';
import { renderVisualSvg, renderReportHtml, watchMeasure, scheduleReport } from '@prismbi/prismbi/server';

const { window } = new JSDOM();

Rendering #

const svg = renderVisualSvg(model, { type: 'line', fields: { category: 'Date[MonthYear]', values: 'Total Sales' } }, { window, width: 640, height: 360 });
const html = renderReportHtml(model, reportDefinition, { window, page: 'Overview', width: 1280, theme: 'light' });

Alerts #

const stop = watchMeasure(model, {
  measure: 'Total Sales',
  filters: [{ target: 'Region[Country]', op: 'eq', value: 'Canada' }],
  when: { below: 50_000 },          // or { above: n }, { changeBy: 0.1 } (10%), or (value, previous) => boolean
  onAlert: (e) => notify(`${e.measure} is ${e.value} (${e.condition})`),
  debounceMs: 500,                   // batch bursts of streamed rows
});

Schedules #

scheduleReport({
  every: 'daily@08:00',             // or milliseconds
  run: async () => {
    const html = renderReportHtml(model, report, { window });
    await mailer.sendMail({ to: 'team@example.com', subject: 'Daily sales', html });
  },
  onError: console.error,
});

Subscriptions and alerts from the report #

Authors define scheduled sends and alerts in the Studio (⏰ Schedules), or in the report definition:

The Studio Schedules dialog: scheduled sends and measure alerts

{ pages: [/* … */],
  subscriptions: [{ id: 's1', name: 'Daily summary', page: 'overview', schedule: 'daily@08:00', recipients: ['team@example.com'] }],
  alerts: [{ id: 'a1', name: 'Sales drop', measure: 'Total Sales', when: { below: 1_000_000 }, recipients: ['ops@example.com'] }] }

Run them on a server with your delivery code:

const stopSends = runSubscriptions(model, report, { window, send: (sub, html) => mailer.sendMail({ to: sub.recipients, subject: sub.name, html }) });
const stopAlerts = watchAlerts(model, report, (alert, e) => notifyChannel(alert.recipients, `${alert.name}: ${e.value}`));