Skip to content

Analytics ​

  Table of Contents

Analytics runs read-only PostgreSQL queries over a stable set of views. Open Data → Analytics, enter SQL under Write the SQL yourself, and select Run or press Cmd+Enter (Ctrl+Enter on Windows and Linux). The editor highlights SQL as you type. Results appear in a table with each column's type, numbers right-aligned and null values marked. Download CSV downloads those same rows without running the query again. The query and question you last typed stay in the browser, so reloading the page or coming back later picks up where you left off. Results don't: run the query again to see them.

The Analytics permission grants access to the entire exposed analytics dataset. Administrators receive it automatically; other users and organization API keys need it explicitly. Queries cannot change data. Pages use the viewer's Analytics permission when calling the API.

From the Command Line ​

fctl runs the same queries from a terminal: fctl analytics schema lists the views and columns, and fctl analytics query "select …" runs a query with the roles of your API key. A coding agent such as Claude Code or Codex can use those two commands to answer questions about your data on its own: it reads the schema, writes a query, and runs it. That needs no AI keys in Factorial, only a Factorial API key with the Analytics permission.

Asking in Plain English ​

The Ask tab writes SQL from a question, such as "How many work orders were completed each week this quarter?". Type the question in the question box and select Ask & run, or press Cmd+Enter. While the model works, the question, the SQL editor, and Run wait. The model writes a query that answers the question, and Factorial puts it in the SQL editor and runs it, read-only, with the same limits as a query you write yourself. The query stays above its results, marked as written by AI, so you can check that it answers what you asked, edit it, and run it again.

The arrow beside the button chooses what it does. Ask & run is the default. Ask only writes the query into the editor without running it, for you to check first. Your choice is remembered in this browser. When the views can't answer a question, the model writes only SQL comments saying what is missing, and Factorial doesn't run them.

A long query is shortened in the editor until you click into it or select Show all.

Factorial shows the query on purpose. Running a query is deterministic: the same query over the same data gives the same result every time, and anyone who reads SQL can check that it answers the question. Asking is not deterministic: the same question asked twice may produce a different query.

Asking uses an API key brought from Anthropic, OpenAI, or Gemini. Factorial sends the provider the question, the views and columns under Schema, and a few written instructions on writing queries for them. The schema is the same for every organization. Factorial never sends rows or results, and no Factorial feature sends your data to a model on its own. The provider bills the key for each question.

Ask is off until an administrator turns on AI features. Until then, and whenever you have no key to ask with, the Ask tab shows a short note saying what would turn it on instead of the question box. An organization can also hide AI features entirely, which removes the Ask tab and the note.

Choosing a key and model ​

With, under the question, lists every key and model the question may use: your own keys first, then the organization's. A key the question can't use stays listed but disabled, with the reason. The first of your keys is chosen by default, with its provider's balanced model. The choice stays selected for the next question. A failed question says why, including when a provider refuses the key or doesn't offer the model to it.

Each provider offers three models, from fastest and cheapest to most capable:

ProviderFastest, cheapestBalanced (default)Most capable
AnthropicClaude Haiku 5.5Claude Sonnet 5.5Claude Opus 5.5
OpenAIGPT-6 LunaGPT-6.1 SolGPT-6 Astra
GeminiGemini 3.5 Flash-LiteGemini 3.8 FlashGemini 4 Argon

Writing one query rarely needs the most capable model. Start with the balanced model, and switch when a query comes back wrong.

AI keys ​

A key belongs to one person or to the organization. Both are managed on the Keys page.

  • Your own keys are on the User AI keys tab. They may ask any of their provider's models. Nobody else can see or use them.
  • Organization keys are on the Organization AI keys tab, added by administrators. Anyone with the Analytics permission can ask with them, using any of their provider's models, and the organization is billed.

To add a key, select Add key, choose a provider, paste the key, optionally name it, and select Save key. Without a name, the key is named after its provider. Each key needs its own name, so a second key from the same provider needs one. Factorial doesn't try the key when it is saved: if the provider refuses it, the first question says why.

To rename a key or paste a new one, select Edit on its row. Leave the key blank to keep the saved one. A key's provider can't change: add a new key for another provider. To remove keys, select them and choose Remove selected. You can always remove your own keys, even after losing the Analytics permission or while AI features are off. Removing a key from Factorial doesn't revoke it with the provider.

Keys are encrypted before they are stored, and Factorial never shows them again. The Keys page shows only each key's name, provider, and last four characters.

Views ​

Expand Schema below the query input to explore view and column names, PostgreSQL types, descriptions, and join hints. These views show current state; they do not reconstruct historical inventory or execution state.

ViewOne row representsContext
analytics.inventoryInventory linePart revision, base unit, quantity, trace number, status
analytics.work_ordersWork orderProcedure revision, output part, owner, schedule, parent
analytics.tasksInstantiated execution taskEffective step title, parent work order or project, assignee, queue
analytics.partsPart revisionPart number, revision, description, preferred and base units

Views include completed and canceled work, and archived, consumed, or scrapped inventory. Filter explicitly for the state needed. work_orders excludes projects. tasks.kind distinguishes PROJECT from WORK_ORDER according to the task's immediate parent. A task's title reflects any applied approved redline.

Part revisions have distinct IDs. Join part_id to analytics.parts.id. For tasks with kind = 'WORK_ORDER', join parent_id to analytics.work_orders.id. Inventory quantities use base_uom, which may differ from the part's preferred display unit. Do not add quantities with different units.

sql
select part_number, part_revision, base_uom, sum(quantity) as available_quantity
from analytics.inventory
where status = 'available'
group by part_number, part_revision, base_uom
order by part_number, part_revision;

View column names, types, units, and row meanings form the public contract. Internal schema changes are handled by updating the views. Breaking changes require a new view; compatible columns may be added. Select named columns in long-lived integrations.

Limits and results ​

Each query has a 10-second deadline, including waiting for an analytics connection. Results contain at most 10,000 rows, 128 columns, and 8 MiB of cell data. Queries may contain at most 64 KiB of SQL. A result stopped by the row or byte limit is marked truncated; its download includes only the returned rows. Narrow the query or query a smaller range to retrieve additional data. Use ORDER BY when row order matters.

A single SELECT, VALUES, or read-only WITH query is supported. Multiple statements, writes, transaction commands, and COPY are rejected. SQL errors appear above the results. Data refreshes only when a query runs.

CSV uses a header row, commas, quoted non-null fields, and CRLF line endings. An unquoted empty field represents SQL null; "" represents an empty string. Embedded quotes and newlines are preserved. A value that spreadsheet software would run as a formula, one starting with =, +, -, @, a tab, or a carriage return that isn't a number, starts with ' in CSV. Negative numbers are unchanged. Use JSON when exact text matters.

API ​

POST /api/v1/analytics/query accepts JSON:

json
{
  "sql": "select part_number, revision from analytics.parts where part_number = $1 order by revision",
  "parameters": ["BRKT-100"],
  "format": "json"
}

Parameters are PostgreSQL text representations or null, with at most 100 parameters. Use explicit casts when PostgreSQL cannot infer a parameter's type, such as $1::uuid. Bind values through parameters rather than interpolating them into SQL. The entire request may be at most 1 MiB.

JSON results include columns, rows, row_count, truncated, and elapsed_ms. Rows are arrays in column order, so duplicate column names remain distinct. Every non-null cell is a string in PostgreSQL text format; null remains JSON null. This preserves exact decimals and large integers. Timestamps with time zones use UTC. Column metadata supplies PostgreSQL type names; unknown result types use oid:<number>.

Set format to csv, or send Accept: text/csv with no explicit format, for a CSV response. Both formats include X-Analytics-Truncated, X-Analytics-Row-Count, and X-Analytics-Elapsed-Ms headers. Check truncation before treating a response as a complete export. Failures return a non-success status with a JSON error message, including when CSV was requested.

GET /api/v1/analytics/schema returns a views array with each view's name, description, and ordered columns. Column descriptions include units, status meanings, and join hints where applicable. This endpoint powers the UI's Schema section.

Both endpoints accept normal API authentication. A custom Page can query using its signed-in viewer:

js
const response = await fetch('/api/v1/analytics/query', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    sql: 'select status, count(*) as count from analytics.tasks group by status order by status'
  })
});
const result = await response.json();
if (!response.ok) throw new Error(result.error);
// result.columns describes each position in result.rows.

Deployment ​

Analytics uses the regular application database. Normal database migrations create a read-only PostgreSQL login, grant access to the analytics views, and generate its credential. No separate setup command or analytics connection URL is required.

At startup, Factorial reads the credential from a private table in the application schema and connects using the regular database's host, database name, and transport settings. The analytics pool is limited to two connections. Submitted SQL runs under the restricted login, which cannot read the credential table or operational tables.

Migrations require permission to create PostgreSQL roles and manage their grants. Each database receives its own analytics login and credential. Include the private credential table and PostgreSQL roles when backing up or restoring a deployment.

The server validates the analytics login, rejecting elevated roles, role memberships, access to operational schemas, write privileges, and executable functions outside PostgreSQL's system schemas. Each query runs in a read-only transaction, and its connection is closed afterward to discard session changes and locks. Default grants cover future analytics views created by the same migration owner; a different migration owner must arrange grants explicitly.

Asking in plain English needs SECRET_ENCRYPTION_KEY, a base64-encoded 32-byte key that encrypts saved AI keys. Create one with openssl rand -base64 32. Without it, keys can't be saved and Ask is disabled. The server must also be able to reach the providers' APIs. Changing or losing the encryption key makes every saved AI key unreadable, and people must save their keys again.

Saved queries, dashboards, and scheduled query alerts are not included in this release.