Use cases
Insight
One metric at a time, powered by useReport.
The stack you choose picks the code on this page. Options marked coming soon can't be chosen yet.
Copies the Quill CLI's install command: curl -fsSL https://quill.co/install.sh | shView as MarkdownThe whole page, with the code for the chosen stack.
import { useState } from 'react';
import { Chart, useDashboardReports, useReport, type SetReportBuilderInput } from '@quillsql/react';
import { MetricTable, type MetricRowsPage } from '../components/blocks/metric-explorer/metric-explorer';
import { Select } from '../components/filter-builder/filter-builder';
import SegmentedControl from '../components/segmented-control/segmented-control';
import { FilterSentence } from './FilterSentence';
import styles from './ReportBuilder.module.css';
const T = 'account_days';
const DAY = `${T}::day`;
const ROWS_PER_PAGE = 5;
/** Days draw as a line, oldest first; plans and statuses as columns. */
const groupBy = (value: string): SetReportBuilderInput => value === DAY
? { groupRowsBy: value, chartType: 'line', chartSort: `rows:${DAY} ascending` }
: { groupRowsBy: value, chartType: 'column', chartSort: '' };
/**
* One metric at a time with useReport alone: the filters are setFilters, and every select is a setReport on the
* Metric history report. The table is the same report's rows, under the same filters, formatted and paged by Quill.
*/
export function InsightBlock() {
const { dashboardReports } = useDashboardReports('Metric explorer');
const report = useReport(Object.values(dashboardReports ?? {}).find((r) => r.name === 'Metric history')?.id,
{ initialPagination: { pageIndex: 0, pageSize: ROWS_PER_PAGE }, showTableNames: false });
const { chart, chartLoading, loading, aggregations, aggregationOptions, groupRowsBy, groupRowsByOptions, groupColumnsBy,
groupColumnsByOptions, filters, setReport, setFilters, table, tableLoading } = report;
const [view, setView] = useState<'chart' | 'table'>('chart');
const page: MetricRowsPage = {
columns: table.columns,
rows: table.rows,
page: table.pagination.pageIndex + 1, pageCount: Math.max(0, table.getPageCount()), rowCount: table.getRowCount(),
loading: loading || tableLoading, onPageChange: (next) => table.setPageIndex(next - 1),
};
return (
<section className={styles.root} aria-label="Insight">
<FilterSentence filters={filters} queryBuilderProps={report.filterQueryBuilderProps} onChange={setFilters} />
<div className={styles.controls}>
<SegmentedControl className={styles.viewSwitch} label="View" value={view} onValueChange={(v) => setView(v as 'chart' | 'table')}
options={[{ value: 'chart', label: 'Chart' }, { value: 'table', label: 'Table' }]} />
{view === 'chart' && (
<>
<Select className={styles.group} label="Group by" options={groupRowsByOptions} value={groupRowsBy} onChange={(v) => setReport(groupBy(v))} />
<Select className={styles.group} label="Segment by" options={[{ value: '', label: 'None' }, ...groupColumnsByOptions]} value={groupColumnsBy}
onChange={(v) => setReport({ groupColumnsBy: v, showLegend: !!v })} />
<Select className={styles.group} label="Aggregate by" options={aggregationOptions} value={aggregations[0]} onChange={(v) => setReport({ aggregations: [v] })} />
</>
)}
</div>
{view === 'chart' ? (
<Chart chart={chart} chartLoading={chartLoading} containerStyle={{ width: '100%', height: 240 }} />
) : (
<MetricTable table={page} rowsPerPage={ROWS_PER_PAGE} caption="Metric history" />
)}
</section>
);
}import { Quill } from '@quillsql/node';
const quill = new Quill({
privateKey: process.env.QUILL_PRIVATE_KEY,
databaseType: 'postgresql',
databaseConnectionString: process.env.DATABASE_URL,
});
// "authenticate" is your existing auth middleware.
app.post('/quill', authenticate, async (req, res) => {
const result = await quill.query({
tenants: [{ tenantField: 'organization_id', tenantIds: [req.user.organizationId] }],
metadata: req.body.metadata,
});
res.send(result);
});