Building a Reporting API

By the end you have a report endpoint that takes a date range and answers with a CSV or XLSX file, plus the same report mailed through the queue and on a schedule.

1. An aggregation groups the documents

Reports group documents instead of listing them. The grouping block picks the dimension:

BlockGroups byExample
aggregation_type_date_histogram_bp_field (date histogram)A date field per interval (day, week, month)Signups per day, revenue per week
aggregation_type_terms_bp_field (terms)The distinct values of a field, with a count per valueOrders per status, tickets per assignee
aggregation_type_cardinality_bp_field (cardinality)Counts distinct valuesUnique customers
aggregation_type_value_count_bp_field (count on field)Counts entriesOrders with a coupon

query_aggregation (aggregation) wraps the grouping block under a name and attaches it to the search. Feed it into the query pin of function_search (search), together with a filter that limits the range: query_bool_range_date_field (date) on the date field with query_bool_range_date_gte (field >= {value}) and query_bool_range_date_lt (field < {value}) in it, inside query_bool_filter (filter). Read the dates from the request with httpconnection_get_query (get query). More query blocks are in Storages.

Wire several aggregations and the studio adds one extra query pin per aggregation on the search block, so one pass over the data can fill a whole dashboard.

2. The download builds a file and replies with it

A function trigger runs the report: a date histogram groups documents per day, aggregation names it and feeds search, and build CSV turns the result rows into a file that reply with file downloads to the caller.

Studio canvas example for the build CSV block: an aggregation straight into a CSV download.

On the canvas: function_build_csv (build CSV) takes the rows as values plus a filename, and httpconnection_set_file (reply with file) streams the file to the caller through the httpconnection_current_request (current request) connection. For a real report, add two things to that wiring:

  1. Take the rows out of the result

    A search with an aggregation returns one object on array, not documents. Put array_get_buckets (get aggregation buckets) between the search and the CSV: it gives one row per bucket, with key, key_as_string and doc_count. It reads the alphabetically first aggregation only.

  2. Wire the header

    Give function_build_csv a header array of the columns, for example with array_of_values (with values). Without one the columns are inferred, no header line is written and the first row is dropped.

For spreadsheets with real types and multiple sheets use file_to_xlsx (build XLS/X) instead, with the same filename and values and its headers pin.

How you know it worked: curl -o report.csv the endpoint with a date range and open the file. You see a header line and one row per day.

3. Heavy reports leave the request

A heavy report does not belong inside the request that asked for it.

  1. Hand off and answer

    The endpoint validates the request, hands the work to function_custom_execute_from_queue (execute in queue), and replies 202 at once. See Queue.

  2. Build and mail

    The queued function runs the same aggregation, builds the file, and mails it with postmarkconnection_send_email_template (send email with template), which takes the CSV or XLSX straight on its file pin. See Email System Setup for the connection.

  3. Or run it on a clock

    A report with its own schedule, such as the Monday morning export, starts from schedule_repeating_event (repeating event) instead of an endpoint. See Repeating Events.

Notes from practice

  • Reports over large storages are read-heavy by definition. Give the search a cache_key so a dashboard refreshed by ten people runs the aggregation once. See Caching Strategies.
  • Branch on the search block's success pin and reply with an error body on failure, per Error Handling. A report that silently returns an empty file is worse than none.
  • Scope report endpoints tightly: they aggregate exactly the data most callers should not bulk-download. *loggedin at minimum, a custom scope for anything financial. See Remote Access Control.
  • Date histograms group on the stored date field; _meta.created is the one every document already has.

Next steps

Storages The query blocks that limit what the aggregation reads. Queue Hand slow reports to the queue and reply immediately. Email System Setup The Postmark connection that mails the finished report. Dashboard with Charts Show the same aggregation on a web page.

Frequently asked

How do I build a CSV report from storage data in RUAL?

Group the documents with an aggregation block such as aggregation_type_date_histogram_bp_field, attach it to a search with query_aggregation, feed the result rows to function_build_csv, and reply with the file through httpconnection_set_file.

How do I email a report on a schedule in RUAL?

Start the flow from schedule_repeating_event instead of an endpoint, run the same aggregation, and mail the built file with postmarkconnection_send_email_template, which accepts the CSV or XLSX directly on its file pin. Reports triggered by a request go through the queue so the API can reply immediately.