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_type | What runs |
|---|---|
Code Report | An Elixir module, Frappe's Script Report |
Report Builder | A saved list view: columns, filters and sort in json |
A Code Report
Add the Report record.
- 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.
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
endRun 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.
fieldtype | Value |
|---|---|
Data, Select, Check | As on a form |
Link | A record the user may select. options is the RecType |
MultiSelectList | A list of names. options is the RecType |
Date | An ISO date |
DateRange | A 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:
| Key | Shows |
|---|---|
columns | %{fieldname, label, fieldtype} with optional options |
rows | Maps by fieldname, with wire values (decimals as strings) |
message | HTML above the table |
chart | %{type, data: %{labels, datasets: [%{name, values}]}}. type is bar, line, pie or donut |
report_summary | Cards, %{label, value, datatype} with an optional indicator (Green, Red, Blue, Orange) |
parent_field, initial_depth | Tree rows. Each row carries indent and its parent's name |
A Report Builder view
- 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.