<!--# if expr="$arg_server = /^(node|python|go|php|ruby|dotnet)$/" --><!--# set var="stack_server" value="$arg_server" --><!--# else --><!--# set var="stack_server" value="node" --><!--# endif --><!--# if expr="$arg_database = /^(postgresql|redshift|mysql|mssql|clickhouse|snowflake|bigquery|databricks)$/" --><!--# set var="stack_database" value="$arg_database" --><!--# else --><!--# set var="stack_database" value="postgresql" --><!--# endif --><!--# if expr="$arg_cache = /^(none|duckdb|redis)$/" --><!--# set var="stack_cache" value="$arg_cache" --><!--# else --><!--# set var="stack_cache" value="none" --><!--# endif -->
# Insight

One metric at a time, powered by useReport.

> The code on this page is for the stack in its URL's query, by default `?server=node&database=postgresql&cache=none`. Server: `node`, `python`, `go`, `php`, `ruby`, `dotnet`. Database: `postgresql`, `redshift`, `mysql`, `mssql`, `clickhouse`, `snowflake`, `bigquery`, `databricks`. Cache: `none`, `duckdb` (`node` only), `redis`. A missing or unknown value is the default, the first listed.

[Open the demo](https://quill.co/docs-demo/blocks/#/insight)

`InsightBlock.tsx`

```tsx
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>
  );
}
```

<!--# include virtual="/docs-md/code/server-file/$stack_server/$stack_database/$stack_cache.md" -->
