cardinal

Reports

Ship Code Reports written in Elixir and saved Report Builder views as fixtures.

An app ships reports as records of the Report RecType in fixtures/report.yaml. Add "Report" to the manifest's fixtures.

report_typeWhat runs
Code ReportAn Elixir module, Frappe's Script Report
Report BuilderA saved list view: columns, filters and sort in json

A Code Report

Add the Report record.

eden/fixtures/report.yaml
- report_name: AI Usage
  ref_rectype: AI Call
  report_type: Code Report
  module: Eden
  roles:
    - {role: System Manager}

Write <App>.Reports.<Name> in lib/reports/ (shortened below). The name drops every character that is not a letter or digit and capitalizes the next letter: AI Usage is Eden.Reports.AIUsage, Item-wise Rate (Simple) is ItemWiseRateSimple.

eden/lib/reports/ai_usage.ex
defmodule Eden.Reports.AIUsage do
  @behaviour Cardinal.Report
  alias Cardinal.{Dates, DB}

  @impl true
  def filters(ctx) do
    today = Dates.today(ctx)

    [
      %{fieldname: "period", label: "Period", fieldtype: "DateRange", reqd: true,
        default: [Date.to_iso8601(Date.add(today, -6)), Date.to_iso8601(today)]}
    ]
  end

  @impl true
  def execute(ctx, %{"period" => [from, to]}) do
    with {:ok, rows} <- DB.get_list(ctx, "AI Call", fields: ["user", "model", "status"], filters: period(ctx, from, to)) do
      {:ok, %{columns: columns(), rows: group(rows), chart: chart(rows)}}
    end
  end
end

Run cardinal migrate -t HOST. The report opens at /desk/query-report/<name>.

Before execute/2 runs, the platform checks the report's roles and the report right on ref_rectype. execute/2 gets the caller's ctx, so its Cardinal.DB reads are permission-checked.

Filters

filters/0 returns a list. Define filters/1 instead when a default depends on the user or the day. Each filter is %{fieldname, label, fieldtype} with optional options, default, reqd and link_filters.

fieldtypeValue
Data, Select, CheckAs on a form
LinkA record the user may select. options is the RecType
MultiSelectListA list of names. options is the RecType
DateAn ISO date
DateRangeA list of two ISO dates

A missing required filter is a 422. In a Link's link_filters, a value %{filter: "company"} reads another filter's value.

What execute returns

{:ok, %{columns: [...], rows: [...]}}, plus any of these keys:

KeyShows
columns%{fieldname, label, fieldtype} with optional options
rowsMaps by fieldname, with wire values (decimals as strings)
messageHTML above the table
chart%{type, data: %{labels, datasets: [%{name, values}]}}. type is bar, line, pie or donut
report_summaryCards, %{label, value, datatype} with an optional indicator (Green, Red, Blue, Orange)
parent_field, initial_depthTree rows. Each row carries indent and its parent's name

A Report Builder view

erp/fixtures/report.yaml
- report_name: Serial No Status
  ref_rectype: Serial No
  report_type: Report Builder
  module: Stock
  json:
    columns: [name, serial_no, item_code, status, warehouse]
    filters: []
    sort: {field: serial_no, order: desc}
  roles:
    - {role: Stock User}

Run a report from code

{:ok, %{columns: cols, rows: rows}} = Cardinal.Report.run(ctx, "AI Usage", %{"period" => [from, to]})

It runs as the ctx's user with the same checks, inside the caller's transaction.

On this page