diff --git a/.changeset/add-filter-operator-keeps-popover-open.md b/.changeset/add-filter-operator-keeps-popover-open.md new file mode 100644 index 0000000000..8acf4cbc51 --- /dev/null +++ b/.changeset/add-filter-operator-keeps-popover-open.md @@ -0,0 +1,10 @@ +--- +'@hyperdx/app': patch +--- + +Choosing an operator in Explore's add-filter popover no longer discards the +filter you were building. The operator list was the only one of the three +rendered into a portal, which puts it outside the popover in the DOM, so +clicking an option registered as a click outside and closed the form — losing +the field you had already picked. The field and value lists were already +exempt. diff --git a/.changeset/explore-compact-chrome.md b/.changeset/explore-compact-chrome.md new file mode 100644 index 0000000000..4f12bfe43a --- /dev/null +++ b/.changeset/explore-compact-chrome.md @@ -0,0 +1,11 @@ +--- +'@hyperdx/app': patch +--- + +Compact the Explore page chrome so results start higher up the screen. "Add +filter" now sits inside the search field, the example filter chips, helper +paragraph, in-field "Search" badge, and the duplicate "Show SQL" toggle (still +available under the results overflow menu) are gone, and Columns / Sort moved +into the results row. The view switcher is now a segmented control that labels +only the view you are on and leaves the rest as icons, with the chart type in an +"As Time series" control that appears beside it while a chart is showing. diff --git a/.changeset/explore-filter-bar-is-sql-only.md b/.changeset/explore-filter-bar-is-sql-only.md new file mode 100644 index 0000000000..0e9f93f0eb --- /dev/null +++ b/.changeset/explore-filter-bar-is-sql-only.md @@ -0,0 +1,18 @@ +--- +'@hyperdx/app': patch +--- + +Explore's filter bar is now SQL in every case. It offers no language switch, +but it read `whereLanguage` from the URL, so a saved search shared with the +Search page, an old link, or a side-panel action could put the bar into Lucene +— a mode with no way back out, since Explore has nothing to switch with. + +Explore now coerces the language on the way in and rewrites the URL to match. A +Lucene clause that comes with it is dropped rather than run, because it cannot +be translated in the browser: `genWhereSQL` needs table metadata, so relabelling +the text as SQL would only fail in ClickHouse. Explore says when it discards +one, and the Search page still opens the search as saved. + +Attribute chips in the event side panel now link with the SQL form of the +expression when the destination only takes SQL, instead of a Lucene condition +that would be thrown away on arrival. diff --git a/.changeset/explore-filter-comparisons.md b/.changeset/explore-filter-comparisons.md new file mode 100644 index 0000000000..bd69fd7ff5 --- /dev/null +++ b/.changeset/explore-filter-comparisons.md @@ -0,0 +1,11 @@ +--- +'@hyperdx/app': patch +--- + +Explore's Add filter popover can now build comparisons, not just equality. Pick +a numeric field and `>`, `>=`, `<` or `<=` join `is` and `is not`; text fields +keep the two they had, since a bound compiles to an unquoted literal and would +be invalid SQL against a string. A second bound narrows the existing one rather +than replacing it, so `>500` followed by `<=900` becomes a `BETWEEN`. This +reaches range filters that the pills, the SQL compiler and the search box could +already express — typing `Duration > 500` has always produced the same pill. diff --git a/.changeset/explore-formula-cards.md b/.changeset/explore-formula-cards.md new file mode 100644 index 0000000000..9870b2ee90 --- /dev/null +++ b/.changeset/explore-formula-cards.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +Explore time series, table, and number views can add formula cards (letter-ref expressions like `A / B`) with click-to-insert series badges, URL persistence, and Add to dashboard export. diff --git a/.changeset/explore-metric-pivot.md b/.changeset/explore-metric-pivot.md new file mode 100644 index 0000000000..5fd2948aa3 --- /dev/null +++ b/.changeset/explore-metric-pivot.md @@ -0,0 +1,25 @@ +--- +'@hyperdx/app': patch +--- + +Make metric sources usable on the Explore page: drilling into a chart series now +carries that series over to the rows behind it, and the filter sidebar can +discover fields again. + +Clicking one series of a metric chart previously landed on the Search page with +nothing but a time range — the group filters were discarded on the way out, so +"which pod is spiking" became "every log in the last hour". The clicked series +now travels with you, re-addressed against the destination source's own resource +attributes expression, so it still resolves when the two sources name their +attribute maps differently. Only resource attributes are carried: they identify +the emitting entity and OpenTelemetry keeps them consistent across signals, +whereas a metric's own data-point attributes could produce a filter that matches +nothing and reads as "this pod logged nothing". Drilling down from Explore also +stays on Explore now instead of handing the reader to the Search page. + +The filter sidebar and the WHERE field's autocomplete were both empty for metric +sources. They read schema from `from.tableName`, which a metric source leaves +blank because its real tables are per metric type. Both now read the tables +behind the selected series. When a chart mixes metric types, field discovery +intersects them, since a sidebar filter applies to every series and offering a +field only one table has would break the others. diff --git a/.changeset/explore-metrics-settings-pivots.md b/.changeset/explore-metrics-settings-pivots.md new file mode 100644 index 0000000000..67236a6398 --- /dev/null +++ b/.changeset/explore-metrics-settings-pivots.md @@ -0,0 +1,13 @@ +--- +'@hyperdx/app': patch +--- + +Add chart settings and contextual series actions to metric views in Explore. +Metric chart settings now survive shared URLs and carry into dashboard tiles. +Series menus can filter Explore in place or pivot to correlated logs and traces +for the selected time bucket. Explore series now filter through the same SQL +query editor as the page query bar, dropping the Lucene/SQL switch that Explore +never used, and read as "And where" when the page search already narrows every +series. A series filter builds queries the way the page bar does: a completed +clause promotes into a filter pill, "Add filter" offers the source's columns, +and the pills travel in shared links. diff --git a/.changeset/explore-pattern-expression-toolbar.md b/.changeset/explore-pattern-expression-toolbar.md new file mode 100644 index 0000000000..4ac53d85fe --- /dev/null +++ b/.changeset/explore-pattern-expression-toolbar.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/app': patch +--- + +Explore: move the pattern expression input up beside the view switcher, into +the slot the chart-type picker uses, instead of leaving it stranded between the +chart and the pattern table it governs. Shortened its label to "Expression". diff --git a/.changeset/explore-patterns-field-picker.md b/.changeset/explore-patterns-field-picker.md new file mode 100644 index 0000000000..6c90836ae5 --- /dev/null +++ b/.changeset/explore-patterns-field-picker.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +Explore: Event patterns pick a field the way Group by does (`on Body`) instead of asking for a SQL expression. Clicking a pattern can open matching events in the list, counts are labeled as estimates, and the view can be added to a dashboard. Varying tokens (`<*>`) in a pattern are highlighted so the stable shape can be read. diff --git a/.changeset/explore-query-editor-highlighting.md b/.changeset/explore-query-editor-highlighting.md new file mode 100644 index 0000000000..eaf97194c3 --- /dev/null +++ b/.changeset/explore-query-editor-highlighting.md @@ -0,0 +1,12 @@ +--- +'@hyperdx/app': patch +--- + +Fix missing SQL syntax highlighting in the Explore query editor. The app +declared `@codemirror/language` on a range no other CodeMirror package +resolved to, so two copies were installed and the highlighter never +recognised the tree the SQL parser produced. Align the range and the +highlighting comes back everywhere it was silently absent. + +The full-statement query editor now also carries line numbers and the shared +code background, so it reads as a code block rather than a tall text field. diff --git a/.changeset/explore-query-editor-ux.md b/.changeset/explore-query-editor-ux.md new file mode 100644 index 0000000000..977495c531 --- /dev/null +++ b/.changeset/explore-query-editor-ux.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +Explore's query editor uses Search and Raw SQL. Search is Lucene with inline filter pills; Raw SQL is the full-query escape hatch. diff --git a/.changeset/explore-query-row.md b/.changeset/explore-query-row.md new file mode 100644 index 0000000000..6970b5cab3 --- /dev/null +++ b/.changeset/explore-query-row.md @@ -0,0 +1,17 @@ +--- +'@hyperdx/app': patch +--- + +Explore's query bar is one row: the search field now sits beside the time range, +Live and Run instead of below them. A filter addon on the field's left edge +marks it as the thing that narrows results and opens the syntax reference, so +the row needs no separate help icon, and Add filter is icon-only beside it. +Explore's own "Show filters" button gives up the funnel it had borrowed and +matches the arrow it collapses with, as it already does on Search. + +The field is SQL-only — Lucene is still rendered for saved searches that use it, +but is no longer something you can author here. The raw query is now reached +through a "Query editor" code icon sitting outside the field, rather than a +button labelled "SQL" within it: the query is the larger thing, with the field +spliced into it wherever `$__filters` appears, and naming the surface instead of +its contents leaves room for sources whose query is not SQL. diff --git a/.changeset/explore-run-is-the-primary-action.md b/.changeset/explore-run-is-the-primary-action.md new file mode 100644 index 0000000000..a9d407ac14 --- /dev/null +++ b/.changeset/explore-run-is-the-primary-action.md @@ -0,0 +1,8 @@ +--- +'@hyperdx/app': patch +--- + +Run is now the primary button on Explore and Save view the secondary one. Run +previously dropped to secondary whenever the form was clean, which left the +toolbar with Save view as its only emphasised action — pointing at saving a +query rather than running one. diff --git a/.changeset/explore-search-and-sql.md b/.changeset/explore-search-and-sql.md new file mode 100644 index 0000000000..027c96fd32 --- /dev/null +++ b/.changeset/explore-search-and-sql.md @@ -0,0 +1,14 @@ +--- +'@hyperdx/app': patch +--- + +Let search and raw SQL work together on the Explore page instead of forcing a +choice between them. The `Search | Raw SQL` toggle is replaced by a `SQL` +button that folds a full statement editor out beneath the search box, closed by +default. While the query is generated it tracks the search above; your first +keystroke hands it over, the button marks it as edited, and "Reset to generated" +hands it back. The search box and filter pills keep applying either way — they +now compile into `$__filters`, and the panel warns if an edit drops that macro. + +Switching to SQL no longer discards your search, so the confirmation dialog that +used to copy the query to the clipboard before wiping it is gone. diff --git a/.changeset/explore-series-cards.md b/.changeset/explore-series-cards.md new file mode 100644 index 0000000000..74f5effa15 --- /dev/null +++ b/.changeset/explore-series-cards.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +Explore aggregated views now use multi-series cards (alias, per-series where, add/remove) instead of a single aggregation bar. The tile editor is unchanged. diff --git a/.changeset/explore-series-needs-a-column.md b/.changeset/explore-series-needs-a-column.md new file mode 100644 index 0000000000..489011423a --- /dev/null +++ b/.changeset/explore-series-needs-a-column.md @@ -0,0 +1,17 @@ +--- +'@hyperdx/app': patch +'@hyperdx/common-utils': patch +--- + +Picking an aggregation that needs a column no longer breaks the chart before +you can name one. Explore commits a series edit as you make it, so choosing +"99th percentile" used to query immediately with no column and fail on +`An incorrect number of arguments was specified for function 'toString'` — a +ClickHouse parser error that never mentions the column you were about to pick. +Explore now waits, and says which field it is waiting for. + +The query layer no longer accepts a blank aggregation column either. It treated +an empty expression as present and rendered `toFloat64OrDefault(toString())`; +it now reports the missing column, as it already did when the expression was +absent entirely. `count` is unaffected, being the one aggregation that takes no +argument. diff --git a/.changeset/explore-shared-group-by.md b/.changeset/explore-shared-group-by.md new file mode 100644 index 0000000000..f9c6c5d430 --- /dev/null +++ b/.changeset/explore-shared-group-by.md @@ -0,0 +1,22 @@ +--- +'@hyperdx/app': patch +--- + +Explore: group by is now one control in the toolbar, shared by the events and +chart views, instead of living inside the series panel. Grouping a chart by a +dimension and switching to events keeps the breakdown — the histogram restacks +on the same dimension rather than falling back to severity or status code — and +the control no longer moves when a second series is added. Hidden on Patterns, +which is already its own grouping, and on Number, which has nothing to split; +the value is kept and comes back on the way out. Traces with no status code +expression now default to grouping by service name in the histogram as well as +in charts, which previously disagreed. + +Picking a dimension no longer means writing SQL. The control is a field picker +shaped like the columns picker beside it — searchable, multi-select, and +including nested map keys such as `ResourceAttributes['host.name']` — with a +SQL tab for groupings a field list cannot express. Grouping by more than one +dimension already worked and still does; the picker just makes it visible. The +control spells out `Group by SeverityText` in a labelled field, built as the +same left-addon shape as the `As ` picker beside it, rather than +leaving an icon to carry the word "group". diff --git a/.changeset/explore-sql-only-series.md b/.changeset/explore-sql-only-series.md new file mode 100644 index 0000000000..1ebacdbe76 --- /dev/null +++ b/.changeset/explore-sql-only-series.md @@ -0,0 +1,10 @@ +--- +'@hyperdx/app': patch +--- + +Stop the Search page's Lucene/SQL choice from following you into Explore. +Adding a chart series on Explore seeded its condition from the cross-page +stored language preference, so switching the Search page to Lucene silently +changed what a new Explore series expected you to type. Explore now starts +every series, and every unset condition, in SQL. Series that explicitly +carry Lucene in a saved search or a shared link still render as Lucene. diff --git a/.changeset/explore-table-bar.md b/.changeset/explore-table-bar.md new file mode 100644 index 0000000000..042fbe2a6d --- /dev/null +++ b/.changeset/explore-table-bar.md @@ -0,0 +1,18 @@ +--- +'@hyperdx/app': patch +--- + +feat: attach a columns and sort bar to the Explore results table + +The Explore list view's table is framed with a bar along its top holding a +"Display" menu (lines per row, resetting column widths), the generated SQL and +CSV download actions, and the Columns and Sort controls. Columns now offers +map/JSON sub-keys alongside top-level columns. Columns and Sort no longer have +their own SQL tabs; a hand-written SELECT or ORDER BY goes in the query editor. + +The query editor toggle is now an "Advanced" switch in the top row, beside the +save actions, and Advanced is a mode: turning it on opens the SQL generated from the current view, and turning +it off returns to the search UI (asking first if the SQL was edited). While it +is on, Columns and Sort are disabled, and the Events view shows the SQL's rows +in the same table, with the row side panel where the selected columns allow. +"Reset to generated" is gone; turning Advanced off and on regenerates. diff --git a/.changeset/explore-visualize-as.md b/.changeset/explore-visualize-as.md new file mode 100644 index 0000000000..ad79060386 --- /dev/null +++ b/.changeset/explore-visualize-as.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': minor +--- + +Explore chart types are collapsed into a single Charts segment and an "As " menu beside it, so Events stays the primary results view. That menu appears only while a chart is on screen, since choosing a chart type next to a table of events changes nothing you can see. diff --git a/.changeset/metric-browse-detached-from-select.md b/.changeset/metric-browse-detached-from-select.md new file mode 100644 index 0000000000..1b37d9a8ac --- /dev/null +++ b/.changeset/metric-browse-detached-from-select.md @@ -0,0 +1,12 @@ +--- +'@hyperdx/app': patch +--- + +On Explore, "Browse metrics" is a labelled button beside the metric name, not +an icon glued to the select. The tile editor keeps the icon button and can +still stage tag filters from the explorer. + +Explore's explorer no longer stages WHERE clauses: filtering stays in the +attribute panel under the series. Applying a different metric still clears the +series filter, and the explorer says so before you commit — along with a +warning when a staged group by would replace the chart's existing one. diff --git a/.changeset/metric-browse-wall.md b/.changeset/metric-browse-wall.md new file mode 100644 index 0000000000..36f0cd45c5 --- /dev/null +++ b/.changeset/metric-browse-wall.md @@ -0,0 +1,19 @@ +--- +'@hyperdx/app': minor +--- + +feat: browse-first metric explorer in Explore + +Metric sources now open on a "Browse" view: a wall of every metric the source +reports, each already charted with a default aggregation for its kind (avg for +gauges, increase for sums, p95 for histograms). Metrics are grouped by the +service or host that reports them and banded by what they measure (latency, +errors and counts, throughput, saturation, queue depth), classified from the +declared unit, then the name's tail, then the kind; the rest are shown as +Unclassified. + +A rail and one search box narrow the wall by quantity, attribute, attribute +value, kind and unit. Clicking a tile opens it in place with the other +aggregations for its kind, split-by chips ranked by cardinality, and a choice +of one chart or small multiples on a shared scale. "Edit as chart" carries the +same query into the time series view. diff --git a/.changeset/metric-name-browse-addon.md b/.changeset/metric-name-browse-addon.md new file mode 100644 index 0000000000..b91b8bbf6e --- /dev/null +++ b/.changeset/metric-name-browse-addon.md @@ -0,0 +1,8 @@ +--- +'@hyperdx/app': patch +--- + +Metrics: the "browse metrics" control now sits inside the metric name field +instead of floating beside it, matching the query bar where the filter action is +part of the field frame. The two read as one control rather than a select and an +unrelated icon button. diff --git a/.changeset/one-sided-filter-range.md b/.changeset/one-sided-filter-range.md new file mode 100644 index 0000000000..5b87ef1fde --- /dev/null +++ b/.changeset/one-sided-filter-range.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/common-utils': patch +--- + +Pinned numeric filters can be one-sided comparisons (`Duration > 1s`, `status >= 500`) instead of only two-sided BETWEEN ranges. diff --git a/.changeset/row-panel-group-by-and-width.md b/.changeset/row-panel-group-by-and-width.md new file mode 100644 index 0000000000..d6281085ab --- /dev/null +++ b/.changeset/row-panel-group-by-and-width.md @@ -0,0 +1,17 @@ +--- +'@hyperdx/app': patch +--- + +Event side panel: group by a field without leaving the panel. Hovering a row in +Column Values already offered "add to filters", which narrows to that one value; +the new action is the opposite move — the breakdown across every value of the +field — and toggles the same shared grouping the Explore toolbar drives, so the +two stay in step. It uses the same field expression as the column toggle, so a +map key groups as `ResourceAttributes['host.name']` rather than a second +spelling of the same field. Timestamps are skipped, since grouping on one gives +a group per row. Explore only, and hidden on cross-source rows, where there is +no grouping to change. + +On Explore, the panel opens at 60% of the viewport instead of 80% and remembers +a width you set. Search, dashboards, and the session player keep the 80% default +and do not persist it. diff --git a/.changeset/series-column-field-picker.md b/.changeset/series-column-field-picker.md new file mode 100644 index 0000000000..14d20b8bfa --- /dev/null +++ b/.changeset/series-column-field-picker.md @@ -0,0 +1,20 @@ +--- +'@hyperdx/app': patch +--- + +On Explore, pick the column an aggregation reduces over from a list, rather +than typing it into a box labelled "SQL column". The series row now reads +`99th Percentile of Duration`, built as the same labelled field as the +`Group by` and `As ` pickers beside it, with a SQL tab still one +click away for expressions a list cannot hold like `Duration / 1e6`. + +For aggregations the query layer coerces with `toFloat64OrDefault` — sum, +average, min, max, percentiles, increase — only numeric fields are offered. +That wrapper turns a non-numeric column into 0 rather than failing, so +averaging `ServiceName` used to buy a chart of zeroes with nothing to say it +had gone wrong. `Count Distinct` and `Any` pass the column through untouched +and still list every field, and `Custom` keeps its expression editor. + +On Explore, the popover behind Group by, event patterns, and the series column +is one component. Search keeps the SQL pattern expression field, and the tile +editor keeps its SQL column input. diff --git a/agent_docs/code_style.md b/agent_docs/code_style.md index cc4b5ff14c..02ccbbc8c1 100644 --- a/agent_docs/code_style.md +++ b/agent_docs/code_style.md @@ -117,7 +117,8 @@ The project uses Mantine UI with **custom variants** defined in `packages/app/sr |---------|----------|---------| | `variant="primary"` | Primary actions (Submit, Save, Create, Run) | `` | | `variant="secondary"` | Secondary actions (Cancel, Clear, auxiliary actions) | `` | -| `variant="danger"` | Destructive actions (Delete, Remove, Rotate API Key) | `` | +| `variant="danger"` | Destructive or error-severity actions (Delete, Remove, Error filter) | `` | +| `variant="warning"` | Caution or warning-severity actions (Warning filter, Slow spans) | `` | | `variant="link"` | Link-style actions with no background or border (View Details, navigation-style CTAs) | `` | | `variant="subtle"` | Transparent background with hover highlight; for toolbar/utility controls that shouldn't draw attention until hovered (collapse toggles, close buttons, auxiliary actions) | `` | @@ -127,11 +128,13 @@ The project uses Mantine UI with **custom variants** defined in `packages/app/sr + ... ... ... +... ... ... ``` diff --git a/knip.json b/knip.json index 7d88c3d288..b91e420530 100644 --- a/knip.json +++ b/knip.json @@ -8,6 +8,9 @@ "entry": ["pages/**/*.{ts,tsx}", "scripts/*.js", "tests/e2e/**/*.ts"], "project": ["src/**/*.{ts,tsx}", "pages/**/*.{ts,tsx}"], "ignore": [".storybook/public/**"], + "ignoreIssues": { + "src/components/Explore/owned/**": ["exports", "types", "nsExports"] + }, "ignoreDependencies": [ "eslint-config-next", "@chromatic-com/storybook", diff --git a/packages/api/src/routers/external-api/v2/utils/dashboards.ts b/packages/api/src/routers/external-api/v2/utils/dashboards.ts index 8ffa9564b3..c45e2f8b8a 100644 --- a/packages/api/src/routers/external-api/v2/utils/dashboards.ts +++ b/packages/api/src/routers/external-api/v2/utils/dashboards.ts @@ -307,6 +307,7 @@ const convertPromqlConfigToExternal = ( case DisplayType.Search: case DisplayType.Markdown: case DisplayType.EventPatterns: + case DisplayType.Treemap: // The editor never saves these for PromQL. Emitting a line chart keeps // the tile a PromQL tile on a GET -> PUT round-trip. logger.error( @@ -415,10 +416,13 @@ export const convertToExternalTileChartConfig = ( sourceId: config.source, numberFormat: config.numberFormat, }; + // Treemap is not in the external dashboard schema yet; drop it + // rather than emitting an undocumented displayType. case DisplayType.Search: case DisplayType.Markdown: case DisplayType.Heatmap: case DisplayType.EventPatterns: + case DisplayType.Treemap: logger.error( { config }, 'Error converting chart config to external chart - unsupported display type for raw SQL config', @@ -673,6 +677,13 @@ export const convertToExternalTileChartConfig = ( where: stringValueOrDefault(config.where, ''), whereLanguage: config.whereLanguage ?? 'lucene', }; + case DisplayType.Treemap: + // Treemap is not in the external dashboard schema yet. + logger.error( + { config }, + 'Error converting chart config to external chart - unsupported display type', + ); + return undefined; case undefined: logger.error( { config }, diff --git a/packages/app/eslint.config.mjs b/packages/app/eslint.config.mjs index dc9b5188bb..1273371963 100644 --- a/packages/app/eslint.config.mjs +++ b/packages/app/eslint.config.mjs @@ -27,43 +27,43 @@ const UI_SYNTAX_RESTRICTIONS = [ selector: 'JSXElement[openingElement.name.name="Button"] JSXAttribute[name.name="variant"][value.value="light"]', message: - 'Use variant="primary", "secondary", or "danger" for Button. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for Button. See agent_docs/code_style.md', }, { selector: 'JSXElement[openingElement.name.name="Button"] JSXAttribute[name.name="variant"][value.value="filled"]', message: - 'Use variant="primary", "secondary", or "danger" for Button. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for Button. See agent_docs/code_style.md', }, { selector: 'JSXElement[openingElement.name.name="Button"] JSXAttribute[name.name="variant"][value.value="outline"]', message: - 'Use variant="primary", "secondary", or "danger" for Button. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for Button. See agent_docs/code_style.md', }, { selector: 'JSXElement[openingElement.name.name="Button"] JSXAttribute[name.name="variant"][value.value="default"]', message: - 'Use variant="primary", "secondary", or "danger" for Button. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for Button. See agent_docs/code_style.md', }, { selector: 'JSXElement[openingElement.name.name="ActionIcon"] JSXAttribute[name.name="variant"][value.value="light"]', message: - 'Use variant="primary", "secondary", or "danger" for ActionIcon. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for ActionIcon. See agent_docs/code_style.md', }, { selector: 'JSXElement[openingElement.name.name="ActionIcon"] JSXAttribute[name.name="variant"][value.value="filled"]', message: - 'Use variant="primary", "secondary", or "danger" for ActionIcon. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for ActionIcon. See agent_docs/code_style.md', }, { selector: 'JSXElement[openingElement.name.name="ActionIcon"] JSXAttribute[name.name="variant"][value.value="outline"]', message: - 'Use variant="primary", "secondary", or "danger" for ActionIcon. See agent_docs/code_style.md', + 'Use variant="primary", "secondary", "danger", "warning", "link", or "subtle" for ActionIcon. See agent_docs/code_style.md', }, ]; diff --git a/packages/app/package.json b/packages/app/package.json index 78c88a0eb2..c43ff410ef 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -14,7 +14,7 @@ "build:clickhouse": "NEXT_PUBLIC_THEME=clickstack NEXT_PUBLIC_IS_LOCAL_MODE=true NEXT_PUBLIC_CLICKHOUSE_BUILD=true next build --webpack && node scripts/prepare-clickhouse-build-export.js", "run:clickhouse": "test -d out && npx rimraf tmp && mkdir tmp && cp -r out tmp/clickstack && echo 'visit http://localhost:3000/clickstack to start' && npx serve tmp -l 3000 || echo 'run build:clickhouse first'", "start": "next start", - "lint": "npx eslint . --ext .ts,.tsx --max-warnings 568", + "lint": "npx eslint . --ext .ts,.tsx --max-warnings 600", "lint:fix": "npx eslint . --ext .ts,.tsx --fix", "lint:styles": "stylelint **/*/*.{css,scss}", "ci:lint": "yarn lint && yarn tsc --noEmit && yarn lint:styles --quiet", diff --git a/packages/app/pages/explore/[savedSearchId].tsx b/packages/app/pages/explore/[savedSearchId].tsx new file mode 100644 index 0000000000..82af56570f --- /dev/null +++ b/packages/app/pages/explore/[savedSearchId].tsx @@ -0,0 +1,3 @@ +import ExplorePage from '@/DBExplorePage'; + +export default ExplorePage; diff --git a/packages/app/pages/explore/index.tsx b/packages/app/pages/explore/index.tsx new file mode 100644 index 0000000000..82af56570f --- /dev/null +++ b/packages/app/pages/explore/index.tsx @@ -0,0 +1,3 @@ +import ExplorePage from '@/DBExplorePage'; + +export default ExplorePage; diff --git a/packages/app/src/DBExplorePage.tsx b/packages/app/src/DBExplorePage.tsx new file mode 100644 index 0000000000..f79ee8b4af --- /dev/null +++ b/packages/app/src/DBExplorePage.tsx @@ -0,0 +1,3865 @@ +import { + FormEvent, + FormEventHandler, + Fragment, + memo, + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; +import dynamic from 'next/dynamic'; +import Head from 'next/head'; +import router from 'next/router'; +import { + parseAsBoolean, + parseAsInteger, + parseAsString, + parseAsStringEnum, + useQueryState, + useQueryStates, +} from 'nuqs'; +import { useForm, useWatch } from 'react-hook-form'; +import { z } from 'zod'; +import { zodResolver } from '@hookform/resolvers/zod'; +import HyperDX from '@hyperdx/browser'; +import { + ClickHouseQueryError, + ColumnMeta, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { renderBuilderConfigAsSqlTemplate } from '@hyperdx/common-utils/dist/core/builderToRawSql'; +import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; +import { buildSearchChartConfig } from '@hyperdx/common-utils/dist/core/searchChartConfig'; +import { + aliasMapToWithClauses, + isBrowser, + splitAndTrimWithBracket, +} from '@hyperdx/common-utils/dist/core/utils'; +import { + BuilderChartConfigWithDateRange, + ChartConfigWithDateRange, + DisplayType, + Filter, + isLogSource, + isMetricSource as isMetricSourceGuard, + isTraceSource, + RawSqlChartConfig, + RawSqlSavedChartConfig, + SavedChartConfig, + SourceKind, + TMetricSource, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { + ActionIcon, + Box, + Button, + Card, + Code, + Flex, + Grid, + Group, + Menu, + Modal, + Paper, + Stack, + Text, + Tooltip, +} from '@mantine/core'; +import { + useDebouncedCallback, + useDisclosure, + useDocumentVisibility, +} from '@mantine/hooks'; +import { notifications } from '@mantine/notifications'; +import { + IconBolt, + IconCheck, + IconChevronDown, + IconClock, + IconCode, + IconDotsVertical, + IconDownload, + IconLayoutGridAdd, + IconLayoutSidebarLeftExpand, + IconPlayerPlay, + IconPlus, + IconStack2, + IconTags, + IconX, +} from '@tabler/icons-react'; +import { keepPreviousData, useIsFetching } from '@tanstack/react-query'; +import { SortingState } from '@tanstack/react-table'; +import CodeMirror from '@uiw/react-codemirror'; + +import { ContactSupportText } from '@/components/ContactSupportText'; +import { cleanClickHouseExpression } from '@/components/DBSearchPageFilters/utils'; +import EmptyState from '@/components/EmptyState'; +import { ErrorBoundary } from '@/components/Error/ErrorBoundary'; +import { ChartConfigDisplaySettings } from '@/components/Explore/owned/ChartDisplaySettingsDrawer'; +import { DBSearchPageFilters } from '@/components/Explore/owned/DBSearchPageFilters'; +import DBSqlRowTableWithSideBar from '@/components/Explore/owned/DBSqlRowTableWithSidebar'; +import { + DBTimeChart, + type SeriesGroupFilter, +} from '@/components/Explore/owned/DBTimeChart'; +import PatternTable from '@/components/Explore/owned/PatternTable'; +import { useSearchPageFilterState } from '@/components/Explore/owned/searchFilters'; +import { + getDefaultExploreLanguage, + hasUneditableLuceneWhere, + toSqlWhere, +} from '@/components/Explore/queryModeSafety'; +import { FavoriteButton } from '@/components/FavoriteButton'; +import { InputControlled } from '@/components/InputControlled'; +import OnboardingModal from '@/components/OnboardingModal'; +import { SavedSearchesFlyout } from '@/components/SavedSearches/SavedSearchesFlyout'; +import SaveToDashboardModal from '@/components/SaveToDashboardModal'; +import { useSearchTotalCount } from '@/components/SearchTotalCountChart'; +import { TableSourceForm } from '@/components/Sources/SourceForm'; +import { SourceSelectControlled } from '@/components/SourceSelect'; +import { Tags } from '@/components/Tags'; +import { TimePicker } from '@/components/TimePicker'; +import { IS_LOCAL_MODE } from '@/config'; +import { useAliasMapFromChartConfig } from '@/hooks/useChartConfig'; +import { useExplainQuery } from '@/hooks/useExplainQuery'; +import { withAppNav } from '@/layout'; +import { + useCreateSavedSearch, + useDeleteSavedSearch, + useSavedSearch, + useUpdateSavedSearch, +} from '@/savedSearch'; +import { getEventBody, useSource, useSources } from '@/source'; +import { useBrandDisplayName } from '@/theme/ThemeProvider'; +import { parseRelativeTimeQuery, useNewTimeQuery } from '@/timeQuery'; +import { formatDurationMs, useLocalStorage, usePrevious } from '@/utils'; +import { metricTableConnections } from '@/utils/metricTableConnections'; + +import ChartSQLPreview, { SQLPreview } from './components/ChartSQLPreview'; +import { DBBarChart } from './components/DBBarChart'; +import DBNumberChart from './components/DBNumberChart'; +import { DBPieChart } from './components/DBPieChart'; +import DBTableChart from './components/DBTableChart'; +import { DBTreemapChart } from './components/DBTreemapChart'; +import { ExploreChartSettings } from './components/Explore/ExploreChartSettings'; +import { ExploreContextBand } from './components/Explore/ExploreContextBand'; +import { + defaultExploreGroupBy, + formatGroupByFields, + parseGroupByFields, + resolveExploreGroupBy, +} from './components/Explore/exploreGroupBy'; +import { ExploreGroupByControl } from './components/Explore/ExploreGroupByControl'; +import { PatternColumnSelector } from './components/Explore/ExplorePatternColumnSelector'; +import { ExploreQueryEditor } from './components/Explore/ExploreQueryEditor'; +import { ExploreResultsToolbar } from './components/Explore/ExploreResultsToolbar'; +import { ExploreSeriesList } from './components/Explore/ExploreSeriesList'; +import { ExploreSqlToggle } from './components/Explore/ExploreSqlToggle'; +import { type QueryConfigMode } from './components/Explore/QueryEditor'; +import { SeveritySummary } from './components/Explore/SeveritySummary'; +import { + aggConfigFromWall, + type EditAsChartRequest, +} from './components/MetricWall/editAsChart'; +import { + MetricBrowse, + MetricBrowseRail, +} from './components/MetricWall/MetricBrowse'; +import { nextSearchForPatternMatch } from './components/Patterns/patternColumn'; +import { DBSearchHeatmapChart } from './components/Search/DBSearchHeatmapChart'; +import DirectTraceSidePanel from './components/Search/DirectTraceSidePanel'; +import { exploreViewSupportsFormulas } from './components/Search/exploreFormulas'; +import { + type AggSortField, + exploreSeriesHaveMetricNames, + exploreSeriesHaveValueExpressions, + seriesAggCondition, + useSearchAggConfig, +} from './components/Search/SearchAggControls'; +import { SearchColumnPicker } from './components/Search/SearchColumnPicker'; +import { SearchSortMenu } from './components/Search/SearchSortMenu'; +import { + isAggregatedSearchView, + isSqlModeSearchView, + SearchView, + SearchViewSwitcher, + searchViewToDisplayType, + useSearchView, + viewShowsHistogram, +} from './components/Search/searchViews'; +import SourceSchemaPreview, { + isSourceSchemaPreviewEnabled, +} from './components/SourceSchemaPreview'; +import { + getRelativeTimeOptionLabel, + LIVE_TAIL_DURATION_MS, + parseTimeRangeInput, +} from './components/TimePicker/utils'; +import { + useColumns, + useMetadataWithSettings, + useResolvedDateTimeColumns, + useTableMetadata, +} from './hooks/useMetadata'; +import { useSqlSuggestions } from './hooks/useSqlSuggestions'; +import { useStableCallback } from './hooks/useStableCallback'; +import { + buildDirectTraceWhereClause, + getDefaultDirectTraceDateRange, +} from './utils/directTrace'; +import { + parseAsJsonEncoded, + parseAsSortingStateString, + parseAsStringEncoded, +} from './utils/queryParsers'; +import { getPreviousDateRange } from './ChartUtils'; +import { LOCAL_STORE_CONNECTIONS_KEY } from './connection'; +import { DBSearchPageAlertModal } from './DBSearchPageAlertModal'; +import { SearchConfig } from './types'; +import { useConfirm } from './useConfirm'; + +import searchPageStyles from '@styles/SearchPage.module.scss'; + +const LIVE_TAIL_REFRESH_FREQUENCY_OPTIONS = [ + { value: '1000', label: '1s' }, + { value: '2000', label: '2s' }, + { value: '4000', label: '4s' }, + { value: '10000', label: '10s' }, + { value: '30000', label: '30s' }, +]; +const DEFAULT_REFRESH_FREQUENCY = 10000; + +const ALLOWED_SOURCE_KINDS = [ + SourceKind.Log, + SourceKind.Trace, + SourceKind.Metric, +]; +const SearchConfigSchema = z.object({ + select: z.string(), + source: z.string(), + where: z.string(), + whereLanguage: z.enum(['sql', 'lucene']), + // Query mode: 'builder' is Search (Lucene WHERE + pills); 'sql' is Raw SQL. + configType: z.enum(['builder', 'sql']), + sqlTemplate: z.string(), + orderBy: z.string(), + filters: z.array( + z.union([ + z.object({ + type: z.literal('sql_ast'), + operator: z.enum(['=', '<', '>', '>=', '<=', '!=']), + left: z.string(), + right: z.string(), + }), + z.object({ + type: z.enum(['sql', 'lucene']), + condition: z.string(), + }), + ]), + ), +}); + +type SearchConfigFromSchema = z.infer; + +const QUERY_KEY_PREFIX = 'search'; + +// Clicks inside the results panel keep the row side panel open (so users can +// scroll the table or select a different row); clicks anywhere else on the page +// dismiss it. +const SEARCH_RESULTS_PANEL_KEEP_OPEN_SELECTOR = + '[data-testid="search-results-panel"]'; + +// Helper function to get the default source id +function getDefaultSourceId( + sources: { id: string; disabled?: boolean }[] | undefined, + lastSelectedSourceId: string | undefined, +): string { + if (!sources || sources.length === 0) return ''; + + // Filter out disabled sources + const enabledSources = sources.filter(s => !s.disabled); + if (enabledSources.length === 0) return ''; + + if ( + lastSelectedSourceId && + enabledSources.some(s => s.id === lastSelectedSourceId) + ) { + return lastSelectedSourceId; + } + return enabledSources[0].id; +} + +function SourceEditModal({ + opened, + onClose, + inputSource, +}: { + opened: boolean; + onClose: () => void; + inputSource: string | undefined; +}) { + return ( + + + + ); +} + +function NewSourceModal({ + opened, + onClose, + onCreate, +}: { + opened: boolean; + onClose: () => void; + onCreate: (source: TSource) => void; +}) { + return ( + + + + ); +} + +/** + * Grafana-style live-tail control rendered inside the datetime input. The bolt + * starts/stops streaming; when live, an attached caret opens a menu to pick the + * auto-refresh cadence (which also resumes live if it was paused). + */ +function SearchLiveControl({ + isLive, + refreshFrequency, + onToggle, + onSelectCadence, +}: { + isLive: boolean; + refreshFrequency: number; + onToggle: () => void; + onSelectCadence: (ms: number) => void; +}) { + const cadenceLabel = + LIVE_TAIL_REFRESH_FREQUENCY_OPTIONS.find( + o => o.value === String(refreshFrequency), + )?.label ?? `${Math.round(refreshFrequency / 1000)}s`; + + // Active state is signalled with brand-colored content (matching the "Live + // Tail" treatment in the datetime input) rather than a fill, so both segments + // stay the same variant and read as one connected control. + const activeContentStyles = isLive + ? { + label: { color: 'var(--color-text-brand)' }, + section: { color: 'var(--color-text-brand)' }, + } + : undefined; + + return ( + + + + + {isLive && ( + + + + + + Refresh every + {LIVE_TAIL_REFRESH_FREQUENCY_OPTIONS.map(o => ( + onSelectCadence(parseInt(o.value, 10))} + rightSection={ + String(refreshFrequency) === o.value ? ( + + ) : null + } + > + {o.label} + + ))} + + + )} + + ); +} + +function SearchRunControl() { + return ( + + ); +} + +function ExpandFiltersButton({ onExpand }: { onExpand: () => void }) { + return ( + + + + + + ); +} + +// Abbreviate large scanned-row counts (1,157,950 -> "1.16M") to keep the stats +// line short. The headline result count stays fully grouped for precision. +const compactNumberFormatter = new Intl.NumberFormat('en-US', { + notation: 'compact', + maximumFractionDigits: 2, +}); + +// Single compact stats line for the results band: " results · +// scanned · ". Folds the result count (count query), +// scanned rows (EXPLAIN) and elapsed time into one dimmed line with the count +// emphasized, instead of three labeled fragments. +function ResultsStats({ + countConfig, + explainConfig, + enabled, + searchElapsedMs, + isSearching, + isLiveTail = false, +}: { + countConfig: BuilderChartConfigWithDateRange; + explainConfig: ChartConfigWithDateRange; + enabled: boolean; + searchElapsedMs: number | null; + isSearching: boolean; + isLiveTail?: boolean; +}) { + const { + totalCount, + isLoading: isCountLoading, + isError: isCountError, + } = useSearchTotalCount(countConfig, QUERY_KEY_PREFIX, { + enableParallelQueries: true, + }); + + const { data, error: explainError } = useExplainQuery(explainConfig, { + enabled, + // Keep the previous row count on screen while a new EXPLAIN runs so the + // scanned-rows value doesn't flash on every live-tail poll (each poll + // changes the dateRange, and thus the query key). + placeholderData: keepPreviousData, + }); + + if (!enabled) { + return null; + } + + const numRows = data?.[0]?.rows; + const scanned = + !explainError && numRows != null + ? compactNumberFormatter.format(Number(numRows)) + : null; + + // During live tail we keep showing the last measured elapsed time and never + // flash the loading state, so the value doesn't flicker between polls. + const showElapsedLoading = isSearching && !isLiveTail; + const showElapsed = showElapsedLoading || searchElapsedMs != null; + + const separator = ( + + · + + ); + + return ( + + + + {isCountLoading ? ( + ··· + ) : totalCount != null && !isCountError ? ( + totalCount.toLocaleString('en-US') + ) : ( + '0' + )} + {' '} + results + + {scanned != null && ( + <> + {separator} + + {scanned} scanned + + + )} + {showElapsed && ( + <> + {separator} + + {showElapsedLoading ? ( + ··· + ) : ( + formatDurationMs(searchElapsedMs!) + )} + + + )} + + ); +} + +// Overflow (3-dots) menu for secondary results actions. Holds "Show generated +// SQL" (opens a modal for the current results/timeline config) and "Export". +// Kept out of the main controls row so it stays uncluttered. +function ResultsOverflowMenu({ + config, + sqlConfig, + showGeneratedSql = true, +}: { + config: ChartConfigWithDateRange; + sqlConfig?: ChartConfigWithDateRange; + showGeneratedSql?: boolean; +}) { + const [opened, { open, close }] = useDisclosure(false); + return ( + <> + + + + + + + + + {showGeneratedSql && ( + } + onClick={open} + data-testid="generated-sql-button" + > + Show generated SQL + + )} + } + disabled + data-testid="export-button" + > + Export (coming soon) + + + + + ); +} + +// "Add to dashboard" action for chart-tile views. Opens the shared +// SaveToDashboardModal with the current aggregation rendered as a tile config. +function AddToDashboardButton({ config }: { config: SavedChartConfig }) { + const [opened, { open, close }] = useDisclosure(false); + return ( + <> + + + + ); +} + +function SaveSearchModalComponent({ + searchedConfig, + opened, + onClose, + isUpdate, + savedSearchId, +}: { + searchedConfig: SearchConfig; + opened: boolean; + onClose: () => void; + isUpdate: boolean; + savedSearchId: string | undefined | null; +}) { + const { data: savedSearch } = useSavedSearch( + { id: savedSearchId ?? '' }, + { + enabled: savedSearchId != null, + }, + ); + + const { + control, + handleSubmit, + formState, + reset: resetForm, + } = useForm({ + ...(isUpdate + ? { + values: { + name: savedSearch?.name ?? '', + }, + } + : {}), + resetOptions: { + keepDirtyValues: true, + keepErrors: true, + }, + }); + + const closeAndReset = () => { + resetForm(); + onClose(); + }; + + const isValidName = (name?: string): boolean => + Boolean(name && name.trim().length > 0); + const [tags, setTags] = useState(savedSearch?.tags || []); + + // Update tags when savedSearch changes + useEffect(() => { + if (savedSearch?.tags) { + setTags(savedSearch.tags); + } + }, [savedSearch]); + const createSavedSearch = useCreateSavedSearch(); + const updateSavedSearch = useUpdateSavedSearch(); + + const { data: sourceObj } = useSource({ + id: searchedConfig.source, + kinds: [SourceKind.Log, SourceKind.Trace], + }); + const effectiveSelect = + searchedConfig.select || sourceObj?.defaultTableSelectExpression || ''; + + const onSubmit = (e: FormEvent) => { + e.preventDefault(); + + handleSubmit(async ({ name }) => { + if (isUpdate) { + if (savedSearchId == null) { + throw new Error('savedSearchId is required for update'); + } + + updateSavedSearch.mutate( + { + id: savedSearchId, + name, + select: effectiveSelect, + where: searchedConfig.where ?? '', + whereLanguage: + searchedConfig.whereLanguage ?? getDefaultExploreLanguage(), + source: searchedConfig.source ?? '', + orderBy: searchedConfig.orderBy ?? '', + filters: searchedConfig.filters ?? [], + tags: tags, + }, + { + onSuccess: () => { + onClose(); + }, + onError: error => { + console.error('Error updating saved search:', error); + notifications.show({ + color: 'red', + title: 'Error', + message: + 'An error occurred while updating your saved search. Please try again.', + }); + }, + }, + ); + } else { + try { + const savedSearch = await createSavedSearch.mutateAsync({ + name, + select: effectiveSelect, + where: searchedConfig.where ?? '', + whereLanguage: + searchedConfig.whereLanguage ?? getDefaultExploreLanguage(), + source: searchedConfig.source ?? '', + orderBy: searchedConfig.orderBy ?? '', + filters: searchedConfig.filters ?? [], + tags: tags, + }); + + router.push(`/explore/${savedSearch.id}${window.location.search}`); + onClose(); + } catch (error) { + console.error('Error creating saved search:', error); + notifications.show({ + color: 'red', + title: 'Error', + message: + 'An error occurred while saving your search. Please try again.', + }); + } + } + })(); + }; + + const isPending = createSavedSearch.isPending || updateSavedSearch.isPending; + + const { data: chartConfig } = useSearchedConfigToChartConfig(searchedConfig); + + return ( + +
+ + {chartConfig != null ? ( + + + SELECT + + {`${chartConfig.select}`} + + FROM + + + {chartConfig?.from.databaseName}.{chartConfig?.from.tableName} + + + WHERE + + {chartConfig.where ? ( + {chartConfig.where} + ) : ( + + None + + )} + + ORDER BY + + {`${chartConfig.orderBy ?? ''}`} + {searchedConfig.filters && searchedConfig.filters.length > 0 && ( + <> + + FILTERS + + + {searchedConfig.filters.map((filter, idx) => ( + + {filter.type === 'sql_ast' + ? `${filter.left} ${filter.operator} ${filter.right}` + : filter.condition} + + ))} + + + )} + + ) : ( + Loading Chart Config... + )} + + + Name + + + + + + Tags + + + {tags.map(tag => ( + + ))} + + + + + + + +
+
+ ); +} +const SaveSearchModal = memo(SaveSearchModalComponent); + +// TODO: This is a hack to set the default time range +const defaultTimeRange = parseTimeRangeInput('Past 15m') as [Date, Date]; + +function useLiveUpdate({ + isLive, + interval, // ms ago to refresh from + refreshFrequency, // ms, how often to refresh + onTimeRangeSelect, + pause, +}: { + isLive: boolean; + interval: number; + refreshFrequency: number; + onTimeRangeSelect: ( + start: Date, + end: Date, + displayedTimeInputValue?: string | null, + ) => void; + pause: boolean; +}) { + const documentState = useDocumentVisibility(); + const isDocumentVisible = documentState === 'visible'; + const [refreshOnVisible, setRefreshOnVisible] = useState(false); + + const refresh = useCallback(() => { + // eslint-disable-next-line no-restricted-syntax + onTimeRangeSelect(new Date(Date.now() - interval), new Date(), null); + }, [onTimeRangeSelect, interval]); + + // When the user comes back to the app after switching tabs, we immediately refresh the list. + useEffect(() => { + if (refreshOnVisible && isDocumentVisible) { + if (!pause) { + refresh(); + } + setRefreshOnVisible(false); + } + }, [refreshOnVisible, isDocumentVisible, pause, refresh]); + + const intervalRef = useRef(null); + useEffect(() => { + if (isLive) { + if (intervalRef.current) { + window.clearInterval(intervalRef.current); + } + + // only start interval if no queries are fetching + if (!pause) { + intervalRef.current = window.setInterval(() => { + if (isDocumentVisible) { + refresh(); + } else { + setRefreshOnVisible(true); + } + }, refreshFrequency); + } + } else { + if (intervalRef.current) { + window.clearInterval(intervalRef.current); + } + } + return () => { + if (intervalRef.current) { + window.clearInterval(intervalRef.current); + } + }; + }, [ + isLive, + isDocumentVisible, + onTimeRangeSelect, + pause, + refresh, + refreshFrequency, + ]); +} + +/** + * Takes in a input search config (user edited search config) and a default search config (saved search or source default config) + * and returns a chart config. + */ +function useSearchedConfigToChartConfig( + { select, source, whereLanguage, where, filters, orderBy }: SearchConfig, + defaultSearchConfig?: Partial, +) { + const { data: sourceObj, isLoading } = useSource({ + id: source, + kinds: [SourceKind.Log, SourceKind.Trace, SourceKind.Metric], + }); + const defaultOrderBy = useDefaultOrderBy(source); + + return useMemo(() => { + if (sourceObj != null) { + const resolvedOrderBy = + orderBy || defaultSearchConfig?.orderBy || defaultOrderBy; + + const chartConfig = buildSearchChartConfig(sourceObj, { + where, + whereLanguage, + filters, + select: select || defaultSearchConfig?.select || null, + displayType: DisplayType.Search, + ...(resolvedOrderBy != null ? { orderBy: resolvedOrderBy } : {}), + }); + + return { + data: chartConfig, + }; + } + + return { data: null, isLoading }; + }, [ + sourceObj, + isLoading, + select, + filters, + defaultSearchConfig, + where, + whereLanguage, + defaultOrderBy, + orderBy, + ]); +} + +const implicitDateTimePrefixes = [ + 'toStartOf', + 'toUnixTimestamp', + 'toDateTime', + 'Timestamp', +] as const; + +function optimizeDefaultOrderBy( + timestampExpr: string, + displayedTimestampExpr: string | undefined, + sortingKey: string | undefined, +) { + const orderByArr: string[] = []; + + const timestampExprParts = splitAndTrimWithBracket(timestampExpr); + const keys = splitAndTrimWithBracket(sortingKey ?? ''); + keys.push(...timestampExprParts); + if (displayedTimestampExpr) { + keys.push(displayedTimestampExpr.trim()); + } + for (const key of keys) { + if ( + !orderByArr.includes(key) && + (implicitDateTimePrefixes.some(v => key.startsWith(v)) || + timestampExprParts.includes(key) || + displayedTimestampExpr?.trim() === key) + ) { + orderByArr.push(key); + } + } + + return orderByArr.length > 1 + ? `(${orderByArr.join(', ')}) DESC` + : `${orderByArr[0]} DESC`; +} + +function useDefaultOrderBy(sourceID: string | undefined | null) { + const { data: source } = useSource({ + id: sourceID, + kinds: [SourceKind.Log, SourceKind.Trace], + }); + const { data: tableMetadata } = useTableMetadata(tcFromSource(source)); + + // When source changes, make sure select and orderby fields are set to default + return useMemo(() => { + // If no source, return undefined so that the orderBy is not set incorrectly + if (!source) return undefined; + const trimmedOrderBy = source.orderByExpression?.trim(); + if (trimmedOrderBy) return trimmedOrderBy; + return optimizeDefaultOrderBy( + source?.timestampValueExpression ?? '', + source.displayedTimestampValueExpression, + tableMetadata?.sorting_key, + ); + }, [source, tableMetadata]); +} + +const LUCENE_WHERE_DROPPED_MESSAGE = + 'This search was written in Lucene. Explore filters in SQL, so the query was cleared — the Search page still opens it as saved.'; + +function formatDroppedFiltersMessage(count: number): string { + const noun = count === 1 ? 'filter' : 'filters'; + const verb = count === 1 ? 'was' : 'were'; + return `${count} ${noun} didn't apply to this source and ${verb} removed.`; +} + +// This is outside as it needs to be a stable reference +const queryStateMap = { + source: parseAsString, + where: parseAsStringEncoded, + select: parseAsStringEncoded, + whereLanguage: parseAsStringEnum<'sql' | 'lucene'>(['sql', 'lucene']), + configType: parseAsStringEnum<'builder' | 'sql'>(['builder', 'sql']), + sqlTemplate: parseAsStringEncoded, + filters: parseAsJsonEncoded(), + orderBy: parseAsStringEncoded, +}; + +function useSearchTelemetry({ + isAnyQueryFetching, + isLive, + sourceId, +}: { + isAnyQueryFetching: boolean; + /** When true the hook suppresses recording and emission so live-tail + * background refetches do not flood the metric. */ + isLive: boolean; + sourceId: string | null; +}) { + const searchStartTimeRef = useRef(null); + const wasFetchingRef = useRef(false); + // Whether the in-flight cycle began as a live-tail refresh, captured on the + // rising edge so a mid-cycle isLive flip can't change how it's treated. + const cycleIsLiveRef = useRef(false); + + // Snapshot latency_ms and source_id together so a later sourceId change does + // not cause the emission effect to re-fire with stale latency data. `emit` + // records whether this cycle should be reported to telemetry (user-initiated + // searches only); latency is still surfaced for display in every case. + const [completedSearch, setCompletedSearch] = useState<{ + latency_ms: number; + source_id: string; + emit: boolean; + } | null>(null); + + useEffect(() => { + if (isAnyQueryFetching) { + // Start the timer once per fetch cycle (for live tail too — we display + // its elapsed time, we just don't emit telemetry for it). + if (!wasFetchingRef.current) { + searchStartTimeRef.current = performance.now(); + cycleIsLiveRef.current = isLive; + // Only blank the displayed timer for user-initiated searches. During + // live tail we keep the previous value so it doesn't flicker between + // background refreshes. + if (!isLive) { + setCompletedSearch(null); + } + } + wasFetchingRef.current = true; + } else { + if (searchStartTimeRef.current != null) { + setCompletedSearch({ + latency_ms: Math.round( + performance.now() - searchStartTimeRef.current, + ), + source_id: sourceId ?? '', + emit: !cycleIsLiveRef.current, + }); + searchStartTimeRef.current = null; + } + wasFetchingRef.current = false; + } + }, [isAnyQueryFetching, isLive, sourceId]); + + // completedSearch is the only dep here — sourceId was snapshotted at + // completion time so changing source after a finished search does not + // re-emit the previous run's latency against the new source. Live-tail + // cycles are recorded for display but flagged emit=false so they never flood + // telemetry. + useEffect(() => { + if (completedSearch == null || !completedSearch.emit) return; + HyperDX.addAction('search executed', { + latency_ms: completedSearch.latency_ms, + source_id: completedSearch.source_id, + }); + }, [completedSearch]); + + return { searchElapsedMs: completedSearch?.latency_ms ?? null }; +} + +function DBExplorePage() { + const brandName = useBrandDisplayName(); + // Next router is laggy behind window.location, which causes race + // conditions with useQueryStates, so we'll parse it directly + const paths = window.location.pathname.split('/'); + const savedSearchId = paths.length === 3 ? paths[2] : null; + + const [rawSearchedConfig, setSearchedConfig] = useQueryStates(queryStateMap); + + // Everything downstream sees a SQL WHERE, whatever the URL carried. + const searchedConfig = useMemo( + () => ({ + ...rawSearchedConfig, + ...toSqlWhere(rawSearchedConfig.where, rawSearchedConfig.whereLanguage), + }), + [rawSearchedConfig], + ); + + const [directTraceId, setDirectTraceId] = useQueryState( + 'traceId', + parseAsStringEncoded, + ); + + const { data: savedSearch } = useSavedSearch( + { id: `${savedSearchId}` }, + { + enabled: savedSearchId != null, + }, + ); + + const { data: sources } = useSources(); + const [lastSelectedSourceId, setLastSelectedSourceId] = useLocalStorage( + 'hdx-last-selected-source-id', + '', + ); + const { data: searchedSource } = useSource({ + id: searchedConfig.source, + kinds: [SourceKind.Log, SourceKind.Trace, SourceKind.Metric], + }); + const directTraceSource = + directTraceId != null && searchedSource?.kind === SourceKind.Trace + ? searchedSource + : undefined; + // Metric sources have no raw rows to list — only aggregated chart views are + // available, and the value expression is chosen via a metric name picker. + const searchedMetricSource = + searchedSource?.kind === SourceKind.Metric + ? (searchedSource as TMetricSource) + : undefined; + const isMetricSource = searchedMetricSource != null; + const chartSourceId = + directTraceId != null && !directTraceSource + ? '' + : (searchedConfig.source ?? ''); + + const [view, setView] = useSearchView(); + const [aggConfig, setAggConfig] = useSearchAggConfig(); + + // Submitted query mode: drives query *execution* (the raw-SQL chart config) + // so results only change on Run. The live form value (`isSqlUiMode`, defined + // once the form exists) gates the SQL-mode UI/chrome instead. + const searchedConfigType: QueryConfigMode = + searchedConfig.configType ?? 'builder'; + const isSqlMode = searchedConfigType === 'sql'; + + // Legacy 3-mode value still consumed by the filters sidebar (denoise gating) + // and a few source-capability checks below. New view types collapse onto + // 'results' for those purposes. + const analysisMode: 'results' | 'delta' | 'pattern' = + view === 'patterns' ? 'pattern' : view === 'heatmap' ? 'delta' : 'results'; + + const [patternColumn, setPatternColumn] = useQueryState( + 'patternColumn', + parseAsString, + ); + + const [isLive, setIsLive] = useQueryState( + 'isLive', + parseAsBoolean.withDefault(true), + ); + + useEffect(() => { + // Only the raw List view supports live tail. + if (view !== 'list') { + setIsLive(false); + } + }, [view, setIsLive]); + + useEffect(() => { + // Metric sources can't render the raw List / heatmap / patterns views, so + // they land on the metric wall instead; other sources have no wall. + // Waits for the source, or a browse link would lose its view on load. + if (searchedSource == null) return; + if (isMetricSource && !isAggregatedSearchView(view) && view !== 'browse') { + setView('browse'); + } else if (!isMetricSource && view === 'browse') { + setView('list'); + } + }, [searchedSource, isMetricSource, view, setView]); + const isMetricBrowse = isMetricSource && view === 'browse'; + + const [isFilterSidebarCollapsed, setIsFilterSidebarCollapsed] = + useLocalStorage('isFilterSidebarCollapsed', false); + + // The search box and pills above narrow every tile on the metric wall. + const metricBrowseFilters = useMemo( + () => [ + ...(searchedConfig.filters ?? []), + ...(searchedConfig.where?.trim() + ? [ + { + type: searchedConfig.whereLanguage, + condition: searchedConfig.where.trim(), + } as Filter, + ] + : []), + ], + [ + searchedConfig.filters, + searchedConfig.where, + searchedConfig.whereLanguage, + ], + ); + + const handleEditAsChart = useCallback( + (request: EditAsChartRequest) => { + setAggConfig(aggConfigFromWall(request)); + setView('timeseries'); + }, + [setAggConfig, setView], + ); + + const [denoiseResults, _setDenoiseResults] = useQueryState( + 'denoise', + parseAsBoolean.withDefault(false), + ); + const setDenoiseResults = useCallback( + (value: boolean) => { + setIsLive(false); + _setDenoiseResults(value); + }, + [setIsLive, _setDenoiseResults], + ); + + // Get default source + const defaultSourceId = useMemo( + () => getDefaultSourceId(sources, lastSelectedSourceId), + [sources, lastSelectedSourceId], + ); + + const { control, setValue, getValues, reset, handleSubmit, formState } = + useForm({ + values: { + select: searchedConfig.select || '', + where: searchedConfig.where || '', + whereLanguage: searchedConfig.whereLanguage, + configType: searchedConfig.configType ?? 'builder', + sqlTemplate: searchedConfig.sqlTemplate ?? '', + source: + searchedConfig.source || + (savedSearchId || directTraceId ? '' : defaultSourceId), + filters: searchedConfig.filters ?? [], + orderBy: searchedConfig.orderBy ?? '', + }, + resetOptions: { + keepDirtyValues: true, + keepErrors: true, + }, + resolver: zodResolver(SearchConfigSchema), + }); + + const inputSource = useWatch({ name: 'source', control }); + + const defaultOrderBy = useDefaultOrderBy(inputSource); + + // The default search config to use when the user hasn't changed the search config + const defaultSearchConfig = useMemo(() => { + let _savedSearch = savedSearch; + // Ensure to not use the saved search if the saved search id is not the same as the current saved search id + if (!savedSearchId || savedSearch?.id !== savedSearchId) { + _savedSearch = undefined; + } + // Ensure to not use the saved search if the input source is not the same as the saved search source + if (inputSource !== savedSearch?.source) { + _savedSearch = undefined; + } + return { + select: + _savedSearch?.select ?? + (searchedSource?.kind === SourceKind.Log || + searchedSource?.kind === SourceKind.Trace + ? searchedSource.defaultTableSelectExpression + : undefined), + ...toSqlWhere(_savedSearch?.where, _savedSearch?.whereLanguage), + source: _savedSearch?.source, + filters: _savedSearch?.filters ?? [], + orderBy: _savedSearch?.orderBy || defaultOrderBy, + }; + }, [searchedSource, inputSource, savedSearch, defaultOrderBy, savedSearchId]); + + // const { data: inputSourceObj } = useSource({ id: inputSource }); + const { data: inputSourceObjs } = useSources(); + const inputSourceObj = inputSourceObjs?.find(s => s.id === inputSource); + + const [displayedTimeInputValue, setDisplayedTimeInputValue] = + useState('Live Tail'); + + const { isReady, searchedTimeRange, onSearch, onTimeRangeSelect } = + useNewTimeQuery({ + initialDisplayValue: 'Live Tail', + initialTimeRange: defaultTimeRange, + showRelativeInterval: isLive ?? true, + setDisplayedTimeInputValue, + updateInput: !isLive, + }); + + // Sync url state back with form state + // (ex. for history navigation) + // TODO: Check if there are any bad edge cases here + const prevSearched = usePrevious(searchedConfig); + useEffect(() => { + if (JSON.stringify(prevSearched) !== JSON.stringify(searchedConfig)) { + reset({ + select: searchedConfig?.select ?? '', + where: searchedConfig?.where ?? '', + whereLanguage: searchedConfig.whereLanguage, + configType: searchedConfig?.configType ?? 'builder', + sqlTemplate: searchedConfig?.sqlTemplate ?? '', + source: searchedConfig?.source ?? undefined, + filters: searchedConfig?.filters ?? [], + orderBy: searchedConfig?.orderBy ?? '', + }); + } + }, [searchedConfig, reset, prevSearched]); + + // Populate searched query with saved search if the query params have + // been wiped (ex. clicking on the same saved search again) + useEffect(() => { + // The raw config, because "no language in the URL" is part of what makes a + // config empty, and the coerced one always names SQL. + const { source, where, select, whereLanguage, filters } = rawSearchedConfig; + const isSearchConfigEmpty = + !source && !where && !select && !whereLanguage && !filters?.length; + + // Landed on saved search (if we just landed on a searchId route) + if ( + savedSearch != null && // Make sure saved search data is loaded + savedSearch.id === savedSearchId && // Make sure we've loaded the correct saved search + isSearchConfigEmpty // Only populate if URL doesn't have explicit config + ) { + setSearchedConfig({ + source: savedSearch.source, + select: savedSearch.select, + ...toSqlWhere(savedSearch.where, savedSearch.whereLanguage), + filters: savedSearch.filters ?? [], + orderBy: savedSearch.orderBy ?? '', + }); + return; + } + + if (savedSearchId == null && directTraceId != null && !source) { + return; + } + + // Landed on a new search - ensure we have a source selected + if (savedSearchId == null && defaultSourceId && isSearchConfigEmpty) { + setSearchedConfig({ + source: defaultSourceId, + where: '', + select: '', + whereLanguage: getDefaultExploreLanguage(), + filters: [], + orderBy: '', + }); + return; + } + }, [ + savedSearch, + rawSearchedConfig, + setSearchedConfig, + savedSearchId, + defaultSourceId, + directTraceId, + sources, + ]); + + // Rewrite a Lucene URL rather than only coercing it on read, so the address + // bar stops advertising a query the page is not running and a later partial + // update can't resurrect it. + useEffect(() => { + if (rawSearchedConfig.whereLanguage !== 'lucene') { + return; + } + if ( + hasUneditableLuceneWhere( + rawSearchedConfig.where, + rawSearchedConfig.whereLanguage, + ) + ) { + notifications.show({ + color: 'yellow', + message: LUCENE_WHERE_DROPPED_MESSAGE, + }); + } + setSearchedConfig( + toSqlWhere(rawSearchedConfig.where, rawSearchedConfig.whereLanguage), + ); + }, [ + rawSearchedConfig.where, + rawSearchedConfig.whereLanguage, + setSearchedConfig, + ]); + + const [_queryErrors, setQueryErrors] = useState<{ + [key: string]: Error | ClickHouseQueryError; + }>({}); + + useEffect(() => { + if (!isBrowser || !IS_LOCAL_MODE) return; + const nullQueryErrors = (event: StorageEvent) => { + if (event.key === LOCAL_STORE_CONNECTIONS_KEY) { + setQueryErrors({}); + } + }; + + window.addEventListener('storage', nullQueryErrors); + return () => { + window.removeEventListener('storage', nullQueryErrors); + }; + }, []); + + const onSubmit = useCallback(() => { + onSearch(displayedTimeInputValue); + handleSubmit( + ({ + select, + where, + whereLanguage, + configType, + sqlTemplate, + source, + filters, + orderBy, + }) => { + setSearchedConfig({ + select, + where, + whereLanguage, + configType, + // A generated template is derived from the search, so persisting it + // would only bloat the URL with something we can rebuild. Only a + // query the user owns is worth carrying. + sqlTemplate: configType === 'sql' ? sqlTemplate : null, + source, + filters, + orderBy, + }); + }, + )(); + setQueryErrors({}); + }, [ + handleSubmit, + setSearchedConfig, + displayedTimeInputValue, + onSearch, + setQueryErrors, + ]); + + const applyPatternColumn = useCallback( + (next: string) => { + setPatternColumn(next || null); + onSubmit(); + }, + [setPatternColumn, onSubmit], + ); + + const handleViewMatchingEvents = useCallback( + (sqlCondition: string) => { + const next = nextSearchForPatternMatch({ + where: getValues('where') ?? '', + whereLanguage: getValues('whereLanguage'), + filters: getValues('filters') ?? [], + sqlCondition, + }); + setValue('where', next.where, { shouldDirty: true }); + setValue('filters', next.filters, { shouldDirty: true }); + setView('list'); + onSubmit(); + }, + [getValues, setValue, setView, onSubmit], + ); + + // The picker stages its own selection and only emits on Apply, so there is no + // half-typed state to hold back here. + const applyGroupBy = useCallback( + (next: string) => { + setAggConfig({ groupBy: next }); + onSubmit(); + }, + [setAggConfig, onSubmit], + ); + + const groupByFields = useMemo( + () => parseGroupByFields(aggConfig.groupBy), + [aggConfig.groupBy], + ); + + // Backs the side panel's group-by action, which flips one dimension the same + // way `toggleColumn` flips one column. + const toggleGroupBy = useCallback( + (field: string) => { + applyGroupBy( + formatGroupByFields( + groupByFields.includes(field) + ? groupByFields.filter(f => f !== field) + : [...groupByFields, field], + ), + ); + }, + [groupByFields, applyGroupBy], + ); + + const debouncedSubmit = useDebouncedCallback(onSubmit, 1000); + const handleSetFilters = useCallback( + (filters: Filter[]) => { + setValue('filters', filters); + debouncedSubmit(); + }, + [debouncedSubmit, setValue], + ); + + // Top-level column names for the active source, used to quote + // filter keys that contain special characters. + const { data: inputSourceColumns } = useColumns( + { + databaseName: inputSourceObj?.from?.databaseName ?? '', + tableName: inputSourceObj?.from?.tableName ?? '', + connectionId: inputSourceObj?.connection ?? '', + }, + { enabled: !!inputSourceObj }, + ); + const knownColumns = useMemo( + () => + inputSourceColumns + ? new Set(inputSourceColumns.map(c => c.name)) + : new Set(), + [inputSourceColumns], + ); + + const watchedSource = useWatch({ + control, + name: 'source', + // Watch will reset when changing saved search, so we need to default to the URL + defaultValue: searchedConfig.source ?? undefined, + }); + const prevSourceRef = useRef(watchedSource); + // Set when the user switches sources via the dropdown. The follow-up + // effect waits for the new source's columns to load and then drops any + // sidebar filters that don't apply to the new schema. + const pendingFilterReconcileRef = useRef(null); + + const watchedSourceObj = useMemo( + () => inputSourceObjs?.find(s => s.id === watchedSource), + [inputSourceObjs, watchedSource], + ); + const { data: watchedSourceColumns } = useColumns( + { + databaseName: watchedSourceObj?.from?.databaseName ?? '', + tableName: watchedSourceObj?.from?.tableName ?? '', + connectionId: watchedSourceObj?.connection ?? '', + }, + { enabled: !!watchedSourceObj }, + ); + + const { dateTimeColumns, onResolvedColumnsChange } = + useResolvedDateTimeColumns(inputSourceColumns); + + const filters = useWatch({ name: 'filters', control }); + const searchFilters = useSearchPageFilterState({ + searchQuery: filters ?? undefined, + onFilterChange: handleSetFilters, + dateTimeColumns, + knownColumns, + }); + + useEffect(() => { + // If the user changes the source dropdown, reset the select and orderby fields + // to match the new source selected + if (watchedSource !== prevSourceRef.current) { + prevSourceRef.current = watchedSource; + const newInputSourceObj = inputSourceObjs?.find( + s => s.id === watchedSource, + ); + if (newInputSourceObj != null) { + // Save the selected source ID to localStorage + setLastSelectedSourceId(newInputSourceObj.id); + + // If the user isn't in a saved search (or the source is different from the saved search source), reset fields + if (savedSearchId == null || savedSearch?.source !== watchedSource) { + setValue('select', ''); + setValue('orderBy', ''); + // Defer filter clearing: wait until the new source's columns load, + // then keep filters whose root column exists on the new schema. + pendingFilterReconcileRef.current = watchedSource ?? null; + // If the user is in a saved search, prefer the saved search's select/orderBy if available + } else { + setValue('select', savedSearch?.select ?? ''); + setValue('orderBy', savedSearch?.orderBy ?? ''); + // Don't clear filters - we're loading from saved search + } + // Push the new source to URL/searchedConfig so the chart re-queries. + // Debounced so a later filter reconcile (which also submits) collapses + // into a single run. + debouncedSubmit(); + } + } + }, [ + watchedSource, + setValue, + savedSearch, + savedSearchId, + inputSourceObjs, + setLastSelectedSourceId, + debouncedSubmit, + ]); + + const retainCompatibleFilters = useStableCallback((columns: ColumnMeta[]) => { + pendingFilterReconcileRef.current = null; + + const allowed = new Set(columns.map(c => c.name)); + + const dropped = searchFilters.retainFiltersByColumns(allowed); + + if (dropped.length > 0) { + notifications.show({ + color: 'yellow', + message: formatDroppedFiltersMessage(dropped.length), + }); + } + }); + + useEffect(() => { + if ( + pendingFilterReconcileRef.current === watchedSource && + watchedSourceColumns + ) { + retainCompatibleFilters(watchedSourceColumns); + } + }, [watchedSource, watchedSourceColumns, retainCompatibleFilters]); + + const onTableScroll = useCallback( + (scrollTop: number) => { + // If the user scrolls a bit down, kick out of live mode + if (scrollTop > 16 && isLive) { + setIsLive(false); + } + }, + [isLive, setIsLive], + ); + + const onSidebarOpen = useCallback(() => { + setIsLive(false); + }, [setIsLive]); + + const [modelFormExpanded, setModelFormExpanded] = useState(false); // Used in local mode + const [saveSearchModalState, setSaveSearchModalState] = useState< + 'create' | 'update' | undefined + >(undefined); + const [ + savedSearchesFlyoutOpened, + { open: openSavedSearchesFlyout, close: closeSavedSearchesFlyout }, + ] = useDisclosure(false); + const chartSearchConfig = useMemo( + () => ({ + select: searchedConfig.select ?? '', + source: chartSourceId, + where: searchedConfig.where ?? '', + whereLanguage: searchedConfig.whereLanguage, + filters: searchedConfig.filters ?? [], + orderBy: searchedConfig.orderBy ?? '', + }), + [ + chartSourceId, + searchedConfig.filters, + searchedConfig.orderBy, + searchedConfig.select, + searchedConfig.where, + searchedConfig.whereLanguage, + ], + ); + + const { data: chartConfig, isLoading: isChartConfigLoading } = + useSearchedConfigToChartConfig(chartSearchConfig, defaultSearchConfig); + + // query error handling + const { hasQueryError, queryError } = useMemo(() => { + const hasQueryError = Object.values(_queryErrors).length > 0; + const queryError: Error | ClickHouseQueryError | null = hasQueryError + ? Object.values(_queryErrors)[0] + : null; + return { hasQueryError, queryError }; + }, [_queryErrors]); + const inputWhere = useWatch({ name: 'where', control }); + const inputWhereLanguage = useWatch({ name: 'whereLanguage', control }); + const inputConfigType: QueryConfigMode = + useWatch({ name: 'configType', control }) ?? 'builder'; + // Live SQL-mode flag: true once the user has taken the generated query over, + // so builder-only chrome (histogram, severity, sort/columns, agg controls, + // event views) hides the instant the SQL becomes theirs. Query *execution* + // still keys off `isSqlMode` (submitted) so nothing re-runs until Run. + const isSqlUiMode = inputConfigType === 'sql'; + + // Heatmap and patterns can't show a hand-written statement, so SQL mode + // shows its rows in the List view instead, and leaving it goes back. + const viewBeforeSqlRef = useRef(null); + useEffect(() => { + if ((isSqlMode || isSqlUiMode) && !isSqlModeSearchView(view)) { + viewBeforeSqlRef.current = view; + setView('list'); + } + }, [isSqlMode, isSqlUiMode, view, setView]); + + // SQL starts folded away — the search box is the faster tool and most + // sessions never open it. A URL carrying a hand-written query opens it, or + // the query it names would be invisible. + const [sqlPanelOpen, setSqlPanelOpen] = useState( + searchedConfigType === 'sql', + ); + // The last text we generated, so its echo through the editor's onChange is + // not mistaken for the user typing. + const lastGeneratedSqlRef = useRef(''); + // query suggestion for 'where' if error + const whereSuggestions = useSqlSuggestions({ + input: inputWhere, + enabled: hasQueryError && inputWhereLanguage === 'sql', + }); + + const queryReady = + chartConfig?.from?.databaseName && + // Metric sources have an empty `from.tableName`; the real table is resolved + // per metric type from `metricTables` at query time. + (chartConfig?.from?.tableName || isMetricSource) && + chartConfig?.timestampValueExpression; + + const updateSavedSearch = useUpdateSavedSearch(); + const deleteSavedSearch = useDeleteSavedSearch(); + const onSaveSearch = useCallback(() => { + if (savedSearch == null) { + setSaveSearchModalState('create'); + } else { + handleSubmit(s => { + updateSavedSearch.mutate( + { + id: savedSearch.id, + ...s, + }, + { + onSuccess: () => { + // Make sure to run the query + onSubmit(); + }, + }, + ); + })(); + } + }, [savedSearch, updateSavedSearch, onSubmit, handleSubmit]); + + const handleUpdateTags = useCallback( + (newTags: string[]) => { + if (savedSearch?.id) { + updateSavedSearch.mutate( + { + id: savedSearch.id, + name: savedSearch.name, + select: searchedConfig.select ?? '', + where: searchedConfig.where ?? '', + whereLanguage: searchedConfig.whereLanguage, + source: searchedConfig.source ?? '', + orderBy: searchedConfig.orderBy ?? '', + filters: searchedConfig.filters ?? [], + tags: newTags, + }, + { + onSuccess: () => { + notifications.show({ + color: 'green', + message: 'Tags updated successfully', + }); + }, + onError: () => { + notifications.show({ + color: 'red', + message: ( + <> + An error occurred. + + ), + }); + }, + }, + ); + } + }, + [savedSearch, searchedConfig, updateSavedSearch], + ); + + const [newSourceModalOpened, setNewSourceModalOpened] = useState(false); + + const isAnyQueryFetching = + useIsFetching({ + queryKey: [QUERY_KEY_PREFIX], + }) > 0; + + const { searchElapsedMs } = useSearchTelemetry({ + isAnyQueryFetching, + isLive: isLive ?? false, + sourceId: chartConfig?.source ?? null, + }); + + const isTabVisible = useDocumentVisibility(); + + // State for collapsing all expanded rows when resuming live tail + const [collapseAllRows, setCollapseAllRows] = useState(false); + + const [interval, setInterval] = useQueryState( + 'liveInterval', + parseAsInteger.withDefault(LIVE_TAIL_DURATION_MS), + ); + + const [refreshFrequency, setRefreshFrequency] = useQueryState( + 'refreshFrequency', + parseAsInteger.withDefault(DEFAULT_REFRESH_FREQUENCY), + ); + + const updateRelativeTimeInputValue = useCallback((interval: number) => { + const label = getRelativeTimeOptionLabel(interval); + if (label) { + setDisplayedTimeInputValue(label); + } + }, []); + + useEffect(() => { + if (isReady && isLive) { + updateRelativeTimeInputValue(interval); + } + // we only want this to run on initial mount + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [updateRelativeTimeInputValue, searchedConfig.source, isReady]); + + useLiveUpdate({ + isLive, + interval, + refreshFrequency, + onTimeRangeSelect, + pause: isAnyQueryFetching || !queryReady || !isTabVisible, + }); + + // Callback to handle when rows are expanded - kick user out of live tail + const onExpandedRowsChange = useCallback( + (hasExpandedRows: boolean) => { + if (hasExpandedRows && isLive) { + setIsLive(false); + } + }, + [isLive, setIsLive], + ); + + const handleResumeLiveTail = useCallback(() => { + setIsLive(true); + updateRelativeTimeInputValue(interval); + // Trigger collapsing all expanded rows + setCollapseAllRows(true); + // Reset the collapse trigger after a short delay + setTimeout(() => setCollapseAllRows(false), 100); + }, [interval, updateRelativeTimeInputValue, setIsLive]); + + const dbSqlRowTableConfig = useMemo(() => { + if (chartConfig == null) { + return undefined; + } + + return { + ...chartConfig, + dateRange: searchedTimeRange, + }; + }, [chartConfig, searchedTimeRange]); + + // Stable key for persisting column widths in localStorage. Scoped per saved + // search when one is loaded, else per source for ad-hoc searches. + const columnSizeTableId = savedSearchId + ? `db-search-saved-${savedSearchId}` + : searchedConfig.source + ? `db-search-source-${searchedConfig.source}` + : undefined; + + const displayedColumns = useMemo(() => { + // `select` is typed as `string | DerivedColumn[]` upstream, but in the + // search page we always supply a string. Guard for type safety. + const rawSelect = + dbSqlRowTableConfig?.select ?? defaultSearchConfig.select ?? ''; + return splitAndTrimWithBracket( + typeof rawSelect === 'string' ? rawSelect : '', + ); + }, [dbSqlRowTableConfig?.select, defaultSearchConfig.select]); + + const toggleColumn = useCallback( + (column: string) => { + const newSelectArray = displayedColumns.includes(column) + ? displayedColumns.filter(s => s !== column) + : [...displayedColumns, column]; + setValue('select', newSelectArray.join(', ')); + onSubmit(); + }, + [displayedColumns, setValue, onSubmit], + ); + + const applyColumns = useCallback( + (columns: string[]) => { + setValue('select', columns.join(', ')); + onSubmit(); + }, + [setValue, onSubmit], + ); + + // Current List-view sort parsed from the orderBy string. + const listSort = useMemo(() => { + const parsed = parseAsSortingStateString.parse( + searchedConfig.orderBy ?? '', + ); + return { + field: parsed?.id as string | undefined, + direction: (parsed?.desc ? 'desc' : 'asc') as 'asc' | 'desc', + }; + }, [searchedConfig.orderBy]); + + const applyListSort = useCallback( + (field: string, direction: 'asc' | 'desc') => { + setIsLive(false); + setSearchedConfig({ + orderBy: `${field} ${direction === 'desc' ? 'DESC' : 'ASC'}`, + }); + }, + [setIsLive, setSearchedConfig], + ); + + const revertListSort = useCallback(() => { + setSearchedConfig({ orderBy: defaultSearchConfig.orderBy }); + }, [setSearchedConfig, defaultSearchConfig.orderBy]); + + // With the SQL editor open the query is written there, so the structured + // controls stand down instead of competing with it. Memoized because the + // results table is, and a fresh element would re-render every row. + const listTableBarActions = useMemo( + () => ( + <> + + ({ + value: column, + label: column, + }))} + activeField={listSort.field} + direction={listSort.direction} + onChange={applyListSort} + onRevert={revertListSort} + canRevert={!!searchedConfig.orderBy} + disabled={sqlPanelOpen} + disabledReason="Turn off Advanced to change the sort" + /> + + ), + [ + inputSourceObj, + searchedTimeRange, + displayedColumns, + applyColumns, + sqlPanelOpen, + listSort, + applyListSort, + revertListSort, + searchedConfig.orderBy, + ], + ); + + const generateSearchUrl = useCallback( + ({ + where, + whereLanguage, + source, + }: { + where: SearchConfig['where']; + whereLanguage: SearchConfig['whereLanguage']; + source?: TSource; + }) => { + // The link points back at Explore, which authors SQL, so a Lucene + // condition from a caller is dropped here rather than one navigation + // later. + const sqlWhere = toSqlWhere(where, whereLanguage); + const qParams = new URLSearchParams({ + whereLanguage: sqlWhere.whereLanguage, + from: searchedTimeRange[0].getTime().toString(), + to: searchedTimeRange[1].getTime().toString(), + isLive: 'false', + liveInterval: interval.toString(), + }); + + // When generating a search based on a different source, + // filters and select for the current source are not preserved. + if (source && source.id !== searchedSource?.id) { + qParams.append('where', sqlWhere.where); + qParams.append('source', source.id); + } else { + qParams.append('select', searchedConfig.select || ''); + qParams.append('where', sqlWhere.where || searchedConfig.where || ''); + qParams.append('filters', JSON.stringify(searchedConfig.filters ?? [])); + qParams.append('source', searchedSource?.id || ''); + } + + return `/explore?${qParams.toString()}`; + }, + [ + interval, + searchedConfig.filters, + searchedConfig.select, + searchedConfig.where, + searchedSource?.id, + searchedTimeRange, + ], + ); + + const handleTableError = useCallback( + (error: Error | ClickHouseQueryError) => { + setIsLive(false); + setQueryErrors(prev => ({ ...prev, DBSqlRowTable: error })); + }, + [setIsLive, setQueryErrors], + ); + + const [isAlertModalOpen, { open: openAlertModal, close: closeAlertModal }] = + useDisclosure(); + const directTraceRangeAppliedRef = useRef(null); + const directTraceFilterAppliedRef = useRef(null); + + useEffect(() => { + if (!isReady || !directTraceId) { + directTraceRangeAppliedRef.current = null; + return; + } + + const searchParams = new URLSearchParams(window.location.search); + if (searchParams.has('from') && searchParams.has('to')) { + return; + } + + if (directTraceRangeAppliedRef.current === directTraceId) { + return; + } + + directTraceRangeAppliedRef.current = directTraceId; + setIsLive(false); + const [start, end] = getDefaultDirectTraceDateRange(); + onTimeRangeSelect(start, end, null); + }, [directTraceId, isReady, onTimeRangeSelect, setIsLive]); + + useEffect(() => { + if (!directTraceId || !directTraceSource) { + directTraceFilterAppliedRef.current = null; + return; + } + + const nextKey = `${directTraceSource.id}:${directTraceId}`; + if (directTraceFilterAppliedRef.current === nextKey) { + return; + } + + directTraceFilterAppliedRef.current = nextKey; + setIsLive(false); + setSearchedConfig({ + source: directTraceSource.id, + where: buildDirectTraceWhereClause( + directTraceSource.traceIdExpression, + directTraceId, + ), + whereLanguage: 'sql', + filters: [], + }); + }, [directTraceId, directTraceSource, setIsLive, setSearchedConfig]); + + useEffect(() => { + if (isReady && queryReady && !isChartConfigLoading) { + // Only trigger if we haven't searched yet (no time range in URL) + const searchParams = new URLSearchParams(window.location.search); + if ( + directTraceId == null && + !searchParams.has('from') && + !searchParams.has('to') + ) { + onSearch('Live Tail'); + } + } + }, [directTraceId, isReady, queryReady, isChartConfigLoading, onSearch]); + + const { data: aliasMap } = useAliasMapFromChartConfig(dbSqlRowTableConfig); + + const aliasWith = useMemo(() => aliasMapToWithClauses(aliasMap), [aliasMap]); + + const histogramTimeChartConfig = useMemo(() => { + if (chartConfig == null) { + return undefined; + } + + // The histogram has always been grouped — severity for logs, status code + // for traces. Letting the toolbar's group by win is what carries a chart's + // breakdown over to the events view: the bars restack on the dimension the + // reader was already looking at, and the source default takes over again + // the moment they clear it. + const variableConfig: Partial< + Pick + > = { groupBy: resolveExploreGroupBy(aggConfig.groupBy, searchedSource) }; + + return { + ...chartConfig, + select: [ + { + aggFn: 'count', + aggCondition: '', + valueExpression: '', + }, + ], + orderBy: undefined, + granularity: 'auto', + dateRange: searchedTimeRange, + displayType: DisplayType.StackedBar, + with: aliasWith, + // Preserve the original table select string for "View Events" links + eventTableSelect: searchedConfig.select ?? undefined, + // Never align to granularity boundaries on the search page: the histogram + // and total count must reflect the user's exact selected range so they + // match the rows shown in the results table. Aligning to bucket + // boundaries (e.g. expanding a 2s selection to a 15s bucket) inflates + // the count beyond what the table shows. In live mode this also avoids + // stale cached data from an unchanging aligned end date. + alignDateRangeToGranularity: false, + // Make sure the end date is inclusive so that the histogram and table counts match + dateRangeEndInclusive: true, + ...variableConfig, + } satisfies BuilderChartConfigWithDateRange; + }, [ + chartConfig, + searchedSource, + aliasWith, + searchedTimeRange, + searchedConfig.select, + aggConfig.groupBy, + ]); + + const defaultAggGroupBy = useMemo( + () => defaultExploreGroupBy(searchedSource), + [searchedSource], + ); + + // Builds the chart config for an aggregated view (time series / number / + // table / bar / pie / treemap) from the current search config plus the + // inline aggregation controls. The renderers apply their own display-type + // conversion (convertToCategoricalChartConfig, etc.). + const aggViewChartConfig = useMemo(() => { + if (chartConfig == null || !isAggregatedSearchView(view)) { + return undefined; + } + // Metric queries require a chosen metric name — the renderer has no query + // path for an empty metric. Hold off until every series has one. + if ( + searchedMetricSource && + !exploreSeriesHaveMetricNames(aggConfig.series) + ) { + return undefined; + } + // Same for the column a non-count aggregation reduces over. Explore + // commits each series edit immediately, so without this the moment you + // pick "99th percentile" the chart fails on the column you haven't typed + // yet. + if ( + !searchedMetricSource && + !exploreSeriesHaveValueExpressions(aggConfig.series) + ) { + return undefined; + } + const groupBy = + view === 'number' + ? undefined + : resolveExploreGroupBy(aggConfig.groupBy, searchedSource); + // Categorical + summary-table views support the structured Sort menu + // (Value = the metric, Name = the group key). Alias a single aggregate as + // "Value" so ordering by it is stable regardless of the expression. + const isCategoricalLike = + view === 'table' || + view === 'bar' || + view === 'pie' || + view === 'treemap'; + + const select = aggConfig.series.map(series => { + const isCount = series.aggFn === 'count'; + const alias = + series.alias || + (isCategoricalLike && aggConfig.series.length === 1 + ? 'Value' + : undefined); + // `filters` is Explore's own field: the pills fold into the WHERE here so + // the renderer, and anything saved from this config, sees only a series. + const { filters: _pills, ...seriesSelect } = series; + return { + ...seriesSelect, + aggCondition: seriesAggCondition(series), + aggConditionLanguage: + series.aggConditionLanguage ?? getDefaultExploreLanguage(), + valueExpression: searchedMetricSource + ? 'Value' + : isCount + ? '' + : (series.valueExpression ?? ''), + ...(alias != null ? { alias } : {}), + }; + }); + + let orderBy: string | undefined; + if (isCategoricalLike) { + const dir = aggConfig.sortDir.toUpperCase(); + if (aggConfig.sort === 'name' && groupBy) { + orderBy = `${groupBy} ${dir}`; + } else if (select[0]?.alias) { + orderBy = `"${select[0].alias}" ${dir}`; + } + } + + const formulas = exploreViewSupportsFormulas(view) + ? aggConfig.formulas.filter( + formula => formula.expression.trim().length > 0, + ) + : []; + + return { + ...chartConfig, + ...(searchedMetricSource + ? { metricTables: searchedMetricSource.metricTables } + : {}), + select, + groupBy, + orderBy, + granularity: view === 'timeseries' ? aggConfig.granularity : undefined, + dateRange: searchedTimeRange, + displayType: + view === 'timeseries' + ? aggConfig.chartType === 'line' + ? DisplayType.Line + : DisplayType.StackedBar + : searchViewToDisplayType(view), + with: aliasWith, + seriesLimit: view === 'timeseries' ? undefined : aggConfig.limit, + alignDateRangeToGranularity: aggConfig.alignDateRangeToGranularity, + fillNulls: aggConfig.fillNulls, + compareToPreviousPeriod: aggConfig.compareToPreviousPeriod, + fitYAxisToData: aggConfig.fitYAxisToData, + numberFormat: aggConfig.numberFormat, + dateRangeEndInclusive: true, + ...(formulas.length > 0 + ? { + formulas, + showOperandSeries: + view === 'number' ? false : aggConfig.showOperandSeries, + } + : {}), + } as BuilderChartConfigWithDateRange; + }, [ + chartConfig, + view, + aggConfig, + searchedSource, + searchedTimeRange, + aliasWith, + searchedMetricSource, + ]); + + const exploreChartSettings = useMemo( + () => ({ + granularity: aggConfig.granularity, + alignDateRangeToGranularity: aggConfig.alignDateRangeToGranularity, + fillNulls: aggConfig.fillNulls, + compareToPreviousPeriod: aggConfig.compareToPreviousPeriod, + fitYAxisToData: aggConfig.fitYAxisToData, + numberFormat: aggConfig.numberFormat, + }), + [aggConfig], + ); + + const handleExploreChartSettingsChange = useCallback( + (settings: ChartConfigDisplaySettings) => { + setAggConfig({ + granularity: settings.granularity ?? 'auto', + alignDateRangeToGranularity: + settings.alignDateRangeToGranularity ?? false, + fillNulls: settings.fillNulls === false ? false : 0, + compareToPreviousPeriod: settings.compareToPreviousPeriod ?? false, + fitYAxisToData: settings.fitYAxisToData ?? false, + numberFormat: settings.numberFormat, + }); + onSubmit(); + }, + [onSubmit, setAggConfig], + ); + + // Dashboard-tile config for the "Add to dashboard" action: reuses the + // aggregated chart config but references the source by id (as tiles do) and + // drops the runtime-only date range so the tile follows the dashboard's own + // time range. + const addToDashboardConfig = useMemo(() => { + if (!aggViewChartConfig || !searchedConfig.source) { + return undefined; + } + return { + name: savedSearch?.name || 'Explore chart', + source: searchedConfig.source, + displayType: aggViewChartConfig.displayType, + select: aggViewChartConfig.select, + where: searchedConfig.where ?? '', + whereLanguage: searchedConfig.whereLanguage, + filters: searchedConfig.filters ?? [], + groupBy: aggViewChartConfig.groupBy, + orderBy: aggViewChartConfig.orderBy, + granularity: aggViewChartConfig.granularity, + seriesLimit: aggViewChartConfig.seriesLimit, + alignDateRangeToGranularity: + aggViewChartConfig.alignDateRangeToGranularity, + fillNulls: aggViewChartConfig.fillNulls, + compareToPreviousPeriod: aggViewChartConfig.compareToPreviousPeriod, + fitYAxisToData: aggViewChartConfig.fitYAxisToData, + numberFormat: aggViewChartConfig.numberFormat, + with: aggViewChartConfig.with, + formulas: aggViewChartConfig.formulas, + showOperandSeries: aggViewChartConfig.showOperandSeries, + } as SavedChartConfig; + }, [aggViewChartConfig, savedSearch?.name, searchedConfig]); + + const patternsAddToDashboardConfig = useMemo< + SavedChartConfig | undefined + >(() => { + if (view !== 'patterns' || !searchedConfig.source) { + return undefined; + } + const select = + patternColumn || + (searchedSource ? (getEventBody(searchedSource) ?? '') : ''); + return { + name: savedSearch?.name || 'Event patterns', + source: searchedConfig.source, + displayType: DisplayType.EventPatterns, + select, + where: searchedConfig.where ?? '', + whereLanguage: searchedConfig.whereLanguage, + filters: searchedConfig.filters ?? [], + } as SavedChartConfig; + }, [view, patternColumn, searchedSource, searchedConfig, savedSearch?.name]); + + // Raw-SQL config for SQL mode. Bypasses buildSearchChartConfig entirely: the + // user-authored sqlTemplate owns the whole statement, and the source metadata + // is carried over so macros ($__sourceTable, $__filters) resolve. The display + // type is picked from the current chart view (aggregated views map 1:1 to raw + // SQL display types). + // + // The search box rides along as one more filter so it keeps working after the + // user takes the SQL over: $__filters expands to the pills AND the search + // text. A template that drops the macro drops both, which the SQL panel warns + // about. + const rawSqlChartConfig = useMemo< + (RawSqlChartConfig & { dateRange: [Date, Date] }) | undefined + >(() => { + if (!isSqlMode || !searchedSource || !searchedConfig.source) { + return undefined; + } + const displayType = isAggregatedSearchView(view) + ? searchViewToDisplayType(view) + : DisplayType.Table; + return { + configType: 'sql', + sqlTemplate: searchedConfig.sqlTemplate ?? '', + connection: searchedSource.connection, + source: searchedConfig.source, + from: searchedSource.from, + displayType, + granularity: view === 'timeseries' ? 'auto' : undefined, + dateRange: searchedTimeRange, + filters: [ + ...(searchedConfig.filters ?? []), + ...(searchedConfig.where?.trim() + ? [ + { + type: searchedConfig.whereLanguage, + condition: searchedConfig.where.trim(), + } as Filter, + ] + : []), + ], + implicitColumnExpression: + isLogSource(searchedSource) || isTraceSource(searchedSource) + ? searchedSource.implicitColumnExpression + : undefined, + bodyExpression: isLogSource(searchedSource) + ? searchedSource.bodyExpression + : undefined, + useTextIndexForImplicitColumn: + isLogSource(searchedSource) || isTraceSource(searchedSource) + ? searchedSource.useTextIndexForImplicitColumn + : undefined, + metricTables: isMetricSourceGuard(searchedSource) + ? searchedSource.metricTables + : undefined, + }; + }, [ + isSqlMode, + searchedSource, + searchedConfig.source, + searchedConfig.sqlTemplate, + searchedConfig.filters, + searchedConfig.where, + searchedConfig.whereLanguage, + view, + searchedTimeRange, + ]); + + // Dashboard-tile config for the "Add to dashboard" action in SQL mode: a raw + // SQL SavedChartConfig (dashboards already support configType 'sql'). + const rawSqlAddToDashboardConfig = useMemo< + RawSqlSavedChartConfig | undefined + >(() => { + if (!rawSqlChartConfig) return undefined; + return { + name: savedSearch?.name || 'Explore SQL chart', + configType: 'sql', + sqlTemplate: rawSqlChartConfig.sqlTemplate, + connection: rawSqlChartConfig.connection, + source: searchedConfig.source ?? undefined, + displayType: rawSqlChartConfig.displayType ?? DisplayType.Table, + granularity: rawSqlChartConfig.granularity, + }; + }, [rawSqlChartConfig, savedSearch?.name, searchedConfig.source]); + + const metadata = useMetadataWithSettings(); + + // The builder config the SQL panel mirrors. aggViewChartConfig already has a + // raw-SQL-compatible display type and an array select. The List view starts + // from the table's own columns and sort, so opening the editor shows the + // query behind the rows on screen; other event views synthesize a count(). + // The generator only takes an array select, hence one plain column per item. + const sqlTemplateBaseConfig = useMemo(() => { + if (aggViewChartConfig) return aggViewChartConfig; + if (!chartConfig) return undefined; + const base = { + ...chartConfig, + displayType: DisplayType.Table, + groupBy: undefined, + granularity: undefined, + dateRange: searchedTimeRange, + }; + if (view === 'list' && displayedColumns.length > 0) { + return { + ...base, + select: displayedColumns.map(column => ({ + aggCondition: '', + valueExpression: column, + })), + }; + } + return { + ...base, + select: [{ aggFn: 'count', aggCondition: '', valueExpression: '' }], + orderBy: undefined, + }; + }, [ + aggViewChartConfig, + chartConfig, + searchedTimeRange, + view, + displayedColumns, + ]); + + // While the query is generated, keep it in step with the search above, so + // opening the panel always shows the statement the current search would run. + // The moment the user types, `configType` flips to 'sql' and this stops — + // regenerating over someone's edits is the behaviour the old read-only + // preview existed to prevent. + useEffect(() => { + if ( + !sqlPanelOpen || + inputConfigType !== 'builder' || + !sqlTemplateBaseConfig + ) + return; + let cancelled = false; + renderBuilderConfigAsSqlTemplate( + sqlTemplateBaseConfig as BuilderChartConfigWithDateRange, + metadata, + ) + .then(result => { + if (cancelled || result.isError) return; + if (getValues('configType') !== 'builder') return; + lastGeneratedSqlRef.current = result.sql; + setValue('sqlTemplate', result.sql); + }) + .catch(() => { + // Leave whatever is there (placeholder included) if conversion fails. + }); + return () => { + cancelled = true; + }; + }, [ + sqlPanelOpen, + inputConfigType, + sqlTemplateBaseConfig, + metadata, + getValues, + setValue, + ]); + + // First keystroke hands the query to the user. Echoes of our own regeneration + // carry the text we just wrote, so they never trip this. + const handleSqlEdit = useCallback( + (next: string) => { + if (next === lastGeneratedSqlRef.current) return; + if (getValues('configType') === 'sql') return; + setValue('configType', 'sql', { shouldDirty: true }); + }, + [getValues, setValue], + ); + + // Advanced mode is the open editor. Leaving it hands the query back to the + // search, so the next time it opens it is regenerated from the UI; edits + // are only thrown away once the user agrees to. + const confirm = useConfirm(); + const handleSqlOpenChange = useCallback( + async (open: boolean) => { + if (open) { + setSqlPanelOpen(true); + return; + } + if (getValues('configType') === 'sql') { + const ok = await confirm( + 'Discard your SQL edits and go back to the search?', + 'Discard', + { variant: 'danger' }, + ); + if (!ok) return; + setValue('configType', 'builder', { shouldDirty: true }); + } + setValue('sqlTemplate', '', { shouldDirty: true }); + lastGeneratedSqlRef.current = ''; + setSqlPanelOpen(false); + if (isSqlMode) { + onSubmit(); + } + }, + [confirm, getValues, setValue, isSqlMode, onSubmit], + ); + + // Put back the view SQL mode moved off, once both the form and the results + // are builder again (restoring earlier would be undone by the move itself). + useEffect(() => { + if (!isSqlMode && !isSqlUiMode && viewBeforeSqlRef.current) { + setView(viewBeforeSqlRef.current); + viewBeforeSqlRef.current = null; + } + }, [isSqlMode, isSqlUiMode, setView]); + + const onFormSubmit = useCallback>( + e => { + e.preventDefault(); + onSubmit(); + return false; + }, + [onSubmit], + ); + + const onSortingChange = useCallback( + (sortState: SortingState | null) => { + setIsLive(false); + const sort = sortState?.at(0); + setSearchedConfig({ + orderBy: sort + ? `${sort.id} ${sort.desc ? 'DESC' : 'ASC'}` + : defaultSearchConfig.orderBy, + }); + }, + [setIsLive, defaultSearchConfig.orderBy, setSearchedConfig], + ); + // Parse the orderBy string into a SortingState. We need the string + // version in other places so we keep this parser separate. + const initialSortBy = useMemo(() => { + const orderBy = parseAsSortingStateString.parse( + searchedConfig.orderBy ?? '', + ); + return orderBy ? [orderBy] : []; + }, [searchedConfig.orderBy]); + + const handleTimeRangeSelect = useCallback( + (d1: Date, d2: Date) => { + onTimeRangeSelect(d1, d2); + setIsLive(false); + }, + [onTimeRangeSelect, setIsLive], + ); + + // Focus a chart series into the actual search. The histogram is grouped by + // severity/status, so a focused series maps to a real column value; applying + // it as an "only" filter re-queries both the chart and the results table so + // they stay in sync (the chart-only visual focus wouldn't touch the table). + const handleFocusSeries = useCallback( + (groupFilters: SeriesGroupFilter[]) => { + // Apply all group filters in one update so a multi-group series focus + // re-queries once, not once per column. setOnlyFilters keys on the clean + // (unquoted) column expression; the chart hands us the raw groupBy one. + searchFilters.setOnlyFilters( + groupFilters.map(({ column, value }) => ({ + property: cleanClickHouseExpression(column), + value, + })), + ); + }, + [searchFilters], + ); + + const filtersChartConfig = useMemo(() => { + const overrides = { + orderBy: undefined, + dateRange: searchedTimeRange, + with: aliasWith, + } as const; + return chartConfig + ? { + ...chartConfig, + ...overrides, + } + : { + timestampValueExpression: '', + connection: '', + from: { + databaseName: '', + tableName: '', + }, + where: '', + select: '', + ...overrides, + }; + }, [chartConfig, searchedTimeRange, aliasWith]); + + // Severity expression for the current source (logs only). Used both to run + // the error/warning summary counts and to know which column a severity pill + // click should filter on. + const severityExpression = + searchedSource?.kind === SourceKind.Log + ? searchedSource?.severityTextExpression + : undefined; + const severityProperty = severityExpression + ? cleanClickHouseExpression(severityExpression) + : undefined; + + // Grouped count (severity value → count) over the searched window, mirroring + // the results query's filters. The SeveritySummary component buckets rows into + // error/warning client-side and renders clickable pills. + const severitySummaryConfig = useMemo< + BuilderChartConfigWithDateRange | undefined + >(() => { + if (chartConfig == null || !severityExpression) { + return undefined; + } + return { + ...chartConfig, + select: [ + { + aggFn: 'count', + aggCondition: '', + valueExpression: '', + }, + ], + groupBy: severityExpression, + orderBy: undefined, + granularity: undefined, + dateRange: searchedTimeRange, + displayType: DisplayType.Table, + with: aliasWith, + alignDateRangeToGranularity: false, + dateRangeEndInclusive: true, + } satisfies BuilderChartConfigWithDateRange; + }, [chartConfig, severityExpression, searchedTimeRange, aliasWith]); + + // Severity pills reflect the structured filter for the severity column, so + // they render as filter chips in the query bar alongside sidebar filters. + const activeSeverityValues = useMemo(() => { + if (!severityProperty) return []; + const included = searchFilters.filters[severityProperty]?.included; + return included ? Array.from(included).map(String) : []; + }, [searchFilters.filters, severityProperty]); + + const handleSeverityToggle = useCallback( + (values: string[], isActive: boolean) => { + if (!severityProperty) return; + // Merge with any severity values already selected so error + warning can + // be active at once (adding one bucket doesn't drop the other). + const next = new Set( + Array.from(searchFilters.filters[severityProperty]?.included ?? []).map( + String, + ), + ); + for (const v of values) { + if (isActive) next.delete(v); + else next.add(v); + } + searchFilters.setIncludedValues(severityProperty, Array.from(next)); + }, + [searchFilters, severityProperty], + ); + + const openNewSourceModal = useCallback(() => { + setNewSourceModalOpened(true); + }, []); + + const [isDrawerChildModalOpen, setDrawerChildModalOpen] = useState(false); + + const rowTableContext = useMemo( + () => ({ + onPropertyAddClick: searchFilters.setFilterValue, + displayedColumns, + toggleColumn, + groupByFields, + toggleGroupBy, + generateSearchUrl, + sqlOnlySearchUrl: true, + dbSqlRowTableConfig, + isChildModalOpen: isDrawerChildModalOpen, + setChildModalOpen: setDrawerChildModalOpen, + // The row side panel is only used by the List view (log/trace sources). + source: + searchedSource?.kind === SourceKind.Metric ? undefined : searchedSource, + }), + [ + searchFilters.setFilterValue, + searchedSource, + dbSqlRowTableConfig, + displayedColumns, + toggleColumn, + groupByFields, + toggleGroupBy, + generateSearchUrl, + isDrawerChildModalOpen, + ], + ); + + // A metric source's real tables are per metric type, so anything that reads + // schema — the filter sidebar, the WHERE field's autocomplete — has to go to + // the tables behind the selected series. `from.tableName` is empty on a + // metric source, and reading it leaves both with nothing to suggest. + const inputMetricTableConnections = useMemo( + () => metricTableConnections(inputSourceObj, aggConfig.series), + [inputSourceObj, aggConfig.series], + ); + + const inputSourceTableConnection = useMemo( + () => inputMetricTableConnections[0] ?? tcFromSource(inputSourceObj), + [inputMetricTableConnections, inputSourceObj], + ); + + const [isSourceSchemaPreviewOpen, setIsSourceSchemaPreviewOpen] = + useState(false); + + const onTimePickerSearch = useCallback( + (range: string) => { + setIsLive(false); + onSearch(range); + }, + [setIsLive, onSearch], + ); + + const onTimePickerRelativeSearch = useCallback( + (rangeMs: number) => { + const _range = parseRelativeTimeQuery(rangeMs); + setIsLive(true); + setInterval(rangeMs); + onTimeRangeSelect(_range[0], _range[1], null); + }, + [setIsLive, setInterval, onTimeRangeSelect], + ); + const directTraceFocusDate = useMemo( + () => + new Date( + (searchedTimeRange[0].getTime() + searchedTimeRange[1].getTime()) / 2, + ), + [searchedTimeRange], + ); + + const onDirectTraceSourceChange = useCallback( + (sourceId: string | null) => { + setIsLive(false); + if (sourceId == null) { + directTraceFilterAppliedRef.current = null; + setSearchedConfig({ + source: null, + where: '', + whereLanguage: getDefaultExploreLanguage(), + filters: [], + }); + return; + } + + const nextSource = sources?.find( + (source): source is Extract => + source.id === sourceId && isTraceSource(source), + ); + if (!nextSource || !directTraceId) { + return; + } + + setSearchedConfig({ + source: nextSource.id, + where: buildDirectTraceWhereClause( + nextSource.traceIdExpression, + directTraceId, + ), + whereLanguage: 'sql', + filters: [], + }); + }, + [directTraceId, setIsLive, setSearchedConfig, sources], + ); + + const closeDirectTraceSidePanel = useCallback(() => { + setDirectTraceId(null); + }, [setDirectTraceId]); + + const clearSaveSearchModalState = useCallback( + () => setSaveSearchModalState(undefined), + [setSaveSearchModalState], + ); + + const onModelFormExpandClose = useCallback(() => { + setModelFormExpanded(false); + }, [setModelFormExpanded]); + + // `Edit source` (singular): operate on the currently selected source. + // Local mode opens the inline edit modal seeded with `inputSource`; + // non-local uses a hard navigation so the page's `useQueryStates` + // (source/where/select/whereLanguage/filters/orderBy) can't merge + // stale /search state into the destination URL, and so + // `router.basePath` is correctly prepended for the /clickstack build. + const onEditCurrentSource = useCallback(() => { + if (IS_LOCAL_MODE) { + setModelFormExpanded(true); + return; + } + if (inputSource) { + window.location.assign(`${router.basePath}/team#source-${inputSource}`); + } else { + window.location.assign(`${router.basePath}/team`); + } + }, [inputSource, setModelFormExpanded]); + + // `Manage sources`: open the all-sources list view. Only wired in + // non-local mode; local has no list-view surface so the menu item + // hides itself when this prop is undefined. We use `window.location` + // for a hard navigation instead of `router.push` so the page's + // `useQueryStates` (source/where/select/whereLanguage/filters/orderBy) + // can't restore its state into the new URL during the client-side + // transition, and so `router.basePath` is correctly prepended for + // the /clickstack build. + const onManageSources = useMemo(() => { + if (IS_LOCAL_MODE) return undefined; + return () => { + window.location.assign(`${router.basePath}/team`); + }; + }, []); + + const setNewSourceModalClosed = useCallback( + () => setNewSourceModalOpened(false), + [setNewSourceModalOpened], + ); + + const onNewSourceCreate = useCallback( + (newSource: TSource) => { + setValue('source', newSource.id); + setNewSourceModalClosed(); + }, + [setValue, setNewSourceModalClosed], + ); + + return ( + + + + {savedSearch ? `${savedSearch.name} Explore` : 'Explore'} -{' '} + {brandName} + + + {!IS_LOCAL_MODE && isAlertModalOpen && ( + + )} + +
+ {/* */} + {/* Band 1: Context */} + + setIsSourceSchemaPreviewOpen(true)} + isSchemaPreviewEnabled={isSourceSchemaPreviewEnabled( + inputSourceObj, + )} + allowedSourceKinds={ALLOWED_SOURCE_KINDS} + data-testid="source-selector" + style={{ minWidth: 150 }} + /> + setIsSourceSchemaPreviewOpen(false)} + /> + + } + savedSearchId={savedSearchId} + savedSearchName={savedSearch?.name} + isDirty={formState.isDirty} + isLocalMode={IS_LOCAL_MODE} + alerts={savedSearch?.alerts} + favoriteButton={ + savedSearch && ( + + ) + } + tagsControl={ + savedSearch && ( + + + + ) + } + onOpenSavedViews={openSavedSearchesFlyout} + onSaveView={onSaveSearch} + onUpdate={() => setSaveSearchModalState('update')} + onSaveAsNew={() => setSaveSearchModalState('create')} + saveDisabled={inputConfigType === 'sql'} + saveDisabledTooltip="SQL searches aren't savable yet" + modeToggle={ + handleSqlOpenChange(!sqlPanelOpen)} + /> + } + onOpenAlert={openAlertModal} + onDelete={() => + deleteSavedSearch.mutate(savedSearch?.id ?? '', { + onSuccess: () => { + router.push('/explore'); + }, + }) + } + /> + + + {/* Band 2: Query editor */} + + + + {view === 'list' && denoiseResults != true && ( + + isLive ? setIsLive(false) : handleResumeLiveTail() + } + onSelectCadence={ms => { + setRefreshFrequency(ms); + if (!isLive) { + handleResumeLiveTail(); + } + }} + /> + )} + + + } + /> + + + {searchedConfig != null && searchedSource != null && ( + + )} + + + + {!queryReady ? ( + } + title="No data to display" + description="Select a source and click the play button to query data." + /> + ) : ( + <> +
+ {!isFilterSidebarCollapsed && + isMetricBrowse && + searchedMetricSource && ( + + setIsFilterSidebarCollapsed(true)} + /> + + )} + {!isFilterSidebarCollapsed && !isMetricBrowse && ( + + setIsFilterSidebarCollapsed(true)} + {...searchFilters} + /> + + )} + {chartConfig && histogramTimeChartConfig && ( + + + + ) + } + stats={ + !isMetricSource && + !isSqlUiMode && + severitySummaryConfig && ( + + ) + } + filterExpand={ + isFilterSidebarCollapsed && ( + setIsFilterSidebarCollapsed(false)} + /> + ) + } + viewSwitcher={ + + } + viewControls={ + !isSqlUiMode && + view === 'patterns' && ( + // Beside the switcher that asked for it, in the slot + // the chart-type picker uses, rather than stranded + // between the chart and the table it governs. + + ) + } + groupByControl={ + // Patterns are their own grouping, so a second one on + // top would be two answers to the same question — the + // value is held and comes back on the way out. Number + // reduces to a single figure and has nothing to split. + !isSqlUiMode && + view !== 'patterns' && + view !== 'number' && + !isMetricBrowse && ( + + ) + } + addToDashboard={ + isSqlUiMode + ? rawSqlAddToDashboardConfig && ( + + ) + : view === 'patterns' + ? patternsAddToDashboardConfig && ( + + ) + : isAggregatedSearchView(view) && + addToDashboardConfig && ( + + ) + } + chartSettings={ + isMetricSource && + aggViewChartConfig && ( + + ) + } + overflowMenu={ + + } + shapeControls={ + !isSqlUiMode && isAggregatedSearchView(view) ? ( + + ) : undefined + } + shapeActions={ + view === 'table' || + view === 'bar' || + view === 'pie' || + view === 'treemap' ? ( + { + setAggConfig({ + sort: field as AggSortField, + sortDir: dir, + }); + onSubmit(); + }} + onRevert={() => { + setAggConfig({ sort: 'value', sortDir: 'desc' }); + onSubmit(); + }} + canRevert={ + aggConfig.sort !== 'value' || + aggConfig.sortDir !== 'desc' + } + disabled={sqlPanelOpen} + disabledReason="Turn off Advanced to change the sort" + /> + ) : undefined + } + /> + + {viewShowsHistogram(view) && + !hasQueryError && + !isSqlUiMode && ( + + + + )} + {hasQueryError && queryError ? ( + <> +
+ {whereSuggestions && whereSuggestions.length > 0 && ( + + + Query Helper + + + {whereSuggestions!.map(s => ( + + + {s.userMessage('where')} + + + + + + ))} + + + )} + + + Error encountered for query with inputs: + + + + + SELECT + + + + + + ORDER BY + + + + + + + {chartConfig.whereLanguage === 'lucene' + ? 'Searched For' + : 'WHERE'} + + + + {chartConfig.whereLanguage === 'lucene' ? ( + + ) : ( + + )} + + + + + + + Error Message: + + + {queryError.message} + + + {queryError instanceof ClickHouseQueryError && ( + + + Original Query: + + + + + + )} +
+ + ) : isSqlUiMode && + view === 'list' && + rawSqlChartConfig && + searchedConfig.source && + dbSqlRowTableConfig ? ( + + + + ) : isSqlUiMode ? ( + + {rawSqlChartConfig ? ( + view === 'timeseries' ? ( + + ) : view === 'number' ? ( + + ) : view === 'bar' ? ( + + ) : view === 'pie' ? ( + + ) : view === 'treemap' ? ( + + ) : ( + + ) + ) : ( + + + Press Run to execute your SQL query. + + + )} + + ) : isMetricBrowse && searchedMetricSource ? ( + + ) : view === 'patterns' ? ( + + + + ) : view === 'heatmap' ? ( + searchedSource != null && isTraceSource(searchedSource) ? ( + + + + ) : ( + + + Event deltas are only available for trace sources. + + + ) + ) : isAggregatedSearchView(view) ? ( + + {isMetricSource && + !exploreSeriesHaveMetricNames(aggConfig.series) && ( + + + Select a metric to visualize + + + Choose a metric name from the series cards above. + + + )} + {!isMetricSource && + !exploreSeriesHaveValueExpressions( + aggConfig.series, + ) && ( + + + Select a field to aggregate + + + Every aggregation except count reduces over a + field. Pick one in the series card above. + + + )} + {view === 'timeseries' && aggViewChartConfig && ( + { + setAggConfig({ + chartType: + type === DisplayType.Line ? 'line' : 'bar', + }); + onSubmit(); + }} + showMVOptimizationIndicator={false} + drillDownPath="/explore" + onFocusSeries={handleFocusSeries} + queryKeyPrefix={QUERY_KEY_PREFIX} + /> + )} + {view === 'number' && aggViewChartConfig && ( + + )} + {view === 'table' && aggViewChartConfig && ( + + )} + {view === 'bar' && aggViewChartConfig && ( + + )} + {view === 'pie' && aggViewChartConfig && ( + + )} + {view === 'treemap' && aggViewChartConfig && ( + + )} + + ) : ( + + {chartConfig && + searchedConfig.source && + dbSqlRowTableConfig && ( + + )} + + )} +
+ )} +
+ + )} +
+
+ ); +} + +const DBExplorePageDynamic = dynamic(async () => DBExplorePage, { + ssr: false, +}); + +// @ts-expect-error next/dynamic component type does not include the getLayout static +DBExplorePageDynamic.getLayout = withAppNav; + +export default DBExplorePageDynamic; diff --git a/packages/app/src/GranularityPicker.tsx b/packages/app/src/GranularityPicker.tsx index f6af477edd..602e01e3c5 100644 --- a/packages/app/src/GranularityPicker.tsx +++ b/packages/app/src/GranularityPicker.tsx @@ -7,14 +7,20 @@ export function GranularityPicker({ value, onChange, disabled, + label, + size, }: { value: Granularity | 'auto' | undefined; onChange: (granularity: Granularity | 'auto' | undefined) => void; disabled?: boolean; + label?: string; + size?: 'xs' | 'sm'; }) { return ( + ) => + setFilter(e.currentTarget.value) + } + leftSection={} + /> + {filter && ( + + )} +
+ + + + + {rowData != null ? ( + + ) : ( + No data + )} + +
+ ); +} diff --git a/packages/app/src/components/Explore/owned/DBRowOverviewPanel.tsx b/packages/app/src/components/Explore/owned/DBRowOverviewPanel.tsx new file mode 100644 index 0000000000..143045a498 --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBRowOverviewPanel.tsx @@ -0,0 +1,450 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { use, useCallback, useMemo } from 'react'; +import isString from 'lodash/isString'; +import pickBy from 'lodash/pickBy'; +import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; +import { Accordion, Box, Flex, Text } from '@mantine/core'; + +import EventTag from '@/components/EventTag'; +import { ExceptionSubpanel } from '@/components/ExceptionSubpanel'; +import { + useLinkedSpanDetails, + useReverseSpanLinks, +} from '@/components/linkedSpans'; +import { NetworkPropertySubpanel } from '@/components/NetworkPropertyPanel'; +import { SpanEventsSubpanel } from '@/components/SpanEventsSubpanel'; +import { SpanLinkedFromSubpanel } from '@/components/SpanLinkedFromSubpanel'; +import { + getValidSpanLinks, + SpanLinksSubpanel, +} from '@/components/SpanLinksSubpanel'; +import { WithClause } from '@/hooks/useRowWhere'; +import { getEventBody } from '@/source'; +import { getHighlightedAttributesFromData } from '@/utils/highlightedAttributes'; +import { resolveRowTimestampAnchor } from '@/utils/rowTimestamps'; + +import { + getJSONColumnNames, + getMapColumnNames, + useRowData, +} from './DBRowDataPanel'; +import { DBRowJsonViewer } from './DBRowJsonViewer'; +import { RowSidePanelContext } from './DBRowSidePanel'; +import DBRowSidePanelHeader from './DBRowSidePanelHeader'; + +const EMPTY_OBJ = {}; +export function RowOverviewPanel({ + source, + rowId, + aliasWith, + dateRange, + hideHeader = false, + flush = false, + 'data-testid': dataTestId, +}: { + source: TSource; + rowId: string | undefined | null; + aliasWith?: WithClause[]; + dateRange?: [Date, Date]; + hideHeader?: boolean; + // When true, drop the horizontal padding so content aligns flush with + // surrounding chrome (e.g. the tab bar in the trace span detail panel). + flush?: boolean; + 'data-testid'?: string; +}) { + const contentPx = flush ? 0 : 'md'; + const { data } = useRowData({ source, rowId, aliasWith, dateRange }); + const { onPropertyAddClick, generateSearchUrl, onOpenLinkedTrace } = + use(RowSidePanelContext); + + const highlightedAttributeValues = useMemo(() => { + const attributeExpressions = + source.kind === SourceKind.Trace || source.kind === SourceKind.Log + ? (source.highlightedRowAttributeExpressions ?? []) + : []; + + return data + ? getHighlightedAttributesFromData( + source, + attributeExpressions, + data.data || [], + data.meta || [], + ) + : []; + }, [source, data]); + + const jsonColumns = getJSONColumnNames(data?.meta); + const mapColumns = getMapColumnNames(data?.meta); + + const eventAttributesExpr = + source.kind === SourceKind.Log || source.kind === SourceKind.Trace + ? source.eventAttributesExpression + : undefined; + + const firstRow = useMemo(() => { + const firstRow = { ...(data?.data?.[0] ?? {}) }; + if (!firstRow) { + return null; + } + return firstRow; + }, [data]); + + // TODO: Use source config to select these in SQL, but we'll just + // assume OTel column names for now + const topLevelAttributeKeys = [ + 'ServiceName', + 'SpanName', + 'Duration', + 'SeverityText', + 'StatusCode', + 'StatusMessage', + 'SpanKind', + 'TraceId', + 'SpanId', + 'ParentSpanId', + 'ScopeName', + 'ScopeVersion', + ]; + const topLevelAttributes = pickBy(firstRow, (value, key) => { + if (value === '') { + return false; + } + if (topLevelAttributeKeys.includes(key)) { + return true; + } + return false; + }); + + const resourceAttributes = firstRow?.__hdx_resource_attributes ?? EMPTY_OBJ; + const flattenedEventAttributes = + firstRow?.__hdx_event_attributes ?? EMPTY_OBJ; + + const dataAttributes = useMemo( + () => + eventAttributesExpr && + firstRow?.[eventAttributesExpr] && + Object.keys(firstRow[eventAttributesExpr]).length > 0 + ? { [eventAttributesExpr]: firstRow[eventAttributesExpr] } + : {}, + [eventAttributesExpr, firstRow], + ); + + const _generateSearchUrl = useCallback( + (query?: string, queryLanguage?: 'sql' | 'lucene' | 'promql') => { + return ( + generateSearchUrl?.({ + where: query, + whereLanguage: queryLanguage, + }) ?? '/' + ); + }, + [generateSearchUrl], + ); + + const isHttpRequest = useMemo(() => { + const attributes = + eventAttributesExpr && dataAttributes?.[eventAttributesExpr]; + return attributes?.['http.url'] != null; + }, [dataAttributes, eventAttributesExpr]); + + const filteredEventAttributes = useMemo(() => { + if (!eventAttributesExpr) return dataAttributes; + + const attributes = dataAttributes?.[eventAttributesExpr]; + return isHttpRequest && attributes + ? { + [eventAttributesExpr]: pickBy( + attributes, + (_, key) => !key.startsWith('http.'), + ), + } + : dataAttributes; + }, [dataAttributes, isHttpRequest, eventAttributesExpr]); + + const exceptionValues = useMemo(() => { + const parsedEvents = + firstRow?.__hdx_events_exception_attributes ?? EMPTY_OBJ; + const stacktrace = + parsedEvents?.['exception.stacktrace'] || + parsedEvents?.['exception.parsed_stacktrace']; + + let parsedStacktrace = stacktrace ?? '[]'; + try { + parsedStacktrace = JSON.parse(stacktrace); + } catch { + // do nothing + } + + return [ + { + stacktrace: parsedStacktrace, + type: parsedEvents?.['exception.type'], + value: + typeof parsedEvents?.['exception.message'] !== 'string' + ? JSON.stringify(parsedEvents?.['exception.message']) + : parsedEvents?.['exception.message'], + mechanism: parsedEvents?.['exception.mechanism'], + }, + ]; + }, [firstRow]); + + const hasException = useMemo(() => { + return ( + Object.keys(firstRow?.__hdx_events_exception_attributes ?? {}).length > 0 + ); + }, [firstRow?.__hdx_events_exception_attributes]); + + const hasSpanEvents = useMemo(() => { + return ( + Array.isArray(firstRow?.__hdx_span_events) && + firstRow?.__hdx_span_events.length > 0 + ); + }, [firstRow?.__hdx_span_events]); + + const validSpanLinks = useMemo(() => { + return getValidSpanLinks(firstRow?.__hdx_span_links); + }, [firstRow?.__hdx_span_links]); + const hasSpanLinks = validSpanLinks.length > 0; + + const rowMeta = data?.meta; + const linkAnchorDate = useMemo( + () => + resolveRowTimestampAnchor({ + timestampValueExpression: source.timestampValueExpression, + row: firstRow, + meta: rowMeta, + }), + [source.timestampValueExpression, firstRow, rowMeta], + ); + + const rowTraceId = firstRow?.__hdx_trace_id; + const rowSpanId = firstRow?.__hdx_span_id; + + const { links: reverseSpanLinks } = useReverseSpanLinks({ + source, + traceId: typeof rowTraceId === 'string' ? rowTraceId : undefined, + spanId: typeof rowSpanId === 'string' ? rowSpanId : undefined, + anchorDate: linkAnchorDate, + }); + + const { details: linkedSpanDetails } = useLinkedSpanDetails({ + source, + links: validSpanLinks, + anchorDate: linkAnchorDate, + }); + + const mainContentColumn = getEventBody(source); + const mainContent = isString(firstRow?.['__hdx_body']) + ? firstRow['__hdx_body'] + : firstRow?.['__hdx_body'] !== undefined + ? JSON.stringify(firstRow['__hdx_body']) + : undefined; + + return ( +
+ {!hideHeader && ( + + + + )} + + {isHttpRequest && ( + + + + HTTP Request + + + + + + + + + )} + + {hasException && ( + + + + Exception + + + + + + + + + )} + + {Object.keys(topLevelAttributes).length > 0 && ( + + + + Top Level Attributes + + + + + + + + + )} + + {Object.keys(filteredEventAttributes).length > 0 && ( + + + + {source.kind === 'log' ? 'Log' : 'Span'} Attributes + + + + + + + + + )} + + {hasSpanEvents && ( + + + + Span Events + + + + + + + + + )} + + {hasSpanLinks && ( + + + + Span Links + + + + + + + + + )} + + {reverseSpanLinks.length > 0 && ( + + + + Linked from + + + + + + + + + )} + + {Object.keys(resourceAttributes).length > 0 && ( + + + + Resource Attributes + + + + + {Object.entries(resourceAttributes).map(([key, value]) => ( + + ))} + + + + )} + +
+ ); +} diff --git a/packages/app/src/components/Explore/owned/DBRowSidePanel.tsx b/packages/app/src/components/Explore/owned/DBRowSidePanel.tsx new file mode 100644 index 0000000000..6e4b95e538 --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBRowSidePanel.tsx @@ -0,0 +1,1455 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { + createContext, + type ReactNode, + use, + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; +import { add } from 'date-fns'; +import { isString } from 'lodash'; +import { ErrorBoundary, FallbackProps } from 'react-error-boundary'; +import { useHotkeys } from 'react-hotkeys-hook'; +import SqlString from 'sqlstring'; +import { + isLogSource, + isTraceSource, + SourceKind, + TLogSource, + TSource, + TTraceSource, +} from '@hyperdx/common-utils/dist/types'; +import { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; +import { + ActionIcon, + Badge, + Box, + Button, + CopyButton, + Drawer, + Flex, + Group, + Stack, + Text, + Tooltip, +} from '@mantine/core'; +import { IconCopy, IconKeyboard, IconShare, IconX } from '@tabler/icons-react'; + +import ContextSubpanel from '@/components/ContextSidePanel'; +import DBInfraPanel from '@/components/DBInfraPanel'; +import { RowSidePanelContext as SharedRowSidePanelContext } from '@/components/DBRowSidePanel'; +import { SourceFrame, Tab } from '@/components/DBRowSidePanel.types'; +import { DBRowSidePanelErrorState } from '@/components/DBRowSidePanelErrorState'; +import { DBSessionPanel, useSessionId } from '@/components/DBSessionPanel'; +import DBTracePanel from '@/components/DBTracePanel'; +import { + DrawerFullWidthToggle, + INITIAL_DRAWER_WIDTH_PERCENT, + INITIAL_ROW_PANEL_WIDTH_PERCENT, + ROW_PANEL_WIDTH_STORAGE_KEY, +} from '@/components/DrawerUtils'; +import LogLevel from '@/components/LogLevel'; +import ServiceMapSidePanel from '@/components/ServiceMap/ServiceMapSidePanel'; +import SidePanelBreadcrumbs, { + BreadcrumbItem, +} from '@/components/SidePanelBreadcrumbs'; +import { SpanLinkData } from '@/components/SpanLinksSubpanel'; +import { renderMs } from '@/components/TimelineChart/utils'; +import TraceLogsPanel from '@/components/TraceLogsPanel'; +import { ViewTraceCalloutButton } from '@/components/ViewTraceCalloutButton'; +import { useCloseOnClickOutside } from '@/hooks/useCloseOnClickOutside'; +import { WithClause } from '@/hooks/useRowWhere'; +import useSidePanelStack, { + reconcileTab, + SidePanelStack, +} from '@/hooks/useSidePanelStack'; +import useWaterfallSearchState from '@/hooks/useWaterfallSearchState'; +import { KeyboardShortcutsModal } from '@/LogSidePanelElements'; +import { getEventBody, useSource } from '@/source'; +import TabBar from '@/TabBar'; +import { SearchConfig } from '@/types'; +import { FormatTime } from '@/useFormatTime'; +import { formatDistanceToNowStrictShort, useLocalStorage } from '@/utils'; +import { getHighlightedAttributesFromData } from '@/utils/highlightedAttributes'; +import { + getRowLookupWindow, + resolveRowTimestampAnchor, +} from '@/utils/rowTimestamps'; +import { useZIndex, ZIndexContext } from '@/zIndex'; + +import { + ROW_DATA_ALIASES, + RowDataPanel, + rowHasK8sContext, + useRowData, +} from './DBRowDataPanel'; +import { RowOverviewPanel } from './DBRowOverviewPanel'; +import DBRowSidePanelHeader from './DBRowSidePanelHeader'; +import useResizable from './useResizable'; + +import styles from '@/../styles/LogSidePanel.module.scss'; + +export type RowSidePanelContextProps = { + onPropertyAddClick?: ( + keyPath: string, + value: string, + action?: 'only' | 'exclude' | 'include', + ) => void; + generateSearchUrl?: ({ + where, + whereLanguage, + source, + }: { + where: SearchConfig['where']; + whereLanguage: SearchConfig['whereLanguage']; + source?: TSource; + }) => string; + /** + * The page `generateSearchUrl` points at authors SQL only (Explore), so + * actions with both forms of an expression must send the SQL one. + */ + sqlOnlySearchUrl?: boolean; + generateChartUrl?: (config: { + aggFn: string; + field: string; + groupBy: string[]; + }) => string; + displayedColumns?: string[]; + toggleColumn?: (column: string) => void; + /** Dimensions the view is currently grouped by, for the toggle's state. */ + groupByFields?: string[]; + /** Only provided where the view has a grouping to change (Explore). */ + toggleGroupBy?: (field: string) => void; + shareUrl?: string; + dbSqlRowTableConfig?: BuilderChartConfigWithDateRange; + isChildModalOpen?: boolean; + setChildModalOpen?: (open: boolean) => void; + source?: TLogSource | TTraceSource; + onOpenLinkedTrace?: (link: SpanLinkData) => void; +}; + +export const RowSidePanelContext = createContext({}); + +// Shared panels (the trace waterfall, field popover, context tab) read the +// context from the shared module. Provide that one too, with the same value, +// so those panels keep working without Explore editing them. +export function ExploreRowSidePanelProvider({ + value, + children, +}: { + value: RowSidePanelContextProps; + children: ReactNode; +}) { + return ( + + {children} + + ); +} + +// Derives the context for rows rendered from `source`, which may differ from +// the source the surrounding search context was built for (e.g. a log event +// opened from a trace waterfall, or a span-link hop into another source's row). +export function deriveRowSidePanelContextForSource( + parentContext: RowSidePanelContextProps, + source: TSource, +): RowSidePanelContextProps { + const { generateSearchUrl } = parentContext; + const sameSource = + parentContext.source == null || parentContext.source.id === source.id; + return { + ...parentContext, + generateSearchUrl: generateSearchUrl + ? args => generateSearchUrl({ ...args, source: args.source ?? source }) + : undefined, + onPropertyAddClick: sameSource + ? parentContext.onPropertyAddClick + : undefined, + displayedColumns: sameSource ? parentContext.displayedColumns : undefined, + toggleColumn: sameSource ? parentContext.toggleColumn : undefined, + groupByFields: sameSource ? parentContext.groupByFields : undefined, + toggleGroupBy: sameSource ? parentContext.toggleGroupBy : undefined, + }; +} + +function SidePanelHeaderActions({ + onClose, + isFullWidth, + onToggleFullWidth, +}: { + onClose: () => void; + isFullWidth?: boolean; + onToggleFullWidth?: () => void; +}) { + const [shortcutsOpen, setShortcutsOpen] = useState(false); + + return ( + <> + + {onToggleFullWidth && ( + + )} + + setShortcutsOpen(true)} + aria-label="Keyboard shortcuts" + > + + + + + {({ copied, copy }) => ( + + + + + + )} + + + + + + + + setShortcutsOpen(false)} + /> + + ); +} + +const SPAN_KIND_LABELS: Record = { + '1': 'Internal', + '2': 'Server', + '3': 'Client', + '4': 'Producer', + '5': 'Consumer', + Internal: 'Internal', + Server: 'Server', + Client: 'Client', + Producer: 'Producer', + Consumer: 'Consumer', + SPAN_KIND_INTERNAL: 'Internal', + SPAN_KIND_SERVER: 'Server', + SPAN_KIND_CLIENT: 'Client', + SPAN_KIND_PRODUCER: 'Producer', + SPAN_KIND_CONSUMER: 'Consumer', +}; + +type DBRowSidePanelProps = { + source: TSource; + rowId: string | undefined; + aliasWith?: WithClause[]; + onClose: () => void; + // Clicking outside the drawer (and outside `keepOpenSelector`) closes it. + // Enabled by default; pass `false` to opt out. + closeOnClickOutside?: boolean; + keepOpenSelector?: string; + /** + * Persist the panel width and open at the narrower row-panel default. + * Explore only — other drawers keep the session-player width. + */ + rememberWidth?: boolean; +}; + +type DBRowSidePanelInnerProps = DBRowSidePanelProps & { + isFullWidth?: boolean; + onToggleFullWidth?: () => void; + persistStacksInUrl?: boolean; + parentBreadcrumbs?: BreadcrumbItem[]; + onNavigateToParent?: () => void; + sidePanelStack: SidePanelStack; +}; + +export const DBRowSidePanelInner = ({ + rowId: initialRowId, + aliasWith: initialAliasWith, + source: rootSource, + onClose, + isFullWidth, + onToggleFullWidth, + parentBreadcrumbs, + onNavigateToParent, + sidePanelStack, +}: DBRowSidePanelInnerProps) => { + const { + sourceStack, + navStack, + tab: persistedTab, + preferredTab, + pushSource, + pushNav, + popOne, + truncateTo, + setTab, + } = sidePanelStack; + + const activeSourceFrame = + sourceStack.length > 0 ? sourceStack[sourceStack.length - 1] : null; + + // Resolve the leaf source (cross-source navigation). Intermediate frames only + // need their stored label/kind for breadcrumbs. + const { + data: activeStackSource, + isLoading: isStackSourceLoading, + isSuccess: isStackSourceSettled, + isError: isStackSourceErrored, + } = useSource({ + id: activeSourceFrame?.sourceId ?? null, + }); + const isResolvingSource = activeSourceFrame != null && isStackSourceLoading; + + const isStackSourceMissing = + activeSourceFrame != null && + ((isStackSourceSettled && activeStackSource == null) || + isStackSourceErrored); + const source = activeStackSource ?? rootSource; + + const baseRowId = activeSourceFrame?.rowId ?? initialRowId; + const baseAliasWith = activeSourceFrame?.aliasWith ?? initialAliasWith; + + const leafNav = navStack.length > 0 ? navStack[navStack.length - 1] : null; + const resolvedRowId = leafNav?.rowId ?? baseRowId; + const resolvedAliasWith = leafNav?.aliasWith ?? baseAliasWith; + + // Avoid querying the (transiently wrong or unavailable) root source with a + // leaf row id while the leaf source is still loading or can no longer be + // resolved. + const skipRowQuery = isResolvingSource || isStackSourceMissing; + const activeRowId = skipRowQuery ? undefined : resolvedRowId; + const activeAliasWith = skipRowQuery ? undefined : resolvedAliasWith; + + // A cross-source frame's row id is synthesized from ids alone ("View Trace" + // builds `TraceId = … AND SpanId = …`), so on its own the lookup has no + // timestamp predicate and scans every part. When the pushing panel stamped + // the origin row's timestamp onto the frame, bound the lookup to a window + // around it. `useRowData` retries unbounded if that window misses. + const frameFocusTimestamp = activeSourceFrame?.focusTimestamp; + const frameDateRange = useMemo<[Date, Date] | undefined>( + () => + // Nav entries carry a full row id (timestamp included), so their lookups + // are already bounded and must not be narrowed by the frame's window — + // surrounding context can walk arbitrarily far from the frame's anchor. + leafNav != null ? undefined : getRowLookupWindow(frameFocusTimestamp), + [leafNav, frameFocusTimestamp], + ); + + const activeDateRange = skipRowQuery ? undefined : frameDateRange; + + const { + data: rowData, + isLoading: isRowLoading, + isSuccess: isRowSuccess, + isError: isRowError, + error: rowError, + } = useRowData({ + source, + rowId: activeRowId, + aliasWith: activeAliasWith, + dateRange: activeDateRange, + }); + + const hasActiveStacks = activeSourceFrame != null || leafNav != null; + + const parentContext = use(RowSidePanelContext); + // Nested rows shouldn't inherit the parent table's row config. + const dbSqlRowTableConfig = hasActiveStacks + ? undefined + : parentContext.dbSqlRowTableConfig; + + const hasOverviewPanel = useMemo(() => { + if (isLogSource(source) || isTraceSource(source)) { + if ( + source.resourceAttributesExpression || + source.eventAttributesExpression + ) { + return true; + } + } else if ( + source.kind === SourceKind.Metric && + source.resourceAttributesExpression + ) { + return true; + } else if (source.kind === SourceKind.Promql) { + return false; + } + return false; + }, [source]); + + const sourceIsTrace = source.kind === SourceKind.Trace; + + const defaultTab = sourceIsTrace + ? Tab.Trace + : hasOverviewPanel + ? Tab.Overview + : Tab.Parsed; + + const handleNavigateToRow = useCallback( + ( + rowId: string, + aliasWith: WithClause[], + label: string, + sourceKind?: SourceKind, + ) => { + // Same-source drilldown (e.g. picking a neighbour out of surrounding + // context) changes which row is shown, not which view of it, so keep the + // reader in their remembered view. reconcileTab drops it if the + // destination doesn't offer that tab. + pushNav( + { rowId, aliasWith, label, sourceKind }, + preferredTab ?? defaultTab, + ); + }, + [pushNav, preferredTab, defaultTab], + ); + + const handleSourceStackPush = useCallback( + (frame: SourceFrame) => { + // Cross-source push (e.g. "View Trace") → jump to the destination + // source's default tab. + const destinationTab = + frame.sourceKind === SourceKind.Trace ? Tab.Trace : Tab.Overview; + pushSource(frame, destinationTab); + }, + [pushSource], + ); + + const handlePanelBack = useCallback(() => { + // Pop one level (nav → source), restoring the tab active before that level + // was entered. When the trail is empty, leave the panel: hand off to an + // embedding parent (session) or close. + if (popOne() === 'none') { + if (onNavigateToParent) { + onNavigateToParent(); + } else { + onClose(); + } + } + }, [popOne, onNavigateToParent, onClose]); + + useHotkeys(['esc'], handlePanelBack); + + const handleBreadcrumbNavigation = useCallback( + (sourceLevel: number, navLevel: number) => + truncateTo(sourceLevel, navLevel), + [truncateTo], + ); + + const normalizedRow = rowData?.data?.[0]; + const timestampValue = normalizedRow?.['__hdx_timestamp']; + + let timestampDate: Date; + if (typeof timestampValue === 'number') { + timestampDate = new Date(timestampValue * 1000); + } else { + timestampDate = new Date(timestampValue); + } + + const mainContentColumn = getEventBody(source); + const mainContent = isString(normalizedRow?.['__hdx_body']) + ? normalizedRow['__hdx_body'] + : normalizedRow?.['__hdx_body'] !== undefined + ? JSON.stringify(normalizedRow['__hdx_body']) + : undefined; + const severityText: string | undefined = + normalizedRow?.['__hdx_severity_text']; + + // Owns the View Trace nudge's auto-open latch. Kept here (above the loading + // gate that unmounts the row content on every navigation) so the one-time + // callout doesn't reset and re-open as the user pages between rows. Resets + // when the panel closes and this component unmounts. + const [viewTraceCalloutAutoOpened, setViewTraceCalloutAutoOpened] = + useState(false); + const handleViewTraceCalloutAutoOpen = useCallback( + () => setViewTraceCalloutAutoOpened(true), + [], + ); + + // Capture the root event body once for the root breadcrumb label. + const [initialMainContent, setInitialMainContent] = useState< + string | undefined + >(undefined); + + useEffect(() => { + if (mainContent != null && initialMainContent == null && !hasActiveStacks) { + // eslint-disable-next-line react-hooks/set-state-in-effect + setInitialMainContent(mainContent); + } + }, [mainContent, initialMainContent, hasActiveStacks]); + + // Once the hop's row loads we learn the landed span name and use it as the crumb label. + const spanLinkFrameRowIdsRef = useRef>(new Set()); + const [resolvedFrameLabels, setResolvedFrameLabels] = useState< + Record + >({}); + + useEffect(() => { + if ( + activeRowId != null && + mainContent != null && + spanLinkFrameRowIdsRef.current.has(activeRowId) && + resolvedFrameLabels[activeRowId] !== mainContent + ) { + // eslint-disable-next-line react-hooks/set-state-in-effect + setResolvedFrameLabels(prev => ({ ...prev, [activeRowId]: mainContent })); + } + }, [activeRowId, mainContent, resolvedFrameLabels]); + + const highlightedAttributeValues = useMemo(() => { + const attributeExpressions: NonNullable< + (TLogSource | TTraceSource)['highlightedRowAttributeExpressions'] + > = []; + if ( + (source.kind === SourceKind.Trace || source.kind === SourceKind.Log) && + source.highlightedRowAttributeExpressions + ) { + attributeExpressions.push(...source.highlightedRowAttributeExpressions); + } + + // Add service name expression to all sources, to maintain compatibility with + // the behavior prior to the addition of highlightedRowAttributeExpressions + if ( + (isLogSource(source) || isTraceSource(source)) && + source.serviceNameExpression + ) { + attributeExpressions.push({ + sqlExpression: source.serviceNameExpression, + }); + } + + return rowData + ? getHighlightedAttributesFromData( + source, + attributeExpressions, + rowData.data || [], + rowData.meta || [], + ) + : []; + }, [source, rowData]); + + const oneHourRange = useMemo(() => { + return [ + add(timestampDate, { minutes: -60 }), + add(timestampDate, { minutes: 60 }), + ] as [Date, Date]; + }, [timestampDate]); + + // For session replay, we need +/-4 hours to get full session + const fourHourRange = useMemo(() => { + return [ + add(timestampDate, { hours: -4 }), + add(timestampDate, { hours: 4 }), + ] as [Date, Date]; + }, [timestampDate]); + + const focusDate = timestampDate; + // Coerce empty / falsy trace ids to undefined so "View Trace" / Trace ID + // is hidden for logs without trace context. + const traceId: string | undefined = + normalizedRow?.['__hdx_trace_id'] || undefined; + + const childSourceId = isLogSource(source) + ? source.traceSourceId + : isTraceSource(source) + ? source.logSourceId + : undefined; + + const traceSourceId = isTraceSource(source) + ? source.id + : isLogSource(source) + ? source.traceSourceId + : source.kind === SourceKind.Session + ? source.traceSourceId + : undefined; + + const traceLogSourceId = isTraceSource(source) + ? childSourceId + : isLogSource(source) + ? source.id + : undefined; + const enableTraceLogs = !!traceId && !!traceLogSourceId; + const isOwnTraceLogSource = traceLogSourceId === source.id; + + const enableServiceMap = traceId && traceSourceId; + + const { data: traceSourceData } = useSource({ id: traceSourceId }); + + const spanId = normalizedRow?.['__hdx_span_id']; + const traceIdExpression = + traceSourceData?.kind === SourceKind.Log || + traceSourceData?.kind === SourceKind.Trace + ? traceSourceData.traceIdExpression + : undefined; + const spanIdExpression = + traceSourceData?.kind === SourceKind.Log || + traceSourceData?.kind === SourceKind.Trace + ? traceSourceData.spanIdExpression + : undefined; + + // The current row's own timestamp, read from the source's + // `timestampValueExpression` rather than the displayed expression (which + // may not be in the ordering key). Undefined when the source exposes no + // better-than-day precision, so a composite `"EventDate, EventTime"` can't + // anchor a window on `EventDate`'s midnight. + const rowMeta = rowData?.meta; + const rowFocusTimestamp = useMemo( + () => + resolveRowTimestampAnchor({ + timestampValueExpression: source.timestampValueExpression, + row: normalizedRow, + meta: rowMeta, + })?.toISOString(), + [source.timestampValueExpression, normalizedRow, rowMeta], + ); + + const traceSpanRowId = useMemo(() => { + const clauses: string[] = []; + if (traceIdExpression && traceId) { + clauses.push( + SqlString.format('?=?', [SqlString.raw(traceIdExpression), traceId]), + ); + } + if (spanIdExpression && spanId) { + clauses.push( + SqlString.format('?=?', [SqlString.raw(spanIdExpression), spanId]), + ); + } + return clauses.length > 0 ? clauses.join(' AND ') : undefined; + }, [traceIdExpression, traceId, spanIdExpression, spanId]); + + const handleSessionEventNavigate = useCallback( + (rowId: string, aliasWith: WithClause[]) => { + if (traceSourceData) { + handleSourceStackPush({ + sourceId: traceSourceData.id, + rowId, + aliasWith, + label: mainContent || 'Session Replay', + sourceKind: traceSourceData.kind as SourceKind, + }); + } + }, + [traceSourceData, handleSourceStackPush, mainContent], + ); + + const handleTraceLogNavigate = useCallback( + (rowId: string, aliasWith: WithClause[], label: string) => { + if (traceLogSourceId == null) { + return; + } + // The row on screen is one of the logs listed, so this is a row change. + if (isOwnTraceLogSource) { + handleNavigateToRow(rowId, aliasWith, label, SourceKind.Log); + return; + } + handleSourceStackPush({ + sourceId: traceLogSourceId, + rowId, + aliasWith, + label, + sourceKind: SourceKind.Log, + // Every log in the trace is within seconds of the origin span. + focusTimestamp: rowFocusTimestamp, + }); + }, + [ + traceLogSourceId, + isOwnTraceLogSource, + handleNavigateToRow, + handleSourceStackPush, + rowFocusTimestamp, + ], + ); + + // "Open trace" on a span link: the link carries only the linked span's + // TraceId + SpanId, so resolve it against the current trace source. + const handleOpenLinkedTrace = useCallback( + (link: SpanLinkData) => { + if (!traceSourceData || !traceIdExpression || !spanIdExpression) { + return; + } + const rowId = [ + SqlString.format('?=?', [ + SqlString.raw(traceIdExpression), + link.TraceId, + ]), + SqlString.format('?=?', [SqlString.raw(spanIdExpression), link.SpanId]), + ].join(' AND '); + // A link often points right back at the row one level up (forward and + // reverse span links come in pairs). Pop back to that breadcrumb instead + // of pushing an endless A <-> B trail. Only when no nav drilldown sits + // on top: popOne would pop the nav entry, not the source frame. + const topFrame = + sourceStack.length > 0 ? sourceStack[sourceStack.length - 1] : null; + if ( + navStack.length === 0 && + topFrame?.originRowId != null && + topFrame.originRowId === rowId + ) { + popOne(); + return; + } + // Mark this frame so its breadcrumb switches from the `Trace ` + // fallback below to the landed span name once the row loads. + spanLinkFrameRowIdsRef.current.add(rowId); + handleSourceStackPush({ + sourceId: traceSourceData.id, + rowId, + label: `Trace ${link.TraceId.slice(0, 8)}`, + sourceKind: traceSourceData.kind as SourceKind, + aliasWith: [], + // The current row's canonical trace/span row id: the exact string a + // link hop targeting this row would compute, so the pop-back check + // above can compare by equality. + originRowId: traceSpanRowId, + }); + }, + [ + traceSourceData, + traceIdExpression, + spanIdExpression, + handleSourceStackPush, + sourceStack, + navStack, + popOne, + traceSpanRowId, + ], + ); + + const rowSidePanelContextValue = useMemo( + () => ({ + // The displayed row belongs to the resolved leaf source, which can + // differ from the searched source after a cross-source hop. + ...deriveRowSidePanelContextForSource(parentContext, source), + onOpenLinkedTrace: handleOpenLinkedTrace, + }), + [parentContext, handleOpenLinkedTrace, source], + ); + + const { rumSessionId, rumServiceName } = useSessionId({ + sourceId: traceSourceId, + traceId, + dateRange: oneHourRange, + enabled: activeRowId != null, + }); + + const hasK8sContext = useMemo( + () => rowHasK8sContext(source, normalizedRow), + [source, normalizedRow], + ); + + const initialRowHighlightHint = useMemo(() => { + if (normalizedRow) { + return { + timestamp: normalizedRow['__hdx_timestamp'], + spanId: normalizedRow['__hdx_span_id'], + body: normalizedRow['__hdx_body'], + }; + } + }, [normalizedRow]); + + const durationMs = normalizedRow?.[ROW_DATA_ALIASES.DURATION_MS]; + const spanKind = normalizedRow?.[ROW_DATA_ALIASES.SPAN_KIND]; + const serviceName = normalizedRow?.[ROW_DATA_ALIASES.SERVICE_NAME]; + const statusCode = normalizedRow?.[ROW_DATA_ALIASES.SEVERITY_TEXT]; + + const formattedDuration = useMemo(() => { + if (durationMs == null || isNaN(Number(durationMs))) return undefined; + return renderMs(Number(durationMs)); + }, [durationMs]); + + const spanKindLabel = useMemo(() => { + if (spanKind == null) return undefined; + return SPAN_KIND_LABELS[String(spanKind)] ?? String(spanKind); + }, [spanKind]); + + const allBreadcrumbs = useMemo((): BreadcrumbItem[] => { + const items: BreadcrumbItem[] = []; + + if (parentBreadcrumbs) { + items.push(...parentBreadcrumbs); + } + + const crumbSourceStack = sourceStack; + const crumbNavStack = navStack; + const hasStack = crumbSourceStack.length > 0 || crumbNavStack.length > 0; + const rootLabel = + initialMainContent || + (rootSource.kind === SourceKind.Trace ? 'Trace' : 'Log'); + + if (hasStack) { + items.push({ + label: rootLabel, + sourceKind: rootSource.kind as SourceKind, + onClick: () => handleBreadcrumbNavigation(0, 0), + }); + } + + crumbSourceStack.forEach((entry, i) => { + const isLeafSource = i === crumbSourceStack.length - 1; + const isCurrent = isLeafSource && crumbNavStack.length === 0; + items.push({ + // Span-link hops resolve to the landed span name once loaded; every + // other frame keeps the label it was pushed with. + label: resolvedFrameLabels[entry.rowId] ?? entry.label, + sourceKind: entry.sourceKind, + onClick: isCurrent + ? undefined + : () => handleBreadcrumbNavigation(i + 1, 0), + }); + }); + + crumbNavStack.forEach((entry, i) => { + const isCurrent = i === crumbNavStack.length - 1; + items.push({ + label: entry.label, + sourceKind: entry.sourceKind, + onClick: isCurrent + ? undefined + : () => handleBreadcrumbNavigation(crumbSourceStack.length, i + 1), + }); + }); + + if (!hasStack) { + items.push({ + label: mainContent || (sourceIsTrace ? 'Trace' : 'Log'), + sourceKind: source.kind as SourceKind, + }); + } + + return items; + }, [ + sourceStack, + navStack, + rootSource.kind, + sourceIsTrace, + mainContent, + initialMainContent, + resolvedFrameLabels, + source.kind, + handleBreadcrumbNavigation, + parentBreadcrumbs, + ]); + + const availableTabs = useMemo(() => { + const tabs: Tab[] = []; + if (hasOverviewPanel && !sourceIsTrace) tabs.push(Tab.Overview); + if (!sourceIsTrace) tabs.push(Tab.Parsed); + if (sourceIsTrace) tabs.push(Tab.Trace); + if (enableTraceLogs) tabs.push(Tab.Logs); + if (enableServiceMap) tabs.push(Tab.ServiceMap); + tabs.push(Tab.Context); + if (rumSessionId != null) tabs.push(Tab.Replay); + if (hasK8sContext) tabs.push(Tab.Infrastructure); + return tabs; + }, [ + hasOverviewPanel, + sourceIsTrace, + enableTraceLogs, + enableServiceMap, + rumSessionId, + hasK8sContext, + ]); + + const displayedTab = reconcileTab(persistedTab, availableTabs, defaultTab); + + const controls = useMemo( + () => ( + + + + + ), + [allBreadcrumbs, handlePanelBack, onClose, isFullWidth, onToggleFullWidth], + ); + + if (isRowLoading || isResolvingSource) { + return
Loading...
; + } + + // The leaf cross-source frame points at a source that no longer resolves + // (deleted / renamed / another workspace via a shared link). Render an + // explicit message that keeps the breadcrumb trail, Back, and Close controls + // working instead of hanging on an indefinite "Loading...". + if (isStackSourceMissing) { + return ( + <> + + {controls} + + + + This source is no longer available. It may have been deleted, + renamed, or belong to a different workspace. Use the back button or + the breadcrumbs above to return. + + + + ); + } + + if (!isRowSuccess) { + if (isRowError && rowError) { + return ( + <> + + {controls} + + + + + + ); + } + return ( + <> + + {controls} + +
Error loading row data
+ + ); + } + + const showLogTraceActions = !sourceIsTrace && traceId && traceSourceId; + + return ( + + + {controls} + + {!sourceIsTrace && severityText && } + {timestampDate && !isNaN(timestampDate.getTime()) && ( + + ·{' '} + {formatDistanceToNowStrictShort(timestampDate)} ago + + )} + {serviceName && ( + <> + + · + + + + Service + + + {serviceName} + + + + )} + {sourceIsTrace && formattedDuration && ( + <> + + · + + + + Duration + + + {formattedDuration} + + + + )} + {sourceIsTrace && statusCode && ( + <> + + · + + + + Status + + + {statusCode} + + + + )} + {sourceIsTrace && spanKindLabel && ( + + {spanKindLabel} + + )} + {(sourceIsTrace ? traceId : showLogTraceActions) && ( + <> + + · + + + {({ copied, copy }) => ( + + + + + Trace ID + + + + )} + + + )} + {showLogTraceActions && ( + { + if (traceSourceData && traceSpanRowId) { + handleSourceStackPush({ + sourceId: traceSourceData.id, + rowId: traceSpanRowId, + label: mainContent || 'Log', + sourceKind: traceSourceData.kind as SourceKind, + aliasWith: [], + focusTimestamp: rowFocusTimestamp, + }); + } + }} + /> + )} + + + + setTab(v)} + /> + {displayedTab === Tab.Overview && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + +
+ )} + {displayedTab === Tab.Trace && sourceIsTrace && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + + + +
+ )} + {displayedTab === Tab.Logs && traceLogSourceId && traceId && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + +
+ )} + {displayedTab === Tab.ServiceMap && enableServiceMap && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + + + +
+ )} + {displayedTab === Tab.Parsed && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + +
+ )} + {displayedTab === Tab.Context && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + +
+ )} + {displayedTab === Tab.Replay && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > +
+ +
+
+ )} + {displayedTab === Tab.Infrastructure && ( + { + console.error(err); + }} + fallbackRender={() => ( +
+ An error occurred while rendering this event. +
+ )} + > + + + +
+ )} +
+ ); +}; + +export const SidePanelErrorFallback = ({ + error, + onClose, +}: FallbackProps & { onClose: () => void }) => ( + + + + +
+ An error occurred while rendering this event. +
+ +
+ {error?.message} +
+
+); + +export default function DBRowSidePanelErrorBoundary({ + onClose, + rowId, + aliasWith, + source, + closeOnClickOutside = true, + keepOpenSelector, + rememberWidth = false, +}: DBRowSidePanelProps) { + const contextZIndex = useZIndex(); + const drawerZIndex = contextZIndex + 10; + + // Explore remembers the width across reloads. Other pages keep the wider + // session-player default and do not write it to storage. + const [storedWidth, setStoredWidth] = useLocalStorage( + ROW_PANEL_WIDTH_STORAGE_KEY, + INITIAL_ROW_PANEL_WIDTH_PERCENT, + ); + const { size, setSize, startResize } = useResizable( + rememberWidth ? storedWidth : INITIAL_DRAWER_WIDTH_PERCENT, + 'right', + rememberWidth ? setStoredWidth : undefined, + ); + + const isFullWidth = size >= 99; + const toggleFullWidth = useCallback(() => { + const next = isFullWidth + ? rememberWidth + ? INITIAL_ROW_PANEL_WIDTH_PERCENT + : INITIAL_DRAWER_WIDTH_PERCENT + : 100; + setSize(next); + if (rememberWidth) { + setStoredWidth(next); + } + }, [isFullWidth, rememberWidth, setSize, setStoredWidth]); + + const { clear: clearTraceWaterfallSearchState } = useWaterfallSearchState({}); + + const sidePanelStack = useSidePanelStack({ initialRowId: rowId }); + + const _onClose = useCallback(() => { + // Reset the tab and navigation stacks so re-opening the drawer starts fresh. + sidePanelStack.clearTrail(); + // Clear waterfall search state on close, so that filters don't + // persist when reopening another trace. + clearTraceWaterfallSearchState(); + onClose(); + }, [sidePanelStack, onClose, clearTraceWaterfallSearchState]); + + useCloseOnClickOutside({ + // Only close on outside click at the root level. When the user has + // navigated deeper (e.g. log row -> trace), Esc pops one level at a time + // via handlePanelBack; an outside click should not skip those levels and + // close the drawer entirely. + enabled: + closeOnClickOutside && + rowId != null && + sidePanelStack.sourceStack.length === 0 && + sidePanelStack.navStack.length === 0, + keepOpenSelector, + onClose: _onClose, + }); + + return ( + + +
+ + ( + + )} + > + + +
+
+
+ ); +} diff --git a/packages/app/src/components/Explore/owned/DBRowSidePanelHeader.tsx b/packages/app/src/components/Explore/owned/DBRowSidePanelHeader.tsx new file mode 100644 index 0000000000..d264c1e7db --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBRowSidePanelHeader.tsx @@ -0,0 +1,144 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import { Box, Button, Flex, Paper, Text } from '@mantine/core'; +import { + IconArrowsDiagonal, + IconArrowsDiagonalMinimize2, +} from '@tabler/icons-react'; + +import AISummarizeButton from '@/components/AISummarizeButton'; +import { useUserPreferences } from '@/useUserPreferences'; + +import { + DBHighlightedAttributesList, + HighlightedAttribute, +} from './DBHighlightedAttributesList'; + +const MAX_MAIN_CONTENT_LENGTH = 2000; + +export default function DBRowSidePanelHeader({ + attributes, + mainContent = '', + mainContentHeader, + // When `true`, the source has a body column configured. An empty value + // for that column renders a soft empty-state paper. When `false` (the + // source has neither body nor implicit column configured), the body + // paper is suppressed entirely; the highlighted attributes still render. + bodyConfigured = true, + severityText, + rowData, +}: { + mainContent?: string; + mainContentHeader?: string; + bodyConfigured?: boolean; + attributes?: HighlightedAttribute[]; + severityText?: string; + rowData?: Record; +}) { + const [bodyExpanded, setBodyExpanded] = React.useState(false); + + const isContentTruncated = mainContent.length > MAX_MAIN_CONTENT_LENGTH; + const mainContentDisplayed = React.useMemo( + () => + bodyExpanded + ? mainContent + : mainContent?.slice(0, MAX_MAIN_CONTENT_LENGTH), + [bodyExpanded, mainContent], + ); + + const [headerElement, setHeaderElement] = useState( + null, + ); + const [headerHeight, setHeaderHeight] = useState(0); + useEffect(() => { + if (!headerElement) return; + + const updateHeight = () => { + const newHeight = headerElement.offsetHeight; + setHeaderHeight(newHeight); + }; + updateHeight(); + + // Set up a resize observer to detect height changes + const resizeObserver = new ResizeObserver(updateHeight); + resizeObserver.observe(headerElement); + + // Clean up the observer on component unmount + return () => { + resizeObserver.disconnect(); + }; + }, [headerElement]); + + const { userPreferences, setUserPreference } = useUserPreferences(); + const { expandSidebarHeader } = userPreferences; + const maxBoxHeight = 120; + + const attributesWithDefault = useMemo(() => { + return attributes ?? []; + }, [attributes]); + + const toggleExpandSidebarHeader = useCallback(() => { + setUserPreference({ + ...userPreferences, + expandSidebarHeader: !expandSidebarHeader, + }); + }, [expandSidebarHeader, setUserPreference, userPreferences]); + + return ( + <> + {!bodyConfigured ? null : mainContent ? ( + + + {mainContentHeader} + {/* Toggles expanded sidebar header*/} + {headerHeight >= maxBoxHeight && ( + + )} + + {mainContentDisplayed} + {isContentTruncated && ( + + )} + + ) : ( + + + No body for this event. + + + )} + + + + + + ); +} diff --git a/packages/app/src/components/Explore/owned/DBRowTable.tsx b/packages/app/src/components/Explore/owned/DBRowTable.tsx new file mode 100644 index 0000000000..e8dc2f5c59 --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBRowTable.tsx @@ -0,0 +1,2143 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import React, { + memo, + use, + useCallback, + useEffect, + useMemo, + useState, +} from 'react'; +import cx from 'classnames'; +import { formatDistance } from 'date-fns'; +import { isString } from 'lodash'; +import curry from 'lodash/curry'; +import ms from 'ms'; +import { useHotkeys } from 'react-hotkeys-hook'; +import { + Bar, + BarChart, + ResponsiveContainer, + Tooltip, + XAxis, + YAxis, +} from 'recharts'; +import { + chSqlToAliasMap, + ClickHouseQueryError, + ColumnMetaType, + convertCHDataTypeToJSType, + extractColumnReferencesFromKey, + isJSDataTypeJSONStringifiable, + JSDataType, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; +import { + DENOISE_NOISE_THRESHOLD, + DENOISE_SAMPLE_SIZE, +} from '@hyperdx/common-utils/dist/drain'; +import { + BuilderChartConfigWithDateRange, + RawSqlChartConfig, + SelectList, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { + Box, + Divider, + Flex, + Group, + Modal, + Text, + Tooltip as MantineTooltip, + UnstyledButton, +} from '@mantine/core'; +import { + IconCode, + IconDownload, + IconRefresh, + IconRotateClockwise, + IconSettings, + IconTextWrap, + IconTextWrapDisabled, +} from '@tabler/icons-react'; +import { FetchNextPageOptions, useQuery } from '@tanstack/react-query'; +import { + ColumnDef, + ColumnResizeMode, + flexRender, + getCoreRowModel, + Row as TableRow, + SortingState, + TableOptions, + useReactTable, +} from '@tanstack/react-table'; +import { useVirtualizer } from '@tanstack/react-virtual'; + +import api from '@/api'; +import { useChartSyncId } from '@/chartSync'; +import ChartErrorState, { + ChartErrorStateVariant, +} from '@/components/charts/ChartErrorState'; +import { SQLPreview } from '@/components/ChartSQLPreview'; +import { CsvExportButton } from '@/components/CsvExportButton'; +import DBRowTableFieldWithPopover from '@/components/DBTable/DBRowTableFieldWithPopover'; +import DBRowTableRowButtons from '@/components/DBTable/DBRowTableRowButtons'; +import { + ROW_SELECTION_COLUMN_WIDTH, + RowSelectionCell, + RowSelectionHeaderCell, +} from '@/components/DBTable/RowSelectionCells'; +import { RowSelectionMenu } from '@/components/DBTable/RowSelectionMenu'; +import TableHeader from '@/components/DBTable/TableHeader'; +import { + highlightText, + TableSearchInput, + TableSearchMatchIndicator, +} from '@/components/DBTable/TableSearchInput'; +import { + createExpandButtonColumn, + ExpandedLogRow, + useExpandableRows, +} from '@/components/ExpandableRowTable'; +import LogLevel from '@/components/LogLevel'; +import { PatternTemplate } from '@/components/Patterns/PatternTemplate'; +import { TableDisplaySettings } from '@/components/Search/TableDisplaySettings'; +import { searchChartConfigDefaults } from '@/defaults'; +import { + useAliasMapFromChartConfig, + useRenderedSqlChartConfig, +} from '@/hooks/useChartConfig'; +import { CsvColumn, useCsvExport } from '@/hooks/useCsvExport'; +import { useColumns, useTableMetadata } from '@/hooks/useMetadata'; +import useOffsetPaginatedQuery from '@/hooks/useOffsetPaginatedQuery'; +import { useGroupedPatterns } from '@/hooks/usePatterns'; +import { useRowSelection } from '@/hooks/useRowSelection'; +import useRowWhere, { + getRowId, + INTERNAL_ROW_FIELDS, + RowWhereResult, + WithClause, +} from '@/hooks/useRowWhere'; +import { useTableSearch } from '@/hooks/useTableSearch'; +import { getLevelExpression, useSource } from '@/source'; +import { + MIN_COLUMN_WIDTH, + MIN_LAST_COLUMN_WIDTH, + UNDEFINED_WIDTH, +} from '@/tableUtils'; +import { getTimeZoneShortName } from '@/timeZone'; +import { useDisplayTimeZone } from '@/useDisplayTimeZone'; +import { FormatTime } from '@/useFormatTime'; +import { + getChartColorInfo, + getLogLevelClass, + logLevelColor, + useLocalStorage, + usePrevious, +} from '@/utils'; +import { csvExportFilename } from '@/utils/csv'; + +import { RowSidePanelContext } from './DBRowSidePanel'; + +import styles from '@styles/LogTable.module.scss'; + +type Row = Record & { duration: number }; +type AccessorFn = (row: Row, column: string) => any; + +const SPECIAL_VALUES = { + not_available: 'NULL', +}; +const ACCESSOR_MAP: Record = { + duration: row => + row.duration >= 0 ? row.duration : SPECIAL_VALUES.not_available, + default: (row, column) => row[column], +}; + +const MAX_SCROLL_FETCH_LINES = 1000; +const EXPAND_COLUMN_SIZE = 32; +const MAX_CELL_LENGTH = 500; +const MAX_CELL_LENGTH_WRAPPED = 50_000; + +function retrieveColumnValue(column: string, row: Row): any { + const accessor = ACCESSOR_MAP[column] ?? ACCESSOR_MAP.default; + return accessor(row, column); +} + +function getResolvedColumnSize( + column: string, + opts: { + aliasMap?: Record; + columnTypeMap: Map; + logLevelColumn?: string; + columnSizeStorage: Record; + }, +): number { + const columnId = opts.aliasMap?.[column] ? `"${column}"` : column; + const stored = opts.columnSizeStorage[columnId]; + if (stored != null) return stored; + const jsType = opts.columnTypeMap.get(column)?._type; + if (jsType === JSDataType.Date) return 170; + if (column === opts.logLevelColumn) return 115; + return 160; +} + +function inferLogLevelColumn(rows: Record[]) { + const MAX_ROWS_TO_INSPECT = 100; + const levelCounts: Record = {}; + const inspectRowCount = Math.min(rows.length, MAX_ROWS_TO_INSPECT); + for (let i = 0; i < inspectRowCount; i++) { + const row = rows[i]; + Object.keys(row).forEach(key => { + const value = row[key]; + if ( + (value?.length || 0) > 0 && + (value?.length || 0) < 512 && // avoid inspecting long strings + isString(value) && + getLogLevelClass(value) != null + ) { + levelCounts[key] = (levelCounts[key] ?? 0) + 1; + } + }); + } + + let maxCount = 0; + let maxKey = ''; + for (const [key, count] of Object.entries(levelCounts)) { + if (count > maxCount) { + maxCount = count; + maxKey = key; + } + } + + if (maxCount > 0) { + return maxKey; + } + + return undefined; +} + +const PatternTrendChartTooltip = () => { + return null; +}; + +const PatternTrendChart = ({ + data, + dateRange, + color, +}: { + data: { bucket: string; count: number }[]; + dateRange: [Date, Date]; + color?: string; +}) => { + const syncId = useChartSyncId(); + return ( +
+
+ + + + // format(new Date(tick * 1000), 'MMM d HH:mm') + // } + tickFormatter={() => ''} + minTickGap={50} + tick={{ fontSize: 12, fontFamily: 'IBM Plex Mono, monospace' }} + /> + + new Intl.NumberFormat('en-US', { + notation: 'compact', + compactDisplay: 'short', + }).format(value) + } + tick={{ fontSize: 12, fontFamily: 'IBM Plex Mono, monospace' }} + /> + + } /> + + +
+
+ ); +}; + +const SqlModal = ({ + opened, + onClose, + config, +}: { + opened: boolean; + onClose: () => void; + config: BuilderChartConfigWithDateRange; +}) => { + const { data: sql, isLoading: isLoadingSql } = useRenderedSqlChartConfig( + config, + { + queryKey: ['SqlModal', config], + placeholderData: prev => prev ?? '', // Avoid flicker when query changes (eg. when in live mode) + enabled: opened, + }, + ); + + return ( + + {sql ? ( + + ) : isLoadingSql ? ( +
+
+ +
+ Loading SQL... +
+ ) : ( +
No SQL available
+ )} +
+ ); +}; + +export const RawLogTable = memo( + ({ + tableId, + displayedColumns, + fetchNextPage, + hasNextPage, + highlightedLineId, + isLive, + isLoading, + rows, + generateRowId, + onRowDetailsClick, + onScroll, + onSettingsClick, + wrapLines = false, + columnNameMap, + dedupRows, + isError, + error, + errorVariant = 'inline', + columnTypeMap, + dateRange, + loadingDate, + config, + onChildModalOpen, + renderRowDetails, + source, + onExpandedRowsChange, + collapseAllRows, + enableSorting = false, + onSortingChange, + sortOrder, + showExpandButton = true, + enableRowSelection = false, + selectionResetKey, + onSelectedRowsChange, + getRowWhere, + variant = 'default', + onRemoveColumn, + tableBarRightSection, + formatPatternColumn = false, + }: { + wrapLines?: boolean; + displayedColumns: string[]; + onSettingsClick?: () => void; + rows: Record[]; + isLoading?: boolean; + fetchNextPage?: (options?: FetchNextPageOptions | undefined) => any; + onRowDetailsClick: (row: Record) => void; + generateRowId: (row: Record) => RowWhereResult; + hasNextPage?: boolean; + highlightedLineId?: string; + onScroll?: (scrollTop: number) => void; + isLive?: boolean; + tableId?: string; + columnNameMap?: Record; + dedupRows?: boolean; + columnTypeMap: Map; + + isError?: boolean; + error?: ClickHouseQueryError | Error; + errorVariant?: ChartErrorStateVariant; + dateRange?: [Date, Date]; + loadingDate?: Date; + config?: BuilderChartConfigWithDateRange; + onChildModalOpen?: (open: boolean) => void; + source?: TSource; + onExpandedRowsChange?: (hasExpandedRows: boolean) => void; + collapseAllRows?: boolean; + showExpandButton?: boolean; + enableRowSelection?: boolean; + selectionResetKey?: string; + onSelectedRowsChange?: (hasSelectedRows: boolean) => void; + renderRowDetails?: (row: { + id: string; + aliasWith?: WithClause[]; + [key: string]: any; + }) => React.ReactNode; + enableSorting?: boolean; + sortOrder?: SortingState; + onSortingChange?: (v: SortingState | null) => void; + getRowWhere?: (row: Record) => RowWhereResult; + variant?: DBRowTableVariant; + onRemoveColumn?: (column: string) => void; + /** When defined (including `null`), the table actions move from the header + * into a bar attached to the top of the table, with this at its right end. */ + tableBarRightSection?: React.ReactNode; + /** Highlight Drain templates. Explore only. */ + formatPatternColumn?: boolean; + }) => { + const hasTableBar = tableBarRightSection !== undefined; + const dedupedRows = useMemo(() => { + const lIds = new Set(); + const returnedRows = dedupRows + ? rows.filter(l => { + const rowWhereResult = generateRowId(l); + if (lIds.has(rowWhereResult.where)) { + return false; + } + lIds.add(rowWhereResult.where); + return true; + }) + : rows; + + return returnedRows.map(r => { + const rowWhereResult = generateRowId(r); + return { + ...r, + [INTERNAL_ROW_FIELDS.ID]: rowWhereResult.where, + [INTERNAL_ROW_FIELDS.ALIAS_WITH]: rowWhereResult.aliasWith, + }; + }); + }, [rows, dedupRows, generateRowId]); + + const _onRowExpandClick = useCallback( + (row: Record) => { + onRowDetailsClick?.(row); + }, + [onRowDetailsClick], + ); + + const timeZone = useDisplayTimeZone(); + + const [columnSizeStorage, setColumnSizeStorage] = useLocalStorage< + Record + >(`${tableId}-column-sizes`, {}); + + //once the user has scrolled within 500px of the bottom of the table, fetch more data if there is any + const FETCH_NEXT_PAGE_PX = 500; + + //we need a reference to the scrolling element for logic down below + const [tableContainerRef, setTableContainerRef] = + useState(null); + const tableContainerRefCallback = useCallback( + (node: HTMLDivElement): (() => void) => { + if (node) { + setTableContainerRef(node); + } + return () => { + setTableContainerRef(null); + }; + }, + [], + ); + + const [containerWidth, setContainerWidth] = useState(0); + useEffect(() => { + if (!tableContainerRef) return; + setContainerWidth(tableContainerRef.clientWidth); + const observer = new ResizeObserver(entries => { + for (const entry of entries) { + setContainerWidth(entry.contentRect.width); + } + }); + observer.observe(tableContainerRef); + return () => observer.disconnect(); + }, [tableContainerRef]); + + // Get the alias map from the config so we resolve correct column ids + const { data: aliasMap } = useAliasMapFromChartConfig(config); + + // Reset scroll when live tail is enabled for the first time + const prevIsLive = usePrevious(isLive); + useEffect(() => { + if (isLive && prevIsLive === false && tableContainerRef != null) { + tableContainerRef.scrollTop = 0; + } + }, [isLive, prevIsLive, tableContainerRef]); + + const logLevelColumn = useMemo(() => { + return inferLogLevelColumn(dedupedRows); + }, [dedupedRows]); + + const csvColumns = useMemo( + () => + displayedColumns.map(col => ({ + dataKey: col, + displayName: columnNameMap?.[col] ?? col, + })), + [displayedColumns, columnNameMap], + ); + + const { csvData, maxRows, isLimited } = useCsvExport( + dedupedRows, + csvColumns, + ); + + const [isSelectionModeOn, setSelectionModeOn] = useLocalStorage( + 'hdx-row-selection-enabled', + false, + ); + // Tables with a bar get an on/off toggle there; others keep selection on. + const isRowSelectionActive = + enableRowSelection && (!hasTableBar || isSelectionModeOn); + const rowSelection = useRowSelection(dedupedRows, { + enabled: isRowSelectionActive, + resetKey: selectionResetKey, + onSelectionChange: onSelectedRowsChange, + }); + + // Expandable rows functionality + const { + expandedRows, + toggleRowExpansion, + collapseAllRows: collapseRows, + } = useExpandableRows(onExpandedRowsChange); + + // Effect to collapse all rows when requested by parent + useEffect(() => { + if (collapseAllRows) { + collapseRows(); + } + }, [collapseAllRows, collapseRows]); + + // Find within page functionality using custom hook + const tableSearch = useTableSearch({ + rows: dedupedRows, + searchableColumns: displayedColumns, + debounceMs: 300, + }); + + const columnSizeOpts = useMemo( + () => ({ aliasMap, columnTypeMap, logLevelColumn, columnSizeStorage }), + [aliasMap, columnTypeMap, logLevelColumn, columnSizeStorage], + ); + + const leadingColumnsWidth = + (showExpandButton ? EXPAND_COLUMN_SIZE : 0) + + (isRowSelectionActive ? ROW_SELECTION_COLUMN_WIDTH : 0); + + const lastColumnWidth = useMemo(() => { + if (displayedColumns.length === 0) return MIN_LAST_COLUMN_WIDTH; + + const lastCol = displayedColumns[displayedColumns.length - 1]; + const lastColId = columnSizeOpts.aliasMap?.[lastCol] + ? `"${lastCol}"` + : lastCol; + const storedLast = columnSizeOpts.columnSizeStorage[lastColId]; + + if (storedLast != null) { + return Math.max(MIN_LAST_COLUMN_WIDTH, storedLast); + } + + const nonLastSum = displayedColumns + .slice(0, -1) + .reduce( + (total, column) => + total + getResolvedColumnSize(column, columnSizeOpts), + 0, + ); + return Math.max( + MIN_LAST_COLUMN_WIDTH, + containerWidth - nonLastSum - leadingColumnsWidth, + ); + }, [displayedColumns, columnSizeOpts, leadingColumnsWidth, containerWidth]); + + const [wrapLinesEnabled, setWrapLinesEnabled] = useLocalStorage( + `${tableId}-wrap-lines`, + wrapLines ?? false, + ); + + const columns = useMemo[]>( + () => [ + ...(showExpandButton + ? [ + createExpandButtonColumn( + expandedRows, + toggleRowExpansion, + highlightedLineId, + ), + ] + : []), + ...(displayedColumns.map((column, i) => { + const jsColumnType = columnTypeMap.get(column)?._type; + const isDate = jsColumnType === JSDataType.Date; + return { + meta: { + column, + jsColumnType, + }, + // If the column is an alias, wrap in quotes. + id: aliasMap?.[column] ? `"${column}"` : column, + // TODO: add support for sorting on Dynamic JSON fields + enableSorting: jsColumnType !== JSDataType.Dynamic, + accessorFn: curry(retrieveColumnValue)(column), // Columns can contain '.' and will not work with accessorKey + header: `${columnNameMap?.[column] ?? column}${isDate ? ` (${timeZone ? getTimeZoneShortName(timeZone) : 'Local'})` : ''}`, + cell: info => { + const value = info.getValue(); // This can be any type realistically (numbers, strings, etc.) + + if (column === '__hdx_pattern_trend') { + return ( +
+ +
+ ); + } + + if (isDate) { + const date = new Date(value); + return ( + + + + ); + } + + const strValue = typeof value === 'string' ? value : `${value}`; + + if (column === logLevelColumn) { + return ; + } + + const maxLen = wrapLinesEnabled + ? MAX_CELL_LENGTH_WRAPPED + : MAX_CELL_LENGTH; + const truncatedStrValue = + strValue.length > maxLen + ? `${strValue.slice(0, maxLen)}...` + : strValue; + + // Apply search highlighting if there's a search query + // Use React Table's row index which corresponds to dedupedRows index + const isCurrentMatch = + !!tableSearch.searchQuery && + tableSearch.matchIndices.length > 0 && + tableSearch.matchIndices[tableSearch.currentMatchIndex] === + info.row.index; + + const displayValue = tableSearch.searchQuery ? ( + highlightText(truncatedStrValue, tableSearch.searchQuery, { + isCurrentMatch, + }) + ) : formatPatternColumn && column === 'pattern' ? ( + + ) : ( + truncatedStrValue + ); + + return ( + + {displayValue} + + ); + }, + size: + i === displayedColumns.length - 1 + ? lastColumnWidth + : getResolvedColumnSize(column, columnSizeOpts), + }; + }) as ColumnDef[]), + ], + [ + timeZone, + highlightedLineId, + displayedColumns, + columnSizeOpts, + columnNameMap, + columnTypeMap, + logLevelColumn, + expandedRows, + toggleRowExpansion, + showExpandButton, + aliasMap, + lastColumnWidth, + wrapLinesEnabled, + formatPatternColumn, + tableSearch.searchQuery, + tableSearch.matchIndices, + tableSearch.currentMatchIndex, + ], + ); + + //called on scroll and possibly on mount to fetch more data as the user scrolls and reaches bottom of table + const fetchMoreOnBottomReached = useCallback( + (containerRefElement?: HTMLDivElement | null) => { + if (containerRefElement) { + const { scrollHeight, scrollTop, clientHeight } = containerRefElement; + if ( + scrollHeight - scrollTop - clientHeight < FETCH_NEXT_PAGE_PX && + !isLoading && + !isError && + hasNextPage + ) { + // Cancel refetch is important to ensure we wait for the last fetch to finish + fetchNextPage?.({ cancelRefetch: false }); + } + } + }, + [fetchNextPage, isLoading, isError, hasNextPage], + ); + + //a check on mount and after a fetch to see if the table is already scrolled to the bottom and immediately needs to fetch more data + useEffect(() => { + fetchMoreOnBottomReached(tableContainerRef); + }, [fetchMoreOnBottomReached, tableContainerRef]); + + const reactTableProps = useMemo((): TableOptions => { + //TODO: fix any + const onColumnSizingChange = (updaterOrValue: any) => { + const state = + updaterOrValue instanceof Function + ? updaterOrValue() + : updaterOrValue; + setColumnSizeStorage({ ...columnSizeStorage, ...state }); + }; + + const initReactTableProps = { + data: dedupedRows, + columns, + getCoreRowModel: getCoreRowModel(), + // debugTable: true, + enableSorting, + manualSorting: true, + onSortingChange: v => { + if (typeof v === 'function') { + const newSortVal = v(sortOrder ?? []); + onSortingChange?.(newSortVal ?? null); + } else { + onSortingChange?.(v ?? null); + } + }, + state: { + sorting: sortOrder ?? [], + }, + defaultColumn: { + minSize: MIN_COLUMN_WIDTH, + }, + enableColumnResizing: true, + columnResizeMode: 'onChange' as ColumnResizeMode, + } satisfies TableOptions; + + const columnSizeProps = { + state: { + columnSizing: columnSizeStorage, + }, + onColumnSizingChange: onColumnSizingChange, + }; + + return tableId + ? { ...initReactTableProps, ...columnSizeProps } + : initReactTableProps; + }, [ + columns, + dedupedRows, + tableId, + sortOrder, + enableSorting, + onSortingChange, + columnSizeStorage, + setColumnSizeStorage, + ]); + + const table = useReactTable(reactTableProps); + + // Sum actual column widths to derive a pixel min-width for the table. + // This enables horizontal scrolling when the viewport is narrower than + // the total column widths. + const tableMinWidth = useMemo(() => { + return ( + leadingColumnsWidth + + displayedColumns.reduce((total, column, i) => { + const size = getResolvedColumnSize(column, columnSizeOpts); + if (i === displayedColumns.length - 1) { + return total + Math.max(MIN_LAST_COLUMN_WIDTH, size); + } + return total + size; + }, 0) + ); + }, [displayedColumns, columnSizeOpts, leadingColumnsWidth]); + + const { rows: _rows } = table.getRowModel(); + + const rowVirtualizer = useVirtualizer({ + count: _rows.length, + // count: hasNextPage ? allRows.length + 1 : allRows.length, + getScrollElement: useCallback( + () => tableContainerRef, + [tableContainerRef], + ), + estimateSize: useCallback(() => 23, []), + overscan: 30, + paddingEnd: 20, + }); + + const items = rowVirtualizer.getVirtualItems(); + const totalSize = rowVirtualizer.getTotalSize(); + + const [paddingTop, paddingBottom] = useMemo( + () => + items.length > 0 + ? [ + Math.max(0, items[0].start - rowVirtualizer.options.scrollMargin), + Math.max(0, totalSize - items[items.length - 1].end), + ] + : [0, 0], + [items, rowVirtualizer.options.scrollMargin, totalSize], + ); + + // Scroll to log id if it's not in window yet + const [scrolledToHighlightedLine, setScrolledToHighlightedLine] = + useState(false); + const [showSql, setShowSql] = useState(false); + + const handleSqlModalOpen = (open: boolean) => { + setShowSql(open); + onChildModalOpen?.(open); + }; + + // Scroll to current match (only when explicitly navigating) + // Fixed: Properly synchronized scroll flag with state updates + useEffect(() => { + if (!tableSearch.shouldScrollToMatch) { + return; + } + + if ( + tableSearch.matchIndices.length > 0 && + rowVirtualizer && + tableSearch.currentMatchIndex < tableSearch.matchIndices.length && + tableSearch.searchQuery.trim() + ) { + const matchRowIndex = + tableSearch.matchIndices[tableSearch.currentMatchIndex]; + const matchedRow = dedupedRows[matchRowIndex]; + + if (matchedRow) { + // Find the corresponding row in the react-table rows + const tableRowIndex = _rows.findIndex( + row => getRowId(row.original) === getRowId(matchedRow), + ); + + if (tableRowIndex !== -1) { + // Use requestAnimationFrame to ensure DOM is ready + requestAnimationFrame(() => { + rowVirtualizer.scrollToIndex(tableRowIndex, { + align: 'center', + }); + // Clear the flag after scrolling completes + tableSearch.clearShouldScrollToMatch(); + }); + } else { + // Row not found in virtual list, clear flag + tableSearch.clearShouldScrollToMatch(); + } + } else { + // No matched row, clear flag + tableSearch.clearShouldScrollToMatch(); + } + } else { + // Conditions not met, clear flag + tableSearch.clearShouldScrollToMatch(); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [ + tableSearch.shouldScrollToMatch, + tableSearch.currentMatchIndex, + tableSearch.matchIndices, + tableSearch.searchQuery, + tableSearch.clearShouldScrollToMatch, + rowVirtualizer, + _rows, + dedupedRows, + ]); + + useEffect(() => { + if ( + scrolledToHighlightedLine || + !highlightedLineId || + rowVirtualizer == null + ) { + return; + } + + const rowIdx = dedupedRows.findIndex( + l => getRowId(l) === highlightedLineId, + ); + if (rowIdx == -1 && highlightedLineId) { + if ( + dedupedRows.length < MAX_SCROLL_FETCH_LINES && + !isLoading && + !isError && + hasNextPage + ) { + fetchNextPage?.({ cancelRefetch: false }); + } + } else { + setScrolledToHighlightedLine(true); + if ( + rowVirtualizer.getVirtualItems().find(l => l.index === rowIdx) == null + ) { + rowVirtualizer.scrollToIndex(rowIdx, { + align: 'center', + }); + } + } + }, [ + dedupedRows, + highlightedLineId, + fetchNextPage, + rowVirtualizer, + scrolledToHighlightedLine, + isLoading, + isError, + hasNextPage, + ]); + + const shiftHighlightedLineId = useCallback( + (shift: number) => { + if (highlightedLineId == null) { + return; + } + + const newIndex = + dedupedRows.findIndex(l => getRowId(l) === highlightedLineId) + shift; + + if (newIndex < 0 || newIndex >= dedupedRows.length) { + return; + } + + const newLine = dedupedRows[newIndex]; + + _onRowExpandClick(newLine); + }, + [highlightedLineId, _onRowExpandClick, dedupedRows], + ); + + useHotkeys(['ArrowRight', 'ArrowDown', 'j'], e => { + e.preventDefault(); + shiftHighlightedLineId(1); + }); + useHotkeys(['ArrowLeft', 'ArrowUp', 'k'], e => { + e.preventDefault(); + shiftHighlightedLineId(-1); + }); + + const getCsvFilename = useCallback( + () => csvExportFilename('hyperdx_search_results'), + [], + ); + + const resetWidthsAction = tableId && + Object.keys(columnSizeStorage).length > 0 && ( + setColumnSizeStorage({})} + title="Reset Column Widths" + display="flex" + > + + + + + ); + const sqlAction = config && ( + handleSqlModalOpen(true)} + title="Show Generated SQL" + tabIndex={0} + display="flex" + > + + + + + ); + const wrapAction = ( + setWrapLinesEnabled(prev => !prev)} + title={`${wrapLinesEnabled ? 'Disable' : 'Enable'} Wrap Lines`} + display="flex" + > + + {wrapLinesEnabled ? ( + + ) : ( + + )} + + + ); + const csvAction = ( + + + + + + ); + const settingsAction = onSettingsClick != null && ( + onSettingsClick()} + title="Settings" + display="flex" + > + + + + + ); + const rowSelectionMenu = isRowSelectionActive && ( + + ); + + return ( + + {hasTableBar && ( + + + {rowSelectionMenu} + + + { + if (!enabled) { + rowSelection.clearSelection(); + } + setSelectionModeOn(enabled); + }, + } + : undefined + } + wrapLines={wrapLinesEnabled} + onWrapLinesChange={setWrapLinesEnabled} + onResetColumnWidths={ + tableId && Object.keys(columnSizeStorage).length > 0 + ? () => setColumnSizeStorage({}) + : undefined + } + /> + + + {sqlAction} + {csvAction} + {settingsAction} + + {tableBarRightSection != null && ( + <> + + {tableBarRightSection} + + )} + + + )} + + {/* Find within page search bar - floating on top right */} + +
{ + fetchMoreOnBottomReached(e.target as HTMLDivElement); + + if (e.target != null) { + const { scrollTop } = e.target as HTMLDivElement; + onScroll?.(scrollTop); + } + }} + ref={tableContainerRefCallback} + // Fixes flickering scroll bar: https://github.com/TanStack/virtual/issues/426#issuecomment-1403438040 + // style={{ overflowAnchor: 'none' }} + > + {config && ( + handleSqlModalOpen(false)} + config={config} + /> + )} + 0, + })} + style={{ minWidth: tableMinWidth }} + id={tableId} + > + + {displayedColumns.length > 0 && + table.getHeaderGroups().map(headerGroup => ( + + {isRowSelectionActive && } + {headerGroup.headers.map((header, headerIndex) => { + const isLast = + headerIndex === headerGroup.headers.length - 1; + return ( + { + onRemoveColumn( + (header.column.columnDef.meta as any) + ?.column, + ); + } + : undefined + } + lastItemButtons={ + hasTableBar ? undefined : ( + + {resetWidthsAction} + {sqlAction} + {wrapAction} + {csvAction} + {settingsAction} + {rowSelectionMenu} + + ) + } + /> + ); + })} + + ))} + + {paddingTop > 0 && ( + + + + + )} + {items.map(virtualRow => { + const row = _rows[virtualRow.index] as TableRow; + const rowId = getRowId(row.original); + const isExpanded = expandedRows[rowId] ?? false; + const isRowSelected = + isRowSelectionActive && rowSelection.isSelected(rowId); + + return ( + // A row and its inline expansion are two `tr`s but one + // virtual item, so the group — not either `tr` — is what the + // virtualizer measures. Measuring the `tr`s directly gave + // both the same `data-index`, and the expanded one took over + // that index's ResizeObserver registration, then left its + // height cached there after it unmounted. + + + {isRowSelectionActive && ( + + )} + + {/* Expand button cell */} + {showExpandButton && ( + + )} + + {/* Content columns grouped back to preserve row hover/click */} + + + {showExpandButton && isExpanded && ( + + {renderRowDetails?.({ + id: rowId, + aliasWith: + row.original[INTERNAL_ROW_FIELDS.ALIAS_WITH], + ...row.original, + })} + + )} + + ); + })} + + + + + {paddingBottom > 0 && ( + + + )} + +
+
+ {flexRender( + row.getVisibleCells()[0].column.columnDef.cell, + row.getVisibleCells()[0].getContext(), + )} + + +
+
+ {isLoading ? ( +
+
+ +
{' '} + {loadingDate != null && ( + <> + Searched .{' '} + + )} + Loading results + {dateRange?.[0] != null && dateRange?.[1] != null ? ( + <> + {' '} + across{' '} + {formatDistance( + dateRange?.[1], + dateRange?.[0], + )}{' '} + {'('} + {' '} + to{' '} + + {')'} + + ) : null} + ... +
+ ) : hasNextPage == false && + isLoading == false && + dedupedRows.length > 0 ? ( +
End of Results
+ ) : isError && error ? ( + + ) : hasNextPage == false && + isLoading == false && + dedupedRows.length === 0 ? ( +
+ No results found. + + Try checking the query explainer in the search bar + if there are any search syntax issues. + + {dateRange?.[0] != null && dateRange?.[1] != null ? ( + + Searched Time Range:{' '} + {formatDistance(dateRange?.[1], dateRange?.[0])}{' '} + {'('} + {' '} + to{' '} + + {')'} + + ) : null} +
+ ) : ( +
+ )} +
+
+
+
+ {/* Match indicator on the right side */} + +
+
+ ); + }, +); + +export function appendSelectWithAdditionalKeys( + select: SelectList, + primaryKeys: string, + partitionKey: string, + extraKeys: string[] = [], +): { select: SelectList; additionalKeysLength: number } { + // Include both the raw key expressions (e.g. toStartOfFiveMinutes(Timestamp)) + // and the extracted column references (e.g. Timestamp). The raw expressions + // are needed so the row WHERE clause can filter on PK expressions directly. + const partitionKeyArr = extractColumnReferencesFromKey(partitionKey); + const primaryKeyArr = extractColumnReferencesFromKey(primaryKeys); + const rawPartitionExprs = splitAndTrimWithBracket(partitionKey); + const rawPrimaryExprs = splitAndTrimWithBracket(primaryKeys); + const allKeys = new Set([ + ...partitionKeyArr, + ...primaryKeyArr, + ...rawPartitionExprs, + ...rawPrimaryExprs, + ...extraKeys, + ]); + if (typeof select === 'string') { + const selectSplit = splitAndTrimWithBracket(select); + const selectColumns = new Set(selectSplit); + const additionalKeys = [...allKeys].filter(k => !selectColumns.has(k)); + return { + select: [...selectColumns, ...additionalKeys].join(','), + additionalKeysLength: additionalKeys.length, + }; + } else { + const additionalKeys = [...allKeys].map(k => ({ valueExpression: k })); + return { + select: [...select, ...additionalKeys], + additionalKeysLength: additionalKeys.length, + }; + } +} + +function getSelectLength(select: SelectList): number { + if (typeof select === 'string') { + return select.split(',').filter(s => s.trim().length > 0).length; + } else { + return select.length; + } +} + +export function useConfigWithAdditionalSelect( + config: BuilderChartConfigWithDateRange, + sourceId?: string, +) { + const { data: tableMetadata } = useTableMetadata({ + databaseName: config.from.databaseName, + tableName: config.from.tableName, + connectionId: config.connection, + }); + + // Only check for row-ID columns for row-level queries (sourceId present). + // Skip for aggregate queries (e.g. patterns) where extra keys are irrelevant. + const { data: columns } = useColumns( + { + databaseName: config.from.databaseName, + tableName: config.from.tableName, + connectionId: config.connection, + }, + { enabled: !!sourceId }, + ); + + const primaryKey = tableMetadata?.primary_key; + const partitionKey = tableMetadata?.partition_key; + + return useMemo(() => { + if (primaryKey == null || partitionKey == null) { + return undefined; + } + + let extraKeys: string[] = []; + + if (sourceId) { + const engineFull = tableMetadata?.engine_full ?? ''; + + const hasBlockColumns = + engineFull.includes('enable_block_number_column = 1') && + engineFull.includes('enable_block_offset_column = 1'); + + if (hasBlockColumns) { + extraKeys = ['_block_number', '_block_offset']; + } else if (columns?.some(c => c.name === '__hdx_id')) { + extraKeys = ['__hdx_id']; + } + } + + const { select, additionalKeysLength } = appendSelectWithAdditionalKeys( + config.select, + primaryKey, + partitionKey, + extraKeys, + ); + + // When block columns are available, the PK + partition + block columns + // uniquely identify a row. Compute the full set of key column names + // (both raw expressions like toStartOfFiveMinutes(Timestamp) and bare + // column references like Timestamp, ServiceName) so the row WHERE clause + // can use only these, avoiding expensive index loading on large columns + // like Body. + const hasBlockColumns = + extraKeys.includes('_block_number') && + extraKeys.includes('_block_offset'); + + const rowKeyColumns = hasBlockColumns + ? new Set([ + ...splitAndTrimWithBracket(partitionKey), + ...splitAndTrimWithBracket(primaryKey), + ...extractColumnReferencesFromKey(partitionKey), + ...extractColumnReferencesFromKey(primaryKey), + ...extraKeys, + ]) + : undefined; + + return { ...config, select, additionalKeysLength, rowKeyColumns }; + }, [primaryKey, partitionKey, config, tableMetadata, columns, sourceId]); +} + +function selectColumnMapWithoutAdditionalKeys( + selectMeta: ColumnMetaType[] | undefined, + additionalKeysLength: number | undefined, +): Map< + string, + { + _type: JSDataType | null; + } +> { + if (selectMeta == null || additionalKeysLength == null) { + return new Map(); + } + const sm = selectMeta.slice(0, selectMeta.length - additionalKeysLength); + + return new Map( + sm?.map(c => [ + c.name, + { + ...c, + _type: convertCHDataTypeToJSType(c.type), + }, + ]), + ); +} + +export type DBRowTableVariant = 'default' | 'muted'; + +function DBSqlRowTableComponent({ + config, + sourceId, + onError, + onRowDetailsClick, + highlightedLineId, + enabled = true, + isLive = false, + queryKeyPrefix, + onScroll, + denoiseResults = false, + onChildModalOpen, + onExpandedRowsChange, + collapseAllRows, + showExpandButton = true, + enableRowSelection = false, + selectionResetKey, + onSelectedRowsChange, + renderRowDetails, + onSortingChange, + initialSortBy, + variant = 'default', + enableSmallFirstWindow, + tableId, + errorVariant, + onResolvedColumnsChange, + tableBarRightSection, +}: { + config: BuilderChartConfigWithDateRange; + sourceId?: string; + onRowDetailsClick?: ( + rowWhere: RowWhereResult, + row: Record, + ) => void; + highlightedLineId?: string; + queryKeyPrefix?: string; + enabled?: boolean; + isLive?: boolean; + renderRowDetails?: (r: { + id: string; + aliasWith?: WithClause[]; + [key: string]: unknown; + }) => React.ReactNode; + onScroll?: (scrollTop: number) => void; + onError?: (error: Error | ClickHouseQueryError) => void; + denoiseResults?: boolean; + onChildModalOpen?: (open: boolean) => void; + onExpandedRowsChange?: (hasExpandedRows: boolean) => void; + collapseAllRows?: boolean; + showExpandButton?: boolean; + enableRowSelection?: boolean; + selectionResetKey?: string; + onSelectedRowsChange?: (hasSelectedRows: boolean) => void; + initialSortBy?: SortingState; + onSortingChange?: (v: SortingState | null) => void; + variant?: DBRowTableVariant; + enableSmallFirstWindow?: boolean; + tableId?: string; + errorVariant?: ChartErrorStateVariant; + onResolvedColumnsChange?: (meta: ColumnMetaType[]) => void; + tableBarRightSection?: React.ReactNode; +}) { + const { data: me } = api.useMe(); + const { toggleColumn, displayedColumns: contextDisplayedColumns } = + use(RowSidePanelContext); + + const [orderBy, setOrderBy] = useState( + initialSortBy?.[0] ?? null, + ); + + const orderByArray = useMemo(() => (orderBy ? [orderBy] : []), [orderBy]); + + const _onSortingChange = useCallback( + (v: SortingState | null) => { + onSortingChange?.(v); + setOrderBy(v?.[0] ?? null); + }, + [setOrderBy, onSortingChange], + ); + + const prevSourceId = usePrevious(sourceId); + useEffect(() => { + if (prevSourceId && prevSourceId !== sourceId) { + _onSortingChange(null); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [sourceId]); + + // Sync local orderBy state with initialSortBy when it changes + // (e.g., when loading a saved search) + const prevInitialSortBy = usePrevious(initialSortBy); + useEffect(() => { + const currentSort = initialSortBy?.[0] ?? null; + const prevSort = prevInitialSortBy?.[0] ?? null; + + // Only sync if initialSortBy actually changed (not orderBy) + // We don't include orderBy in deps to avoid infinite loop + if (JSON.stringify(currentSort) !== JSON.stringify(prevSort)) { + setOrderBy(currentSort); + } + }, [initialSortBy, prevInitialSortBy]); + + const mergedConfigObj = useMemo(() => { + const base = { + ...searchChartConfigDefaults(me?.team), + ...config, + }; + if (orderByArray.length) { + base.orderBy = orderByArray.map(o => { + return { + valueExpression: o.id, + ordering: o.desc ? 'DESC' : 'ASC', + }; + }); + } + return base; + }, [me, config, orderByArray]); + + const mergedConfig = useConfigWithAdditionalSelect(mergedConfigObj, sourceId); + + const { data, fetchNextPage, hasNextPage, isFetching, isError, error } = + useOffsetPaginatedQuery(mergedConfig ?? config, { + enabled: + enabled && mergedConfig != null && getSelectLength(config.select) > 0, + isLive, + queryKeyPrefix, + enableSmallFirstWindow, + }); + + // The first N columns are the select columns from the user + // We can't use names as CH may rewrite the names + // We have to do the subtraction here because the select length can + // differ from returned columns (eg. SELECT *) + // we have to subtract the additional key length as the pk merging + // can dedup the columns between the user select and pk + const columnMap = useMemo(() => { + return selectColumnMapWithoutAdditionalKeys( + data?.meta, + mergedConfig?.additionalKeysLength, + ); + }, [data, mergedConfig]); + + const columns = useMemo(() => Array.from(columnMap.keys()), [columnMap]); + + // CH may rewrite column names in the result set (e.g. expression formatting), + // so we cannot rely on string matching between CH column names and SELECT expressions. + // Instead, use position-based mapping: columns[i] (CH name) corresponds to + // contextDisplayedColumns[i] (the original SELECT expression), since both arrays + // are ordered by the user's SELECT list. + const onRemoveColumnFromTable = useCallback( + (chColumnName: string) => { + if (!toggleColumn || !contextDisplayedColumns) return; + + const colIdx = columns.indexOf(chColumnName); + if (colIdx >= 0 && colIdx < contextDisplayedColumns.length) { + toggleColumn(contextDisplayedColumns[colIdx]); + return; + } + + // Fallback: direct match + if (contextDisplayedColumns.includes(chColumnName)) { + toggleColumn(chColumnName); + return; + } + + // Alias match: find the SELECT expression that ends with "AS chColumnName" + const exprWithAlias = contextDisplayedColumns.find(expr => + expr.trim().toLowerCase().endsWith(` as ${chColumnName.toLowerCase()}`), + ); + if (exprWithAlias) { + toggleColumn(exprWithAlias); + } else { + console.warn( + 'onRemoveColumnFromTable: could not find SELECT expr for', + chColumnName, + ); + } + }, + [toggleColumn, contextDisplayedColumns, columns], + ); + + // FIXME: do this on the db side ? + // Or, the react-table should render object-type cells as JSON.stringify + const objectTypeColumns = useMemo(() => { + return columns.filter(c => { + const columnType = columnMap.get(c)?._type; + return isJSDataTypeJSONStringifiable(columnType); + }); + }, [columns, columnMap]); + const processedRows = useMemo(() => { + const rows = data?.data ?? []; + return rows.map(row => { + const newRow = { ...row }; + objectTypeColumns.forEach(c => { + if (columnMap.get(c)?._type === JSDataType.JSON) { + // special rule for json + // for json {SomePath: /c}, CH will return {SomePath: \/c} + // add this to make sure md5 get correct result + newRow[c] = JSON.stringify(row[c]).replace(/\//g, '\\/'); + } else { + newRow[c] = JSON.stringify(row[c]); + } + }); + return newRow; + }); + }, [data, objectTypeColumns, columnMap]); + + const aliasMap = useMemo( + () => chSqlToAliasMap(data?.chSql ?? { sql: '', params: {} }), + [data], + ); + + const getRowWhere = useRowWhere({ + meta: data?.meta, + aliasMap, + primaryKeyColumns: mergedConfig?.rowKeyColumns, + }); + + const _onRowDetailsClick = useCallback( + (row: Record) => { + return onRowDetailsClick?.(getRowWhere(row), row); + }, + [onRowDetailsClick, getRowWhere], + ); + + useEffect(() => { + if (isError && onError && error) { + onError(error); + } + }, [isError, onError, error]); + + // Surface the result-set column types upward. + // `data?.meta` keeps a stable identity per query result. + useEffect(() => { + if (data?.meta != null && data.meta.length > 0) { + onResolvedColumnsChange?.(data.meta); + } + }, [data?.meta, onResolvedColumnsChange]); + + const { data: source } = useSource({ id: sourceId }); + const patternColumn = columns[columns.length - 1]; + const groupedPatterns = useGroupedPatterns({ + config, + samples: DENOISE_SAMPLE_SIZE, + bodyValueExpression: patternColumn ?? '', + levelExpression: getLevelExpression(source), + totalCount: undefined, + enabled: denoiseResults, + }); + const noisyPatterns = useQuery({ + queryKey: ['noisy-patterns', config], + queryFn: async () => { + return Object.values(groupedPatterns.data).filter( + p => + p.count / (groupedPatterns.sampledRowCount ?? 1) > + DENOISE_NOISE_THRESHOLD, + ); + }, + enabled: + denoiseResults && + groupedPatterns.data != null && + Object.values(groupedPatterns.data).length > 0 && + groupedPatterns.miner != null, + }); + const noisyPatternIds = useMemo(() => { + return noisyPatterns.data?.map(p => p.id) ?? []; + }, [noisyPatterns.data]); + + const denoisedRows = useQuery({ + queryKey: [ + 'denoised-rows', + config, + denoiseResults, + // Only include processed rows if denoising is enabled + // This helps prevent the queryKey from getting extremely large + // and causing memory issues, when it's not used. + ...(denoiseResults ? [processedRows] : []), + noisyPatternIds, + patternColumn, + ], + queryFn: async () => { + if (!denoiseResults) { + return []; + } + // No noisy patterns, so no need to denoise + if (noisyPatternIds.length === 0) { + return processedRows; + } + + const matchedLogs = await groupedPatterns.miner?.matchLogs( + processedRows.map(row => row[patternColumn]), + ); + return processedRows.filter((row, i) => { + const match = matchedLogs?.[i]; + return !noisyPatternIds.includes(`${match}`); + }); + }, + placeholderData: (previousData, previousQuery) => { + // If it's the same search, but new data, return the previous data while we load + if ( + previousQuery?.queryKey?.[0] === 'denoised-rows' && + previousQuery?.queryKey?.[1] === config + ) { + return previousData; + } + return undefined; + }, + gcTime: isLive ? ms('30s') : ms('5m'), // more aggressive gc for live data, since it can end up holding lots of data + enabled: + denoiseResults && + noisyPatterns.isSuccess && + processedRows.length > 0 && + groupedPatterns.miner != null, + }); + + const isLoading = denoiseResults + ? isFetching || + denoisedRows.isFetching || + noisyPatterns.isFetching || + groupedPatterns.isLoading + : isFetching; + + const loadingDate = + data?.window?.direction === 'ASC' + ? data?.window?.endTime + : data?.window?.startTime; + + return ( + <> + {denoiseResults && ( + + + Removed Noisy Event Patterns + + + {noisyPatterns.data?.map(p => ( + + {p.pattern} + + ))} + {noisyPatternIds.length === 0 && ( + No noisy patterns found + )} + + + )} + + + ); +} +export const DBSqlRowTable = memo(DBSqlRowTableComponent); + +export type RawSqlRowTableConfig = RawSqlChartConfig & { + dateRange: [Date, Date]; +}; + +/** + * The same row table, fed by a hand-written statement instead of the builder. + * Rows are identified from their own values (plus any aliases in the SQL), so + * a row opens in the side panel whenever the statement selects the source's + * columns; there are no extra key columns to lean on, since the SQL is not + * ours to append to. Raw SQL does not paginate. + */ +function DBRawSqlRowTableComponent({ + config, + sourceId, + onRowDetailsClick, + highlightedLineId, + enabled = true, + queryKeyPrefix, + renderRowDetails, + onScroll, + onError, + tableId, + errorVariant, + tableBarRightSection, +}: { + config: RawSqlRowTableConfig; + sourceId?: string; + onRowDetailsClick?: ( + rowWhere: RowWhereResult, + row: Record, + ) => void; + highlightedLineId?: string; + enabled?: boolean; + queryKeyPrefix?: string; + renderRowDetails?: (r: { + id: string; + aliasWith?: WithClause[]; + [key: string]: unknown; + }) => React.ReactNode; + onScroll?: (scrollTop: number) => void; + onError?: (error: Error | ClickHouseQueryError) => void; + tableId?: string; + errorVariant?: ChartErrorStateVariant; + tableBarRightSection?: React.ReactNode; +}) { + const { data, isFetching, isError, error } = useOffsetPaginatedQuery(config, { + enabled, + queryKeyPrefix, + }); + + const columnMap = useMemo( + () => selectColumnMapWithoutAdditionalKeys(data?.meta, 0), + [data], + ); + const columns = useMemo(() => Array.from(columnMap.keys()), [columnMap]); + + const rows = useMemo(() => { + const objectTypeColumns = columns.filter(c => + isJSDataTypeJSONStringifiable(columnMap.get(c)?._type), + ); + return (data?.data ?? []).map(row => { + const newRow = { ...row }; + objectTypeColumns.forEach(c => { + newRow[c] = JSON.stringify(row[c]); + }); + return newRow; + }); + }, [data, columns, columnMap]); + + const aliasMap = useMemo( + () => chSqlToAliasMap(data?.chSql ?? { sql: '', params: {} }), + [data], + ); + const getRowWhere = useRowWhere({ meta: data?.meta, aliasMap }); + + const _onRowDetailsClick = useCallback( + (row: Record) => onRowDetailsClick?.(getRowWhere(row), row), + [onRowDetailsClick, getRowWhere], + ); + + useEffect(() => { + if (isError && onError && error) { + onError(error); + } + }, [isError, onError, error]); + + const { data: source } = useSource({ id: sourceId }); + + return ( + + ); +} +export const DBRawSqlRowTable = memo(DBRawSqlRowTableComponent); diff --git a/packages/app/src/components/Explore/owned/DBSearchPageFilters.tsx b/packages/app/src/components/Explore/owned/DBSearchPageFilters.tsx new file mode 100644 index 0000000000..40d1913523 --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBSearchPageFilters.tsx @@ -0,0 +1,1863 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { memo, useCallback, useEffect, useMemo, useState } from 'react'; +import cx from 'classnames'; +import { + TableConnection, + TableMetadata, +} from '@hyperdx/common-utils/dist/core/metadata'; +import { + type FilterRange, + FilterState, +} from '@hyperdx/common-utils/dist/filters'; +import { + BuilderChartConfigWithDateRange, + SourceKind, +} from '@hyperdx/common-utils/dist/types'; +import { + Accordion, + ActionIcon, + Box, + Button, + Center, + Checkbox, + Divider, + Flex, + Group, + Loader, + MantineStyleProps, + NumberInput, + ScrollArea, + Stack, + Tabs, + Text, + TextInput, + Tooltip, + UnstyledButton, +} from '@mantine/core'; +import { notifications } from '@mantine/notifications'; +import { + IconArrowBarToLeft, + IconChartBar, + IconChartBarOff, + IconChevronDown, + IconChevronRight, + IconChevronUp, + IconFilterOff, + IconMinus, + IconPlus, + IconRefresh, + IconSearch, + IconShadow, + IconSitemap, +} from '@tabler/icons-react'; + +import { FilterSettingsPanel } from '@/components/DBSearchPageFilters/FilterSettingsPopover'; +import { NestedFilterGroup } from '@/components/DBSearchPageFilters/NestedFilterGroup'; +import { + PinShareIndicator, + PinShareMenu, +} from '@/components/DBSearchPageFilters/PinShareMenu'; +import { SharedFiltersSection } from '@/components/DBSearchPageFilters/SharedFilters'; +import { + getFilterStateEntry, + groupFacetsByBaseName, + toQuotedClickHouseKeyExpression, +} from '@/components/DBSearchPageFilters/utils'; +import { IS_CLICKHOUSE_BUILD } from '@/config'; +import { + useColumns, + useGetValuesDistribution, + useJsonColumns, + useTableMetadata, +} from '@/hooks/useMetadata'; +import { usePinnedFiltersApi } from '@/pinnedFilters'; +import { useSource } from '@/source'; +import { useLocalStorage } from '@/utils'; + +import { resolveTableConnections, useFetchFacets } from './exploreFilterHooks'; +import { + FilterStateHook, + IS_ROOT_SPAN_COLUMN_NAME, + usePinnedFilters, +} from './searchFilters'; +import useResizable from './useResizable'; + +import resizeStyles from '@styles/ResizablePanel.module.scss'; +import classes from '@styles/SearchPage.module.scss'; + +/* The initial number of values per filter to render */ +const INITIAL_MAX_VALUES_DISPLAYED = 10; + +/* The maximum number of values per filter to render at once after loading more */ +const SHOW_MORE_MAX_VALUES_DISPLAYED = 50; + +// This function will clean json string attributes specifically. It will turn a string like +// 'toString(ResourceAttributes.`hdx`.`sdk`.`version`)' into 'ResourceAttributes.hdx.sdk.version'. +export function cleanedFacetName(key: string): string { + if (key.startsWith('toString')) { + return key + .slice('toString('.length, key.length - 1) + .split('.') + .map(str => + str.startsWith('`') && str.endsWith('`') ? str.slice(1, -1) : str, + ) + .join('.'); + } + return key; +} + +/** Value-level pin callbacks and state (personal + shared). */ +type ValuePinHandlers = { + onPinClick: (value: string | boolean) => void; + isPinned: (value: string | boolean) => boolean; + onSharedPinClick?: (value: string | boolean) => void; + isSharedPinned?: (value: string | boolean) => boolean; +}; + +/** Field/group-level pin callbacks and state (personal + shared). */ +type FieldPinHandlers = { + onFieldPinClick?: VoidFunction; + isFieldPinned?: boolean; + onToggleSharedFieldPin?: VoidFunction; + isSharedFieldPinned?: boolean; +}; + +type FilterCheckboxProps = { + columnName: string; + label: string; + value?: 'included' | 'excluded' | false; + pinned: boolean; + sharedPinned?: boolean; + onChange?: (checked: boolean) => void; + onClickOnly?: VoidFunction; + onClickExclude?: VoidFunction; + onClickPin: VoidFunction; + onClickSharedPin?: VoidFunction; + className?: string; + percentage?: number; + isPercentageLoading?: boolean; +}; + +const TextButton = ({ + onClick, + label, + ms, + display, + 'data-testid': dataTestId, +}: { + onClick?: VoidFunction; + label: React.ReactNode; + ms?: MantineStyleProps['ms']; + display?: MantineStyleProps['display']; + 'data-testid'?: string; +}) => { + return ( + + + {label} + + + ); +}; + +type FilterPercentageProps = { + percentage: number; + isLoading?: boolean; + 'data-testid'?: string; +}; + +const FilterPercentage = ({ + percentage, + isLoading, + 'data-testid': dataTestId, +}: FilterPercentageProps) => { + const formattedPercentage = + percentage < 1 + ? `<1%` + : percentage >= 99.5 + ? `>99%` + : `~${Math.round(percentage)}%`; + + return ( + + {formattedPercentage} + + ); +}; + +const FilterCheckbox = ({ + columnName, + value, + label, + pinned, + sharedPinned, + onChange, + onClickOnly, + onClickExclude, + onClickPin, + onClickSharedPin, + className, + percentage, + isPercentageLoading, +}: FilterCheckboxProps) => { + const [pinMenuOpened, setPinMenuOpened] = useState(false); + const testIdPrefix = `filter-checkbox-${columnName}-${label}`; + const isPinnedAny = pinned || sharedPinned; + return ( +
+ onChange?.(!value)} + style={{ minWidth: 0 }} + wrap="nowrap" + > + { + // taken care by the onClick in the group + }} + indeterminate={value === 'excluded'} + data-testid={`${testIdPrefix}-input`} + /> + 22 ? 0 : 1500} + label={label} + position="right" + withArrow + fz="xxs" + color="gray" + > + + + {label || (empty)} + + {percentage != null && ( + + )} + + + +
+ {onClickOnly && ( + + )} + {onClickExclude && ( + + )} + +
+ +
+ ); +}; + +const FilterRangeDisplay = ({ + range, + onRangeChange, +}: { + name: string; + range: { min: number; max: number }; + onClearClick: VoidFunction; + onRangeChange?: (range: { min: number; max: number }) => void; +}) => { + const [localMin, setLocalMin] = useState(range.min); + const [localMax, setLocalMax] = useState(range.max); + + const handleMinChange = (value: number | string) => { + const numValue = typeof value === 'string' ? parseFloat(value) : value; + if (!isNaN(numValue)) { + setLocalMin(numValue); + if (onRangeChange) { + onRangeChange({ min: numValue, max: localMax }); + } + } + }; + + const handleMaxChange = (value: number | string) => { + const numValue = typeof value === 'string' ? parseFloat(value) : value; + if (!isNaN(numValue)) { + setLocalMax(numValue); + if (onRangeChange) { + onRangeChange({ min: localMin, max: numValue }); + } + } + }; + + return ( + + + + + to + + + + + ); +}; + +type SelectedValues = { + included: Set; + excluded: Set; + range?: FilterRange; +}; + +export type FilterGroupProps = { + name: string; + options: { value: string | boolean; label: string }[]; + optionsLoading?: boolean; + selectedValues?: SelectedValues; + onChange: (value: string | boolean) => void; + onClearClick: VoidFunction; + onOnlyClick: (value: string | boolean) => void; + onExcludeClick: (value: string | boolean) => void; + valuePins: ValuePinHandlers; + fieldPins?: FieldPinHandlers; + onColumnToggle?: VoidFunction; + isColumnDisplayed?: boolean; + onLoadMore: (key: string) => void; + loadMoreLoading: boolean; + hasLoadedMore: boolean; + isDefaultExpanded?: boolean; + showFilterCounts?: boolean; + 'data-testid'?: string; + chartConfig: BuilderChartConfigWithDateRange; + isLive?: boolean; + onRangeChange?: (range: { min: number; max: number }) => void; + distributionKey?: string; +}; + +/** + * Inner body of a FilterGroup — only mounted when expanded. + * All expensive hooks (useGetValuesDistribution, sorting memos, etc.) + * live here so collapsed groups pay near-zero cost. + */ +const FilterGroupBody = ({ + name, + options, + optionsLoading, + selectedValues, + onChange, + onOnlyClick, + onExcludeClick, + isPinned, + onPinClick, + isSharedPinned, + onSharedPinClick, + onLoadMore, + loadMoreLoading, + hasLoadedMore, + chartConfig, + isLive, + distributionKey, + showDistributions, + onDistributionError, + onFetchingDistributionChange, +}: { + name: string; + options: { value: string | boolean; label: string }[]; + optionsLoading?: boolean; + selectedValues: SelectedValues; + onChange: (value: string | boolean) => void; + onOnlyClick: (value: string | boolean) => void; + onExcludeClick: (value: string | boolean) => void; + isPinned: (value: string | boolean) => boolean; + onPinClick: (value: string | boolean) => void; + isSharedPinned?: (value: string | boolean) => boolean; + onSharedPinClick?: (value: string | boolean) => void; + onLoadMore: (key: string) => void; + loadMoreLoading: boolean; + hasLoadedMore: boolean; + chartConfig: BuilderChartConfigWithDateRange; + isLive?: boolean; + distributionKey?: string; + showDistributions: boolean; + onDistributionError: () => void; + onFetchingDistributionChange: (isFetching: boolean) => void; +}) => { + const [search, setSearch] = useState(''); + // "Show More" button when there's lots of options + const [shouldShowMore, setShowMore] = useState(false); + // Track recently moved items for highlight animation + const [recentlyMoved, setRecentlyMoved] = useState>( + new Set(), + ); + // For live searches, don't refresh percentages when date range changes + const [dateRange, setDateRange] = useState<[Date, Date]>( + chartConfig.dateRange, + ); + + useEffect(() => { + if (!isLive) { + setDateRange(chartConfig.dateRange); + } + }, [chartConfig.dateRange, isLive]); + + const handleSetSearch = useCallback( + (value: string) => { + setSearch(value); + if (value && !hasLoadedMore) { + onLoadMore(name); + } + }, + [hasLoadedMore, name, onLoadMore], + ); + + const { + data: distributionData, + isFetching: isFetchingDistribution, + error: distributionError, + } = useGetValuesDistribution( + { + chartConfig: { ...chartConfig, dateRange }, + key: distributionKey || name, + limit: 100, // The 100 most common values are enough to find any values that are present in at least 1% of rows + }, + { + enabled: showDistributions, + }, + ); + + useEffect(() => { + onFetchingDistributionChange(isFetchingDistribution); + }, [isFetchingDistribution, onFetchingDistributionChange]); + + useEffect(() => { + if (distributionError) { + notifications.show({ + color: 'red', + title: 'Error loading filter distribution', + message: distributionError?.message, + autoClose: 5000, + }); + onDistributionError(); + } + }, [distributionError, onDistributionError]); + + const totalAppliedFiltersSize = + selectedValues.included.size + + selectedValues.excluded.size + + (selectedValues.range != null ? 1 : 0); + + // Loaded options + any selected options that aren't in the loaded list + const augmentedOptions = useMemo(() => { + const selectedSet = new Set([ + ...selectedValues.included, + ...selectedValues.excluded, + ]); + return [ + ...Array.from(selectedSet) + .filter(value => !options.find(option => option.value === value)) + .map(value => ({ value, label: value.toString() })), + ...options, + ]; + }, [options, selectedValues]); + + const displayedItemLimit = shouldShowMore + ? SHOW_MORE_MAX_VALUES_DISPLAYED + : INITIAL_MAX_VALUES_DISPLAYED; + + // Options matching search, sorted appropriately + const sortedMatchingOptions = useMemo(() => { + // When searching, sort alphabetically + if (search) { + return augmentedOptions + .filter(option => { + return ( + option.value && + option.label.toLowerCase().includes(search.toLowerCase()) + ); + }) + .toSorted((a, b) => + a.label.localeCompare(b.label, undefined, { numeric: true }), + ); + } + + // When not searching, sort by personal pinned, shared pinned, selected, + // distribution, then alphabetically + return augmentedOptions.toSorted((a, b) => { + const aPinned = isPinned(a.value); + const aShared = isSharedPinned?.(a.value) ?? false; + const aIncluded = selectedValues.included.has(a.value); + const aExcluded = selectedValues.excluded.has(a.value); + const bPinned = isPinned(b.value); + const bShared = isSharedPinned?.(b.value) ?? false; + const bIncluded = selectedValues.included.has(b.value); + const bExcluded = selectedValues.excluded.has(b.value); + + // First sort by personal pinned status + if (aPinned && !bPinned) return -1; + if (!aPinned && bPinned) return 1; + + // Then sort by shared pinned status + if (aShared && !bShared) return -1; + if (!aShared && bShared) return 1; + + // Then sort by included status + if (aIncluded && !bIncluded) return -1; + if (!aIncluded && bIncluded) return 1; + + // Then sort by excluded status + if (aExcluded && !bExcluded) return -1; + if (!aExcluded && bExcluded) return 1; + + // Then sort by estimated percentage of rows with this value, if available + const aPercentage = distributionData?.get(a.value.toString()) ?? 0; + const bPercentage = distributionData?.get(b.value.toString()) ?? 0; + if (aPercentage !== bPercentage) { + return bPercentage - aPercentage; + } + + // Finally sort alphabetically/numerically + return a.label.localeCompare(b.label, undefined, { numeric: true }); + }); + }, [ + search, + augmentedOptions, + isPinned, + isSharedPinned, + selectedValues.included, + selectedValues.excluded, + distributionData, + ]); + + // The subset of options to be displayed + const displayedOptions = useMemo(() => { + return sortedMatchingOptions.length <= displayedItemLimit + ? sortedMatchingOptions + : sortedMatchingOptions.slice(0, displayedItemLimit); + }, [sortedMatchingOptions, displayedItemLimit]); + + // Simple highlight animation when checkbox is checked + const handleChange = useCallback( + (value: string | boolean) => { + // If checking (not unchecking), trigger highlight animation + const wasIncluded = selectedValues.included.has(value); + if (!wasIncluded) { + setRecentlyMoved(prev => new Set(prev).add(value)); + setTimeout(() => { + setRecentlyMoved(prev => { + const newSet = new Set(prev); + newSet.delete(value); + return newSet; + }); + }, 600); + } + onChange(value); + }, + [onChange, selectedValues], + ); + + const isLimitingDisplayedItems = + sortedMatchingOptions.length > displayedOptions.length; + + const showShowMoreButton = + !search && + augmentedOptions.length > INITIAL_MAX_VALUES_DISPLAYED && + totalAppliedFiltersSize < augmentedOptions.length; + + return ( + + {augmentedOptions.length > 5 && ( +
+ ) => + handleSetSearch(event.currentTarget.value) + } + rightSectionWidth={20} + rightSection={} + classNames={{ + input: 'ps-0.5', + }} + /> +
+ )} + {displayedOptions.map(option => ( + handleChange(option.value)} + onClickOnly={() => onOnlyClick(option.value)} + onClickExclude={() => onExcludeClick(option.value)} + onClickPin={() => onPinClick(option.value)} + sharedPinned={isSharedPinned?.(option.value)} + onClickSharedPin={ + onSharedPinClick ? () => onSharedPinClick(option.value) : undefined + } + isPercentageLoading={isFetchingDistribution} + percentage={ + showDistributions && distributionData + ? (distributionData.get(option.value.toString()) ?? 0) + : undefined + } + /> + ))} + {optionsLoading ? ( + + + + Loading... + + + ) : displayedOptions.length === 0 ? ( + + + No options found + + + ) : null} + {isLimitingDisplayedItems && (shouldShowMore || search) && ( + + Search to see more + + )} + {loadMoreLoading && ( + + + + Loading more... + + + )} + {showShowMoreButton && ( +
+ + Less + + ) : ( + <> + Show more + + ) + } + onClick={() => { + // When show more is clicked, immediately show all and also fetch more from server. + setShowMore(!shouldShowMore); + if (!shouldShowMore) { + onLoadMore?.(name); + } + }} + /> +
+ )} + {onLoadMore && + !showShowMoreButton && + !shouldShowMore && + !hasLoadedMore && + !loadMoreLoading && ( +
+ + Load more + + } + onClick={() => onLoadMore(name)} + /> +
+ )} +
+ ); +}; + +type FilterGroupActionsProps = { + name: string; + hasRange: boolean; + showDistributions: boolean; + isFetchingDistribution: boolean; + isColumnDisplayed: boolean; + isFieldPinned: boolean; + isSharedFieldPinned: boolean; + totalAppliedFiltersSize: number; + toggleShowDistributions: VoidFunction; + onColumnToggle?: VoidFunction; + onFieldPinClick: VoidFunction; + onToggleSharedFieldPin: VoidFunction; + onClearClick: VoidFunction; +}; +function FilterGroupActions({ + name, + hasRange, + showDistributions, + isFetchingDistribution, + isColumnDisplayed, + isFieldPinned, + isSharedFieldPinned, + totalAppliedFiltersSize, + toggleShowDistributions, + onColumnToggle, + onFieldPinClick, + onToggleSharedFieldPin, + onClearClick, +}: FilterGroupActionsProps) { + return ( + + {!hasRange && ( + <> + + + {isFetchingDistribution ? ( +
+ +
+ ) : showDistributions ? ( + + ) : ( + + )} +
+
+ {onColumnToggle && ( + + + {isColumnDisplayed ? ( + + ) : ( + + )} + + + )} + + + )} + {totalAppliedFiltersSize > 0 && ( + + + + + + )} +
+ ); +} + +const voidFunc = () => {}; + +export const FilterGroup = ({ + name, + options, + optionsLoading, + selectedValues: _selectedValues, + onChange, + onClearClick, + onOnlyClick, + onExcludeClick, + valuePins, + fieldPins, + onColumnToggle, + isColumnDisplayed, + onLoadMore, + loadMoreLoading, + hasLoadedMore, + isDefaultExpanded, + showFilterCounts, + 'data-testid': dataTestId, + chartConfig, + isLive, + distributionKey, + onRangeChange, +}: FilterGroupProps) => { + const [isExpanded, setExpanded] = useState(isDefaultExpanded ?? false); + const [showDistributions, setShowDistributions] = useState(false); + const [isFetchingDistribution, setIsFetchingDistribution] = useState(false); + + const selectedValues: SelectedValues = useMemo( + () => _selectedValues ?? { included: new Set(), excluded: new Set() }, + [_selectedValues], + ); + + const hasRange = selectedValues.range != null; + + const toggleShowDistributions = useCallback(() => { + setShowDistributions(prev => { + if (!prev) { + setExpanded(true); + } + return !prev; + }); + }, []); + + const onDistributionError = useCallback(() => { + setShowDistributions(false); + }, []); + + useEffect(() => { + if (isDefaultExpanded) { + setExpanded(true); + } + }, [isDefaultExpanded]); + + const totalAppliedFiltersSize = + selectedValues.included.size + + selectedValues.excluded.size + + (hasRange ? 1 : 0); + + const hasOptions = options.length > 0 || totalAppliedFiltersSize > 0; + + // An empty map attribute key (e.g. LogAttributes['']) yields an empty name, + // which Mantine rejects as an Accordion.Item value and throws during render. + const displayName = name.trim() ? name : '(empty)'; + + return ( + { + setExpanded(v === displayName); + }} + > + + +
+ + 26 ? 0 : 1500} + label={displayName} + position="top" + withArrow + fz="xxs" + color="gray" + > + + {displayName} + {showFilterCounts && ( + {` (${totalAppliedFiltersSize > 0 ? totalAppliedFiltersSize : options.length})`} + )} + + + + +
+ + {selectedValues.range?.min != null && + selectedValues.range.max != null ? ( + + ) : ( + isExpanded && ( + + ) + )} + +
+
+
+ ); +}; + +const DBSearchPageFiltersComponent = ({ + filters: filterState, + clearFilter, + setFilterValue: _setFilterValue, + isLive, + chartConfig, + analysisMode, + setAnalysisMode, + sourceId, + tableConnections, + showDelta, + denoiseResults, + setDenoiseResults, + setFilterRange, + onColumnToggle, + displayedColumns, + onCollapse, +}: { + analysisMode: 'results' | 'delta' | 'pattern'; + setAnalysisMode?: (mode: 'results' | 'delta' | 'pattern') => void; + isLive: boolean; + chartConfig: BuilderChartConfigWithDateRange; + sourceId?: string; + /** + * Tables to discover fields and values from when the source can't say. A + * metric source fans out to one table per metric type, so the caller resolves + * one per selected series. Memoize it — it keys the discovery queries. + */ + tableConnections?: TableConnection[]; + showDelta?: boolean; + denoiseResults: boolean; + setDenoiseResults: (denoiseResults: boolean) => void; + setFilterRange: (key: string, range: { min: number; max: number }) => void; + onColumnToggle?: (column: string) => void; + displayedColumns?: string[]; + onCollapse?: () => void; +} & FilterStateHook) => { + const setFilterValue = useCallback( + ( + property: string, + value: string | boolean, + action?: 'only' | 'exclude' | 'include' | undefined, + ) => { + return _setFilterValue(property, value, action); + }, + [_setFilterValue], + ); + const { + toggleFilterPin, + toggleFieldPin, + isFilterPinned, + isFieldPinned, + getPinnedFields, + pinnedFilters, + toggleSharedFieldPin, + isSharedFieldPinned, + toggleSharedFilterPin, + isSharedFilterPinned, + resetPersonalPins, + resetSharedFilters, + hasPersonalPins, + hasSharedPins, + } = usePinnedFilters(sourceId ?? null); + const { data: pinnedFiltersApiData } = usePinnedFiltersApi(sourceId ?? null); + const [isSharedFiltersVisible, setSharedFiltersVisible] = useLocalStorage( + 'hdx-shared-filters-visible', + true, + ); + const [showFilterCounts, setShowFilterCounts] = useLocalStorage( + 'hdx-show-filter-counts', + true, + ); + const [isSharedFiltersExpanded, setSharedFiltersExpanded] = useLocalStorage( + 'hdx-shared-filters-expanded', + true, + ); + const [showAllValues, setShowAllValues] = useLocalStorage( + 'hdx-show-all-filter-values', + true, + ); + const { size, startResize } = useResizable(16, 'left'); + + const { data: source } = useSource({ id: sourceId }); + // A metric source has no single table of its own, so schema lookups here read + // the per-series metric tables the caller resolved. Any one of them answers + // for columns and metadata; field discovery intersects them in `useFacets`. + const sourceTableConnection = useMemo( + () => resolveTableConnections(source, tableConnections)[0], + [source, tableConnections], + ); + const { data: jsonColumns } = useJsonColumns(sourceTableConnection); + + // Special case for live tail + const [dateRange, setDateRange] = useState<[Date, Date]>( + chartConfig.dateRange, + ); + + const { data: columns } = useColumns(sourceTableConnection); + + const { data: tableMetadata } = useTableMetadata(sourceTableConnection); + + useEffect(() => { + if (!isLive) { + // eslint-disable-next-line react-hooks/set-state-in-effect + setDateRange(chartConfig.dateRange); + } + }, [chartConfig.dateRange, isLive]); + + const showRefreshButton = isLive && dateRange !== chartConfig.dateRange; + + // Conditionally backtick-quote facet keys that contain special characters and + // match known column names, so they can be used in the ClickHouse query to get + // key values. + const knownColumns = useMemo( + () => (columns ? new Set(columns.map(c => c.name)) : new Set()), + [columns], + ); + + const [showMoreFields, setShowMoreFields] = useState(false); + const { + data: fetchFacetsData, + isLoading: isFacetsLoading, + isFetching: isFacetsFetching, + error, + loadMoreFacetsForKey, + loadMoreLoadingKeys, + extraFacetKeys, + } = useFetchFacets({ + chartConfig, + sourceId: sourceId ?? null, + tableConnections, + dateRange, + mode: showAllValues ? 'all' : 'exact', + filterState, + showMoreFields, + }); + const facets = fetchFacetsData.keyValues; + + useEffect(() => { + if (error) { + notifications.show({ + color: 'red', + title: error?.name, + message: error?.message, + autoClose: 5000, + }); + } + }, [error]); + + // Merge pinned filter values into the queried facets, so that pinned values are always available + const facetsWithPinnedValues = useMemo(() => { + const facetsMap = new Map((facets ?? []).map(f => [f.key, f.value])); + const mergedKeys = new Set([ + ...facetsMap.keys(), + ...Object.keys(pinnedFilters), + ...getPinnedFields(), + ]); + + return Array.from(mergedKeys).map(key => { + const queriedValues = facetsMap.get(key); + const pinnedValues = pinnedFilters[key]; + const mergedValues = new Set([ + ...(queriedValues ?? []), + ...(pinnedValues ?? []), + ]); + + return { key, value: Array.from(mergedValues) }; + }); + }, [facets, pinnedFilters, getPinnedFields]); + + // Build the set of team-pinned fields for the Shared Filters section, + // so we can avoid duplicating them in the regular Filters list below. + const sharedFilterKeys = useMemo(() => { + if (!isSharedFiltersVisible || !pinnedFiltersApiData?.team) { + return new Set(); + } + const team = pinnedFiltersApiData.team; + return new Set([...team.fields, ...Object.keys(team.filters)]); + }, [isSharedFiltersVisible, pinnedFiltersApiData]); + + // Build the facet list for the Shared Filters section. + // For each team-pinned field: merge pinned values with dynamic facet values. + const sharedFacets = useMemo(() => { + if (sharedFilterKeys.size === 0) return []; + + const facetMap = new Map( + (facetsWithPinnedValues ?? []).map(f => [f.key, f.value]), + ); + + return Array.from(sharedFilterKeys).map(key => { + const teamVals = pinnedFiltersApiData?.team?.filters[key] ?? []; + const dynamicValues = facetMap.get(key) ?? []; + + let merged: (string | boolean)[]; + if (teamVals.length > 0) { + merged = [...teamVals]; + for (const v of dynamicValues) { + if (!merged.some(existing => existing === v)) { + merged.push(v); + } + } + } else { + // Field-only pin — show dynamic values + merged = [...dynamicValues]; + } + + return { key, value: merged }; + }); + }, [sharedFilterKeys, facetsWithPinnedValues, pinnedFiltersApiData]); + + const shownFacets = useMemo(() => { + const _facets: { key: string; value: (string | boolean)[] }[] = []; + for (const _facet of facetsWithPinnedValues ?? []) { + const facet = structuredClone(_facet); + if (jsonColumns?.some(col => facet.key.startsWith(col))) { + facet.key = `toString(${facet.key})`; + } + + // Skip fields already shown in the Shared Filters section + if (sharedFilterKeys.has(facet.key)) { + continue; + } + + // don't include empty facets, unless they are already selected or pinned + const filter = filterState[facet.key]; + const hasSelectedValues = + filter && (filter.included.size > 0 || filter.excluded.size > 0); + const isPinned = isFieldPinned(facet.key); + if (facet.value?.length > 0 || hasSelectedValues || isPinned) { + _facets.push(facet); + } + } + // get remaining filterState that are not in _facets + const remainingFilterState = Object.keys(filterState).filter( + key => + !_facets.some(facet => facet.key === key) && !sharedFilterKeys.has(key), + ); + for (const key of remainingFilterState) { + _facets.push({ + key, + value: Array.from(filterState[key].included), + }); + } + + // prioritize facets that are primary keys + _facets.sort((a, b) => { + const aIsPk = isFieldPrimary(tableMetadata, a.key); + const bIsPk = isFieldPrimary(tableMetadata, b.key); + return aIsPk && !bIsPk ? -1 : bIsPk && !aIsPk ? 1 : 0; + }); + + // prioritize facets that are pinned (either personal or shared) + _facets.sort((a, b) => { + const aPinned = isFieldPinned(a.key) || isSharedFieldPinned(a.key); + const bPinned = isFieldPinned(b.key) || isSharedFieldPinned(b.key); + return aPinned && !bPinned ? -1 : bPinned && !aPinned ? 1 : 0; + }); + + // among pinned, prioritize shared over personal + _facets.sort((a, b) => { + const aShared = isSharedFieldPinned(a.key); + const bShared = isSharedFieldPinned(b.key); + return aShared && !bShared ? -1 : bShared && !aShared ? 1 : 0; + }); + + // prioritize facets that have checked items + _facets.sort((a, b) => { + const aChecked = filterState?.[a.key]?.included.size > 0; + const bChecked = filterState?.[b.key]?.included.size > 0; + return aChecked && !bChecked ? -1 : bChecked && !aChecked ? 1 : 0; + }); + + // prioritize facets that have a range + _facets.sort((a, b) => { + const aRange = filterState?.[a.key]?.range; + const bRange = filterState?.[b.key]?.range; + if (aRange && !bRange) return -1; + if (!aRange && bRange) return 1; + return 0; + }); + + return _facets; + }, [ + facetsWithPinnedValues, + filterState, + tableMetadata, + isFieldPinned, + isSharedFieldPinned, + jsonColumns, + sharedFilterKeys, + ]); + + // Check if shared facets have active selections + const showSharedClearButton = useMemo( + () => + sharedFacets.some(facet => { + const f = filterState[facet.key]; + return ( + f && (f.included.size > 0 || f.excluded.size > 0 || f.range != null) + ); + }), + [sharedFacets, filterState], + ); + + // Check if non-shared facets have active selections + const showFiltersClearButton = useMemo( + () => + shownFacets.some(facet => { + const f = filterState[facet.key]; + return ( + f && (f.included.size > 0 || f.excluded.size > 0 || f.range != null) + ); + }), + [shownFacets, filterState], + ); + + const clearSharedSelections = useCallback(() => { + for (const facet of sharedFacets) { + clearFilter(facet.key); + } + }, [sharedFacets, clearFilter]); + + const clearRegularSelections = useCallback(() => { + for (const facet of shownFacets) { + clearFilter(facet.key); + } + }, [shownFacets, clearFilter]); + + const parentSpanIdExpr = + source?.kind === SourceKind.Trace + ? source.parentSpanIdExpression + : undefined; + + const setRootSpansOnly = useCallback( + (rootSpansOnly: boolean) => { + if (!parentSpanIdExpr) return; + + if (rootSpansOnly) { + if (columns?.some(col => col.name === IS_ROOT_SPAN_COLUMN_NAME)) { + setFilterValue(IS_ROOT_SPAN_COLUMN_NAME, true, 'only'); + } else { + setFilterValue(parentSpanIdExpr, '', 'only'); + } + } else { + clearFilter(parentSpanIdExpr); + clearFilter(IS_ROOT_SPAN_COLUMN_NAME); + } + }, + [setFilterValue, clearFilter, parentSpanIdExpr, columns], + ); + + const isRootSpansOnly = useMemo(() => { + if (!parentSpanIdExpr || source?.kind !== SourceKind.Trace) return false; + + const parentSpanIdFilter = filterState?.[parentSpanIdExpr]; + const isRootSpanFilter = filterState?.[IS_ROOT_SPAN_COLUMN_NAME]; + return ( + (parentSpanIdFilter?.included.size === 1 && + parentSpanIdFilter?.included.has('')) || + (isRootSpanFilter?.included.size === 1 && + isRootSpanFilter?.included.has(true)) + ); + }, [filterState, source, parentSpanIdExpr]); + + /** + * Renders a list of facets as FilterGroup and NestedFilterGroup components. + * Used for both the Shared Filters section and the regular Filters section. + */ + const renderFacetList = useCallback( + ( + facets: { key: string; value: (string | boolean)[] }[], + options?: { keyPrefix?: string; isDefaultExpanded?: boolean }, + ) => { + const { keyPrefix = '', isDefaultExpanded: forceExpanded } = + options ?? {}; + const { grouped, nonGrouped } = groupFacetsByBaseName(facets); + + const makeValuePins = (key: string): ValuePinHandlers => ({ + onPinClick: (value: string | boolean) => toggleFilterPin(key, value), + isPinned: (value: string | boolean) => isFilterPinned(key, value), + onSharedPinClick: (value: string | boolean) => + toggleSharedFilterPin(key, value), + isSharedPinned: (value: string | boolean) => + isSharedFilterPinned(key, value), + }); + + const makeFieldPins = (key: string): FieldPinHandlers => ({ + onFieldPinClick: () => toggleFieldPin(key), + isFieldPinned: isFieldPinned(key), + onToggleSharedFieldPin: () => toggleSharedFieldPin(key), + isSharedFieldPinned: isSharedFieldPinned(key), + }); + + return ( + <> + {grouped.map(group => ( + ({ + ...child, + sqlKey: toQuotedClickHouseKeyExpression( + child.key, + knownColumns, + ), + }))} + selectedValues={group.children.reduce((acc, child) => { + acc[child.key] = getFilterStateEntry( + filterState, + child.key, + ) ?? { + included: new Set(), + excluded: new Set(), + }; + return acc; + }, {} as FilterState)} + onChange={(key, value) => setFilterValue(key, value)} + onClearClick={key => clearFilter(key)} + onOnlyClick={(key, value) => setFilterValue(key, value, 'only')} + onExcludeClick={(key, value) => + setFilterValue(key, value, 'exclude') + } + onPinClick={(key, value) => toggleFilterPin(key, value)} + isPinned={(key, value) => isFilterPinned(key, value)} + onSharedPinClick={(key, value) => + toggleSharedFilterPin(key, value) + } + isSharedPinned={(key, value) => isSharedFilterPinned(key, value)} + onFieldPinClick={key => toggleFieldPin(key)} + isFieldPinned={key => isFieldPinned(key)} + onToggleSharedFieldPin={key => toggleSharedFieldPin(key)} + isSharedFieldPinned={key => isSharedFieldPinned(key)} + showFilterCounts={showFilterCounts} + onColumnToggle={onColumnToggle} + displayedColumns={displayedColumns} + onLoadMore={loadMoreFacetsForKey} + loadMoreLoading={group.children.reduce( + (acc, child) => { + acc[child.key] = loadMoreLoadingKeys.has(child.key); + return acc; + }, + {} as Record, + )} + hasLoadedMore={group.children.reduce( + (acc, child) => { + acc[child.key] = extraFacetKeys.has(child.key); + return acc; + }, + {} as Record, + )} + isDefaultExpanded={ + forceExpanded ?? + group.children.some(child => { + const entry = getFilterStateEntry(filterState, child.key); + return ( + (entry && + (entry.included.size > 0 || entry.excluded.size > 0)) || + isFieldPinned(child.key) || + isSharedFieldPinned(child.key) + ); + }) + } + chartConfig={chartConfig} + isLive={isLive} + /> + ))} + {nonGrouped.map(facet => { + const facetSqlKey = toQuotedClickHouseKeyExpression( + facet.key, + knownColumns, + ); + return ( + ({ + value, + label: value.toString(), + }))} + optionsLoading={isFacetsLoading} + selectedValues={ + getFilterStateEntry(filterState, facet.key) ?? { + included: new Set(), + excluded: new Set(), + } + } + onChange={value => setFilterValue(facet.key, value)} + onClearClick={() => clearFilter(facet.key)} + onOnlyClick={value => setFilterValue(facet.key, value, 'only')} + onExcludeClick={value => + setFilterValue(facet.key, value, 'exclude') + } + valuePins={makeValuePins(facet.key)} + fieldPins={makeFieldPins(facet.key)} + onColumnToggle={ + onColumnToggle ? () => onColumnToggle(facetSqlKey) : undefined + } + isColumnDisplayed={displayedColumns?.includes(facetSqlKey)} + onLoadMore={loadMoreFacetsForKey} + loadMoreLoading={loadMoreLoadingKeys.has(facet.key)} + hasLoadedMore={extraFacetKeys.has(facet.key)} + isDefaultExpanded={(() => { + const entry = getFilterStateEntry(filterState, facet.key); + return ( + forceExpanded ?? + (isFieldPrimary(tableMetadata, facet.key) || + isFieldPinned(facet.key) || + isSharedFieldPinned(facet.key) || + (entry != null && + (entry.included.size > 0 || + entry.excluded.size > 0 || + entry.range != null))) + ); + })()} + chartConfig={chartConfig} + isLive={isLive} + onRangeChange={range => setFilterRange(facet.key, range)} + /> + ); + })} + + ); + }, + [ + filterState, + setFilterValue, + clearFilter, + toggleFilterPin, + isFilterPinned, + toggleSharedFilterPin, + isSharedFilterPinned, + toggleFieldPin, + isFieldPinned, + toggleSharedFieldPin, + isSharedFieldPinned, + onColumnToggle, + displayedColumns, + loadMoreFacetsForKey, + loadMoreLoadingKeys, + showFilterCounts, + isFacetsLoading, + chartConfig, + isLive, + setFilterRange, + tableMetadata, + knownColumns, + extraFacetKeys, + ], + ); + + return ( + +
+ + + + + Filters {isFacetsFetching && '···'} + + + {showRefreshButton && ( + setDateRange(chartConfig.dateRange)} + /> + } + /> + )} + {showFiltersClearButton && ( + + + + + + )} + + {onCollapse && ( + + + + + + )} + + + {setAnalysisMode && ( + + setAnalysisMode(value as 'results' | 'delta' | 'pattern') + } + orientation="vertical" + w="100%" + placement="right" + > + + + Results Table + + {showDelta && ( + + Event Deltas + + )} + {!IS_CLICKHOUSE_BUILD && ( + + Event Patterns + + )} + + + )} + {isSharedFiltersVisible && ( + 0} + opened={isSharedFiltersExpanded} + onToggle={() => + setSharedFiltersExpanded(!isSharedFiltersExpanded) + } + showClearButton={showSharedClearButton} + onClearSelections={clearSharedSelections} + > + {renderFacetList(sharedFacets, { + keyPrefix: 'shared-', + isDefaultExpanded: true, + })} + + )} + + {/* Divider between shared and regular filters */} + {isSharedFiltersVisible && sharedFacets.length > 0 && ( + + )} + + + {analysisMode === 'results' && ( + + + + + Denoise Results + + + + } + onChange={() => setDenoiseResults(!denoiseResults)} + /> + )} + + {source?.kind === SourceKind.Trace && + source.parentSpanIdExpression && ( + + + + + Root Spans Only + + + + } + onChange={event => setRootSpansOnly(event.target.checked)} + /> + )} + + {isFacetsLoading ? ( + + + + ) : ( + shownFacets.length === 0 && ( + No filters available + ) + )} + {/* Show facets even when loading to ensure pinned filters are visible while loading */} + {renderFacetList(shownFacets)} + + + + {showMoreFields && ( +
+ + Not seeing a filter? + + + {`Try searching instead (e.g. column:foo)`} + +
+ )} +
+
+
+ + ); +}; + +function isFieldPrimary(tableMetadata: TableMetadata | undefined, key: string) { + return tableMetadata?.primary_key?.includes(key); +} +export const DBSearchPageFilters = memo(DBSearchPageFiltersComponent); diff --git a/packages/app/src/components/Explore/owned/DBSqlRowTableWithSidebar.tsx b/packages/app/src/components/Explore/owned/DBSqlRowTableWithSidebar.tsx new file mode 100644 index 0000000000..e2466a7ebd --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBSqlRowTableWithSidebar.tsx @@ -0,0 +1,267 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { useCallback, useState } from 'react'; +import { useQueryState } from 'nuqs'; +import { + ClickHouseQueryError, + ColumnMetaType, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { + BuilderChartConfigWithDateRange, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { SortingState } from '@tanstack/react-table'; + +import { ChartErrorStateVariant } from '@/components/charts/ChartErrorState'; +import { DBRowSidePanelErrorState } from '@/components/DBRowSidePanelErrorState'; +import { RowWhereResult, WithClause } from '@/hooks/useRowWhere'; +import { useSource } from '@/source'; +import TabBar from '@/TabBar'; +import { useLocalStorage } from '@/utils'; +import { parseAsStringEncoded } from '@/utils/queryParsers'; + +import { RowDataPanel, useRowData } from './DBRowDataPanel'; +import { RowOverviewPanel } from './DBRowOverviewPanel'; +import DBRowSidePanel, { + ExploreRowSidePanelProvider, + RowSidePanelContextProps, +} from './DBRowSidePanel'; +import { + DBRawSqlRowTable, + DBRowTableVariant, + DBSqlRowTable, + RawSqlRowTableConfig, +} from './DBRowTable'; + +interface Props { + sourceId: string; + config: BuilderChartConfigWithDateRange; + onError?: (error: Error | ClickHouseQueryError) => void; + onScroll?: (scrollTop: number) => void; + onSidebarOpen?: (rowId: string) => void; + onExpandedRowsChange?: (hasExpandedRows: boolean) => void; + onPropertyAddClick?: (keyPath: string, value: string) => void; + context?: RowSidePanelContextProps; + enabled?: boolean; + isLive?: boolean; + queryKeyPrefix?: string; + denoiseResults?: boolean; + collapseAllRows?: boolean; + onSortingChange?: (v: SortingState | null) => void; + initialSortBy?: SortingState; + variant?: DBRowTableVariant; + enableSmallFirstWindow?: boolean; + tableId?: string; + errorVariant?: ChartErrorStateVariant; + enableRowSelection?: boolean; + selectionResetKey?: string; + onSelectedRowsChange?: (hasSelectedRows: boolean) => void; + onResolvedColumnsChange?: (meta: ColumnMetaType[]) => void; + // Clicking outside the row side panel (and outside `keepOpenSelector`) closes + // it. Enabled by default; pass `false` to opt out. + closeOnClickOutside?: boolean; + keepOpenSelector?: string; + rememberWidth?: boolean; + tableBarRightSection?: React.ReactNode; + /** Show the rows of this hand-written statement instead of `config`'s. */ + rawSqlConfig?: RawSqlRowTableConfig; +} + +// Clicking the results table (selecting/switching rows, scrolling) keeps the +// row side panel open by default; callers can widen this via `keepOpenSelector`. +const DEFAULT_KEEP_OPEN_SELECTOR = '[data-testid="search-results-table"]'; + +export default function DBSqlRowTableWithSideBar({ + sourceId, + config, + onError, + onScroll, + context, + onExpandedRowsChange, + denoiseResults, + collapseAllRows, + isLive, + enabled, + onSidebarOpen, + onSortingChange, + initialSortBy, + variant, + enableSmallFirstWindow, + tableId, + errorVariant, + enableRowSelection, + selectionResetKey, + onSelectedRowsChange, + onResolvedColumnsChange, + closeOnClickOutside = true, + keepOpenSelector = DEFAULT_KEEP_OPEN_SELECTOR, + rememberWidth = false, + tableBarRightSection, + rawSqlConfig, +}: Props) { + const { data: sourceData } = useSource({ id: sourceId }); + const [rowId, setRowId] = useQueryState('rowWhere', parseAsStringEncoded); + const [rowSource, setRowSource] = useQueryState('rowSource'); + const [aliasWith, setAliasWith] = useState([]); + + const onOpenSidebar = useCallback( + (rowWhere: RowWhereResult) => { + setRowId(rowWhere.where); + setAliasWith(rowWhere.aliasWith); + setRowSource(sourceId); + onSidebarOpen?.(rowWhere.where); + }, + [setRowId, setAliasWith, setRowSource, sourceId, onSidebarOpen], + ); + + const onCloseSidebar = useCallback(() => { + setRowId(null); + setRowSource(null); + }, [setRowId, setRowSource]); + const renderRowDetails = useCallback( + (r: { id: string; aliasWith?: WithClause[]; [key: string]: unknown }) => { + if (!sourceData) { + return
Loading...
; + } + return ( + + ); + }, + [sourceData], + ); + + return ( + + {sourceData && (rowSource === sourceId || !rowSource) && ( + + )} + {rawSqlConfig ? ( + + ) : ( + + )} + + ); +} + +enum InlineTab { + Overview = 'overview', + ColumnValues = 'columnValues', +} + +function RowOverviewPanelWrapper({ + source, + rowId, + aliasWith, +}: { + source: TSource; + rowId: string; + aliasWith?: WithClause[]; +}) { + // Use localStorage to persist the selected tab + const [activeTab, setActiveTab] = useLocalStorage( + 'hdx-expanded-row-default-tab', + InlineTab.ColumnValues, + ); + + // Surface the same error state the row side panel shows (e.g. `SELECT *` + // failures on Distributed/Merge tables) rather than silently rendering an + // empty expanded row. Both tabs load the same row data, so a failure here + // affects the whole expanded row. + const { isError, error } = useRowData({ source, rowId, aliasWith }); + + if (isError && error) { + return ( +
+
+ +
+
+ ); + } + + return ( +
+
+ +
+
+ {activeTab === InlineTab.Overview && ( +
+ +
+ )} + {activeTab === InlineTab.ColumnValues && ( + + )} +
+
+ ); +} diff --git a/packages/app/src/components/Explore/owned/DBTimeChart.tsx b/packages/app/src/components/Explore/owned/DBTimeChart.tsx new file mode 100644 index 0000000000..e715c796ea --- /dev/null +++ b/packages/app/src/components/Explore/owned/DBTimeChart.tsx @@ -0,0 +1,1147 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import React, { + memo, + useCallback, + useEffect, + useId, + useMemo, + useRef, + useState, +} from 'react'; +import { add, differenceInSeconds } from 'date-fns'; +import { + convertGranularityToSeconds, + getAlignedDateRange, +} from '@hyperdx/common-utils/dist/core/utils'; +import { + isBuilderChartConfig, + isPromqlChartConfig, + isRawSqlChartConfig, +} from '@hyperdx/common-utils/dist/guards'; +import { + BuilderChartConfigWithDateRange, + ChartConfigWithDateRange, + DisplayType, + SourceKind, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { Popover, Portal } from '@mantine/core'; +import { IconChartBar, IconChartLine } from '@tabler/icons-react'; + +import api from '@/api'; +import { ChartAnnotation } from '@/components/charts/chartAnnotations'; +import ChartContainer from '@/components/charts/ChartContainer'; +import ChartErrorState, { + ChartErrorStateVariant, +} from '@/components/charts/ChartErrorState'; +import { useChartTooltipZIndex } from '@/components/charts/ChartTooltip'; +import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; +import DisplaySwitcher from '@/components/charts/DisplaySwitcher'; +import HiddenSeriesIndicator from '@/components/charts/HiddenSeriesIndicator'; +import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; +import { + MAX_LOADABLE_TIME_CHART_SERIES, + resolveRenderedSeriesCap, +} from '@/defaults'; +import { type ActiveClickPayload, MemoChart } from '@/HDXMultiSeriesTimeChart'; +import { + getMinGranularitySeconds, + useQueriedChartConfig, +} from '@/hooks/useChartConfig'; +import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; +import { useChartNumberFormats, useSource } from '@/source'; +import type { NumberFormat } from '@/types'; + +import { ChartSeriesTooltip } from './ChartSeriesTooltip'; +import { + AGG_FNS, + buildEventsSearchUrl, + ChartKeyJoiner, + convertToTimeChartConfig, + formatResponseForTimeChart, + getPreviousDateRange, + shouldFillNullsWithZero, + tryExpandConfigVariables, + useTimeChartSettings, +} from './ChartUtils'; + +/** A single group column / value pair decoded from a chart series key. */ +export type SeriesGroupFilter = { column: string; value: string }; + +// Only one pinned tooltip at a time across all charts. Module-level (not +// context) because charts can be scattered with no common provider, and their +// onClick stopPropagation hides cross-chart clicks from Mantine's click-outside. +const pinnedTooltipRegistry = new Map void>(); + +function broadcastTooltipPinned(activeId: string) { + pinnedTooltipRegistry.forEach((dismiss, id) => { + if (id !== activeId) { + dismiss(); + } + }); +} + +// Registers this chart's dismiss handler and returns a callback to close every +// other chart's pinned tooltip (call it when pinning this one). +function useCrossChartPinDismiss(onDismiss: () => void): () => void { + const id = useId(); + // Keep the latest onDismiss without re-subscribing each render. + const onDismissRef = useRef(onDismiss); + useEffect(() => { + onDismissRef.current = onDismiss; + }, [onDismiss]); + + useEffect(() => { + pinnedTooltipRegistry.set(id, () => onDismissRef.current()); + return () => { + pinnedTooltipRegistry.delete(id); + }; + }, [id]); + + return useCallback(() => broadcastTooltipPinned(id), [id]); +} + +// Decode a Recharts series key (e.g. "count · error · api") into the +// underlying group-column filters. This is the same decode `buildSearchUrl` +// uses to build a drill-down URL, extracted so the focus callback can hand the +// caller structured filters (rather than a display string) to apply to a +// sibling results list. +export function decodeSeriesGroupFilters({ + seriesKey, + groupColumns, + isSingleValueColumn, +}: { + seriesKey: string | undefined; + groupColumns: string[]; + isSingleValueColumn: boolean | undefined; +}): SeriesGroupFilter[] { + const seriesKeys = seriesKey?.split(ChartKeyJoiner); + const groupFilters: SeriesGroupFilter[] = []; + + if (seriesKeys?.length && groupColumns?.length) { + // When the series has multiple value columns, the key is prefixed with the + // value column name (e.g. "count · error"), so the group values start at + // index 1. (The "no group columns" case the original inline code also + // guarded is impossible here — this block only runs when groupColumns is + // non-empty.) + const startsWithValueColumn = !(isSingleValueColumn ?? true); + const groupValues = startsWithValueColumn + ? seriesKeys.slice(1) + : seriesKeys; + + groupValues.forEach((value, index) => { + if (groupColumns[index] != null) { + groupFilters.push({ column: groupColumns[index], value }); + } + }); + } + + return groupFilters; +} + +// The interactive PINNED tooltip, rendered over the chart in a body-portaled +// Mantine Popover anchored at the clicked point. Hover uses the recharts tooltip +// in MemoChart instead; this is only for the click-locked state. +function ChartTooltipOverlay({ + payload, + buildSearchUrl, + buildRelatedTraceUrl, + drillInLabel, + focusLabel, + showSingleSeriesActions, + seriesActions = 'icons', + onDismiss, + onFocusSeries, + onShowAllSeries, + fallbackNumberFormat, + numberFormatByKey, + previousPeriodOffsetSeconds, + hiddenSeriesCount, + onLoadAllSeries, + expanded, +}: { + payload: ActiveClickPayload | undefined; + buildSearchUrl: (key?: string, value?: number) => string | null; + buildRelatedTraceUrl?: (key?: string, value?: number) => string | null; + drillInLabel?: string; + focusLabel?: string; + showSingleSeriesActions?: boolean; + seriesActions?: 'icons' | 'menu'; + onDismiss: () => void; + /** Focus a series by its raw series key (dataKey) and display name. */ + onFocusSeries: (payload: { dataKey?: string; name: string }) => void; + /** Clear an active series focus; undefined when nothing is focused. */ + onShowAllSeries?: () => void; + fallbackNumberFormat?: NumberFormat; + /** Per-value-column formats, keyed by result column name. */ + numberFormatByKey: Map; + previousPeriodOffsetSeconds?: number; + /** Series dropped by the chart's render cap (see ChartSeriesTooltip). */ + hiddenSeriesCount?: number; + /** Render every series on the chart, bypassing the cap. */ + onLoadAllSeries?: () => void; + /** "Load all" is active: render every row in the scrollable tooltip body. */ + expanded?: boolean; +}) { + const isOpen = + payload != null && + payload.activePayload != null && + payload.activePayload.length > 0; + + const popoverZIndex = useChartTooltipZIndex({ pinned: true }); + + const dropdownRef = useRef(null); + + // The pinned tooltip anchors at `position: fixed` viewport coords captured + // once at click time. When a surrounding scroll container scrolls, the chart + // moves but the fixed tooltip stays glued to the viewport, detaching from its + // data point (Mantine's closeOnClickOutside/closeOnEscape don't fire on + // scroll). Dismiss on scroll instead so it never floats away — but ignore + // scrolls originating inside the tooltip's own scrollable series list, or a + // long tooltip couldn't be scrolled without instantly closing. + useEffect(() => { + if (!isOpen) return; + const handleScroll = (e: Event) => { + const target = e.target; + if (target instanceof Node && dropdownRef.current?.contains(target)) { + return; + } + onDismiss(); + }; + window.addEventListener('scroll', handleScroll, { + capture: true, + passive: true, + }); + return () => { + window.removeEventListener('scroll', handleScroll, { capture: true }); + }; + }, [isOpen, onDismiss]); + + // Dismiss on outside click. Mantine's closeOnClickOutside misses it because + // the chart's recharts onClick calls stopPropagation (see + // HDXMultiSeriesTimeChart handleClick); a capture-phase listener sees the + // click regardless, ignoring clicks inside the tooltip's own dropdown. + useEffect(() => { + if (!isOpen) return; + const handleMouseDown = (e: MouseEvent) => { + const target = e.target; + if (target instanceof Node) { + if (dropdownRef.current?.contains(target)) return; + // Don't dismiss when clicking inside a portaled child of the tooltip + // (e.g. the per-series action Menu.Dropdown, which uses withinPortal + // and therefore renders outside dropdownRef.current in the DOM). + if ( + target instanceof Element && + target.closest('[data-tooltip-child-portal]') + ) { + return; + } + } + onDismiss(); + }; + document.addEventListener('mousedown', handleMouseDown, true); + return () => { + document.removeEventListener('mousedown', handleMouseDown, true); + }; + }, [isOpen, onDismiss]); + + if (!isOpen) { + return null; + } + + return ( + // Portal to body so the `position: fixed` anchor resolves against the + // viewport: dashboard tiles use CSS transforms, and a transformed ancestor + // would otherwise make `fixed` resolve against it and throw the tooltip off. + + { + if (!opened) { + onDismiss(); + } + }} + closeOnClickOutside={seriesActions !== 'menu'} + closeOnEscape + trapFocus={false} + withinPortal + position="bottom" + offset={12} + middlewares={{ flip: true, shift: true }} + returnFocus={false} + zIndex={popoverZIndex} + > + + {/* 1x1 anchor at the clicked data point. */} +
+ + + + + + + ); +} + +type DBTimeChartComponentProps = { + config: ChartConfigWithDateRange; + disableQueryChunking?: boolean; + disableDrillDown?: boolean; + enableParallelQueries?: boolean; + enabled?: boolean; + logReferenceTimestamp?: number; + onSettled?: () => void; + onTimeRangeSelect?: (start: Date, end: Date) => void; + queryKeyPrefix?: string; + referenceLines?: React.ReactNode; + /** Event markers (e.g. alert firing/recovery) drawn as dashed lines with labels. */ + annotations?: ChartAnnotation[]; + setDisplayType?: (type: DisplayType) => void; + showDisplaySwitcher?: boolean; + showLegend?: boolean; + sourceId?: string; + /** Names of series that should not be shown in the chart */ + hiddenSeries?: string[]; + title?: React.ReactNode; + toolbarPrefix?: React.ReactNode[]; + toolbarSuffix?: React.ReactNode[]; + showMVOptimizationIndicator?: boolean; + showDateRangeIndicator?: boolean; + errorVariant?: ChartErrorStateVariant; + /** + * Called when the user clicks "Focus" on a series in the drill-down menu, + * with the group-column filters decoded from that series. When provided, the + * consumer owns focus behavior — e.g. the search page applies these as search + * filters so both the chart AND the sibling results list narrow to the series. + * When omitted, Focus falls back to a chart-only visual isolation (legend + * behavior), which is all a standalone chart can do. + */ + onFocusSeries?: (filters: SeriesGroupFilter[]) => void; + /** + * Page a series drill-down opens. Defaults to the Search page; Explore points + * it at itself so drilling into a chart doesn't hand the reader to a + * different page mid-investigation. + */ + drillDownPath?: '/search' | '/explore'; +}; + +function DBTimeChartComponent({ + config, + disableQueryChunking, + disableDrillDown, + enableParallelQueries, + enabled = true, + logReferenceTimestamp, + onTimeRangeSelect, + queryKeyPrefix, + referenceLines, + annotations, + setDisplayType, + showDisplaySwitcher = true, + showLegend = true, + sourceId, + hiddenSeries, + title, + toolbarPrefix, + toolbarSuffix, + showMVOptimizationIndicator = true, + showDateRangeIndicator = true, + errorVariant, + onFocusSeries, + drillDownPath, +}: DBTimeChartComponentProps) { + const [selectedSeriesSet, setSelectedSeriesSet] = useState>( + new Set(), + ); + + // When the render cap hides series, the hidden-series notice lets the user + // opt into rendering every series (accepting the memory/perf cost). Store the + // query shape the opt-in was enabled at (not a bare boolean) so it can be + // gated on the current shape during render — this resets the opt-in the + // instant the query changes, with no setState-in-effect and no one-commit + // window where a stale opt-in pairs with the new shape. `queryShapeIdentity` + // is derived below; `showAllSeries` is defined right after it. + const [showAllSeriesShape, setShowAllSeriesShape] = useState( + null, + ); + + const handleClearSelectedSeries = useCallback(() => { + setSelectedSeriesSet(new Set()); + }, []); + + const handleToggleSeries = useCallback( + (seriesName: string, isShiftKey?: boolean) => { + setSelectedSeriesSet(prev => { + const newSet = new Set(prev); + + if (isShiftKey) { + // Shift-click: add to selection + if (newSet.has(seriesName)) { + newSet.delete(seriesName); + } else { + newSet.add(seriesName); + } + } else { + // Regular click: toggle selection + if (newSet.has(seriesName) && newSet.size === 1) { + // If this is the only selected item, clear selection (show all) + newSet.clear(); + } else { + // Otherwise, select only this one + newSet.clear(); + newSet.add(seriesName); + } + } + + return newSet; + }); + }, + [], + ); + + const { data: source } = useSource({ + id: sourceId || config.source, + }); + const minGranularitySeconds = getMinGranularitySeconds(source); + // Both useTimeChartSettings and convertToTimeChartConfig resolve 'auto', so the + // minimum has to be in `config` before they run. + const configWithFloor = useMemo( + () => ({ ...config, minGranularitySeconds }), + [config, minGranularitySeconds], + ); + + const originalDateRange = config.dateRange; + const { + displayType: displayTypeProp, + dateRange, + granularity, + fillNulls, + } = useTimeChartSettings(configWithFloor); + + const { data: me, isLoading: isLoadingMe } = api.useMe(); + + const queriedConfig = useMemo( + () => convertToTimeChartConfig(configWithFloor), + [configWithFloor], + ); + + // Stable identity for the query's SHAPE, excluding the sliding time window. + // `queriedConfig` (and the `config` it derives from) is a fresh object literal + // on every render — dashboard tiles rebuild the tile config inline each render + // (e.g. on hover) and live ranges tick the dateRange/granularity — so keying + // effects on its object reference, or serializing the whole thing, would fire + // them on unrelated re-renders / every live tick. Stripping the time fields + // yields a value that changes only when the user re-authors the query. + const queryShapeIdentity = useMemo(() => { + // Serialize every top-level field except the sliding time window. + const shape: Record = { ...queriedConfig }; + delete shape.dateRange; + delete shape.granularity; + delete shape.dateRangeEndInclusive; + // Builder configs normalize both `0` and null seriesLimit to `undefined`, + // which JSON.stringify drops — so a null<->0 edit wouldn't change the shape + // and the reset effect below wouldn't fire. Fold in the raw value so the + // two states serialize differently. + return JSON.stringify({ + shape, + rawSeriesLimit: config.seriesLimit ?? null, + }); + }, [queriedConfig, config.seriesLimit]); + + // "Load all" is active only while the shape it was enabled at still matches. + // Deriving it (instead of resetting a boolean in an effect) means a query + // change drops the opt-in in the same render, so a stale opt-in can never + // pair with the new shape — and there's no setState-in-effect. + const showAllSeries = showAllSeriesShape === queryShapeIdentity; + const enableShowAllSeries = useCallback( + () => setShowAllSeriesShape(queryShapeIdentity), + [queryShapeIdentity], + ); + // "Load all" only helps when it actually raises the cap. If the tile's own + // seriesLimit already meets or exceeds the load-all bound (or is unlimited), + // clicking would render the same set — so don't offer the affordance rather + // than flip showAllSeries to a state where onLoadAll goes permanently + // undefined for a no-op. + const loadAllCanRaiseCap = + MAX_LOADABLE_TIME_CHART_SERIES > + resolveRenderedSeriesCap(config.seriesLimit); + const loadAllHandler = + showAllSeries || !loadAllCanRaiseCap ? undefined : enableShowAllSeries; + + // Determine whether the config can be optimized with an MV, to drive the MV + // optimization indicator and the MV-derived date-range indicator in the + // toolbar. Only those two indicators consume `mvOptimizationData`, so skip + // this extra ClickHouse EXPLAIN when both are hidden — which includes the + // edit-modal preview (ChartPreviewPanel passes showMVOptimizationIndicator and + // showDateRangeIndicator both false), so the EXPLAIN is skipped there too. + const builderQueriedConfig: BuilderChartConfigWithDateRange | undefined = + isBuilderChartConfig(queriedConfig) ? queriedConfig : undefined; + const { data: mvOptimizationData } = useMVOptimizationExplanation( + builderQueriedConfig, + { enabled: showMVOptimizationIndicator || showDateRangeIndicator }, + ); + + const { data, isLoading, isError, error, isPlaceholderData, isSuccess } = + useQueriedChartConfig(queriedConfig, { + placeholderData: (prev: any) => prev, + queryKey: [ + queryKeyPrefix, + queriedConfig, + 'chunked', + { + disableQueryChunking, + enableParallelQueries, + parallelizeWhenPossible: me?.team?.parallelizeWhenPossible, + }, + ], + enabled: enabled && !isLoadingMe, + enableQueryChunking: !disableQueryChunking, + enableParallelQueries: + enableParallelQueries && me?.team?.parallelizeWhenPossible, + }); + + const previousPeriodChartConfig: ChartConfigWithDateRange = useMemo(() => { + const previousPeriodDateRange = + queriedConfig.alignDateRangeToGranularity === false + ? getPreviousDateRange(originalDateRange) + : getAlignedDateRange( + getPreviousDateRange(originalDateRange), + granularity, + ); + + return { + ...queriedConfig, + dateRange: previousPeriodDateRange, + }; + }, [queriedConfig, originalDateRange, granularity]); + + const previousPeriodOffsetSeconds = useMemo(() => { + return config.compareToPreviousPeriod + ? differenceInSeconds( + dateRange[0], + previousPeriodChartConfig.dateRange[0], + ) + : undefined; + }, [ + config.compareToPreviousPeriod, + dateRange, + previousPeriodChartConfig.dateRange, + ]); + + const { data: previousPeriodData, isLoading: isPreviousPeriodLoading } = + useQueriedChartConfig(previousPeriodChartConfig, { + placeholderData: (prev: any) => prev, + queryKey: [queryKeyPrefix, previousPeriodChartConfig, 'chunked'], + enabled: !!(enabled && config.compareToPreviousPeriod), + enableQueryChunking: true, + }); + + const isLoadingOrPlaceholder = + isLoading || + isPreviousPeriodLoading || + !data?.isComplete || + (config.compareToPreviousPeriod && !previousPeriodData?.isComplete) || + isPlaceholderData; + + // Where a metric chart's drill-down lands. Loaded here so the clicked series + // can be re-addressed against the destination's own attribute expression + // rather than arriving as a bare time range. + const { data: drillDownTargetSource } = useSource({ + id: + source?.kind === SourceKind.Metric || source?.kind === SourceKind.Trace + ? source.logSourceId + : undefined, + }); + const { data: relatedTraceSource } = useSource({ + id: + source?.kind === SourceKind.Metric && + drillDownTargetSource?.kind === SourceKind.Log + ? drillDownTargetSource.traceSourceId + : undefined, + }); + + const { formatByColumn, chartFormat: axisNumberFormat } = + useChartNumberFormats(queriedConfig, data?.meta); + + const { + error: resultFormattingError, + graphResults, + timestampColumn, + groupColumns, + valueColumns, + isSingleValueColumn, + lineData, + hiddenSeriesCount, + renderedSeriesCount, + } = useMemo(() => { + const defaultResponse = { + error: null, + graphResults: [], + timestampColumn: undefined, + lineData: [], + groupColumns: [], + valueColumns: [], + isSingleValueColumn: true, + hiddenSeriesCount: 0, + renderedSeriesCount: 0, + }; + + if (data == null || !isSuccess) { + return defaultResponse; + } + + try { + const formatResult = formatResponseForTimeChart({ + currentPeriodResponse: data, + previousPeriodResponse: config.compareToPreviousPeriod + ? previousPeriodData + : undefined, + dateRange, + granularity, + generateEmptyBuckets: shouldFillNullsWithZero(fillNulls), + source, + hiddenSeries, + previousPeriodOffsetSeconds, + // "Load all" (from the warning / pinned tooltip) overrides everything; + // otherwise the per-tile Series Limit drives the cap (null = default, + // 0 = unlimited). On builder group-by charts the SQL CTE already trims + // to seriesLimit, so this is a no-op there; on raw SQL it's the only + // cardinality guard. "Load all" is bounded (not truly unlimited) so a + // runaway high-cardinality result can't exhaust browser memory; drawn + // lines stay capped at HARD_LINES_LIMIT either way. Take the max of the + // bound and the tile's own cap so load-all can only ever RAISE the + // rendered count — never reduce it when a tile's seriesLimit already + // exceeds the bound. (A `0`/unlimited tile shows no affordance, so the + // Infinity case is unreachable here.) + maxSeries: showAllSeries + ? Math.max( + MAX_LOADABLE_TIME_CHART_SERIES, + resolveRenderedSeriesCap(config.seriesLimit), + ) + : resolveRenderedSeriesCap(config.seriesLimit), + }); + return { + ...defaultResponse, + ...formatResult, + }; + } catch (e: unknown) { + console.error(e); + return { + ...defaultResponse, + error: e, + }; + } + }, [ + data, + dateRange, + granularity, + isSuccess, + fillNulls, + source, + config.compareToPreviousPeriod, + config.seriesLimit, + previousPeriodData, + hiddenSeries, + previousPeriodOffsetSeconds, + showAllSeries, + ]); + + // To enable backward compatibility, allow non-controlled usage of displayType + const [displayTypeLocal, setDisplayTypeLocal] = useState(displayTypeProp); + + const displayType = useMemo(() => { + if (setDisplayType) { + return displayTypeProp; + } else { + return displayTypeLocal; + } + }, [displayTypeLocal, displayTypeProp, setDisplayType]); + + const handleSetDisplayType = useCallback( + (type: DisplayType) => { + if (setDisplayType) { + setDisplayType(type); + } else { + setDisplayTypeLocal(type); + } + }, + [setDisplayType], + ); + + useEffect(() => { + if (config.compareToPreviousPeriod) { + setDisplayTypeLocal(DisplayType.Line); + } + }, [config.compareToPreviousPeriod]); + + const [activeClickPayload, setActiveClickPayload] = useState< + ActiveClickPayload | undefined + >(undefined); + + const dismissPinned = useCallback(() => setActiveClickPayload(undefined), []); + const notifyTooltipPinned = useCrossChartPinDismiss(dismissPinned); + + // Dismiss any open pin when the query shape changes: its frozen snapshot + // belongs to the previous query, and the resync effect would otherwise + // repaint it with the new query's series at the stale click anchor. (The + // "load all" opt-in resets on its own — it's derived from + // `showAllSeriesShape === queryShapeIdentity` above — so it isn't touched + // here.) Keyed on `queryShapeIdentity` (a stable serialization of the query + // shape) rather than the `queriedConfig` object reference — which is new + // every render — so unrelated re-renders (tile hover) and live-range ticks + // don't dismiss the pin, while re-authoring the query still does. + useEffect(() => { + dismissPinned(); + }, [queryShapeIdentity, dismissPinned]); + + // Pin the tooltip on click. Not gated on `source`: source-less charts still + // show values/percent-change, and the drill-down actions hide themselves when + // there's no source. `disableDrillDown` stays an explicit opt-out. + const setPinnedPayload = useCallback( + (payload: ActiveClickPayload | undefined) => { + if (disableDrillDown) { + return; + } + // Pinning here closes any other chart's pinned tooltip. + if (payload != null) { + notifyTooltipPinned(); + } + setActiveClickPayload(payload); + }, + [disableDrillDown, notifyTooltipPinned], + ); + + // In-place refresh of the already-open pin's frozen snapshot (used by the + // chart's resync effect after "load all" / live ticks). Unlike + // setPinnedPayload this does NOT broadcast the cross-chart pin-dismiss — it + // isn't opening a new pin, just repainting the current one's rows. + const refreshPinnedPayload = useCallback( + (payload: ActiveClickPayload | undefined) => { + if (disableDrillDown) { + return; + } + setActiveClickPayload(payload); + }, + [disableDrillDown], + ); + + const clickedActiveLabelDate = useMemo(() => { + return activeClickPayload?.activeLabel != null + ? new Date(Number.parseInt(activeClickPayload.activeLabel) * 1000) + : undefined; + }, [activeClickPayload]); + + const buildSearchUrl = useCallback( + ( + seriesKey?: string, + seriesValue?: number, + targetSource: TSource | undefined = drillDownTargetSource, + ) => { + // Raw SQL charts are not supported for drill-down as we don't know the source which is being used. + if ( + clickedActiveLabelDate == null || + source == null || + isRawSqlChartConfig(config) || + isPromqlChartConfig(config) + ) { + return null; + } + + // The search page has no variable machinery, so the expressions read here + // and handed to buildEventsSearchUrl must be final SQL/Lucene. + // `whereLanguage` is pinned to buildEventsSearchUrl's default first, since + // expansion below leaves nothing for it to expand. + const expandedConfig = tryExpandConfigVariables({ + ...config, + whereLanguage: config.whereLanguage || 'lucene', + }); + + // Parse the series key to extract group values + const seriesKeys = seriesKey?.split(ChartKeyJoiner); + const groupFilters = decodeSeriesGroupFilters({ + seriesKey, + groupColumns, + isSingleValueColumn, + }); + + // Build value range filter for Y-axis if provided + let valueRangeFilter: + | { + expression: string; + value: number; + } + | undefined; + + // Metric formula configs with hidden operand series project only the + // formula column(s), so value columns no longer map positionally onto + // `select` — skip the value-range filter rather than misattributing a + // formula value to an operand's expression. (With operands shown, the + // operand columns still map by index and formula columns fall past the + // `< expandedConfig.select.length` bound below.) + const operandsHidden = + isBuilderChartConfig(expandedConfig) && + (expandedConfig.formulas?.length ?? 0) > 0 && + expandedConfig.showOperandSeries === false; + + if ( + seriesValue && + !operandsHidden && + Array.isArray(expandedConfig.select) && + expandedConfig.select.length > 0 + ) { + // Determine which value column to filter on + let valueExpression: string | undefined; + + if ( + (isSingleValueColumn ?? true) && + expandedConfig.select.length === 1 + ) { + const firstSelect = expandedConfig.select[0]; + const aggFn = + typeof firstSelect === 'string' ? undefined : firstSelect.aggFn; + // Only add value range filter if the aggregation is attributable + const isAttributable = + AGG_FNS.find(fn => fn.value === aggFn)?.isAttributable !== false; + + if (isAttributable) { + valueExpression = + typeof firstSelect === 'string' + ? firstSelect + : firstSelect.valueExpression; + } + } else if (seriesKeys?.length && (valueColumns?.length ?? 0) > 0) { + const firstPart = seriesKeys[0]; + const valueColumnIndex = valueColumns?.findIndex( + col => col === firstPart, + ); + + if ( + valueColumnIndex != null && + valueColumnIndex >= 0 && + valueColumnIndex < expandedConfig.select.length + ) { + const selectItem = expandedConfig.select[valueColumnIndex]; + const aggFn = + typeof selectItem === 'string' ? undefined : selectItem.aggFn; + // Only add value range filter if the aggregation is attributable + const isAttributable = + AGG_FNS.find(fn => fn.value === aggFn)?.isAttributable !== false; + + if (isAttributable) { + valueExpression = + typeof selectItem === 'string' + ? selectItem + : selectItem.valueExpression; + } + } + } + + if (valueExpression) { + valueRangeFilter = { + expression: valueExpression, + value: seriesValue, + }; + } + } + + // Calculate time range from clicked date and granularity + const from = clickedActiveLabelDate; + const to = add(clickedActiveLabelDate, { + seconds: convertGranularityToSeconds(granularity), + }); + + return buildEventsSearchUrl({ + source, + config: expandedConfig, + dateRange: [from, to], + groupFilters, + valueRangeFilter, + targetSource, + basePath: drillDownPath, + }); + }, + [ + clickedActiveLabelDate, + config, + granularity, + source, + drillDownTargetSource, + drillDownPath, + groupColumns, + valueColumns, + isSingleValueColumn, + ], + ); + + const buildRelatedTraceUrl = useCallback( + (seriesKey?: string, seriesValue?: number) => + relatedTraceSource + ? buildSearchUrl(seriesKey, seriesValue, relatedTraceSource) + : null, + [buildSearchUrl, relatedTraceSource], + ); + + // Focus a series from the drill-down menu. When the consumer supplied an + // onFocusSeries handler, decode the series into its group-column filters and + // hand them up so the consumer can narrow both the chart and any sibling + // results list. Otherwise fall back to chart-only visual isolation, which is + // the best a standalone chart (e.g. a dashboard tile) can do. + const handleFocusSeries = useCallback( + ({ dataKey, name }: { dataKey?: string; name: string }) => { + if (onFocusSeries) { + const groupFilters = decodeSeriesGroupFilters({ + seriesKey: dataKey, + groupColumns, + isSingleValueColumn, + }); + if (groupFilters.length > 0) { + onFocusSeries(groupFilters); + return; + } + } + handleToggleSeries(name); + }, + [onFocusSeries, groupColumns, isSingleValueColumn, handleToggleSeries], + ); + + const toolbarItemsMemo = useMemo(() => { + const allToolbarItems = []; + + if (toolbarPrefix && toolbarPrefix.length > 0) { + allToolbarItems.push(...toolbarPrefix); + } + + if (source && showMVOptimizationIndicator && builderQueriedConfig) { + allToolbarItems.push( + , + ); + } + + const mvDateRange = mvOptimizationData?.optimizedConfig?.dateRange; + const isAlignedToChartGranularity = + queriedConfig.alignDateRangeToGranularity !== false; + + if ( + showDateRangeIndicator && + (mvDateRange || isAlignedToChartGranularity) + ) { + const mvGranularity = isAlignedToChartGranularity + ? undefined + : mvOptimizationData?.explanations.find(e => e.success)?.mvConfig + .minGranularity; + + allToolbarItems.push( + , + ); + } + + if (showDisplaySwitcher) { + allToolbarItems.push( + , + }, + { + value: DisplayType.StackedBar, + label: config.compareToPreviousPeriod + ? 'Bar Chart Unavailable When Comparing to Previous Period' + : 'Display as Bar Chart', + icon: , + disabled: config.compareToPreviousPeriod, + }, + ]} + />, + ); + } + + if (hiddenSeriesCount > 0) { + allToolbarItems.push( + , + ); + } + + if (toolbarSuffix && toolbarSuffix.length > 0) { + allToolbarItems.push(...toolbarSuffix); + } + + return allToolbarItems; + }, [ + builderQueriedConfig, + config, + displayType, + handleSetDisplayType, + showDisplaySwitcher, + source, + toolbarPrefix, + toolbarSuffix, + showMVOptimizationIndicator, + showDateRangeIndicator, + mvOptimizationData, + queriedConfig, + hiddenSeriesCount, + renderedSeriesCount, + loadAllHandler, + ]); + + return ( + + {isLoading && !data ? ( +
+ Loading Chart Data... +
+ ) : isError ? ( + + ) : resultFormattingError ? ( + + ) : graphResults.length === 0 ? ( +
+ No data found within time range. +
+ ) : ( + <> + {/* Pinned (click-locked) tooltip; hover is handled in MemoChart. */} + 0 ? handleClearSelectedSeries : undefined + } + fallbackNumberFormat={queriedConfig.numberFormat} + numberFormatByKey={formatByColumn} + previousPeriodOffsetSeconds={previousPeriodOffsetSeconds} + // "+N more" in the pinned tooltip loads every series (same escape + // hatch as the hidden-series warning). Only offered while capped. + hiddenSeriesCount={hiddenSeriesCount} + onLoadAllSeries={loadAllHandler} + // Once loaded, render the full set in the scrollable tooltip body + // (not just the 20-row preview) so "load all" actually shows them. + expanded={showAllSeries} + /> + + + )} +
+ ); +} + +export const DBTimeChart = memo(DBTimeChartComponent); diff --git a/packages/app/src/components/Explore/owned/PatternSidePanel.tsx b/packages/app/src/components/Explore/owned/PatternSidePanel.tsx new file mode 100644 index 0000000000..5d70024a52 --- /dev/null +++ b/packages/app/src/components/Explore/owned/PatternSidePanel.tsx @@ -0,0 +1,209 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import * as React from 'react'; +import { JSDataType } from '@hyperdx/common-utils/dist/clickhouse'; +import { TSource } from '@hyperdx/common-utils/dist/types'; +import { Button, Card, Drawer, Stack, Text } from '@mantine/core'; +import { IconList } from '@tabler/icons-react'; + +import { IsolatedChartSyncProvider } from '@/chartSync'; +// Easter egg: April Fools 2026 — see aiSummarize/ for details. +import AISummarizePatternButton from '@/components/AISummarizePatternButton'; +import { DrawerBody, DrawerHeader } from '@/components/DrawerUtils'; +import { PatternTemplate } from '@/components/Patterns/PatternTemplate'; +import { + LEVEL_COLUMN_ALIAS, + Pattern, + PATTERN_COLUMN_ALIAS, + SERVICE_NAME_COLUMN_ALIAS, + TIMESTAMP_COLUMN_ALIAS, +} from '@/hooks/usePatterns'; +import useRowWhere, { RowWhereResult } from '@/hooks/useRowWhere'; +import { getFirstTimestampValueExpression } from '@/source'; +import { useZIndex, ZIndexContext } from '@/zIndex'; + +import DBRowSidePanel from './DBRowSidePanel'; +import { RawLogTable } from './DBRowTable'; + +import styles from '@styles/LogSidePanel.module.scss'; + +export default function PatternSidePanel({ + isOpen, + onClose, + pattern, + bodyValueExpression, + source, + onViewMatchingEvents, + highlightPattern = false, +}: { + isOpen: boolean; + onClose: () => void; + pattern: Pattern; + bodyValueExpression: string; + source: TSource; + onViewMatchingEvents?: () => void; + highlightPattern?: boolean; +}) { + const contextZIndex = useZIndex(); + const drawerZIndex = contextZIndex + 100; + + const [selectedRowWhere, setSelectedRowWhere] = + React.useState(null); + + const columnTypeMap = React.useMemo( + () => + new Map([ + [TIMESTAMP_COLUMN_ALIAS, { _type: JSDataType.Date }], + [PATTERN_COLUMN_ALIAS, { _type: JSDataType.String }], + [LEVEL_COLUMN_ALIAS, { _type: JSDataType.String }], + [SERVICE_NAME_COLUMN_ALIAS, { _type: JSDataType.String }], + ]), + [], + ); + + const columnNameMap = React.useMemo( + () => ({ + [TIMESTAMP_COLUMN_ALIAS]: 'Timestamp', + [SERVICE_NAME_COLUMN_ALIAS]: 'Service', + [LEVEL_COLUMN_ALIAS]: 'Level', + [PATTERN_COLUMN_ALIAS]: 'Body', + }), + [], + ); + + const displayedColumns = React.useMemo(() => { + return [ + TIMESTAMP_COLUMN_ALIAS, + SERVICE_NAME_COLUMN_ALIAS, + LEVEL_COLUMN_ALIAS, + PATTERN_COLUMN_ALIAS, + ]; + }, []); + + const getRowWhere = useRowWhere({ + meta: [ + { name: 'body', type: 'String' }, + { name: 'ts', type: 'DateTime64(9)' }, + ], + aliasMap: { + body: bodyValueExpression, + ts: getFirstTimestampValueExpression(source.timestampValueExpression), + }, + }); + + const handleRowClick = React.useCallback( + (row: Record) => { + const rowWhereResult = getRowWhere({ + body: row[PATTERN_COLUMN_ALIAS], + ts: row[TIMESTAMP_COLUMN_ALIAS], + }); + setSelectedRowWhere(rowWhereResult); + }, + [getRowWhere], + ); + + const INITIAL_LIMIT = 100; + const [showAll, setShowAll] = React.useState(false); + + React.useEffect(() => { + setShowAll(false); + }, [pattern]); + + const displayedSamples = React.useMemo(() => { + if (showAll || pattern.samples.length <= INITIAL_LIMIT) { + return pattern.samples; + } + return pattern.samples.slice(0, INITIAL_LIMIT); + }, [pattern.samples, showAll]); + + const handleCloseRowSidePanel = React.useCallback(() => { + setSelectedRowWhere(null); + }, []); + + return ( + + + +
+ + + + + + {highlightPattern ? ( + + ) : ( + pattern.pattern + )} + + {onViewMatchingEvents != null && ( + + )} + + + + + ~{pattern.count?.toLocaleString()} Sample Events + + ({ where: row.id, aliasWith: [] })} + displayedColumns={displayedColumns} + columnTypeMap={columnTypeMap} + columnNameMap={columnNameMap} + onRowDetailsClick={handleRowClick} + wrapLines={false} + showExpandButton={false} + isLive={false} + /> + {!showAll && pattern.samples.length > INITIAL_LIMIT && ( + + )} + + + + {selectedRowWhere && ( + + )} +
+
+
+
+ ); +} diff --git a/packages/app/src/components/Explore/owned/PatternTable.tsx b/packages/app/src/components/Explore/owned/PatternTable.tsx new file mode 100644 index 0000000000..05488d9f21 --- /dev/null +++ b/packages/app/src/components/Explore/owned/PatternTable.tsx @@ -0,0 +1,216 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { useMemo, useState } from 'react'; +import { ClickHouseQueryError } from '@hyperdx/common-utils/dist/clickhouse'; +import { + BuilderChartConfigWithDateRange, + SourceKind, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { Box, Code, Container, Text } from '@mantine/core'; +import { IconBracketsContain } from '@tabler/icons-react'; + +import { SQLPreview } from '@/components/ChartSQLPreview'; +import EmptyState from '@/components/EmptyState'; +import { + buildPatternColumnExpression, + patternMatchSqlCondition, +} from '@/components/Patterns/patternColumn'; +import { PatternColumnSelector } from '@/components/Patterns/PatternColumnSelector'; +import { useSearchTotalCount } from '@/components/SearchTotalCountChart'; +import { Pattern, useGroupedPatterns } from '@/hooks/usePatterns'; +import { getLevelExpression } from '@/source'; + +import { RawLogTable } from './DBRowTable'; +import PatternSidePanel from './PatternSidePanel'; + +const emptyMap = new Map(); + +export default function PatternTable({ + config, + totalCountConfig, + totalCountQueryKeyPrefix, + bodyValueExpression, + patternColumn: externalPatternColumn, + draftPatternColumn: externalDraftPatternColumn, + onDraftPatternColumnChange: externalOnDraftPatternColumnChange, + onSubmit: externalOnSubmit, + onViewMatchingEvents, + highlightPatterns = false, + source, +}: { + config: BuilderChartConfigWithDateRange; + totalCountConfig: BuilderChartConfigWithDateRange; + bodyValueExpression: string; + patternColumn?: string | null; + draftPatternColumn?: string; + onDraftPatternColumnChange?: (value: string) => void; + onSubmit?: () => void; + /** Switch the search to events matching this Drain template. */ + onViewMatchingEvents?: (sqlCondition: string) => void; + /** Explore-only pattern highlighting and empty state. */ + highlightPatterns?: boolean; + totalCountQueryKeyPrefix: string; + source?: TSource; +}) { + const SAMPLES = 10_000; + + const [selectedPattern, setSelectedPattern] = useState(null); + + // Search edits the expression here and applies it with the page query. + // Explore keeps its picker in the toolbar, and dashboard tiles configure + // the expression in the editor, so neither passes these handlers. + const hasExternalPatternColumn = + externalOnDraftPatternColumnChange != null && externalOnSubmit != null; + + const effectiveBodyValueExpression = buildPatternColumnExpression({ + patternColumn: externalPatternColumn ?? null, + fallback: bodyValueExpression, + }); + + const { + error: totalCountError, + isLoading: isTotalCountLoading, + isTotalCountComplete, + totalCount, + } = useSearchTotalCount(totalCountConfig, totalCountQueryKeyPrefix); + + const { + data: groupedResults, + isLoading: isGroupedPatternsLoading, + error: groupedPatternsError, + patternQueryConfig, + } = useGroupedPatterns({ + config, + samples: SAMPLES, + bodyValueExpression: effectiveBodyValueExpression, + levelExpression: getLevelExpression(source), + serviceNameExpression: + ((source?.kind === SourceKind.Log || source?.kind === SourceKind.Trace) && + source.serviceNameExpression) || + '', + totalCount, + }); + + const isLoading = + isTotalCountLoading || !isTotalCountComplete || isGroupedPatternsLoading; + + const error = totalCountError || groupedPatternsError; + + const sortedGroupedResults = useMemo(() => { + return Object.values(groupedResults).sort( + (a, b) => b.count - a.count, + ) as Pattern[]; + }, [groupedResults]); + + const handleViewMatchingEvents = (pattern: Pattern) => { + const sqlCondition = patternMatchSqlCondition( + effectiveBodyValueExpression, + pattern.pattern, + ); + if (sqlCondition == null || onViewMatchingEvents == null) { + return; + } + onViewMatchingEvents(sqlCondition); + }; + + return ( + <> + {hasExternalPatternColumn && ( + + )} + {error ? ( + + + + Error Message: + + + {error.message} + + + {error instanceof ClickHouseQueryError && ( + + + Original Query: + + + + + + )} + + ) : highlightPatterns && + !isLoading && + sortedGroupedResults.length === 0 ? ( + } + title="No patterns found" + description="No repeating event shapes in this sample of 10,000. Try a different field or a wider time range." + /> + ) : ( + <> + setSelectedPattern(row as Pattern)} + hasNextPage={false} + fetchNextPage={() => {}} + highlightedLineId={''} + columnTypeMap={emptyMap} + generateRowId={row => ({ where: row.id, aliasWith: [] })} + columnNameMap={{ + __hdx_pattern_trend: 'Trend', + countStr: highlightPatterns ? 'Est. count' : 'Count', + pattern: 'Pattern', + level: 'Level', + }} + config={patternQueryConfig} + showExpandButton={false} + /> + {selectedPattern && source && ( + setSelectedPattern(null)} + onViewMatchingEvents={ + onViewMatchingEvents != null + ? () => handleViewMatchingEvents(selectedPattern) + : undefined + } + highlightPattern={highlightPatterns} + /> + )} + + )} + + ); +} diff --git a/packages/app/src/components/Explore/owned/SQLEditor.tsx b/packages/app/src/components/Explore/owned/SQLEditor.tsx new file mode 100644 index 0000000000..451d5628b0 --- /dev/null +++ b/packages/app/src/components/Explore/owned/SQLEditor.tsx @@ -0,0 +1,202 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { useCallback, useEffect, useMemo, useRef } from 'react'; +import { useController, UseControllerProps } from 'react-hook-form'; +import { acceptCompletion } from '@codemirror/autocomplete'; +import { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { Paper, useMantineColorScheme } from '@mantine/core'; +import CodeMirror, { + Compartment, + EditorView, + keymap, + Prec, + ReactCodeMirrorRef, +} from '@uiw/react-codemirror'; + +import { + createCodeMirrorSqlDialect, + createCodeMirrorStyleTheme, + DEFAULT_CODE_MIRROR_BASIC_SETUP, + type SQLCompletion, +} from '@/components/SQLEditor/utils'; +import { useMultipleAllFields } from '@/hooks/useMetadata'; +import { clickhouseSql } from '@/utils/codeMirror'; + +type SQLEditorProps = { + value: string; + onChange: (value: string) => void; + placeholder?: string; + height?: string; + enableLineWrapping?: boolean; + tableConnections?: TableConnection[]; + additionalCompletions?: SQLCompletion[]; + dateRange?: [Date, Date]; + timestampValueExpression?: string; + onSubmit?: () => void; + /** Gutter line numbers, for editors long enough to navigate by line. */ + showLineNumbers?: boolean; + /** Applied to the surrounding surface, so callers can restyle it. */ + className?: string; +}; + +export const createRunQueryKeyBinding = (onSubmit: () => void) => ({ + key: 'Mod-Enter', + run: () => { + onSubmit(); + return true; + }, +}); + +export default function SQLEditor({ + onChange, + placeholder, + value, + height, + enableLineWrapping = false, + tableConnections, + additionalCompletions, + dateRange, + timestampValueExpression, + onSubmit, + showLineNumbers = false, + className, +}: SQLEditorProps) { + const { colorScheme } = useMantineColorScheme(); + const ref = useRef(null); + const compartmentRef = useRef(new Compartment()); + + // The gutter shares the editor's fill and is set off only by its rule, so + // the numbers read as part of the block rather than a second panel. + const lineNumberTheme = useMemo( + () => + showLineNumbers + ? [ + EditorView.theme({ + '.cm-gutters': { + background: 'transparent', + borderRight: '1px solid var(--color-border)', + color: 'var(--color-text-muted)', + }, + '.cm-lineNumbers .cm-gutterElement': { padding: '0 8px' }, + }), + ] + : [], + [showLineNumbers], + ); + + const runQueryKeymap = useMemo( + () => + onSubmit == null + ? [] + : [Prec.highest(keymap.of([createRunQueryKeyBinding(onSubmit)]))], + [onSubmit], + ); + + const { data: fields } = useMultipleAllFields(tableConnections ?? [], { + dateRange, + timestampValueExpression, + }); + + const updateAutocompleteColumns = useCallback( + (viewRef: EditorView) => { + const identifiers: string[] = [ + // Suggest database and table names for autocompletion + ...new Set(tableConnections?.map(tc => tc.tableName) ?? []), + ...new Set(tableConnections?.map(tc => tc.databaseName) ?? []), + ...new Set( + tableConnections?.map(tc => `${tc.databaseName}.${tc.tableName}`) ?? + [], + ), + + // Suggest column names for autocompletion, including Map keys + ...(fields?.map(column => { + if (column.path.length > 1) { + return `${column.path[0]}['${column.path[1]}']`; + } + return column.path[0]; + }) ?? []), + ]; + + viewRef.dispatch({ + effects: compartmentRef.current.reconfigure( + createCodeMirrorSqlDialect({ + identifiers, + additionalCompletions, + includeAggregateFunctions: true, + includeRegularFunctions: true, + }), + ), + }); + }, + [additionalCompletions, fields, tableConnections], + ); + + useEffect(() => { + if (ref.current != null && ref.current.view != null) { + updateAutocompleteColumns(ref.current.view); + } + }, [updateAutocompleteColumns]); + + return ( + + + + ); +} + +export function SQLEditorControlled({ + onValueChange, + ...props +}: Omit & + UseControllerProps & { + /** + * Fired alongside the form update, for callers that need to react to the + * user editing (rather than to the value changing, which also happens when + * the field is set programmatically). + */ + onValueChange?: (value: string) => void; + }) { + const { field } = useController(props); + + return ( + { + field.onChange(value); + onValueChange?.(value); + }} + /> + ); +} diff --git a/packages/app/src/components/Explore/owned/SQLInlineEditor.tsx b/packages/app/src/components/Explore/owned/SQLInlineEditor.tsx new file mode 100644 index 0000000000..0ee109a9b9 --- /dev/null +++ b/packages/app/src/components/Explore/owned/SQLInlineEditor.tsx @@ -0,0 +1,466 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import cx from 'classnames'; +import { useController, UseControllerProps } from 'react-hook-form'; +import { useHotkeys } from 'react-hotkeys-hook'; +import { + acceptCompletion, + autocompletion, + closeCompletion, + Completion, + startCompletion, +} from '@codemirror/autocomplete'; +import { + Field, + TableConnectionChoice, +} from '@hyperdx/common-utils/dist/core/metadata'; +import { + Flex, + Paper, + Text, + Tooltip, + useMantineColorScheme, +} from '@mantine/core'; +import { IconInfoCircle } from '@tabler/icons-react'; +import CodeMirror, { + Compartment, + EditorView, + keymap, + Prec, + ReactCodeMirrorRef, + tooltips, +} from '@uiw/react-codemirror'; + +import { EDITOR_INPUT_HEIGHTS } from '@/components/editorInputHeights'; +import { KEYWORDS_FOR_WHERE_OR_ORDER_BY } from '@/components/SQLEditor/constants'; +import { + createCodeMirrorSqlDialect, + createCodeMirrorStyleTheme, + DEFAULT_CODE_MIRROR_BASIC_SETUP, +} from '@/components/SQLEditor/utils'; +import { useSqlVariableCompletions } from '@/components/SQLEditor/variableCompletions'; +import { + useVariableValidation, + VariableIssueIndicator, + variableValidationState, +} from '@/components/SQLEditor/variableValidation'; +import { useMultipleAllFields } from '@/hooks/useMetadata'; +import { useStableCallback } from '@/hooks/useStableCallback'; +import { useSource } from '@/source'; +import { useQueryHistory } from '@/utils'; +import { clickhouseSql } from '@/utils/codeMirror'; + +import styles from '@/components/SQLEditor/SQLInlineEditor.module.scss'; + +type SQLInlineEditorProps = { + filterField?: (field: Field) => boolean; + value: string; + onChange: (value: string) => void; + placeholder?: string; + onSubmit?: () => void; + error?: React.ReactNode; + size?: string; + label?: React.ReactNode; + disableKeywordAutocomplete?: boolean; + enableHotkey?: boolean; + tooltipText?: string; + additionalSuggestions?: string[]; + queryHistoryType?: string; + parentRef?: HTMLElement | null; + allowMultiline?: boolean; + /** Collapsed editor height in px. Defaults from `size` (xs → 30, else 36). */ + minHeight?: number; + dateRange?: [Date, Date]; + sourceId?: string; + // With multiple tableConnections, offer only fields present in ALL of them + // (intersection) rather than the union — for an expression that must be valid + // against every connection, e.g. a chart-level Group By over multiple series. + intersectFields?: boolean; + // Show a line-number gutter. Off by default so existing inline usages stay + // compact; opt-in for editor-style surfaces (e.g. the Explore query editor). + showLineNumbers?: boolean; + // Whether the dashboard variables in scope apply to this expression: offer + // them as completions, and warn about the references that won't work. + enableVariables?: boolean; +}; + +const MAX_EDITOR_HEIGHT = '150px'; + +export default function SQLInlineEditor({ + tableConnection, + tableConnections, + filterField, + onChange, + placeholder, + onSubmit, + error, + value, + size, + label, + disableKeywordAutocomplete, + enableHotkey, + tooltipText, + additionalSuggestions, + queryHistoryType, + parentRef, + allowMultiline = true, + minHeight, + dateRange, + sourceId, + intersectFields, + showLineNumbers = false, + enableVariables = false, +}: SQLInlineEditorProps & TableConnectionChoice) { + const { colorScheme } = useMantineColorScheme(); + const _tableConnections = tableConnection + ? [tableConnection] + : tableConnections; + const { data: source } = useSource({ id: sourceId }); + const { data: fields } = useMultipleAllFields(_tableConnections ?? [], { + dateRange, + timestampValueExpression: source?.timestampValueExpression, + intersect: intersectFields, + }); + const filteredFields = useMemo(() => { + return filterField ? fields?.filter(filterField) : fields; + }, [fields, filterField]); + + // query search history + const [queryHistory, setQueryHistory] = useQueryHistory(queryHistoryType); + + const onSelectSearchHistory = useCallback( + (view: EditorView, from: number, to: number, q: string) => { + // update history into search bar + view.dispatch({ + changes: { from, to, insert: q }, + }); + // close history bar; + closeCompletion(view); + // update history order + setQueryHistory(q); + // execute search + if (onSubmit) onSubmit(); + }, + [onSubmit, setQueryHistory], + ); + + const createHistoryList = useMemo(() => { + return () => { + return { + from: 0, + options: queryHistory.map(q => { + return { + label: q, + section: 'Search History', + type: 'keyword', + apply: ( + view: EditorView, + _completion: Completion, + from: number, + to: number, + ) => { + onSelectSearchHistory(view, from, to, q); + }, + }; + }), + }; + }; + }, [queryHistory, onSelectSearchHistory]); + + // Dashboard variables in scope, offered alongside the column identifiers, + // and checked for the references that won't expand. + const variableCompletions = useSqlVariableCompletions({ + enabled: enableVariables, + }); + const variableIssues = useVariableValidation(value, { + enabled: enableVariables, + language: 'sql', + }); + + const [isFocused, setIsFocused] = useState(false); + + const ref = useRef(null); + + const compartmentRef = useRef(new Compartment()); + + const hasNonEmptyValue = value.trim().length > 0; + + const updateAutocompleteColumns = useCallback( + (viewRef: EditorView) => { + const identifiers = [ + ...(filteredFields?.map(column => { + if (column.path.length > 1) { + return `${column.path[0]}['${column.path[1]}']`; + } + return column.path[0]; + }) ?? []), + ...(additionalSuggestions ?? []), + ]; + + const auto = createCodeMirrorSqlDialect({ + identifiers, + keywords: KEYWORDS_FOR_WHERE_OR_ORDER_BY, + includeRegularFunctions: !disableKeywordAutocomplete, + additionalCompletions: variableCompletions, + }); + + const queryHistoryList = autocompletion({ + compareCompletions: () => { + return 0; + }, // don't sort the history search + override: [createHistoryList], + }); + viewRef.dispatch({ + effects: compartmentRef.current.reconfigure( + hasNonEmptyValue ? auto : queryHistoryList, + ), + }); + }, + [ + filteredFields, + additionalSuggestions, + variableCompletions, + disableKeywordAutocomplete, + createHistoryList, + hasNonEmptyValue, + ], + ); + + useEffect(() => { + if (ref.current != null && ref.current.view != null) { + updateAutocompleteColumns(ref.current.view); + } + // Otherwise we'll update the columns in `onCreateEditor` hook + }, [updateAutocompleteColumns]); + + useHotkeys( + ['/', 's'], + () => { + if (enableHotkey) { + ref.current?.view?.focus(); + } + }, + { + preventDefault: true, + enableOnFormTags: false, + enableOnContentEditable: false, + }, + [enableHotkey], + ); + + /** + * If the editor is inside a modal, we need to position the tooltip + * relative to the modal. This ensures that the autocompletion results + * are properly calculated off the correct element. + */ + const tooltipExt = useMemo(() => { + if (parentRef == null) { + return []; + } + // A body-level parent is already outside every scroll container, so the + // tooltip should be free to use the whole viewport — CodeMirror's default + // space. + if (parentRef === parentRef.ownerDocument.body) { + return [tooltips({ parent: parentRef })]; + } + return [ + tooltips({ + parent: parentRef, + tooltipSpace: view => { + const box = view.dom.getBoundingClientRect(); + const parentBox = parentRef.getBoundingClientRect(); + return { + ...box, + right: box.right ?? 0, + left: parentBox.left ?? box.left, + top: parentBox.top ?? box.top, + bottom: parentBox.bottom ?? box.bottom, + }; + }, + }), + ]; + }, [parentRef]); + + // Stable so a new onSubmit identity (every live tail tick) doesn't reconfigure the editor. + const submitFromEditor = useStableCallback(() => { + if (onSubmit == null) { + return false; + } + if (queryHistoryType && ref?.current?.view) { + setQueryHistory(ref?.current?.view.state.doc.toString()); + } + onSubmit(); + return true; + }); + + const cmExtensions = useMemo( + () => [ + ...tooltipExt, + createCodeMirrorStyleTheme(MAX_EDITOR_HEIGHT), + + // Enable line wrapping when multiline is allowed (regardless of focus) + ...(allowMultiline ? [EditorView.lineWrapping] : []), + + // eslint-disable-next-line react-hooks/refs + compartmentRef.current.of( + clickhouseSql({ + upperCaseKeywords: true, + }), + ), + + // Configure Enter key to submit search, and Shift + Enter to insert new line when multiline is allowed + Prec.highest( + keymap.of([ + { + key: 'Enter', + run: submitFromEditor, + }, + ...(allowMultiline + ? [ + { + key: 'Shift-Enter', + run: () => { + // Allow default behavior (insert new line) + return false; + }, + }, + ] + : []), + ]), + ), + keymap.of([ + { + key: 'Tab', + run: acceptCompletion, + }, + ]), + ], + [allowMultiline, submitFromEditor, tooltipExt], + ); + + // A new object on every render makes CodeMirror reconfigure and re-inject + // its theme's CSS rules. + const basicSetup = useMemo( + () => ({ + ...DEFAULT_CODE_MIRROR_BASIC_SETUP, + lineNumbers: showLineNumbers, + }), + [showLineNumbers], + ); + + const onClickCodeMirror = useCallback(() => { + if (ref?.current?.view) { + startCompletion(ref.current.view); + } + }, []); + + const validationState = variableValidationState(variableIssues, !!error); + const baseHeight = + minHeight ?? + (size === 'xs' ? EDITOR_INPUT_HEIGHTS.xs : EDITOR_INPUT_HEIGHTS.sm); + + return ( +
+ + {label != null && ( + + + + {label} + {tooltipText && } + + + + )} +
+ { + setIsFocused(true); + }, [setIsFocused])} + onBlur={useCallback(() => { + setIsFocused(false); + }, [setIsFocused])} + extensions={cmExtensions} + onCreateEditor={updateAutocompleteColumns} + basicSetup={basicSetup} + placeholder={placeholder} + onClick={onClickCodeMirror} + /> +
+ +
+
+ ); +} + +function SQLInlineEditorControlledComponent({ + placeholder, + filterField, + additionalSuggestions, + queryHistoryType, + ...props +}: Omit & + UseControllerProps & + TableConnectionChoice) { + const { field, fieldState } = useController(props); + + // Guard against wrongly typed values + const value = field.value || props.defaultValue; + + let stringValue = ''; + if (typeof value === 'string') { + stringValue = value; + } else if (value !== undefined) { + console.error('SQLInlineEditor: value is not a string', value); + } + + return ( + + ); +} +export const SQLInlineEditorControlled = memo( + SQLInlineEditorControlledComponent, +); diff --git a/packages/app/src/components/Explore/owned/exploreFilterHooks.ts b/packages/app/src/components/Explore/owned/exploreFilterHooks.ts new file mode 100644 index 0000000000..537dbf1f87 --- /dev/null +++ b/packages/app/src/components/Explore/owned/exploreFilterHooks.ts @@ -0,0 +1,453 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { useCallback, useEffect, useMemo, useState } from 'react'; +import produce from 'immer'; +import { + TableConnection, + tcFromSource, +} from '@hyperdx/common-utils/dist/core/metadata'; +import { + FilterState, + filtersToQuery, +} from '@hyperdx/common-utils/dist/filters'; +import { + BuilderChartConfigWithDateRange, + isMetricSource, + TSource, +} from '@hyperdx/common-utils/dist/types'; + +import { toQuotedClickHouseKeyExpression } from '@/components/DBSearchPageFilters/utils'; +import { + Facet, + useColumns, + useDateTimeColumns, + useGetKeyValues, + useJsonColumns, + useMapColumns, + useMetadataWithSettings, + useMultipleAllFields, +} from '@/hooks/useMetadata'; +import { useSource } from '@/source'; +import { mergePath } from '@/utils'; + +import { escapeFilterStateKeys, usePinnedFilters } from './searchFilters'; + +const INITIAL_LOAD_LIMIT = 20; + +/* The maximum number of values per filter to load when "Load More" is clicked */ +const LOAD_MORE_LOAD_LIMIT = 10000; + +/** + * Decide which table(s) key-value discovery reads from. + * + * The source stays authoritative whenever we have one, so its metadata + * materialized views keep serving discovery. `fallbacks` — the table + * connections a caller passes — are consulted in the two cases the source + * cannot answer: + * + * - No source id is provided + * - A metric source, whose `from.tableName` is empty because the real tables + * are per metric type; the caller resolves one per selected series + * + * More than one connection only arises for a metric source charting several + * metric types at once. Field discovery intersects them, since a sidebar filter + * applies to every series and offering a field only one table has would break + * the others. Value lookups use the first instead: an intersected field exists + * in all of them, so any one table can answer. + */ +export function resolveTableConnections( + source: TSource | undefined, + fallbacks: TableConnection[] | undefined, +): TableConnection[] { + const usable = (fallbacks ?? []).filter( + tc => !!tc.databaseName && !!tc.tableName && !!tc.connectionId, + ); + if (!source) { + return usable.length > 0 ? usable : [tcFromSource(undefined)]; + } + if (isMetricSource(source) && usable.length > 0) { + return usable; + } + return [tcFromSource(source)]; +} + +/** Single-table form of {@link resolveTableConnections}. */ +export function resolveTableConnection( + source: TSource | undefined, + fallback: TableConnection | undefined, +): TableConnection { + return resolveTableConnections(source, fallback ? [fallback] : [])[0]; +} + +function useFacets({ + chartConfig, + sourceId, + tableConnections: tableConnectionFallbacks, + mode, + dateRange, + filterState, + showMoreFields, + enabled, + disableValues, +}: { + chartConfig: BuilderChartConfigWithDateRange; + sourceId: string | null; + /** + * Tables where keys and values are discovered. Used when sourceId is not + * provided or references a metrics source. Memoize it — it keys the queries. + */ + tableConnections?: TableConnection[]; + mode: 'all' | 'exact'; + dateRange: [Date, Date]; + filterState?: FilterState; + showMoreFields?: boolean; + enabled?: boolean; + disableValues?: boolean; +}) { + const { data: source } = useSource({ + id: sourceId, + }); + const tableConnections = useMemo( + () => resolveTableConnections(source, tableConnectionFallbacks), + [source, tableConnectionFallbacks], + ); + const tableConnection = tableConnections[0]; + const { data: columns, isLoading: isColumnsLoading } = + useColumns(tableConnection); + const dateTimeColumns = useDateTimeColumns(columns); + const knownColumns = useMemo( + () => (columns ? new Set(columns.map(c => c.name)) : new Set()), + [columns], + ); + const { data: jsonColumns } = useJsonColumns(tableConnection); + const { data: mapColumns } = useMapColumns(tableConnection); + + const { + data: allFields, + error: allFieldsError, + isLoading: isAllFieldsLoading, + } = useMultipleAllFields(tableConnections, { + dateRange, + timestampValueExpression: source?.timestampValueExpression, + enabled, + intersect: tableConnections.length > 1, + }); + + const { isFieldPinned, isSharedFieldPinned } = usePinnedFilters( + sourceId ?? null, + ); + + const keysToFetch = useMemo(() => { + if (!allFields) { + return []; + } + + const strings = allFields + .sort((a, b) => { + // First show low cardinality fields + const isLowCardinality = (type: string) => + type.includes('LowCardinality'); + return ( + (isLowCardinality(b.type) ? 1 : 0) - + (isLowCardinality(a.type) ? 1 : 0) + ); + }) + .filter( + field => field.jsType && ['string'].includes(field.jsType), + // todo: add number type with sliders :D + ) + .map(({ path, type }) => { + return { + type, + path: mergePath(path, jsonColumns ?? [], mapColumns ?? []), + isMapSubField: path.length > 1, + }; + }) + .filter( + field => + showMoreFields || + field.type.includes('LowCardinality') || // query only low cardinality fields by default + field.isMapSubField || // always include Map/JSON sub-fields (e.g. LogAttributes, ResourceAttributes keys) + (filterState && Object.keys(filterState).includes(field.path)) || // keep selected fields + isFieldPinned(field.path) || // keep personally pinned fields + isSharedFieldPinned(field.path), // keep team-shared fields + ) + .map(({ path }) => path) + .filter( + path => + !['body', 'timestamp', '_hdx_body'].includes(path.toLowerCase()), + ); + return strings; + }, [ + allFields, + jsonColumns, + mapColumns, + filterState, + showMoreFields, + isFieldPinned, + isSharedFieldPinned, + ]); + + const { escapedKeysToFetch, sqlKeyToUiKey } = useMemo(() => { + // Don't fetch any keys until the column list is loaded, + // since we need the real column names to escape correctly. + if (isColumnsLoading) { + return { escapedKeysToFetch: [], sqlKeyToUiKey: new Map() }; + } + + const sqlKeyToUiKey = new Map(); + const escapedKeysToFetch = keysToFetch.map(key => { + const sqlKey = toQuotedClickHouseKeyExpression(key, knownColumns); + sqlKeyToUiKey.set(sqlKey, key); + return sqlKey; + }); + return { escapedKeysToFetch, sqlKeyToUiKey }; + }, [isColumnsLoading, keysToFetch, knownColumns]); + + const facetsChartConfig = useMemo( + () => + mode === 'all' + ? { ...chartConfig, dateRange, where: '', filters: [] } + : { ...chartConfig, dateRange }, + [chartConfig, dateRange, mode], + ); + + const { data: rawFacets, ...rest } = useGetKeyValues( + { + chartConfig: facetsChartConfig, + limit: INITIAL_LOAD_LIMIT, + keys: escapedKeysToFetch, + mode, + }, + { enabled: enabled && !disableValues }, + ); + + // Map the (escaped) result keys back to the original UI keys. + const facets = useMemo( + () => + rawFacets?.map(f => ({ + ...f, + key: sqlKeyToUiKey.get(f.key) ?? f.key, + })), + [rawFacets, sqlKeyToUiKey], + ); + + const metadata = useMetadataWithSettings(); + const loadMoreFacetsForKey = useCallback( + async (key: string): Promise => { + try { + const sqlKey = toQuotedClickHouseKeyExpression(key, knownColumns); + if (mode === 'exact') { + const strippedFilterState: FilterState = { ...filterState }; + delete strippedFilterState[key]; + if (sqlKey !== key) delete strippedFilterState[sqlKey]; + const newKeyVals = await metadata.getKeyValuesWithMVs({ + chartConfig: { + ...chartConfig, + dateRange, + filters: filtersToQuery( + escapeFilterStateKeys(strippedFilterState, knownColumns), + { dateTimeColumns }, + ), + }, + keys: [sqlKey], + limit: LOAD_MORE_LOAD_LIMIT, + disableRowLimit: true, + source, + }); + return { + key, + value: newKeyVals[0].value?.map(val => val.toString()) ?? [], + }; + } + + if ( + !tableConnection.databaseName || + !tableConnection.tableName || + !tableConnection.connectionId + ) { + throw new Error( + 'loadMoreFacetsForKey: a source or table connection must be defined', + ); + } + const newKeyVals = await metadata.getAllKeyValues({ + databaseName: tableConnection.databaseName, + tableName: tableConnection.tableName, + connectionId: tableConnection.connectionId, + metadataMVs: tableConnection.metadataMVs, + keyExpressions: [sqlKey], + maxValuesPerKey: LOAD_MORE_LOAD_LIMIT, + dateRange, + timestampValueExpression: + source?.timestampValueExpression ?? + chartConfig.timestampValueExpression ?? + '', + }); + return { + key, + value: + newKeyVals.length > 0 + ? (newKeyVals[0].value?.map(val => val.toString()) ?? []) + : [], + }; + } catch (error) { + console.error('failed to fetch more keys', error); + } + return undefined; + }, + [ + mode, + tableConnection, + metadata, + chartConfig, + dateRange, + knownColumns, + source, + filterState, + dateTimeColumns, + ], + ); + + return { + ...rest, + error: allFieldsError ?? rest.error, + data: { keys: allFields, keyValues: facets }, + isLoading: isAllFieldsLoading || rest.isLoading, + loadMoreFacetsForKey, + }; +} + +export function useFetchFacets({ + chartConfig, + sourceId, + tableConnections, + dateRange, + mode, + filterState, + showMoreFields, + disableValues, +}: { + chartConfig: BuilderChartConfigWithDateRange; + sourceId: string | null; + /** + * Tables where keys and values are discovered. Used when sourceId is not + * provided or references a metrics source. Memoize it — it keys the queries. + */ + tableConnections?: TableConnection[]; + dateRange: [Date, Date]; + mode: 'all' | 'exact'; + filterState?: FilterState; + showMoreFields?: boolean; + disableValues?: boolean; +}) { + const facetsQuery = useFacets({ + chartConfig, + sourceId, + tableConnections, + mode, + dateRange, + filterState, + showMoreFields, + enabled: true, + disableValues, + }); + + // Serialized, because callers pass this array inline. Keying the reset effect + // below on its identity would fire it every render. + const tableConnectionsKey = JSON.stringify(tableConnections ?? []); + + const [extraFacets, setExtraFacets] = useState(null); + const facets = useMemo(() => { + const base = facetsQuery.data.keyValues; + const hasExtras = !!extraFacets && extraFacets.length > 0; + + if (base === undefined && !hasExtras) return undefined; + if (!hasExtras) return base; + if (base === undefined) return extraFacets ?? undefined; + + const seenFacets = new Set(); + const output: Facet[] = []; + for (const facet of base) { + seenFacets.add(facet.key); + const extraFacet = extraFacets!.find(ef => ef.key === facet.key); + if (extraFacet) { + // Union values: primary is query-scoped and must not be overridden; + // extras from "Load More" only append (see PR #2329, commit 8938b05ef). + const seenValues = new Set(facet.value); + const merged = [...facet.value]; + for (const v of extraFacet.value) { + if (!seenValues.has(v)) { + seenValues.add(v); + merged.push(v); + } + } + output.push({ key: facet.key, value: merged }); + } else { + output.push(facet); + } + } + for (const extraFacet of extraFacets) { + if (!seenFacets.has(extraFacet.key)) { + output.push(extraFacet); + } + } + return output; + }, [facetsQuery.data, extraFacets]); + + const [extraFacetKeys, setExtraFacetKeys] = useState>( + () => new Set(), + ); + const [loadMoreLoadingKeys, setLoadMoreLoadingKeys] = useState>( + () => new Set(), + ); + const areExtraFacetsLoading = loadMoreLoadingKeys.size > 0; + const loadMoreFacetsForKey = useCallback( + async (key: string) => { + const strategy = facetsQuery.loadMoreFacetsForKey; + setLoadMoreLoadingKeys(prev => + produce(prev, draft => { + draft.add(key); + }), + ); + const newFacet = await strategy(key); + if (newFacet) { + setExtraFacets(prev => [...(prev ?? []), newFacet]); + setExtraFacetKeys(prev => + produce(prev, draft => { + draft.add(key); + }), + ); + } + setLoadMoreLoadingKeys(prev => + produce(prev, draft => { + draft.delete(key); + }), + ); + }, + [facetsQuery.loadMoreFacetsForKey], + ); + + // Clear extras when the query scope that produced them changes; otherwise + // they'd persist against a query they were never fetched for. + useEffect(() => { + // eslint-disable-next-line react-hooks/set-state-in-effect + setExtraFacets(null); + setExtraFacetKeys(new Set()); + }, [ + sourceId, + tableConnectionsKey, + dateRange, + mode, + filterState, + chartConfig.where, + chartConfig.whereLanguage, + ]); + + return { + ...facetsQuery, + data: { keys: facetsQuery.data.keys, keyValues: facets }, + loadMoreFacetsForKey, + areExtraFacetsLoading, + loadMoreLoadingKeys, + extraFacetKeys, + }; +} diff --git a/packages/app/src/components/Explore/owned/searchFilters.tsx b/packages/app/src/components/Explore/owned/searchFilters.tsx new file mode 100644 index 0000000000..d6001336f9 --- /dev/null +++ b/packages/app/src/components/Explore/owned/searchFilters.tsx @@ -0,0 +1,722 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import produce from 'immer'; +import { + type FilterState, + filtersToQuery, + parseQuery, +} from '@hyperdx/common-utils/dist/filters'; +import type { Filter } from '@hyperdx/common-utils/dist/types'; + +import { + cleanClickHouseExpression, + toQuotedClickHouseKeyExpression, +} from '@/components/DBSearchPageFilters/utils'; +import { usePinnedFiltersApi, useUpdatePinnedFilters } from '@/pinnedFilters'; +import { useLocalStorage } from '@/utils'; + +export const IS_ROOT_SPAN_COLUMN_NAME = 'isRootSpan'; + +// Filter keys live in two forms. +// 1. In-memory `FilterState`: use the clean/unquoted forms of each key. This is what +// the UI renders, what test ids are built from, and what every key comparison expects. +// 2. The persisted `Filter[]`: Uses the quoted/bracketed ClickHouse form so it emits +// valid SQL verbatim. Persisted in URL params, local storage, and MongoDB for saved searches. + +// Convert the clean FilterState keys to valid SQL (quoted/bracket) keys. +export const escapeFilterStateKeys = ( + filters: FilterState, + knownColumns: Set, +): FilterState => { + const escaped: FilterState = {}; + for (const [key, value] of Object.entries(filters)) { + escaped[toQuotedClickHouseKeyExpression(key, knownColumns)] = value; + } + return escaped; +}; + +// Convert valid SQL/persisted keys to clean FilterState keys. +const unescapeFilterStateKeys = (filters: FilterState): FilterState => { + const cleaned: FilterState = {}; + for (const [key, value] of Object.entries(filters)) { + cleaned[cleanClickHouseExpression(key)] = value; + } + return cleaned; +}; + +export const areFiltersEqual = (a: FilterState, b: FilterState) => { + const aKeys = Object.keys(a); + const bKeys = Object.keys(b); + + if (aKeys.length !== bKeys.length) { + return false; + } + + for (const key of aKeys) { + if (!b[key]) return false; + + // Check included values + if (a[key].included.size !== b[key].included.size) return false; + for (const value of a[key].included) { + if (!b[key].included.has(value)) return false; + } + + // Check excluded values + if (a[key].excluded.size !== b[key].excluded.size) return false; + for (const value of a[key].excluded) { + if (!b[key].excluded.has(value)) return false; + } + + // Check range + if (a[key].range?.min !== b[key].range?.min) return false; + if (a[key].range?.max !== b[key].range?.max) return false; + if ((a[key].range?.minOp ?? '>=') !== (b[key].range?.minOp ?? '>=')) { + return false; + } + if ((a[key].range?.maxOp ?? '<=') !== (b[key].range?.maxOp ?? '<=')) { + return false; + } + } + + return true; +}; + +// parseQuery (and its helpers) now live in common-utils as the inverse of +// filtersToQuery; re-export so existing `@/searchFilters` importers keep working. +export { parseQuery }; + +export const useSearchPageFilterState = ({ + searchQuery = [], + onFilterChange, + dateTimeColumns, + knownColumns, +}: { + searchQuery?: Filter[]; + onFilterChange: (filters: Filter[]) => void; + dateTimeColumns?: ReadonlyMap; + /** + * Top-level column names on the table, used to quote + * column names that contain special characters + * (eg. service-name --> `service-name`). + **/ + knownColumns: Set; +}) => { + // Access knownColumns through a ref so the returned mutators (which depend on + // updateFilterQuery) keep stable identities across knownColumns reference + // changes. The known columns are only used by the mutators, which are called + // on user input, not render, so avoid re-render by using a stable ref. + const knownColumnsRef = useRef>(knownColumns); + useEffect(() => { + knownColumnsRef.current = knownColumns; + }, [knownColumns]); + + // Persisted filters carry canonical (escaped) keys; convert back to the clean + // keys the sidebar/comparisons use as they enter in-memory FilterState. + const parsedQuery = useMemo(() => { + try { + return { + filters: unescapeFilterStateKeys(parseQuery(searchQuery).filters), + }; + } catch (e) { + console.error(e); + return { filters: {} }; + } + }, [searchQuery]); + + const [filters, setFilters] = useState({}); + + useEffect(() => { + if (!areFiltersEqual(filters, parsedQuery.filters)) { + setFilters(parsedQuery.filters); + } + // only react to changes in parsed query + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [parsedQuery.filters]); + + const updateFilterQuery = useCallback( + (newFilters: FilterState) => { + // Escape filter keys here prior to saving so the URL / saved + // search holds valid-SQL keys while FilterState stays clean. + const escapedFilters = escapeFilterStateKeys( + newFilters, + knownColumnsRef.current, + ); + onFilterChange(filtersToQuery(escapedFilters, { dateTimeColumns })); + }, + [onFilterChange, dateTimeColumns], + ); + + const setFilterValue = useCallback( + ( + property: string, + value: string | boolean, + action?: 'only' | 'exclude' | 'include', + ) => { + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + if (!draft[property]) { + draft[property] = { included: new Set(), excluded: new Set() }; + } + + if (action === 'only') { + draft[property] = { + included: new Set([value]), + excluded: new Set(), + }; + return; + } + + if (action === 'exclude') { + // Remove from included if it was there + draft[property].included.delete(value); + // Toggle in excluded + if (draft[property].excluded.has(value)) { + draft[property].excluded.delete(value); + } else { + draft[property].excluded.add(value); + } + return; + } + + // Regular toggle (include) + draft[property].excluded.delete(value); + if (draft[property].included.has(value)) { + draft[property].included.delete(value); + } else { + draft[property].included.add(value); + } + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + // Apply several "only" filters atomically: each listed property is set to + // exactly its given value in a single state update, emitting onFilterChange + // (and therefore a re-query) once rather than once per property. Use this + // instead of looping setFilterValue(..., 'only') when scoping to multiple + // columns at once (e.g. focusing a multi-group chart series). + const setOnlyFilters = useCallback( + (entries: { property: string; value: string | boolean }[]) => { + if (entries.length === 0) { + return; + } + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + for (const { property, value } of entries) { + draft[property] = { + included: new Set([value]), + excluded: new Set(), + }; + } + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + // Set a property's included values to exactly `values` (an OR-set) in one + // update. Unlike setFilterValue(..., 'only') which takes a single value, this + // scopes a column to multiple values at once (e.g. a "severity: error" pill + // that maps to both ERROR and FATAL). Passing an empty array clears the + // filter entirely. + const setIncludedValues = useCallback( + (property: string, values: (string | boolean)[]) => { + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + if (values.length === 0) { + delete draft[property]; + } else { + draft[property] = { + included: new Set(values), + excluded: new Set(), + }; + } + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + const mergeFilterValues = useCallback( + (incoming: FilterState) => { + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + for (const [field, sel] of Object.entries(incoming)) { + if (!draft[field]) { + draft[field] = { included: new Set(), excluded: new Set() }; + } + for (const value of sel.included) { + draft[field].excluded.delete(value); + draft[field].included.add(value); + } + for (const value of sel.excluded) { + draft[field].included.delete(value); + draft[field].excluded.add(value); + } + if (sel.range != null) { + draft[field].range = sel.range; + } + } + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + const setFilterRange = useCallback( + (property: string, range: { min: number; max: number }) => { + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + if (!draft[property]) { + draft[property] = { included: new Set(), excluded: new Set() }; + } + draft[property].range = range; + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + const clearFilter = useCallback( + (property: string) => { + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + delete draft[property]; + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + // Swap one value for another within the same set (included or excluded), + // preserving polarity, in a single state update. Two setFilterValue calls + // would emit onFilterChange twice (one query run each); this emits once. + const replaceFilterValue = useCallback( + ( + property: string, + oldValue: string | boolean, + newValue: string | boolean, + action: 'include' | 'exclude', + ) => { + setFilters(prevFilters => { + const newFilters = produce(prevFilters, draft => { + if (!draft[property]) { + draft[property] = { included: new Set(), excluded: new Set() }; + } + const set = + action === 'exclude' + ? draft[property].excluded + : draft[property].included; + set.delete(oldValue); + set.add(newValue); + }); + updateFilterQuery(newFilters); + return newFilters; + }); + }, + [updateFilterQuery], + ); + + const clearAllFilters = useCallback(() => { + setFilters(() => ({})); + updateFilterQuery({}); + }, [updateFilterQuery]); + + // Keep only filters whose root column appears in `allowedColumnNames`. + // Returns the keys of the filters that were dropped so callers can surface + // a notice when filter state was thrown away (e.g. on source change). + const retainFiltersByColumns = useCallback( + (allowedColumnNames: Set): string[] => { + const dropped: string[] = []; + const kept: FilterState = {}; + for (const [key, value] of Object.entries(filters)) { + // Filter keys are dot-normalized, top-level columns are stored as-is, + // nested JSON/Map keys as `Root.nested.path`. An exact match handles + // the rare case of a column with dots in its name. + const dotIdx = key.indexOf('.'); + const rootColumn = dotIdx > 0 ? key.slice(0, dotIdx) : key; + if (allowedColumnNames.has(key) || allowedColumnNames.has(rootColumn)) { + kept[key] = value; + } else { + dropped.push(key); + } + } + if (dropped.length > 0) { + setFilters(kept); + updateFilterQuery(kept); + } + return dropped; + }, + [filters, updateFilterQuery], + ); + + return { + filters, + setFilters, + setFilterValue, + setOnlyFilters, + setIncludedValues, + mergeFilterValues, + replaceFilterValue, + setFilterRange, + clearFilter, + clearAllFilters, + retainFiltersByColumns, + }; +}; + +type PinnedFilters = { + [key: string]: (string | boolean)[]; +}; + +export type FilterStateHook = ReturnType; + +/** + * Merge team-level and personal pinned filter data into a single view. + * Fields and filter values are unioned (deduplicated). + */ +export function mergePinnedData( + team: { fields: string[]; filters: PinnedFilters } | null, + personal: { fields: string[]; filters: PinnedFilters } | null, +): { fields: string[]; filters: PinnedFilters } { + const teamFields = team?.fields ?? []; + const personalFields = personal?.fields ?? []; + const fields = [...new Set([...teamFields, ...personalFields])]; + + const teamFilters = team?.filters ?? {}; + const personalFilters = personal?.filters ?? {}; + const allKeys = new Set([ + ...Object.keys(teamFilters), + ...Object.keys(personalFilters), + ]); + + const filters: PinnedFilters = {}; + for (const key of allKeys) { + const teamVals = teamFilters[key] ?? []; + const personalVals = personalFilters[key] ?? []; + const merged = [...teamVals]; + for (const v of personalVals) { + if (!merged.some(existing => existing === v)) { + merged.push(v); + } + } + filters[key] = merged; + } + + return { fields, filters }; +} + +/** + * Toggle a value in a PinnedFilters map. Returns a new map with the value + * added or removed under the given property key. + */ +function toggleValueInFilters( + filters: PinnedFilters, + property: string, + value: string | boolean, +): PinnedFilters { + const updated = { ...filters }; + if (!updated[property]) { + updated[property] = []; + } + const idx = updated[property].findIndex((v: string | boolean) => v === value); + if (idx >= 0) { + updated[property] = updated[property].filter( + (_: string | boolean, i: number) => i !== idx, + ); + if (updated[property].length === 0) { + delete updated[property]; + } + } else { + updated[property] = [...updated[property], value]; + } + return updated; +} + +/** + * Hook for personal pinned filters stored in localStorage. + * This is the original storage mechanism, per-user, per-browser. + */ +function usePersonalPinnedFilters(sourceId: string | null) { + const [_pinnedFilters, _setPinnedFilters] = useLocalStorage<{ + [sourceId: string]: PinnedFilters; + }>('hdx-pinned-search-filters', {}); + + const [_pinnedFields, _setPinnedFields] = useLocalStorage<{ + [sourceId: string]: string[]; + }>('hdx-pinned-fields', {}); + + const filters = useMemo( + () => + !sourceId || !_pinnedFilters[sourceId] ? {} : _pinnedFilters[sourceId], + [_pinnedFilters, sourceId], + ); + + const fields = useMemo( + () => + !sourceId || !_pinnedFields[sourceId] ? [] : _pinnedFields[sourceId], + [_pinnedFields, sourceId], + ); + + const setFilters = useCallback( + (val: PinnedFilters | ((pf: PinnedFilters) => PinnedFilters)) => { + if (!sourceId) return; + _setPinnedFilters(prev => { + const updated = { ...prev }; + updated[sourceId] = + val instanceof Function ? val(prev[sourceId] ?? {}) : val; + return updated; + }); + }, + [sourceId, _setPinnedFilters], + ); + + const setFields = useCallback( + (val: string[] | ((pf: string[]) => string[])) => { + if (!sourceId) return; + _setPinnedFields(prev => { + const updated = { ...prev }; + updated[sourceId] = + val instanceof Function ? val(prev[sourceId] ?? []) : val; + return updated; + }); + }, + [sourceId, _setPinnedFields], + ); + + return { filters, fields, setFilters, setFields }; +} + +export function usePinnedFilters(sourceId: string | null) { + // Personal pins: localStorage (per-user, per-browser) + const personal = usePersonalPinnedFilters(sourceId); + + // Team/shared pins: MongoDB via API (shared across team) + const { data: teamApiData } = usePinnedFiltersApi(sourceId); + const updateTeamMutation = useUpdatePinnedFilters(); + + // Optimistic state keyed by sourceId so it is automatically ignored when + // the source changes, no useEffect needed to clear stale state. + const [optimisticTeam, setOptimisticTeam] = useState<{ + sourceId: string; + fields: string[]; + filters: PinnedFilters; + } | null>(null); + + const effectiveTeam = useMemo( + () => + optimisticTeam?.sourceId === sourceId + ? { fields: optimisticTeam.fields, filters: optimisticTeam.filters } + : { + fields: teamApiData?.team?.fields ?? [], + filters: teamApiData?.team?.filters ?? {}, + }, + [optimisticTeam, sourceId, teamApiData], + ); + + // Merge team + personal into a unified view for read operations + const { fields: pinnedFields, filters: pinnedFilters } = useMemo( + () => + mergePinnedData(effectiveTeam, { + fields: personal.fields, + filters: personal.filters, + }), + [effectiveTeam, personal.fields, personal.filters], + ); + + // Debounce for team API writes, cancelled on unmount to prevent stale writes. + const pendingTeamUpdateRef = useRef | null>( + null, + ); + + useEffect(() => { + return () => { + if (pendingTeamUpdateRef.current) { + clearTimeout(pendingTeamUpdateRef.current); + pendingTeamUpdateRef.current = null; + } + }; + }, []); + + const flushTeamUpdate = useCallback( + (newFields: string[], newFilters: PinnedFilters) => { + if (!sourceId) return; + + setOptimisticTeam({ sourceId, fields: newFields, filters: newFilters }); + + if (pendingTeamUpdateRef.current) { + clearTimeout(pendingTeamUpdateRef.current); + } + pendingTeamUpdateRef.current = setTimeout(() => { + updateTeamMutation.mutate( + { + source: sourceId, + fields: newFields, + filters: newFilters, + }, + { + onSettled: () => setOptimisticTeam(null), + }, + ); + pendingTeamUpdateRef.current = null; + }, 300); + }, + [sourceId, updateTeamMutation], + ); + + // Personal pin: value-level pin (localStorage, instant) + const toggleFilterPin = useCallback( + (property: string, value: string | boolean) => { + personal.setFilters(prev => toggleValueInFilters(prev, property, value)); + // When pinning a value, also pin the field if not already pinned + personal.setFields(prev => + prev.includes(property) ? prev : [...prev, property], + ); + }, + [personal], + ); + + // Personal pin: field-level pin (localStorage, instant) + const toggleFieldPin = useCallback( + (field: string) => { + personal.setFields(prev => { + const idx = prev.indexOf(field); + return idx >= 0 ? prev.filter((_, i) => i !== idx) : [...prev, field]; + }); + }, + [personal], + ); + + // Personal-only checks (not merged), so team pins don't show as personal + const isFilterPinned = useCallback( + (property: string, value: string | boolean): boolean => { + return ( + personal.filters[property] != null && + personal.filters[property].some((v: string | boolean) => v === value) + ); + }, + [personal.filters], + ); + + const isFieldPinned = useCallback( + (field: string): boolean => { + return personal.fields.includes(field); + }, + [personal.fields], + ); + + // Merged view for getPinnedFields (used for sorting and always-fetch logic) + const getPinnedFields = useCallback((): string[] => { + return pinnedFields; + }, [pinnedFields]); + + // Team pin: field-level (MongoDB via API, debounced) + const toggleSharedFieldPin = useCallback( + (field: string) => { + const currentFields = [...effectiveTeam.fields]; + const currentFilters = { ...effectiveTeam.filters }; + const fieldIndex = currentFields.indexOf(field); + + if (fieldIndex >= 0) { + // Removing field from shared, also clean up its filter values + const newFields = currentFields.filter((_, i) => i !== fieldIndex); + delete currentFilters[field]; + flushTeamUpdate(newFields, currentFilters); + } else { + // Adding field to shared + flushTeamUpdate([...currentFields, field], currentFilters); + } + }, + [effectiveTeam, flushTeamUpdate], + ); + + const isSharedFieldPinned = useCallback( + (field: string): boolean => { + // A field is shared if it's in the fields list OR has shared filter values + return ( + effectiveTeam.fields.includes(field) || + (effectiveTeam.filters[field] != null && + effectiveTeam.filters[field].length > 0) + ); + }, + [effectiveTeam], + ); + + // Team pin: value-level (MongoDB via API, debounced) + const toggleSharedFilterPin = useCallback( + (property: string, value: string | boolean) => { + const newFilters = toggleValueInFilters( + effectiveTeam.filters, + property, + value, + ); + // When sharing a value, also add the field to shared fields + const newFields = effectiveTeam.fields.includes(property) + ? effectiveTeam.fields + : [...effectiveTeam.fields, property]; + + flushTeamUpdate(newFields, newFilters); + }, + [effectiveTeam, flushTeamUpdate], + ); + + const isSharedFilterPinned = useCallback( + (property: string, value: string | boolean): boolean => { + const vals = effectiveTeam.filters[property]; + return vals != null && vals.some(v => v === value); + }, + [effectiveTeam], + ); + + const resetPersonalPins = useCallback(() => { + personal.setFields(() => []); + personal.setFilters(() => ({})); + }, [personal]); + + const resetSharedFilters = useCallback(() => { + flushTeamUpdate([], {}); + }, [flushTeamUpdate]); + + const hasPersonalPins = useMemo( + () => + personal.fields.length > 0 || Object.keys(personal.filters).length > 0, + [personal.fields, personal.filters], + ); + + const hasSharedPins = useMemo( + () => + effectiveTeam.fields.length > 0 || + Object.keys(effectiveTeam.filters).length > 0, + [effectiveTeam], + ); + + return { + toggleFilterPin, + toggleFieldPin, + isFilterPinned, + isFieldPinned, + getPinnedFields, + pinnedFilters, + toggleSharedFieldPin, + isSharedFieldPinned, + toggleSharedFilterPin, + isSharedFilterPinned, + resetPersonalPins, + resetSharedFilters, + hasPersonalPins, + hasSharedPins, + }; +} diff --git a/packages/app/src/components/Explore/owned/useResizable.tsx b/packages/app/src/components/Explore/owned/useResizable.tsx new file mode 100644 index 0000000000..b0e7a9aac3 --- /dev/null +++ b/packages/app/src/components/Explore/owned/useResizable.tsx @@ -0,0 +1,109 @@ +// Explore-owned copy. The shared module stays as it is on main, so this prototype does not conflict with it. +import { + MouseEvent as ReactMouseEvent, + useCallback, + useEffect, + useRef, + useState, +} from 'react'; + +const MIN_PANEL_PERCENT = 10; +const MAX_PANEL_OFFSET_PX = 25; + +type ResizeDirection = 'left' | 'right' | 'top' | 'bottom'; + +/** + * All `startResize` reads off the event. A React mouse event satisfies it, so + * `onMouseDown={startResize}` still type-checks, and callers that synthesise + * one do not have to fake a whole SyntheticEvent. + */ +type ResizeStartEvent = Pick< + ReactMouseEvent, + 'preventDefault' | 'clientX' | 'clientY' +>; + +function useResizable( + initialSizePercent: number, + direction: ResizeDirection = 'right', + /** + * Called once when the drag finishes, not on every frame — a caller + * persisting the size would otherwise write on every mousemove. + */ + onResizeEnd?: (sizePercent: number) => void, +) { + const [sizePercentage, setSizePercentage] = useState(initialSizePercent); + + // Track drag start + const startPosRef = useRef(0); + const startSizeRef = useRef(0); + // Mirrors the state so `endResize` can report the final size without + // depending on it, which would re-register the listeners mid-drag. + const latestSizeRef = useRef(initialSizePercent); + + const setSize = useCallback((next: number) => { + latestSizeRef.current = next; + setSizePercentage(next); + }, []); + + const isVertical = direction === 'top' || direction === 'bottom'; + const axis: 'clientX' | 'clientY' = isVertical ? 'clientY' : 'clientX'; + + // For right/bottom, positive drag increases size; for left/top, it decreases. + const directionMultiplier = + direction === 'right' || direction === 'top' ? -1 : 1; + + const handleResize = useCallback( + (e: globalThis.MouseEvent) => { + const containerSize = isVertical ? window.innerHeight : window.innerWidth; + const delta = e[axis] - startPosRef.current; + const deltaPercent = (delta / containerSize) * 100; + + const offsetWidth = isVertical + ? window.innerHeight + : document.body.offsetWidth; + // Clamp to min and max + const maxPercent = + ((offsetWidth - MAX_PANEL_OFFSET_PX) / containerSize) * 100; + + const minPercent = MIN_PANEL_PERCENT; + + const newSize = startSizeRef.current + deltaPercent * directionMultiplier; + + setSize(Math.min(Math.max(minPercent, newSize), maxPercent)); + }, + [isVertical, axis, directionMultiplier, setSize], + ); + + const endResize = useCallback(() => { + document.removeEventListener('mousemove', handleResize); + // eslint-disable-next-line react-hooks/immutability + document.removeEventListener('mouseup', endResize); + onResizeEnd?.(latestSizeRef.current); + }, [handleResize, onResizeEnd]); + + const startResize = useCallback( + (e: ResizeStartEvent) => { + e.preventDefault(); + startPosRef.current = e[axis]; + startSizeRef.current = sizePercentage; + document.addEventListener('mousemove', handleResize); + document.addEventListener('mouseup', endResize); + }, + [axis, sizePercentage, handleResize, endResize], + ); + + useEffect(() => { + return () => { + document.removeEventListener('mousemove', handleResize); + document.removeEventListener('mouseup', endResize); + }; + }, [handleResize, endResize]); + + return { + size: sizePercentage, + setSize, + startResize, + }; +} + +export default useResizable; diff --git a/packages/app/src/components/Explore/promoteWhereToFilters.ts b/packages/app/src/components/Explore/promoteWhereToFilters.ts new file mode 100644 index 0000000000..b33a8d955c --- /dev/null +++ b/packages/app/src/components/Explore/promoteWhereToFilters.ts @@ -0,0 +1,317 @@ +import type { + FilterRange, + FilterState, +} from '@hyperdx/common-utils/dist/filters'; + +import { parseDurationBound } from './durationBound'; +import type { QueryLanguage } from './queryModeSafety'; + +export type WherePromotion = { + filters: FilterState; + remainder: string; +}; + +// Equality and one-sided numeric comparisons. Wildcards and ILIKE stay typed +// because FilterState serializes to SQL IN / BETWEEN / >/=/<=. +const LUCENE_ATOM = /^(-)?([\w.]+):(?:"((?:\\"|[^"])*)"|([\w.-]+))$/; +const LUCENE_CMP = + /^([\w.]+):(>=|<=|>|<)(\d+(?:\.\d+)?(?:ns|us|µs|ms|s|m|h)?)$/i; +const SQL_ATOM = + /^(NOT\s+)?(`?[\w.]+`?)\s*(=|!=|<>)\s*(?:'((?:\\'|[^'])*)'|"((?:\\"|[^"])*)"|(\d+(?:\.\d+)?))$/i; +const SQL_CMP = + /^(`?[\w.]+`?)\s*(>=|<=|>|<)\s*([+-]?\d+(?:\.\d+)?(?:e[+-]?\d+)?)$/i; + +function emptySelection(): FilterState[string] { + return { included: new Set(), excluded: new Set() }; +} + +function addValue( + filters: FilterState, + field: string, + value: string, + excluded: boolean, +) { + if (!filters[field]) { + filters[field] = emptySelection(); + } + if (excluded) { + filters[field].included.delete(value); + filters[field].excluded.add(value); + } else { + filters[field].excluded.delete(value); + filters[field].included.add(value); + } +} + +function splitTopLevel( + input: string, + separator: 'AND' | 'OR', + { allowEmptyLast = false }: { allowEmptyLast?: boolean } = {}, +): string[] | null { + const parts: string[] = []; + let current = ''; + let depth = 0; + let quote: "'" | '"' | null = null; + const upper = input.toUpperCase(); + const sep = ` ${separator} `; + let i = 0; + + while (i < input.length) { + const ch = input[i]; + if (quote) { + current += ch; + if (ch === '\\' && i + 1 < input.length) { + current += input[i + 1]; + i += 2; + continue; + } + if (ch === quote) { + quote = null; + } + i += 1; + continue; + } + if (ch === "'" || ch === '"') { + quote = ch; + current += ch; + i += 1; + continue; + } + if (ch === '(') { + depth += 1; + current += ch; + i += 1; + continue; + } + if (ch === ')') { + depth -= 1; + if (depth < 0) { + return null; + } + current += ch; + i += 1; + continue; + } + if (depth === 0 && upper.slice(i, i + sep.length) === sep) { + const part = current.trim(); + if (!part) { + return null; + } + parts.push(part); + current = ''; + i += sep.length; + continue; + } + current += ch; + i += 1; + } + + if (quote != null || depth !== 0) { + return null; + } + const last = current.trim(); + if (last) { + parts.push(last); + } else if (!allowEmptyLast) { + return null; + } + return parts; +} + +function unwrapParens(text: string): string { + let trimmed = text.trim(); + while (trimmed.startsWith('(') && trimmed.endsWith(')')) { + const inner = trimmed.slice(1, -1).trim(); + let depth = 0; + let wrapped = true; + for (const ch of inner) { + if (ch === '(') depth += 1; + if (ch === ')') depth -= 1; + if (depth < 0) { + wrapped = false; + break; + } + } + if (!wrapped || depth !== 0) { + break; + } + trimmed = inner; + } + return trimmed; +} + +type Atom = { field: string; value: string; excluded: boolean }; + +function parseLuceneAtom(text: string): Atom | null { + const match = unwrapParens(text).match(LUCENE_ATOM); + if (!match) { + return null; + } + return { + field: match[2], + value: match[3]?.replaceAll('\\"', '"') ?? match[4] ?? '', + excluded: Boolean(match[1]), + }; +} + +function parseSqlAtom(text: string): Atom | null { + const match = unwrapParens(text).match(SQL_ATOM); + if (!match) { + return null; + } + const value = (match[4] ?? match[5] ?? match[6] ?? '').replaceAll("\\'", "'"); + const excluded = Boolean(match[1]) || match[3] !== '='; + return { + field: match[2].replace(/`/g, ''), + value, + excluded, + }; +} + +function parseAtom(text: string, language: QueryLanguage): Atom | null { + return language === 'sql' ? parseSqlAtom(text) : parseLuceneAtom(text); +} + +function parseComparison( + text: string, + language: QueryLanguage, +): FilterState | null { + const raw = unwrapParens(text); + const match = raw.match(language === 'sql' ? SQL_CMP : LUCENE_CMP); + if (!match) { + return null; + } + const field = match[1].replace(/`/g, ''); + const op = match[2]; + if (op !== '>' && op !== '>=' && op !== '<' && op !== '<=') { + return null; + } + const bound = + language === 'sql' ? Number(match[3]) : parseDurationBound(match[3]); + if (bound == null || !Number.isFinite(bound)) { + return null; + } + const range: FilterRange = + op === '>' || op === '>=' + ? { min: bound, minOp: op } + : { max: bound, maxOp: op }; + return { + [field]: { included: new Set(), excluded: new Set(), range }, + }; +} + +function parsePromotablePart( + text: string, + language: QueryLanguage, +): FilterState | null { + const atom = parseAtom(text, language); + if (atom) { + const filters: FilterState = {}; + addValue(filters, atom.field, atom.value, atom.excluded); + return filters; + } + + const comparison = parseComparison(text, language); + if (comparison) { + return comparison; + } + + const orParts = splitTopLevel(unwrapParens(text), 'OR'); + if (orParts == null || orParts.length < 2) { + return null; + } + const atoms = orParts.map(part => parseAtom(part, language)); + if (atoms.some(a => a == null)) { + return null; + } + const parsed = atoms.filter((a): a is Atom => a != null); + const field = parsed[0].field; + if (parsed.some(a => a.field !== field || a.excluded)) { + // Cross-field OR (and excluded-OR) is not representable in FilterState. + return null; + } + const filters: FilterState = {}; + for (const a of parsed) { + addValue(filters, a.field, a.value, false); + } + return filters; +} + +function mergeStates(into: FilterState, extra: FilterState) { + for (const [field, sel] of Object.entries(extra)) { + for (const value of sel.included) { + addValue(into, field, String(value), false); + } + for (const value of sel.excluded) { + addValue(into, field, String(value), true); + } + if (sel.range != null) { + if (!into[field]) { + into[field] = emptySelection(); + } + into[field].range = sel.range; + } + } +} + +function hasClauses(filters: FilterState): boolean { + return Object.values(filters).some( + sel => sel.included.size > 0 || sel.excluded.size > 0 || sel.range != null, + ); +} + +/** + * Peel complete equality and numeric-comparison clauses out of a typed + * Lucene/SQL string and into FilterState. The last token is only committed + * when `commitTrailing` is true (Enter, blur, example chips) or the string + * ends in whitespace / a trailing AND, so typing `level:error` does not freeze + * into a pill before `level:errors`. + */ +export function promoteWhereToFilters( + where: string, + language: QueryLanguage, + { commitTrailing = false }: { commitTrailing?: boolean } = {}, +): WherePromotion { + if (!where) { + return { filters: {}, remainder: where }; + } + + const hasTrailingWhitespace = where !== where.trimEnd(); + const trimmedEnd = where.trimEnd(); + const hasTrailingAnd = /\sAND$/i.test(trimmedEnd); + const body = hasTrailingAnd + ? trimmedEnd.replace(/\sAND$/i, '').trimEnd() + : trimmedEnd; + const commitLast = commitTrailing || hasTrailingWhitespace || hasTrailingAnd; + + if (!body) { + return { filters: {}, remainder: where }; + } + + const parts = splitTopLevel(body, 'AND', { allowEmptyLast: true }); + if (parts == null) { + return { filters: {}, remainder: where }; + } + + const filters: FilterState = {}; + const remainderParts: string[] = []; + + parts.forEach((part, i) => { + const parsed = parsePromotablePart(part, language); + const isLast = i === parts.length - 1; + if (parsed != null && (!isLast || commitLast)) { + mergeStates(filters, parsed); + } else { + remainderParts.push(part); + } + }); + + if (!hasClauses(filters)) { + return { filters: {}, remainder: where }; + } + + return { + filters, + remainder: remainderParts.join(' AND '), + }; +} diff --git a/packages/app/src/components/Explore/queryEditorLanguage.ts b/packages/app/src/components/Explore/queryEditorLanguage.ts new file mode 100644 index 0000000000..f7bacb9b5b --- /dev/null +++ b/packages/app/src/components/Explore/queryEditorLanguage.ts @@ -0,0 +1,60 @@ +import { + autocompletion, + Completion, + CompletionSource, +} from '@codemirror/autocomplete'; +import { type Extension } from '@codemirror/state'; +import { EditorView } from '@uiw/react-codemirror'; + +import { KEYWORDS_FOR_WHERE_OR_ORDER_BY } from '@/components/SQLEditor/constants'; +import { createCodeMirrorSqlDialect } from '@/components/SQLEditor/utils'; + +import type { QueryLanguage } from './queryModeSafety'; + +export const queryEditorBaseTheme = EditorView.theme({ + '&': { backgroundColor: 'transparent', fontSize: '13px' }, + '&.cm-focused': { outline: 'none' }, + '.cm-content': { + fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', + }, + '.cm-activeLine, .cm-activeLineGutter': { backgroundColor: 'transparent' }, + '.cm-gutters': { backgroundColor: 'transparent', border: 'none' }, + '.cm-lineNumbers .cm-gutterElement': { padding: '0 8px' }, +}); + +function luceneCompletions(fields: string[]): CompletionSource { + const fieldOpts: Completion[] = fields.map(label => ({ + label, + type: 'variable', + apply: `${label}:`, + })); + const keywordOpts: Completion[] = ['AND', 'OR', 'NOT', 'TO'].map(label => ({ + label, + type: 'keyword', + })); + const all = [...fieldOpts, ...keywordOpts]; + + return context => { + const word = context.matchBefore(/[\w.$-]*/); + if (!word) return null; + if (word.from === word.to && !context.explicit) return null; + return { from: word.from, options: all, validFor: /^[\w.$-]*$/ }; + }; +} + +export function languageExtensions( + language: QueryLanguage, + fields: string[], +): Extension[] { + if (language === 'sql') { + return createCodeMirrorSqlDialect({ + identifiers: fields, + keywords: KEYWORDS_FOR_WHERE_OR_ORDER_BY, + includeRegularFunctions: true, + }); + } + // Plaintext with completions, no grammar. Explore never asks for Lucene — + // it discards a carried Lucene WHERE rather than render one — so this is + // only here for other callers of the editor. + return [autocompletion({ override: [luceneCompletions(fields)] })]; +} diff --git a/packages/app/src/components/Explore/queryModeSafety.ts b/packages/app/src/components/Explore/queryModeSafety.ts new file mode 100644 index 0000000000..1fa2f3795e --- /dev/null +++ b/packages/app/src/components/Explore/queryModeSafety.ts @@ -0,0 +1,39 @@ +export type QueryLanguage = 'sql' | 'lucene'; + +/** + * Explore is SQL-only: what you type in the search box is a WHERE clause, which + * is the same language as the full statement behind the code toggle. That is + * the whole point of the two coexisting — anything you learn in the box carries + * over when you open the statement. + */ +export function getDefaultExploreLanguage(): QueryLanguage { + return 'sql'; +} + +/** + * A Lucene WHERE still reaches Explore from a saved search shared with the + * Search page, an old link, or a side-panel action on a source that defines a + * Lucene expression for an attribute. + * + * It cannot be translated on the way in — `genWhereSQL` needs table metadata + * and a round trip — so there is nothing to put in the box that the box could + * then edit. Explore drops it and says so, rather than running a query the + * search field claims to hold but cannot express. + */ +export function hasUneditableLuceneWhere( + where: string | null | undefined, + whereLanguage: string | null | undefined, +): boolean { + return whereLanguage === 'lucene' && (where ?? '').trim() !== ''; +} + +/** The WHERE Explore will author, with any Lucene clause discarded. */ +export function toSqlWhere( + where: string | null | undefined, + whereLanguage: string | null | undefined, +): { where: string; whereLanguage: QueryLanguage } { + return { + where: whereLanguage === 'lucene' ? '' : (where ?? ''), + whereLanguage: 'sql', + }; +} diff --git a/packages/app/src/components/FieldPicker.module.scss b/packages/app/src/components/FieldPicker.module.scss new file mode 100644 index 0000000000..b3aef3b666 --- /dev/null +++ b/packages/app/src/components/FieldPicker.module.scss @@ -0,0 +1,69 @@ +/* A field with a fixed left addon, built the same way as the "As " + picker these share a toolbar row with: the label names the frame rather than + being something you can press, and the addon fill is what says so. */ + +/* The switcher beside these is a 24px segment inside 4px of padding, so this + has to come out at the same height or the row reads as ragged. */ +$control-height: 32px; + +/* Sits on the surface rather than taking the field fill: nothing here is typed + into, and the input colour promised an editable box. The border and the + addon carry the frame instead. */ +.control { + display: flex; + align-items: center; + flex-shrink: 0; + overflow: hidden; + background: var(--color-bg-surface); + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-default); +} + +/* Full-contrast text, not dimmed: the addon fill already sets the label apart + from the value, so weight carries the hierarchy instead — the field name + beside it is the heavier of the two. */ +.label { + align-self: stretch; + display: flex; + align-items: center; + padding-inline: 8px; + border-right: 1px solid var(--color-border); + background: var(--color-bg-field-addon); + color: var(--color-text); + white-space: nowrap; +} + +/* Beside a Mantine input rather than in the toolbar — matches the default + input height so the series row does not step. */ +.tall { + height: 36px; +} + +.target { + display: flex; + align-items: center; + gap: 6px; + height: $control-height; + padding-inline: 8px; + white-space: nowrap; + + /* Stops short of the addon rather than matching it: the value is dimmed + whenever it is showing a source default, and dimmed text on the addon + grey is a poor read in light mode. */ + &:hover:not(:disabled) { + background: var(--color-bg-control-hover); + } + + &:disabled { + cursor: not-allowed; + opacity: 0.6; + } +} + +/* A long field name truncates rather than pushing the rest of the toolbar off + the row. */ +.value { + overflow: hidden; + max-width: 220px; + text-overflow: ellipsis; +} diff --git a/packages/app/src/components/FieldPicker.tsx b/packages/app/src/components/FieldPicker.tsx new file mode 100644 index 0000000000..e2c6b70fb5 --- /dev/null +++ b/packages/app/src/components/FieldPicker.tsx @@ -0,0 +1,354 @@ +import { useMemo, useState } from 'react'; +import { + type Field, + tcFromSource, +} from '@hyperdx/common-utils/dist/core/metadata'; +import { TSource } from '@hyperdx/common-utils/dist/types'; +import { + Box, + Button, + Checkbox, + Group, + Popover, + Radio, + ScrollArea, + SegmentedControl, + Text, + TextInput, + UnstyledButton, +} from '@mantine/core'; +import { IconChevronDown } from '@tabler/icons-react'; + +import { fieldIdentifier } from '@/components/Explore/fieldIdentifier'; +import SQLInlineEditor from '@/components/Explore/owned/SQLInlineEditor'; +import { useMultipleAllFields } from '@/hooks/useMetadata'; + +import classes from './FieldPicker.module.scss'; + +export type FieldPickerSelection = 'single' | 'multi'; + +/** The SQL tab needs the room; the field list reads better narrow. */ +const DEFAULT_WIDTH = { fields: 360, sql: 520 }; + +/** + * A field with a fixed left addon — "Group by | ServiceName". The common + * trigger shape across the toolbar; event patterns keeps its own plain button + * because it sits outside that row. + * + * The visible label is a sibling of the button rather than its content, so + * pass `aria-label` carrying the whole phrase: without it the button announces + * a bare field name and the operation is lost. + */ +export function FieldPickerTarget({ + label, + value, + muted, + disabled, + onClick, + containerTestId, + tall, + ref, + className, + ...rest +}: { + label: string; + value: string; + /** Dims the value to mark it as a fallback rather than a choice. */ + muted?: boolean; + disabled?: boolean; + onClick: () => void; + 'aria-label': string; + 'data-testid'?: string; + containerTestId?: string; + /** Match the 36px Mantine input beside it instead of the 32px toolbar row. */ + tall?: boolean; + /** Popover.Target clones this and hands the ref down. */ + ref?: React.Ref; + className?: string; +}) { + return ( +
+ + {label} + + {/* Popover.Target clones this with props of its own, `className` among + them, so the spread has to come first or the target renders unstyled: + the value overflows the field and the chevron wraps below it. */} + + + {value} + + + +
+ ); +} + +/** + * "Pick a field" as a popover: a searchable list of the source's fields, plus + * a SQL tab for the expressions a list cannot offer. + * + * The trigger is a render prop rather than a prop of its own because the three + * callers genuinely differ — group by and the series column are labelled addon + * fields, event patterns a button — and the caller places `Popover.Target` + * itself so it can decide how much of the trigger is the clickable part. + * + * Fields load lazily on open: several of these sit in one toolbar, and a field + * list is a query against the table. + */ +export function FieldPicker({ + tableSource, + dateRange, + selection, + selected, + onApply, + trigger, + sqlPlaceholder, + description, + hint, + secondaryAction, + filterField, + emptyLabel = 'No fields found', + sqlMultiline = false, + sqlMinHeight, + sqlSize, + enableVariables, + width = DEFAULT_WIDTH, +}: { + tableSource?: TSource; + dateRange?: [Date, Date]; + selection: FieldPickerSelection; + /** Committed fields. Pinned to the top of the list and kept even when absent + * from the schema: an expression typed on the SQL tab is still a choice. */ + selected: string[]; + /** The SQL tab passes its whole expression as a single entry. */ + onApply: (next: string[]) => void; + trigger: (args: { opened: boolean; toggle: () => void }) => React.ReactNode; + sqlPlaceholder?: string; + /** Above the tabs — what this picker decides. */ + description?: React.ReactNode; + /** Below the list — why the list is not everything. */ + hint?: React.ReactNode; + /** Single-select only — "Use default". Multi-select gets a Clear that + * resets the draft, which the caller cannot reach. */ + secondaryAction?: { label: string; disabled?: boolean; onClick: () => void }; + /** Narrows what is offered, e.g. numeric fields only for a percentile. The + * SQL tab is deliberately left unfiltered — it is the escape hatch. */ + filterField?: (field: Field) => boolean; + emptyLabel?: string; + sqlMultiline?: boolean; + sqlMinHeight?: number; + sqlSize?: string; + enableVariables?: boolean; + width?: { fields: number; sql: number }; +}) { + const [opened, setOpened] = useState(false); + const [search, setSearch] = useState(''); + const [mode, setMode] = useState<'fields' | 'sql'>('fields'); + const [draft, setDraft] = useState(selected); + const [sqlDraft, setSqlDraft] = useState(''); + + // Reopening starts from what is committed rather than from an abandoned + // edit, which a later Apply would otherwise send. + const handleOpenChange = (next: boolean) => { + if (next) { + setDraft(selected); + setSqlDraft(selected.join(', ')); + setSearch(''); + setMode('fields'); + } + setOpened(next); + }; + + const tableConnection = tcFromSource(tableSource); + const { data: fields } = useMultipleAllFields( + tableConnection ? [tableConnection] : [], + { + dateRange, + timestampValueExpression: tableSource?.timestampValueExpression, + enabled: opened && Boolean(tableConnection), + }, + ); + + // Nested map keys belong here — `ResourceAttributes['host']` is as ordinary a + // choice as a top-level column, so this is every field rather than the + // table's columns. + const available = useMemo( + () => + (fields ?? []) + .filter(field => !filterField || filterField(field)) + .map(fieldIdentifier), + [fields, filterField], + ); + + const options = useMemo(() => { + const q = search.trim().toLowerCase(); + const union = Array.from(new Set([...selected, ...available])); + return q ? union.filter(f => f.toLowerCase().includes(q)) : union; + }, [search, available, selected]); + + const commit = (next: string[]) => { + onApply(next); + setOpened(false); + }; + + return ( + + {trigger({ opened, toggle: () => handleOpenChange(!opened) })} + + {description ? ( + + {description} + + ) : null} + setMode(next === 'sql' ? 'sql' : 'fields')} + data={[ + { label: 'Fields', value: 'fields' }, + { label: 'SQL', value: 'sql' }, + ]} + /> + {mode === 'sql' ? ( + + commit([sqlDraft])} + placeholder={sqlPlaceholder} + size={sqlSize} + allowMultiline={sqlMultiline} + minHeight={sqlMinHeight} + disableKeywordAutocomplete + enableVariables={enableVariables} + sourceId={tableSource?.id} + dateRange={dateRange} + /> + + + ) : ( + <> + setSearch(e.currentTarget.value)} + mb="xs" + autoFocus + /> + + + {options.length === 0 ? ( + + {emptyLabel} + + ) : selection === 'single' ? ( + commit([field])} + > + {options.map(field => ( + + ))} + + ) : ( + options.map(field => ( + + setDraft(prev => + prev.includes(field) + ? prev.filter(f => f !== field) + : [...prev, field], + ) + } + /> + )) + )} + + + {hint ? ( + + {hint} + + ) : null} + + {selection === 'multi' ? ( + <> + {/* Clears the in-flight draft rather than committing, so + Clear then Apply is how you group by nothing. */} + + + + ) : ( + // Single-select commits on pick, so an Apply here would only + // ever re-send what the radio already sent. + secondaryAction && ( + + ) + )} + + + )} + + + ); +} diff --git a/packages/app/src/components/MetricNameSelect.tsx b/packages/app/src/components/MetricNameSelect.tsx index 9879baf7be..f7e9cdc245 100644 --- a/packages/app/src/components/MetricNameSelect.tsx +++ b/packages/app/src/components/MetricNameSelect.tsx @@ -190,8 +190,8 @@ export function MetricNameSelect({ } // Reported in the description rather than the `error` slot, which belongs // to form validation for this field. Kept below the input: it appears - // mid-session, and above the input it pushes the field down out of - // alignment with the browse-metrics button beside it. + // mid-session, and above the input it pushes the whole field down, out of + // alignment with the rest of the series row. inputWrapperOrder={['label', 'input', 'description', 'error']} // Keyed off `activeSearch`, not the debounced value: the latter trails by // 300ms, so every transition landed a beat after the list it describes — diff --git a/packages/app/src/components/MetricWall/MetricBrowse.tsx b/packages/app/src/components/MetricWall/MetricBrowse.tsx new file mode 100644 index 0000000000..74f2289cef --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricBrowse.tsx @@ -0,0 +1,181 @@ +import { useCallback } from 'react'; +import { Filter, TMetricSource } from '@hyperdx/common-utils/dist/types'; +import { Alert, Center, Loader, Text } from '@mantine/core'; +import { IconLayoutGrid } from '@tabler/icons-react'; + +import EmptyState from '@/components/EmptyState'; +import { METRIC_KIND_LABELS } from '@/utils/metricKinds'; + +import type { EditAsChartRequest } from './editAsChart'; +import { MetricDrillDown } from './MetricDrillDown'; +import { MetricWall } from './MetricWall'; +import { MetricWallRail } from './MetricWallRail'; +import { MetricWallSearch } from './MetricWallSearch'; +import { useMetricBrowse } from './useMetricBrowse'; +import { WallItem, wallItemKey } from './wallGrouping'; + +import styles from './MetricWall.module.scss'; + +/** The facet rail, rendered in the page's filter sidebar slot. */ +export function MetricBrowseRail({ + source, + dateRange, + onCollapse, +}: { + source: TMetricSource; + dateRange: [Date, Date]; + onCollapse: () => void; +}) { + const { matching, query, state, update } = useMetricBrowse({ + source, + dateRange, + }); + return ( + update({ query: q })} + railKey={state.railKey} + onRailKeyChange={key => update({ railKey: key })} + onCollapse={onCollapse} + /> + ); +} + +/** + * The browse-first landing for metric sources: every metric already charted + * with a sensible default, grouped by who reports it and banded by what it + * measures, to narrow and open rather than assemble from an empty chart. + */ +export function MetricBrowse({ + source, + dateRange, + searchFilters, + onEditAsChart, +}: { + source: TMetricSource; + dateRange: [Date, Date]; + searchFilters: Filter[]; + onEditAsChart: (request: EditAsChartRequest) => void; +}) { + const { + metrics, + failedKinds, + isLoading, + error, + state, + update, + query, + grouping, + sections, + matching, + filters, + } = useMetricBrowse({ source, dateRange }); + + const expandedKey = state.tile + ? wallItemKey(state.tile.metricId, state.tile.entity) + : undefined; + + const openTile = useCallback( + (item: WallItem) => + update({ + tile: + wallItemKey(item.metric.id, item.entity) === expandedKey + ? undefined + : { metricId: item.metric.id, entity: item.entity }, + }), + [update, expandedKey], + ); + + const renderDrillDown = (item: WallItem) => + state.tile ? ( + + state.tile && update({ tile: { ...state.tile, ...patch } }) + } + onClose={() => update({ tile: undefined })} + onEditAsChart={(agg, splitBy) => + onEditAsChart({ + metric: item.metric, + agg, + filters: item.entity ? [item.entity, ...filters] : filters, + splitBy, + }) + } + /> + ) : null; + + let body: React.ReactNode; + if (isLoading) { + body = ( +
+ +
+ ); + } else if (error) { + body = ( + + {error.message} + + ); + } else if (metrics.length === 0) { + body = ( + } + title="No metrics reported" + description="This source has not reported any metrics in the selected time range." + /> + ); + } else if (sections.length === 0) { + body = ( + } + title="No metrics match" + description="Remove a filter or change the search to see more metrics." + /> + ); + } else { + body = ( + + ); + } + + return ( +
+ update({ query: q })} + grouping={grouping} + onGroupingChange={g => + update({ grouping: g === 'entity' ? undefined : g }) + } + matchCount={matching.length} + totalCount={metrics.length} + /> + {failedKinds.length > 0 && ( + + Could not read{' '} + {failedKinds.map(k => METRIC_KIND_LABELS[k]).join(', ')} metrics. + + )} + {body} +
+ ); +} diff --git a/packages/app/src/components/MetricWall/MetricDrillDown.tsx b/packages/app/src/components/MetricWall/MetricDrillDown.tsx new file mode 100644 index 0000000000..57387afb25 --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricDrillDown.tsx @@ -0,0 +1,269 @@ +import { useMemo } from 'react'; +import { Filter, TMetricSource } from '@hyperdx/common-utils/dist/types'; +import { + ActionIcon, + Button, + Chip, + Group, + SegmentedControl, + Text, + Tooltip, +} from '@mantine/core'; +import { IconChartLine, IconX } from '@tabler/icons-react'; + +import { DBTimeChart } from '@/components/DBTimeChart'; +import { Sparkline } from '@/components/Sparkline'; +import { getColorFromCSSToken } from '@/utils'; +import { METRIC_KIND_LABELS } from '@/utils/metricKinds'; + +import { METRIC_QUANTITY_LABELS } from './classifyMetric'; +import { QUANTITY_COLORS } from './MetricTile'; +import { + buildTileConfig, + defaultTileAgg, + MetricFilterClause, + resolveTileAgg, + TileAgg, + tileAggOptions, +} from './tileDefaults'; +import { useSplitCardinality } from './useMetricAttributeStats'; +import { formatMetricValue, useMetricSeries } from './useMetricSeries'; +import { ExpandedTile } from './useMetricWallState'; +import { WallItem } from './wallGrouping'; + +import styles from './MetricWall.module.scss'; + +const MAX_MULTIPLES = 12; +// Past this many values a split draws more lines than anyone can read. +const HIGH_CARDINALITY = 50; + +function SmallMultiples({ + config, + unit, + color, +}: { + config: ReturnType; + unit?: string; + color: string; +}) { + const { series, isLoading } = useMetricSeries({ + ...config, + seriesLimit: MAX_MULTIPLES, + }); + // One scale for every panel, or twelve flat-looking charts hide the one + // series that is actually on fire. + const yDomain = useMemo<[number, number] | undefined>(() => { + const ys = series.flatMap(s => s.points.map(p => p.y)); + return ys.length ? [Math.min(0, ...ys), Math.max(...ys)] : undefined; + }, [series]); + + if (isLoading) { + return ( + + Loading… + + ); + } + return ( + <> + + Same split, broken out · {series.length} series · shared y scale + +
+ {series.slice(0, MAX_MULTIPLES).map(s => ( +
+ + {s.label} + +
+ +
+ + {formatMetricValue(s.latest, unit)} + +
+ ))} +
+ + ); +} + +/** + * A tile opened where it sits. The tile already carried a valid query, so + * nothing has to be rebuilt: this only offers the other readings of it. + */ +export function MetricDrillDown({ + item, + tile, + source, + dateRange, + filters, + searchFilters, + onChange, + onClose, + onEditAsChart, +}: { + item: WallItem; + tile: ExpandedTile; + source: TMetricSource; + dateRange: [Date, Date]; + filters: MetricFilterClause[]; + searchFilters: Filter[]; + onChange: (patch: Partial) => void; + onClose: () => void; + onEditAsChart: (agg: TileAgg, splitBy?: string) => void; +}) { + const { metric, entity } = item; + const agg = resolveTileAgg(metric.type, tile.agg); + const defaultAgg = defaultTileAgg(metric.type); + const quantity = metric.classification.quantity; + const splits = useSplitCardinality({ + source, + metricName: metric.name, + metricType: metric.type, + dateRange, + }); + + const config = useMemo( + () => + buildTileConfig({ + source, + metricName: metric.name, + metricType: metric.type, + agg, + filters: entity ? [entity, ...filters] : filters, + searchFilters, + dateRange, + groupBy: tile.splitBy, + }), + [ + source, + metric, + agg, + entity, + filters, + searchFilters, + dateRange, + tile.splitBy, + ], + ); + + return ( +
+ +
+ + {metric.name} + + + {METRIC_KIND_LABELS[metric.type]} + {metric.unit ? ` · unit ${metric.unit}` : ''} · classified as{' '} + {METRIC_QUANTITY_LABELS[quantity]} + {entity ? ` · ${entity.key} ${entity.value}` : ''} + +
+ + + onChange({ agg: id === defaultAgg.id ? undefined : id }) + } + data={tileAggOptions(metric.type).map(a => ({ + value: a.id, + label: a.id === defaultAgg.id ? `${a.label} (default)` : a.label, + }))} + aria-label="Aggregation" + /> + + + + + + + +
+ + {tile.multiples && tile.splitBy ? ( + + ) : ( +
+ +
+ )} + + + + Split by + + {splits.isLoading && ( + + Loading attributes… + + )} + {splits.data?.map(({ key, values }) => ( + + onChange({ splitBy: checked ? key : undefined }) + } + styles={{ + label: { opacity: values > HIGH_CARDINALITY ? 0.55 : 1 }, + }} + > + {key} {values} + + ))} + {splits.data?.length === 0 && ( + + Nothing to split by + + )} + + onChange({ multiples: v === 'multiples' || undefined }) + } + data={[ + { value: 'one', label: 'One chart' }, + { value: 'multiples', label: 'Small multiples' }, + ]} + aria-label="Show the split as" + /> + +
+ ); +} diff --git a/packages/app/src/components/MetricWall/MetricTile.tsx b/packages/app/src/components/MetricWall/MetricTile.tsx new file mode 100644 index 0000000000..2a2f8e4230 --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricTile.tsx @@ -0,0 +1,113 @@ +import { memo, useMemo } from 'react'; +import { + ChartPaletteToken, + Filter, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { Group, Text, UnstyledButton } from '@mantine/core'; +import { useInViewport } from '@mantine/hooks'; + +import { Sparkline } from '@/components/Sparkline'; +import { getColorFromCSSToken } from '@/utils'; +import { METRIC_KIND_LABELS } from '@/utils/metricKinds'; + +import { METRIC_QUANTITY_LABELS, MetricQuantity } from './classifyMetric'; +import { + buildTileConfig, + defaultTileAgg, + MetricFilterClause, +} from './tileDefaults'; +import { formatMetricValue, useMetricSeries } from './useMetricSeries'; +import { WallItem } from './wallGrouping'; + +import styles from './MetricWall.module.scss'; + +export const QUANTITY_COLORS: Record = { + latency: 'chart-blue', + errors: 'chart-red', + throughput: 'chart-green', + saturation: 'chart-light-blue', + queue: 'chart-orange', + unclassified: 'chart-gray', +}; + +function MetricTileComponent({ + item, + source, + dateRange, + filters, + searchFilters, + onOpen, +}: { + item: WallItem; + source: TMetricSource; + dateRange: [Date, Date]; + /** Attribute filters from the wall's own search. */ + filters: MetricFilterClause[]; + searchFilters: Filter[]; + onOpen: (item: WallItem) => void; +}) { + const { metric, entity } = item; + const agg = defaultTileAgg(metric.type); + // A wall of forty tiles would fire forty queries on load; only the ones on + // screen ask for data. + const { ref, inViewport } = useInViewport(); + + const config = useMemo( + () => + buildTileConfig({ + source, + metricName: metric.name, + metricType: metric.type, + agg, + filters: entity ? [entity, ...filters] : filters, + searchFilters, + dateRange, + }), + [source, metric, agg, entity, filters, searchFilters, dateRange], + ); + const { series } = useMetricSeries(config, { enabled: inViewport }); + const first = series[0]; + const quantity = metric.classification.quantity; + const guessed = metric.classification.reason === 'fallback'; + + return ( + onOpen(item)} + data-testid="metric-tile" + aria-label={`Open ${metric.name}`} + > + + + {METRIC_QUANTITY_LABELS[quantity]} + + + {metric.unit ?? METRIC_KIND_LABELS[metric.type]} + + + {metric.name} +
+ {first && ( + + )} +
+ + + {agg.label} + {guessed ? ' (guessed)' : ''} + + + {formatMetricValue(first?.latest, metric.unit)} + + +
+ ); +} + +export const MetricTile = memo(MetricTileComponent); diff --git a/packages/app/src/components/MetricWall/MetricWall.module.scss b/packages/app/src/components/MetricWall/MetricWall.module.scss new file mode 100644 index 0000000000..9c179cf055 --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricWall.module.scss @@ -0,0 +1,103 @@ +.rail { + width: 220px; + flex-shrink: 0; + height: 100%; + overflow-y: auto; + border-right: 1px solid var(--color-border); + padding: var(--mantine-spacing-xs); +} + +.main { + flex: 1; + min-height: 0; + min-width: 0; + overflow-y: auto; + padding: var(--mantine-spacing-xs) var(--mantine-spacing-sm) + var(--mantine-spacing-md); +} + +.section { + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-sm); + background: var(--color-bg-surface); + padding: var(--mantine-spacing-sm); + margin-bottom: var(--mantine-spacing-sm); +} + +.bandHeader { + display: flex; + justify-content: space-between; + align-items: baseline; + margin: var(--mantine-spacing-xs) 0 6px; +} + +.grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); + gap: var(--mantine-spacing-xs); +} + +.tile { + display: flex; + flex-direction: column; + gap: 2px; + min-height: 112px; + padding: 8px; + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-sm); + background: var(--color-bg-body); + text-align: left; + cursor: pointer; + + &:hover, + &:focus-visible { + background: var(--color-bg-hover); + } +} + +.tileName { + font-family: var(--mantine-font-family-monospace); + font-size: var(--mantine-font-size-xs); + line-height: 1.3; + word-break: break-all; +} + +.spark { + position: relative; + flex: 1; + min-height: 36px; +} + +.drill { + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-sm); + background: var(--color-bg-body); + padding: var(--mantine-spacing-sm); + margin-bottom: var(--mantine-spacing-sm); +} + +.multiple { + display: flex; + flex-direction: column; + height: 86px; + padding: 6px 8px; + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-sm); +} + +.facet { + display: flex; + justify-content: space-between; + width: 100%; + padding: 2px 6px; + border-radius: var(--mantine-radius-xs); + font-size: var(--mantine-font-size-xs); + + &:hover { + background: var(--color-bg-hover); + } +} + +.facetActive { + background: var(--color-bg-highlighted); +} diff --git a/packages/app/src/components/MetricWall/MetricWall.tsx b/packages/app/src/components/MetricWall/MetricWall.tsx new file mode 100644 index 0000000000..0971389a12 --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricWall.tsx @@ -0,0 +1,132 @@ +import { useState } from 'react'; +import { Filter, TMetricSource } from '@hyperdx/common-utils/dist/types'; +import { Button, Group, Text } from '@mantine/core'; + +import { METRIC_QUANTITY_LABELS } from './classifyMetric'; +import { MetricTile } from './MetricTile'; +import { MetricFilterClause } from './tileDefaults'; +import { WallBand, WallItem, WallSection } from './wallGrouping'; + +import styles from './MetricWall.module.scss'; + +const INITIAL_SECTIONS = 10; + +function bandTitle(band: WallBand): string { + return band.quantity === 'all' + ? 'All metrics' + : METRIC_QUANTITY_LABELS[band.quantity]; +} + +function sectionTitle(section: WallSection): { + title: string; + subtitle?: string; +} { + if (section.entity) { + return { title: section.entity.value, subtitle: section.entity.key }; + } + return section.id === 'unscoped' + ? { title: 'Other', subtitle: 'no service.name or host.name' } + : { title: 'All metrics' }; +} + +export function MetricWall({ + sections, + source, + dateRange, + filters, + searchFilters, + expandedKey, + onOpen, + renderDrillDown, +}: { + sections: WallSection[]; + source: TMetricSource; + dateRange: [Date, Date]; + filters: MetricFilterClause[]; + searchFilters: Filter[]; + expandedKey?: string; + onOpen: (item: WallItem) => void; + renderDrillDown: (item: WallItem) => React.ReactNode; +}) { + const [showAll, setShowAll] = useState(false); + const visible = showAll ? sections : sections.slice(0, INITIAL_SECTIONS); + + return ( + <> + {visible.map(section => { + const { title, subtitle } = sectionTitle(section); + return ( +
+ + + + {title} + + {subtitle && ( + + {subtitle} + + )} + + + {section.metricCount} metric + {section.metricCount === 1 ? '' : 's'} + + + {section.bands.map(band => { + const expanded = band.items.find(i => i.key === expandedKey); + return ( +
+
+ + {bandTitle(band)} · {band.items.length} + + {band.quantity === 'unclassified' && ( + + No declared unit, name did not parse + + )} +
+ {expanded && renderDrillDown(expanded)} +
+ {band.items.map(item => ( + + ))} +
+
+ ); + })} +
+ ); + })} + {sections.length > INITIAL_SECTIONS && ( + + )} + + ); +} diff --git a/packages/app/src/components/MetricWall/MetricWallRail.tsx b/packages/app/src/components/MetricWall/MetricWallRail.tsx new file mode 100644 index 0000000000..988fc8583c --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricWallRail.tsx @@ -0,0 +1,214 @@ +import { useMemo } from 'react'; +import cx from 'classnames'; +import { TMetricSource } from '@hyperdx/common-utils/dist/types'; +import { + ActionIcon, + Group, + Loader, + Stack, + Text, + Tooltip, + UnstyledButton, +} from '@mantine/core'; +import { IconLayoutSidebarLeftCollapse } from '@tabler/icons-react'; + +import { METRIC_KIND_LABELS, QUERYABLE_KINDS } from '@/utils/metricKinds'; + +import { METRIC_QUANTITIES, METRIC_QUANTITY_LABELS } from './classifyMetric'; +import { + hasMetricQueryToken, + MetricQueryToken, + toggleMetricQueryToken, +} from './parseMetricQuery'; +import { useAttributeValueCounts } from './useMetricAttributeStats'; +import { rankAttributeKeys, WallMetric } from './useMetricWallCatalog'; + +import styles from './MetricWall.module.scss'; + +const MAX_KEYS = 15; + +function Facet({ + label, + count, + active, + onClick, + mono = false, +}: { + label: string; + count: number; + active: boolean; + onClick: () => void; + mono?: boolean; +}) { + return ( + + + {label} + + + {count} + + + ); +} + +function FacetGroup({ + title, + children, +}: { + title: string; + children: React.ReactNode; +}) { + return ( + + + {title} + + {children} + + ); +} + +/** + * Narrow the wall by what the metrics measure and carry, rather than by + * picking one: every pick becomes a pill in the search box. + */ +export function MetricWallRail({ + metrics, + source, + dateRange, + query, + onQueryChange, + railKey, + onRailKeyChange, + onCollapse, +}: { + /** The metrics on the wall right now. */ + metrics: WallMetric[]; + source: TMetricSource; + dateRange: [Date, Date]; + query: string; + onQueryChange: (query: string) => void; + railKey?: string; + onRailKeyChange: (key: string | undefined) => void; + onCollapse: () => void; +}) { + const quantityCounts = useMemo(() => { + const counts = new Map(); + for (const m of metrics) { + const q = m.classification.quantity; + counts.set(q, (counts.get(q) ?? 0) + 1); + } + return counts; + }, [metrics]); + const kindCounts = useMemo(() => { + const counts = new Map(); + for (const m of metrics) counts.set(m.type, (counts.get(m.type) ?? 0) + 1); + return counts; + }, [metrics]); + const keys = useMemo( + () => rankAttributeKeys(metrics).slice(0, MAX_KEYS), + [metrics], + ); + const values = useAttributeValueCounts({ + source, + attributeKey: railKey, + dateRange, + }); + + const toggle = (token: MetricQueryToken) => + onQueryChange(toggleMetricQueryToken(query, token)); + + return ( + + ); +} diff --git a/packages/app/src/components/MetricWall/MetricWallSearch.tsx b/packages/app/src/components/MetricWall/MetricWallSearch.tsx new file mode 100644 index 0000000000..83c8dae88d --- /dev/null +++ b/packages/app/src/components/MetricWall/MetricWallSearch.tsx @@ -0,0 +1,140 @@ +import { useState } from 'react'; +import { Group, Pill, SegmentedControl, Text, TextInput } from '@mantine/core'; +import { IconSearch } from '@tabler/icons-react'; + +import { METRIC_KIND_LABELS } from '@/utils/metricKinds'; + +import { METRIC_QUANTITY_LABELS } from './classifyMetric'; +import { + formatMetricQuery, + formatMetricQueryToken, + MetricQueryToken, + parseMetricQuery, +} from './parseMetricQuery'; +import { WallGrouping } from './wallGrouping'; + +function pillLabel(token: MetricQueryToken): string { + switch (token.type) { + case 'quantity': + return METRIC_QUANTITY_LABELS[token.value]; + case 'kind': + return METRIC_KIND_LABELS[token.value]; + case 'has': + return `has ${token.key}`; + case 'attr': + return `${token.key} = ${token.value}`; + case 'unit': + return `unit ${token.value}`; + } +} + +/** + * One box for finding and narrowing: typing a filter (`unit:ms`, + * `has:http.route`, `service.name=api`) turns it into a pill on space. + */ +export function MetricWallSearch({ + query, + onQueryChange, + grouping, + onGroupingChange, + matchCount, + totalCount, +}: { + query: string; + onQueryChange: (query: string) => void; + grouping: WallGrouping; + onGroupingChange: (grouping: WallGrouping) => void; + matchCount: number; + totalCount: number; +}) { + const parsed = parseMetricQuery(query); + // The typed text is held locally so a half-typed `unit:` stays text until + // the user finishes it. + const [draft, setDraft] = useState(null); + const text = draft ?? parsed.text; + + const commit = (nextText: string) => { + const next = parseMetricQuery(nextText); + onQueryChange( + formatMetricQuery({ + text: next.text, + tokens: [...parsed.tokens, ...next.tokens], + }), + ); + }; + + const removeToken = (token: MetricQueryToken) => + onQueryChange( + formatMetricQuery({ + text: parsed.text, + tokens: parsed.tokens.filter( + t => formatMetricQueryToken(t) !== formatMetricQueryToken(token), + ), + }), + ); + + return ( + + } + placeholder="Search metrics, or type unit:ms · has:http.route · service.name=api" + value={text} + onChange={e => { + const value = e.currentTarget.value; + if (value.endsWith(' ') && parseMetricQuery(value).tokens.length) { + setDraft(null); + commit(value); + return; + } + setDraft(value); + if (!parseMetricQuery(value).tokens.length) commit(value); + }} + onBlur={() => { + if (draft != null) commit(draft); + setDraft(null); + }} + onKeyDown={e => { + if (e.key === 'Enter') { + commit(text); + setDraft(null); + } else if ( + e.key === 'Backspace' && + text === '' && + parsed.tokens.length > 0 + ) { + removeToken(parsed.tokens[parsed.tokens.length - 1]); + } + }} + data-testid="metric-wall-search" + /> + {parsed.tokens.map(token => ( + removeToken(token)} + size="sm" + > + {pillLabel(token)} + + ))} + + {matchCount === totalCount + ? `${totalCount} metrics` + : `${matchCount} of ${totalCount} metrics`} + + onGroupingChange(v as WallGrouping)} + data={[ + { value: 'entity', label: 'Entity' }, + { value: 'quantity', label: 'Quantity' }, + { value: 'flat', label: 'Flat' }, + ]} + aria-label="Group metrics by" + /> + + ); +} diff --git a/packages/app/src/components/MetricWall/__tests__/MetricBrowse.test.tsx b/packages/app/src/components/MetricWall/__tests__/MetricBrowse.test.tsx new file mode 100644 index 0000000000..2acb696d13 --- /dev/null +++ b/packages/app/src/components/MetricWall/__tests__/MetricBrowse.test.tsx @@ -0,0 +1,184 @@ +import { useSyncExternalStore } from 'react'; +import { + MetricsDataType, + MetricSourceSchema, + SourceKind, +} from '@hyperdx/common-utils/dist/types'; +import { screen, within } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import { classifyMetric } from '@/components/MetricWall/classifyMetric'; +import { + MetricBrowse, + MetricBrowseRail, +} from '@/components/MetricWall/MetricBrowse'; +import { + WallMetric, + wallMetricId, +} from '@/components/MetricWall/useMetricWallCatalog'; + +// The rail and the wall each read the URL state, so the mock shares one value. +const mockUrl: { value: unknown; listeners: Set<() => void> } = { + value: null, + listeners: new Set(), +}; +jest.mock('nuqs', () => ({ + __esModule: true, + parseAsJson: () => ({}), + useQueryState: () => { + const value = useSyncExternalStore( + listener => { + mockUrl.listeners.add(listener); + return () => mockUrl.listeners.delete(listener); + }, + () => mockUrl.value, + ); + const setValue = (next: unknown) => { + mockUrl.value = typeof next === 'function' ? next(mockUrl.value) : next; + mockUrl.listeners.forEach(l => l()); + }; + return [value, setValue]; + }, +})); + +beforeEach(() => { + mockUrl.value = null; +}); + +function metric( + name: string, + type: MetricsDataType, + unit: string | undefined, + entities: string[], +): WallMetric { + return { + id: wallMetricId(type, name), + name, + type, + unit, + classification: classifyMetric({ name, type, unit }), + keys: new Set(['http.route']), + entities: entities.map(e => { + const [key, value] = e.split('='); + return { key, value }; + }), + }; +} + +const mockMetrics = [ + metric('http.server.request.duration', MetricsDataType.Histogram, 'ms', [ + 'service.name=api', + ]), + metric('http.server.error.count', MetricsDataType.Sum, '1', [ + 'service.name=api', + ]), + metric('node_load1', MetricsDataType.Gauge, '1', ['host.name=ip-10']), +]; + +jest.mock('@/components/MetricWall/useMetricWallCatalog', () => ({ + ...jest.requireActual('@/components/MetricWall/useMetricWallCatalog'), + useMetricWallCatalog: () => ({ + metrics: mockMetrics, + failedKinds: [], + isLoading: false, + error: null, + }), +})); + +jest.mock('@/components/MetricWall/useMetricAttributeStats', () => ({ + useAttributeValueCounts: () => ({ data: [], isLoading: false }), +})); + +// Tiles and the drill-down query ClickHouse; these tests are about the +// wall's layout and how narrowing and opening move through it. +jest.mock('@/components/MetricWall/MetricTile', () => ({ + QUANTITY_COLORS: {}, + MetricTile: ({ + item, + onOpen, + }: { + item: { metric: { name: string } }; + onOpen: (i: unknown) => void; + }) => ( + + ), +})); + +jest.mock('@/components/MetricWall/MetricDrillDown', () => ({ + MetricDrillDown: ({ item }: { item: { metric: { name: string } } }) => ( +
{item.metric.name}
+ ), +})); + +const source = MetricSourceSchema.parse({ + id: 'src', + kind: SourceKind.Metric, + name: 'Metrics', + connection: 'conn', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + resourceAttributesExpression: 'ResourceAttributes', + metricTables: { gauge: 'otel_metrics_gauge' }, +}); + +const dateRange: [Date, Date] = [new Date(0), new Date(1000)]; + +function renderBrowse() { + renderWithMantine( + <> + + + , + ); +} + +describe('MetricBrowse', () => { + it('lands on every metric, grouped by who reports it', () => { + renderBrowse(); + + const sections = screen.getAllByTestId('metric-wall-section'); + expect(sections).toHaveLength(2); + expect(within(sections[0]).getByText('api')).toBeInTheDocument(); + expect(within(sections[0]).getAllByTestId('metric-tile')).toHaveLength(2); + expect(screen.getByText('3 metrics')).toBeInTheDocument(); + }); + + it('narrows by quantity from the rail', async () => { + const user = userEvent.setup(); + renderBrowse(); + + const rail = screen.getByTestId('metric-wall-rail'); + await user.click(within(rail).getByRole('button', { name: /Latency/ })); + + expect(screen.getAllByTestId('metric-tile')).toHaveLength(1); + expect(screen.getByText('1 of 3 metrics')).toBeInTheDocument(); + }); + + it('opens a tile in place and closes it on a second click', async () => { + const user = userEvent.setup(); + renderBrowse(); + + await user.click(screen.getByText('node_load1')); + expect(screen.getByTestId('metric-drill-down')).toHaveTextContent( + 'node_load1', + ); + + await user.click(screen.getByText('node_load1', { selector: 'button' })); + expect(screen.queryByTestId('metric-drill-down')).not.toBeInTheDocument(); + }); +}); diff --git a/packages/app/src/components/MetricWall/__tests__/classifyMetric.test.ts b/packages/app/src/components/MetricWall/__tests__/classifyMetric.test.ts new file mode 100644 index 0000000000..3c7d791845 --- /dev/null +++ b/packages/app/src/components/MetricWall/__tests__/classifyMetric.test.ts @@ -0,0 +1,35 @@ +import { MetricsDataType } from '@hyperdx/common-utils/dist/types'; + +import { classifyMetric } from '@/components/MetricWall/classifyMetric'; + +const { Gauge, Sum, Histogram } = MetricsDataType; + +describe('classifyMetric', () => { + it.each([ + ['http.server.request.duration', Histogram, 'ms', 'latency', 'unit'], + ['jvm.gc.duration', Histogram, 's', 'latency', 'unit'], + ['node_cpu_seconds_total', Sum, 's', 'saturation', 'unit'], + ['container_memory_working_set_bytes', Gauge, 'By', 'saturation', 'unit'], + ['k8s.pod.cpu.utilization', Gauge, '%', 'saturation', 'unit'], + ['system.disk.io', Sum, 'By/s', 'throughput', 'unit'], + ['http.server.error.count', Sum, '1', 'errors', 'name'], + ['kafka.consumer.lag', Gauge, '1', 'queue', 'name'], + ['db_connection_errors_total', Sum, undefined, 'errors', 'name'], + ['orders_processed', Sum, undefined, 'unclassified', 'fallback'], + ['biz.checkout.v2', Gauge, undefined, 'unclassified', 'fallback'], + ])('%s (%s, %s) is %s by %s', (name, type, unit, quantity, reason) => { + expect(classifyMetric({ name, type, unit })).toEqual({ quantity, reason }); + }); + + it('reads a dimensionless sum as a counter when nothing else says what it is', () => { + expect( + classifyMetric({ name: 'orders.processed', type: Sum, unit: '1' }), + ).toEqual({ quantity: 'errors', reason: 'kind' }); + }); + + it('never classifies from the head of the name', () => { + expect( + classifyMetric({ name: 'latency.orders', type: Gauge }).quantity, + ).toBe('unclassified'); + }); +}); diff --git a/packages/app/src/components/MetricWall/__tests__/editAsChart.test.ts b/packages/app/src/components/MetricWall/__tests__/editAsChart.test.ts new file mode 100644 index 0000000000..bb2f60d77a --- /dev/null +++ b/packages/app/src/components/MetricWall/__tests__/editAsChart.test.ts @@ -0,0 +1,41 @@ +import { MetricsDataType } from '@hyperdx/common-utils/dist/types'; + +import { classifyMetric } from '@/components/MetricWall/classifyMetric'; +import { aggConfigFromWall } from '@/components/MetricWall/editAsChart'; +import { resolveTileAgg } from '@/components/MetricWall/tileDefaults'; + +describe('aggConfigFromWall', () => { + it('carries the tile aggregation, scope and split into one series', () => { + const name = 'http.server.request.duration'; + const type = MetricsDataType.Histogram; + + const { series, groupBy } = aggConfigFromWall({ + metric: { + id: `${type}:${name}`, + name, + type, + unit: 'ms', + classification: classifyMetric({ name, type, unit: 'ms' }), + keys: new Set(), + entities: [], + }, + agg: resolveTileAgg(type, 'p99'), + filters: [{ key: 'service.name', value: 'api' }], + splitBy: 'k8s.pod.name', + }); + + expect(series).toEqual([ + expect.objectContaining({ + aggFn: 'quantile', + level: 0.99, + metricName: name, + metricType: type, + valueExpression: 'Value', + aggCondition: expect.stringContaining( + "ResourceAttributes['service.name'] = 'api'", + ), + }), + ]); + expect(groupBy).toContain("ResourceAttributes['k8s.pod.name']"); + }); +}); diff --git a/packages/app/src/components/MetricWall/__tests__/parseMetricQuery.test.ts b/packages/app/src/components/MetricWall/__tests__/parseMetricQuery.test.ts new file mode 100644 index 0000000000..9ed2843de8 --- /dev/null +++ b/packages/app/src/components/MetricWall/__tests__/parseMetricQuery.test.ts @@ -0,0 +1,77 @@ +import { MetricsDataType } from '@hyperdx/common-utils/dist/types'; + +import { + formatMetricQuery, + matchesMetricQuery, + parseMetricQuery, + toggleMetricQueryToken, +} from '@/components/MetricWall/parseMetricQuery'; + +describe('parseMetricQuery', () => { + it('splits filters from free text', () => { + expect( + parseMetricQuery('unit:ms has:http.route service.name=api request'), + ).toEqual({ + text: 'request', + tokens: [ + { type: 'unit', value: 'ms' }, + { type: 'has', key: 'http.route' }, + { type: 'attr', key: 'service.name', value: 'api' }, + ], + }); + }); + + it('reads quoted values and ignores unknown facets as text', () => { + expect( + parseMetricQuery('env="prod eu" quantity:latency kind:nope'), + ).toEqual({ + text: 'kind:nope', + tokens: [ + { type: 'attr', key: 'env', value: 'prod eu' }, + { type: 'quantity', value: 'latency' }, + ], + }); + }); + + it('round-trips through formatMetricQuery', () => { + const query = 'unit:ms env="prod eu" request'; + expect(formatMetricQuery(parseMetricQuery(query))).toBe(query); + }); + + it('toggles a facet on and off', () => { + const on = toggleMetricQueryToken('duration', { + type: 'quantity', + value: 'latency', + }); + expect(on).toBe('quantity:latency duration'); + expect( + toggleMetricQueryToken(on, { type: 'quantity', value: 'latency' }), + ).toBe('duration'); + }); +}); + +describe('matchesMetricQuery', () => { + const metric = { + name: 'http.server.request.duration', + type: MetricsDataType.Histogram, + unit: 'ms', + description: 'Duration of HTTP server requests', + quantity: 'latency' as const, + hasKey: (key: string) => key === 'http.route', + }; + + it.each([ + ['server', true], + ['HTTP SERVER requests', true], + ['server duration', false], + ['unit:ms', true], + ['unit:s', false], + ['has:http.route', true], + ['http.route=/cart', true], + ['k8s.pod.name=a', false], + ['quantity:latency kind:histogram', true], + ['kind:gauge', false], + ])('%s matches: %s', (query, expected) => { + expect(matchesMetricQuery(metric, parseMetricQuery(query))).toBe(expected); + }); +}); diff --git a/packages/app/src/components/MetricWall/__tests__/tileDefaults.test.ts b/packages/app/src/components/MetricWall/__tests__/tileDefaults.test.ts new file mode 100644 index 0000000000..6f011b7fd9 --- /dev/null +++ b/packages/app/src/components/MetricWall/__tests__/tileDefaults.test.ts @@ -0,0 +1,72 @@ +import { + MetricsDataType, + MetricSourceSchema, + SourceKind, +} from '@hyperdx/common-utils/dist/types'; + +import { + attributeClauseSql, + buildTileConfig, + defaultTileAgg, + resolveTileAgg, +} from '@/components/MetricWall/tileDefaults'; + +const source = MetricSourceSchema.parse({ + id: 'src', + kind: SourceKind.Metric, + name: 'Metrics', + connection: 'conn', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + resourceAttributesExpression: 'ResourceAttributes', + metricTables: { gauge: 'otel_metrics_gauge' }, +}); + +describe('tile defaults', () => { + it.each([ + [MetricsDataType.Gauge, 'avg'], + [MetricsDataType.Sum, 'increase'], + [MetricsDataType.Histogram, 'p95'], + [MetricsDataType.ExponentialHistogram, 'p95'], + ])('defaults %s to %s', (type, id) => { + expect(defaultTileAgg(type).id).toBe(id); + }); + + it('falls back to the default for an agg the kind does not offer', () => { + expect(resolveTileAgg(MetricsDataType.Histogram, 'avg').id).toBe('p95'); + expect(resolveTileAgg(MetricsDataType.Histogram, 'p99').level).toBe(0.99); + }); + + it('matches an attribute on either map, escaping quotes', () => { + expect(attributeClauseSql({ key: 'service.name', value: "o'k" })).toBe( + "(ResourceAttributes['service.name'] = 'o''k' OR Attributes['service.name'] = 'o''k')", + ); + }); + + it('builds a metric chart config the renderer understands', () => { + const config = buildTileConfig({ + source, + metricName: 'http.server.request.duration', + metricType: MetricsDataType.Histogram, + agg: defaultTileAgg(MetricsDataType.Histogram), + filters: [{ key: 'service.name', value: 'api' }], + dateRange: [new Date(0), new Date(1000)], + groupBy: 'k8s.pod.name', + }); + + expect(config.metricTables).toBe(source.metricTables); + expect(config.select).toEqual([ + expect.objectContaining({ + aggFn: 'quantile', + level: 0.95, + valueExpression: 'Value', + metricName: 'http.server.request.duration', + metricType: MetricsDataType.Histogram, + }), + ]); + expect(config.where).toContain( + "ResourceAttributes['service.name'] = 'api'", + ); + expect(config.groupBy).toContain("ResourceAttributes['k8s.pod.name']"); + }); +}); diff --git a/packages/app/src/components/MetricWall/__tests__/wallGrouping.test.ts b/packages/app/src/components/MetricWall/__tests__/wallGrouping.test.ts new file mode 100644 index 0000000000..eb4013e7a9 --- /dev/null +++ b/packages/app/src/components/MetricWall/__tests__/wallGrouping.test.ts @@ -0,0 +1,93 @@ +import { MetricsDataType } from '@hyperdx/common-utils/dist/types'; + +import { classifyMetric } from '@/components/MetricWall/classifyMetric'; +import { parseMetricQuery } from '@/components/MetricWall/parseMetricQuery'; +import { + WallMetric, + wallMetricId, +} from '@/components/MetricWall/useMetricWallCatalog'; +import { groupWallMetrics } from '@/components/MetricWall/wallGrouping'; + +function metric( + name: string, + type: MetricsDataType, + unit: string | undefined, + entities: string[], + keys: string[] = [], +): WallMetric { + return { + id: wallMetricId(type, name), + name, + type, + unit, + classification: classifyMetric({ name, type, unit }), + keys: new Set(keys), + entities: entities.map(e => { + const [key, value] = e.split('='); + return { key, value }; + }), + }; +} + +const metrics = [ + metric( + 'http.server.request.duration', + MetricsDataType.Histogram, + 'ms', + ['service.name=api', 'service.name=web'], + ['http.route'], + ), + metric('http.server.error.count', MetricsDataType.Sum, '1', [ + 'service.name=api', + ]), + metric('node_load1', MetricsDataType.Gauge, '1', ['host.name=ip-10']), + metric('orders_processed', MetricsDataType.Sum, undefined, []), +]; + +describe('groupWallMetrics', () => { + it('groups by entity, busiest first, then bands by quantity', () => { + const sections = groupWallMetrics(metrics, parseMetricQuery(''), 'entity'); + + expect(sections.map(s => s.id)).toEqual([ + 'service.name=api', + 'host.name=ip-10', + 'service.name=web', + 'unscoped', + ]); + expect(sections[0].bands.map(b => b.quantity)).toEqual([ + 'latency', + 'errors', + ]); + expect(sections[3].bands[0].quantity).toBe('unclassified'); + }); + + it('lets an entity filter pick the sections', () => { + const sections = groupWallMetrics( + metrics, + parseMetricQuery('service.name=web'), + 'entity', + ); + + expect(sections.map(s => s.id)).toEqual(['service.name=web']); + }); + + it('narrows by attribute key and quantity', () => { + const sections = groupWallMetrics( + metrics, + parseMetricQuery('has:http.route quantity:latency'), + 'quantity', + ); + + expect(sections).toHaveLength(1); + expect(sections[0].bands[0].items.map(i => i.metric.name)).toEqual([ + 'http.server.request.duration', + ]); + }); + + it('lists everything in one band when flat', () => { + const [section] = groupWallMetrics(metrics, parseMetricQuery(''), 'flat'); + + expect(section.bands).toHaveLength(1); + expect(section.metricCount).toBe(4); + }); +}); diff --git a/packages/app/src/components/MetricWall/classifyMetric.ts b/packages/app/src/components/MetricWall/classifyMetric.ts new file mode 100644 index 0000000000..a242e94139 --- /dev/null +++ b/packages/app/src/components/MetricWall/classifyMetric.ts @@ -0,0 +1,118 @@ +import { MetricsDataType } from '@hyperdx/common-utils/dist/types'; + +export const METRIC_QUANTITIES = [ + 'latency', + 'errors', + 'throughput', + 'saturation', + 'queue', + 'unclassified', +] as const; + +export type MetricQuantity = (typeof METRIC_QUANTITIES)[number]; + +export const METRIC_QUANTITY_LABELS: Record = { + latency: 'Latency', + errors: 'Errors & counts', + throughput: 'Throughput', + saturation: 'Saturation', + queue: 'Queue depth', + unclassified: 'Unclassified', +}; + +/** Which of the four signals decided the band, shown on the tile caption. */ +type ClassificationReason = 'unit' | 'name' | 'kind' | 'fallback'; + +export type MetricClassification = { + quantity: MetricQuantity; + reason: ClassificationReason; +}; + +const TIME_UNITS = new Set(['ns', 'us', 'ms', 's', 'min', 'h', 'd']); +const BYTE_UNITS = /^(bit|By|KBy|MBy|GBy|KiBy|MiBy|GiBy|TiBy)$/; + +// Tails that name what a metric measures, checked longest first so +// `seconds_total` wins over `total`. +const NAME_TAILS: [string, MetricQuantity][] = [ + ['seconds_total', 'latency'], + ['duration', 'latency'], + ['latency', 'latency'], + ['errors', 'errors'], + ['error', 'errors'], + ['failures', 'errors'], + ['count', 'errors'], + ['total', 'errors'], + ['bytes', 'saturation'], + ['usage', 'saturation'], + ['utilization', 'saturation'], + ['used', 'saturation'], + ['load1', 'saturation'], + ['load5', 'saturation'], + ['load15', 'saturation'], + ['lag', 'queue'], + ['queue', 'queue'], + ['depth', 'queue'], + ['backlog', 'queue'], +]; + +function nameTokens(name: string): string[] { + return name.split(/[._]/).filter(Boolean); +} + +function isCpuTime(name: string): boolean { + return nameTokens(name).some(t => t.toLowerCase() === 'cpu'); +} + +function classifyByUnit( + unit: string, + name: string, +): MetricQuantity | undefined { + if (unit.endsWith('/s')) return 'throughput'; + if (unit === '%') return 'saturation'; + if (BYTE_UNITS.test(unit)) return 'saturation'; + // CPU time is a resource footprint, not how long something took. + if (TIME_UNITS.has(unit)) return isCpuTime(name) ? 'saturation' : 'latency'; + return undefined; +} + +function classifyByName(name: string): MetricQuantity | undefined { + const tokens = nameTokens(name.toLowerCase()); + const lastTwo = tokens.slice(-2).join('_'); + const last = tokens[tokens.length - 1] ?? ''; + for (const [tail, quantity] of NAME_TAILS) { + if (lastTwo === tail || last === tail) { + if (tail === 'seconds_total' && isCpuTime(name)) return 'saturation'; + return quantity; + } + } + return undefined; +} + +/** + * Band a metric by what it measures. Four signals in order, each less + * trustworthy than the one before: the declared unit, the tail of the name + * (the head only says who emitted it), the kind, then an honest + * "Unclassified" rather than a confident guess. + */ +export function classifyMetric({ + name, + type, + unit, +}: { + name: string; + type: MetricsDataType; + unit?: string; +}): MetricClassification { + const byUnit = unit ? classifyByUnit(unit, name) : undefined; + if (byUnit) return { quantity: byUnit, reason: 'unit' }; + + const byName = classifyByName(name); + if (byName) return { quantity: byName, reason: 'name' }; + + // A dimensionless sum is a counter whatever it is called. With no declared + // unit at all there is nothing to say it was meant as one. + if (unit && type === MetricsDataType.Sum) { + return { quantity: 'errors', reason: 'kind' }; + } + return { quantity: 'unclassified', reason: 'fallback' }; +} diff --git a/packages/app/src/components/MetricWall/editAsChart.ts b/packages/app/src/components/MetricWall/editAsChart.ts new file mode 100644 index 0000000000..b90ba3a833 --- /dev/null +++ b/packages/app/src/components/MetricWall/editAsChart.ts @@ -0,0 +1,46 @@ +import { + createEmptyExploreSeries, + ExploreSeries, + SearchAggConfig, +} from '@/components/Search/SearchAggControls'; + +import { + attributeClauseSql, + attributeGroupBySql, + MetricFilterClause, + TileAgg, +} from './tileDefaults'; +import type { WallMetric } from './useMetricWallCatalog'; + +export type EditAsChartRequest = { + metric: WallMetric; + agg: TileAgg; + filters: MetricFilterClause[]; + splitBy?: string; +}; + +/** + * Carry a drilled-down tile into the chart editor as the same query: one + * series with the tile's aggregation and scope, split the same way. + */ +export function aggConfigFromWall({ + metric, + agg, + filters, + splitBy, +}: EditAsChartRequest): Pick { + const series: ExploreSeries = { + ...createEmptyExploreSeries(), + aggFn: agg.aggFn, + ...(agg.level != null ? { level: agg.level } : {}), + aggCondition: filters.map(attributeClauseSql).join(' AND '), + aggConditionLanguage: 'sql', + valueExpression: 'Value', + metricName: metric.name, + metricType: metric.type, + }; + return { + series: [series], + groupBy: splitBy ? attributeGroupBySql(splitBy) : '', + }; +} diff --git a/packages/app/src/components/MetricWall/parseMetricQuery.ts b/packages/app/src/components/MetricWall/parseMetricQuery.ts new file mode 100644 index 0000000000..fb98865bd6 --- /dev/null +++ b/packages/app/src/components/MetricWall/parseMetricQuery.ts @@ -0,0 +1,169 @@ +import { MetricsDataType } from '@hyperdx/common-utils/dist/types'; + +import { METRIC_QUANTITIES, MetricQuantity } from './classifyMetric'; + +export type MetricQueryToken = + | { type: 'unit'; value: string } + | { type: 'has'; key: string } + | { type: 'attr'; key: string; value: string } + | { type: 'quantity'; value: MetricQuantity } + | { type: 'kind'; value: MetricsDataType }; + +export type ParsedMetricQuery = { + /** Free text, matched against the metric name and description. */ + text: string; + tokens: MetricQueryToken[]; +}; + +function isMetricKind(value: string): value is MetricsDataType { + return (Object.values(MetricsDataType) as string[]).includes(value); +} + +function isMetricQuantity(value: string): value is MetricQuantity { + return (METRIC_QUANTITIES as readonly string[]).includes(value); +} + +// Words, `key="quoted value"`, or `"quoted text"`. +const WORD = /(?:[^\s"]+="[^"]*"|"[^"]*"|\S+)/g; + +function unquote(value: string): string { + return value.length >= 2 && value.startsWith('"') && value.endsWith('"') + ? value.slice(1, -1) + : value; +} + +function parseWord(word: string): MetricQueryToken | undefined { + const prefixed = /^(unit|has|quantity|kind):(.+)$/.exec(word); + if (prefixed) { + const [, prefix, raw] = prefixed; + const value = unquote(raw); + if (prefix === 'unit') return { type: 'unit', value }; + if (prefix === 'has') return { type: 'has', key: value }; + if (prefix === 'quantity' && isMetricQuantity(value)) { + return { type: 'quantity', value }; + } + if (prefix === 'kind' && isMetricKind(value)) { + return { type: 'kind', value }; + } + return undefined; + } + const eq = word.indexOf('='); + if (eq > 0 && eq < word.length - 1) { + return { + type: 'attr', + key: word.slice(0, eq), + value: unquote(word.slice(eq + 1)), + }; + } + return undefined; +} + +/** + * One box, two jobs: free text finds metrics by name and description, while + * `unit:ms`, `has:http.route`, `service.name=api`, `quantity:latency` and + * `kind:gauge` narrow the wall semantically. + */ +export function parseMetricQuery(query: string): ParsedMetricQuery { + const text: string[] = []; + const tokens: MetricQueryToken[] = []; + for (const word of query.match(WORD) ?? []) { + const token = parseWord(word); + if (token) tokens.push(token); + else text.push(unquote(word)); + } + return { text: text.join(' '), tokens }; +} + +function quoteIfNeeded(value: string): string { + return /\s/.test(value) ? `"${value}"` : value; +} + +export function formatMetricQueryToken(token: MetricQueryToken): string { + switch (token.type) { + case 'unit': + return `unit:${quoteIfNeeded(token.value)}`; + case 'has': + return `has:${quoteIfNeeded(token.key)}`; + case 'attr': + return `${token.key}=${quoteIfNeeded(token.value)}`; + case 'quantity': + return `quantity:${token.value}`; + case 'kind': + return `kind:${token.value}`; + } +} + +export function formatMetricQuery({ text, tokens }: ParsedMetricQuery) { + return [...tokens.map(formatMetricQueryToken), text.trim()] + .filter(Boolean) + .join(' '); +} + +function sameToken(a: MetricQueryToken, b: MetricQueryToken): boolean { + return formatMetricQueryToken(a) === formatMetricQueryToken(b); +} + +export function hasMetricQueryToken( + query: string, + token: MetricQueryToken, +): boolean { + return parseMetricQuery(query).tokens.some(t => sameToken(t, token)); +} + +/** Add the token, or take it away if it is already there. */ +export function toggleMetricQueryToken( + query: string, + token: MetricQueryToken, +): string { + const parsed = parseMetricQuery(query); + const without = parsed.tokens.filter(t => !sameToken(t, token)); + return formatMetricQuery({ + text: parsed.text, + tokens: + without.length === parsed.tokens.length + ? [...parsed.tokens, token] + : without, + }); +} + +export type MatchableMetric = { + name: string; + type: MetricsDataType; + unit?: string; + description?: string; + quantity: MetricQuantity; + hasKey: (key: string) => boolean; +}; + +/** + * Whether a metric belongs on the narrowed wall. An attribute value can only + * be checked by querying, so `key=value` narrows to metrics carrying the key + * and the tiles apply the value. + */ +export function matchesMetricQuery( + metric: MatchableMetric, + { text, tokens }: ParsedMetricQuery, +): boolean { + const needle = text.trim().toLowerCase(); + if ( + needle && + !metric.name.toLowerCase().includes(needle) && + !metric.description?.toLowerCase().includes(needle) + ) { + return false; + } + return tokens.every(token => { + switch (token.type) { + case 'unit': + return metric.unit === token.value; + case 'has': + return metric.hasKey(token.key); + case 'attr': + return metric.hasKey(token.key); + case 'quantity': + return metric.quantity === token.value; + case 'kind': + return metric.type === token.value; + } + }); +} diff --git a/packages/app/src/components/MetricWall/tileDefaults.ts b/packages/app/src/components/MetricWall/tileDefaults.ts new file mode 100644 index 0000000000..7e7825623e --- /dev/null +++ b/packages/app/src/components/MetricWall/tileDefaults.ts @@ -0,0 +1,131 @@ +import { escapeSqlString } from '@hyperdx/common-utils/dist/core/utils'; +import { + AggregateFunction, + BuilderChartConfigWithDateRange, + DisplayType, + Filter, + MetricsDataType, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; + +export type TileAgg = { + id: string; + label: string; + aggFn: AggregateFunction; + level?: number; +}; + +const GAUGE_AGGS: TileAgg[] = [ + { id: 'avg', label: 'avg', aggFn: 'avg' }, + { id: 'min', label: 'min', aggFn: 'min' }, + { id: 'max', label: 'max', aggFn: 'max' }, + { id: 'last', label: 'last', aggFn: 'last_value' }, +]; + +const SUM_AGGS: TileAgg[] = [ + { id: 'increase', label: 'increase', aggFn: 'increase' }, + { id: 'sum', label: 'sum', aggFn: 'sum' }, +]; + +// Histograms only aggregate as quantiles or a count. +const HISTOGRAM_AGGS: TileAgg[] = [ + { id: 'p95', label: 'p95', aggFn: 'quantile', level: 0.95 }, + { id: 'p50', label: 'p50', aggFn: 'quantile', level: 0.5 }, + { id: 'p99', label: 'p99', aggFn: 'quantile', level: 0.99 }, + { id: 'count', label: 'count', aggFn: 'count' }, +]; + +/** The aggregations a tile of this kind offers, the default first. */ +export function tileAggOptions(type: MetricsDataType): TileAgg[] { + switch (type) { + case MetricsDataType.Sum: + return SUM_AGGS; + case MetricsDataType.Histogram: + case MetricsDataType.ExponentialHistogram: + return HISTOGRAM_AGGS; + default: + return GAUGE_AGGS; + } +} + +export function defaultTileAgg(type: MetricsDataType): TileAgg { + return tileAggOptions(type)[0]; +} + +/** Resolve an agg id for this kind, falling back to the kind's default. */ +export function resolveTileAgg( + type: MetricsDataType, + aggId: string | undefined, +): TileAgg { + return tileAggOptions(type).find(a => a.id === aggId) ?? defaultTileAgg(type); +} + +export type MetricFilterClause = { key: string; value: string }; + +function quoteSql(value: string): string { + return `'${escapeSqlString(value)}'`; +} + +/** + * An attribute match that works whether the key lives on the resource or the + * data point, since the wall does not ask the reader to know which. + */ +export function attributeClauseSql({ key, value }: MetricFilterClause) { + const k = quoteSql(key); + const v = quoteSql(value); + return `(ResourceAttributes[${k}] = ${v} OR Attributes[${k}] = ${v})`; +} + +/** Split-by expression for a key, from either attribute map. */ +export function attributeGroupBySql(key: string): string { + const k = quoteSql(key); + return `if(ResourceAttributes[${k}] != '', ResourceAttributes[${k}], Attributes[${k}])`; +} + +export function buildTileConfig({ + source, + metricName, + metricType, + agg, + filters, + searchFilters = [], + dateRange, + groupBy, +}: { + source: TMetricSource; + metricName: string; + metricType: MetricsDataType; + agg: TileAgg; + filters: MetricFilterClause[]; + /** The page's own search box and pills, which narrow every tile too. */ + searchFilters?: Filter[]; + dateRange: [Date, Date]; + groupBy?: string; +}): BuilderChartConfigWithDateRange { + return { + source: source.id, + connection: source.connection, + from: { databaseName: source.from.databaseName, tableName: '' }, + metricTables: source.metricTables, + timestampValueExpression: source.timestampValueExpression, + where: filters.map(attributeClauseSql).join(' AND '), + whereLanguage: 'sql', + filters: searchFilters, + select: [ + { + aggFn: agg.aggFn, + ...(agg.level != null ? { level: agg.level } : {}), + aggCondition: '', + aggConditionLanguage: 'sql', + valueExpression: 'Value', + metricName, + metricType, + }, + ], + ...(groupBy ? { groupBy: attributeGroupBySql(groupBy) } : {}), + granularity: 'auto', + dateRange, + displayType: DisplayType.Line, + dateRangeEndInclusive: true, + }; +} diff --git a/packages/app/src/components/MetricWall/useMetricAttributeStats.ts b/packages/app/src/components/MetricWall/useMetricAttributeStats.ts new file mode 100644 index 0000000000..cba3113e30 --- /dev/null +++ b/packages/app/src/components/MetricWall/useMetricAttributeStats.ts @@ -0,0 +1,178 @@ +import { subHours } from 'date-fns'; +import { + ChSql, + chSql, + ResponseJSON, + tableExpr, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { + MetricsDataType, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { useQuery } from '@tanstack/react-query'; + +import { getClickhouseClient } from '@/clickhouse'; +import { useMetadataQueryAttribution } from '@/queryAttribution'; +import { getMetricTableName } from '@/utils'; +import { QUERYABLE_KINDS } from '@/utils/metricKinds'; + +const MAX_EXEC_SECONDS = 8; +const MAX_VALUES = 20; +const MAX_SPLIT_KEYS = 30; +const WINDOW_HOURS = 1; + +function recentWindow(dateRange: [Date, Date]): [Date, Date] { + const end = dateRange[1]; + const start = subHours(end, WINDOW_HOURS); + return [start > dateRange[0] ? start : dateRange[0], end]; +} + +function timeFilter(source: TMetricSource, [start, end]: [Date, Date]) { + const ts = source.timestampValueExpression || 'TimeUnix'; + return chSql`${{ Identifier: ts }} >= fromUnixTimestamp64Milli(${{ Int64: start.getTime() }}) + AND ${{ Identifier: ts }} <= fromUnixTimestamp64Milli(${{ Int64: end.getTime() }})`; +} + +async function runQuery( + source: TMetricSource, + sql: ChSql, + signal: AbortSignal, + attribution: ReturnType, +): Promise { + const result = (await getClickhouseClient({ attribution }) + .query<'JSON'>({ + query: sql.sql, + query_params: sql.params, + format: 'JSON', + abort_signal: signal, + connectionId: source.connection, + clickhouse_settings: { + max_execution_time: MAX_EXEC_SECONDS, + timeout_overflow_mode: 'break', + }, + }) + .then(res => res.json())) as ResponseJSON; + return result?.data ?? []; +} + +const valueExpr = (key: string) => + chSql`if(ResourceAttributes[${{ String: key }}] != '', ResourceAttributes[${{ String: key }}], Attributes[${{ String: key }}])`; + +/** + * The values of one attribute across every metric, with how many metrics + * report each. Only loads for the attribute the rail has open. + */ +export function useAttributeValueCounts({ + source, + attributeKey, + dateRange, +}: { + source: TMetricSource; + attributeKey?: string; + dateRange: [Date, Date]; +}) { + const range = recentWindow(dateRange); + const attribution = useMetadataQueryAttribution(); + return useQuery({ + queryKey: [ + 'metric-wall-attribute-values', + source.id, + attributeKey, + range[0].getTime(), + range[1].getTime(), + ], + queryFn: async ({ signal }) => { + const counts = new Map(); + const settled = await Promise.allSettled( + QUERYABLE_KINDS.flatMap(kind => { + const table = source.metricTables?.[kind]; + if (!table || !attributeKey) return []; + const value = valueExpr(attributeKey); + return [ + runQuery<{ value: string; metrics: string }>( + source, + chSql` + SELECT ${value} AS value, uniq(MetricName) AS metrics + FROM ${tableExpr({ database: source.from.databaseName, table })} + WHERE ${timeFilter(source, range)} AND ${value} != '' + GROUP BY value + ORDER BY metrics DESC + LIMIT ${{ Int32: MAX_VALUES }} + `, + signal, + attribution, + ), + ]; + }), + ); + for (const result of settled) { + if (result.status !== 'fulfilled') continue; + for (const row of result.value) { + counts.set( + row.value, + (counts.get(row.value) ?? 0) + Number(row.metrics), + ); + } + } + return Array.from(counts, ([value, count]) => ({ value, count })) + .sort((a, b) => b.count - a.count) + .slice(0, MAX_VALUES); + }, + enabled: !!attributeKey, + staleTime: 1000 * 60 * 5, + }); +} + +/** + * Every attribute one metric carries, with its distinct value count, so the + * split chips can say how many series each one would draw. + */ +export function useSplitCardinality({ + source, + metricName, + metricType, + dateRange, +}: { + source: TMetricSource; + metricName: string; + metricType: MetricsDataType; + dateRange: [Date, Date]; +}) { + const range = recentWindow(dateRange); + const table = getMetricTableName(source, metricType); + const attribution = useMetadataQueryAttribution(); + return useQuery({ + queryKey: [ + 'metric-wall-split-cardinality', + source.id, + metricType, + metricName, + range[0].getTime(), + range[1].getTime(), + ], + queryFn: async ({ signal }) => { + const rows = await runQuery<{ key: string; n: string }>( + source, + chSql` + SELECT kv.1 AS key, uniq(kv.2) AS n + FROM ${tableExpr({ database: source.from.databaseName, table: table ?? '' })} + ARRAY JOIN arrayConcat( + arrayZip(mapKeys(ResourceAttributes), mapValues(ResourceAttributes)), + arrayZip(mapKeys(Attributes), mapValues(Attributes)) + ) AS kv + WHERE MetricName = ${{ String: metricName }} AND ${timeFilter(source, range)} + GROUP BY key + ORDER BY n ASC + LIMIT ${{ Int32: MAX_SPLIT_KEYS }} + `, + signal, + attribution, + ); + return rows + .map(row => ({ key: row.key, values: Number(row.n) })) + .filter(row => row.values > 1); + }, + enabled: !!table, + staleTime: 1000 * 60 * 5, + }); +} diff --git a/packages/app/src/components/MetricWall/useMetricBrowse.ts b/packages/app/src/components/MetricWall/useMetricBrowse.ts new file mode 100644 index 0000000000..d77e63f163 --- /dev/null +++ b/packages/app/src/components/MetricWall/useMetricBrowse.ts @@ -0,0 +1,57 @@ +import { useMemo } from 'react'; +import { TMetricSource } from '@hyperdx/common-utils/dist/types'; + +import { parseMetricQuery } from './parseMetricQuery'; +import { MetricFilterClause } from './tileDefaults'; +import { useMetricWallCatalog } from './useMetricWallCatalog'; +import { useMetricWallState } from './useMetricWallState'; +import { groupWallMetrics } from './wallGrouping'; + +/** + * The wall and the rail render in different page slots but read the same + * catalog and URL state; the catalog fetch is shared through the query cache. + */ +export function useMetricBrowse({ + source, + dateRange, +}: { + source: TMetricSource; + dateRange: [Date, Date]; +}) { + const [state, update] = useMetricWallState(); + const query = state.query ?? ''; + const grouping = state.grouping ?? 'entity'; + const catalog = useMetricWallCatalog({ source, dateRange }); + + const parsed = useMemo(() => parseMetricQuery(query), [query]); + const sections = useMemo( + () => groupWallMetrics(catalog.metrics, parsed, grouping), + [catalog.metrics, parsed, grouping], + ); + const matching = useMemo(() => { + const ids = new Set(); + for (const s of sections) + for (const b of s.bands) for (const i of b.items) ids.add(i.metric.id); + return catalog.metrics.filter(m => ids.has(m.id)); + }, [sections, catalog.metrics]); + // Attribute values can only be checked by the query, so they ride along on + // every tile rather than narrowing the catalog. + const filters = useMemo( + () => + parsed.tokens.flatMap(t => + t.type === 'attr' ? [{ key: t.key, value: t.value }] : [], + ), + [parsed], + ); + + return { + ...catalog, + state, + update, + query, + grouping, + sections, + matching, + filters, + }; +} diff --git a/packages/app/src/components/MetricWall/useMetricSeries.ts b/packages/app/src/components/MetricWall/useMetricSeries.ts new file mode 100644 index 0000000000..648e9f2574 --- /dev/null +++ b/packages/app/src/components/MetricWall/useMetricSeries.ts @@ -0,0 +1,84 @@ +import { useMemo } from 'react'; +import { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; + +import { + convertToTimeChartConfig, + formatResponseForTimeChart, + useTimeChartSettings, +} from '@/ChartUtils'; +import { sparklinePointsFromGraphResults } from '@/components/NumberTileBackgroundChart'; +import { SparklinePoint } from '@/components/Sparkline'; +import { useQueriedChartConfig } from '@/hooks/useChartConfig'; + +export type MetricSeries = { + key: string; + label: string; + color: string; + points: SparklinePoint[]; + latest?: number; +}; + +/** + * Run a tile's time-series query and hand back each series as sparkline + * points, so one query can feed a single tile or a row of small multiples. + */ +export function useMetricSeries( + config: BuilderChartConfigWithDateRange, + { enabled = true }: { enabled?: boolean } = {}, +) { + const { dateRange, granularity } = useTimeChartSettings(config); + const queriedConfig = useMemo( + () => convertToTimeChartConfig(config), + [config], + ); + const { data, isLoading, isError, error } = useQueriedChartConfig( + queriedConfig, + { + enabled, + placeholderData: prev => prev, + queryKey: ['metric-wall-series', queriedConfig], + }, + ); + + const series = useMemo(() => { + if (data == null) return []; + try { + const { graphResults, timestampColumn, lineData } = + formatResponseForTimeChart({ + currentPeriodResponse: data, + dateRange, + granularity, + generateEmptyBuckets: false, + }); + return lineData.map(line => { + const points = sparklinePointsFromGraphResults( + graphResults, + timestampColumn?.name, + line.dataKey, + ); + return { + key: line.dataKey, + label: line.displayName, + color: line.color, + points, + latest: points[points.length - 1]?.y, + }; + }); + } catch { + return []; + } + }, [data, dateRange, granularity]); + + return { series, isLoading, isError, error }; +} + +const compact = new Intl.NumberFormat(undefined, { + notation: 'compact', + maximumFractionDigits: 1, +}); + +export function formatMetricValue(value: number | undefined, unit?: string) { + if (value == null || !Number.isFinite(value)) return '–'; + const number = compact.format(value); + return unit && unit !== '1' ? `${number} ${unit}` : number; +} diff --git a/packages/app/src/components/MetricWall/useMetricWallCatalog.ts b/packages/app/src/components/MetricWall/useMetricWallCatalog.ts new file mode 100644 index 0000000000..4f86426545 --- /dev/null +++ b/packages/app/src/components/MetricWall/useMetricWallCatalog.ts @@ -0,0 +1,211 @@ +import { useMemo } from 'react'; +import { subHours } from 'date-fns'; +import { + chSql, + ResponseJSON, + tableExpr, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { + MetricsDataType, + SourceKind, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { useQuery } from '@tanstack/react-query'; + +import { getClickhouseClient } from '@/clickhouse'; +import { useMetadataWithSettings } from '@/hooks/useMetadata'; +import { useMetricCatalog } from '@/hooks/useMetricCatalog'; +import { useMetadataQueryAttribution } from '@/queryAttribution'; +import { QUERYABLE_KINDS } from '@/utils/metricKinds'; + +import { classifyMetric, MetricClassification } from './classifyMetric'; +import { MetricFilterClause } from './tileDefaults'; + +/** Resource attributes that name who emitted a metric, in order of preference. */ +export const ENTITY_KEYS = ['service.name', 'host.name'] as const; + +const MAX_METRICS_PER_KIND = 3000; +const MAX_ENTITIES_PER_METRIC = 50; +const MAX_KEYS_PER_METRIC = 200; +const MAX_EXEC_SECONDS = 8; +// Entities and attribute keys come from recent data only: reading every map +// over the catalog's multi-day window would be the most expensive query on +// the page, and what is reporting now is what the wall is about. +const STATS_WINDOW_HOURS = 1; + +export type WallMetric = { + id: string; + name: string; + type: MetricsDataType; + unit?: string; + description?: string; + classification: MetricClassification; + keys: Set; + /** Who reports this metric; empty when the source carries no entity keys. */ + entities: MetricFilterClause[]; +}; + +export function wallMetricId(type: MetricsDataType, name: string) { + return `${type}:${name}`; +} + +type StatsRow = { MetricName: string; entities: string[]; keys: string[] }; +type MetricStats = { entities: MetricFilterClause[]; keys: string[] }; + +function parseEntity(encoded: string): MetricFilterClause | undefined { + const eq = encoded.indexOf('='); + if (eq <= 0) return undefined; + return { key: encoded.slice(0, eq), value: encoded.slice(eq + 1) }; +} + +function useMetricWallStats({ + source, + dateRange, +}: { + source: TMetricSource; + dateRange: [Date, Date]; +}) { + const metadata = useMetadataWithSettings(); + const attribution = useMetadataQueryAttribution(); + const databaseName = source.from.databaseName; + const connectionId = source.connection; + const timestampExpression = source.timestampValueExpression || 'TimeUnix'; + const tables = QUERYABLE_KINDS.flatMap(kind => { + const tableName = source.metricTables?.[kind]; + return tableName ? [{ kind, tableName }] : []; + }); + const end = dateRange[1]; + const start = new Date( + Math.max( + dateRange[0].getTime(), + subHours(end, STATS_WINDOW_HOURS).getTime(), + ), + ); + + return useQuery({ + queryKey: [ + 'useMetricWallStats', + connectionId, + databaseName, + tables, + timestampExpression, + start.getTime(), + end.getTime(), + ], + queryFn: async ({ signal }) => { + const client = getClickhouseClient({ attribution }); + const stats = new Map(); + await Promise.allSettled( + tables.map(async ({ kind, tableName }) => { + const columns = await metadata.getColumns({ + databaseName, + tableName, + connectionId, + }); + const names = new Set(columns.map(c => c.name)); + if (!names.has('ResourceAttributes') || !names.has('Attributes')) { + return; + } + // Aggregate parameters must be literals, so the limits are inlined. + const sql = chSql` + SELECT + MetricName, + groupUniqArray(${String(MAX_ENTITIES_PER_METRIC)})(multiIf( + ResourceAttributes['service.name'] != '', concat('service.name=', ResourceAttributes['service.name']), + ResourceAttributes['host.name'] != '', concat('host.name=', ResourceAttributes['host.name']), + '' + )) AS entities, + groupUniqArrayArray(${String(MAX_KEYS_PER_METRIC)})( + arrayConcat(mapKeys(ResourceAttributes), mapKeys(Attributes)) + ) AS keys + FROM ${tableExpr({ database: databaseName, table: tableName })} + WHERE ${{ Identifier: timestampExpression }} >= fromUnixTimestamp64Milli(${{ Int64: start.getTime() }}) + AND ${{ Identifier: timestampExpression }} <= fromUnixTimestamp64Milli(${{ Int64: end.getTime() }}) + GROUP BY MetricName + LIMIT ${{ Int32: MAX_METRICS_PER_KIND }} + `; + const result = (await client + .query<'JSON'>({ + query: sql.sql, + query_params: sql.params, + format: 'JSON', + abort_signal: signal, + connectionId, + clickhouse_settings: { + max_execution_time: MAX_EXEC_SECONDS, + timeout_overflow_mode: 'break', + }, + }) + .then(res => res.json())) as ResponseJSON; + for (const row of result?.data ?? []) { + stats.set(wallMetricId(kind, row.MetricName), { + entities: row.entities.flatMap(e => parseEntity(e) ?? []), + keys: row.keys, + }); + } + }), + ); + return stats; + }, + enabled: + source.kind === SourceKind.Metric && !!databaseName && tables.length > 0, + staleTime: 1000 * 60 * 5, + }); +} + +/** + * Everything the wall is built from: each metric the source reports, how it + * is classified, who reports it, and which attribute keys it carries. + */ +export function useMetricWallCatalog({ + source, + dateRange, +}: { + source: TMetricSource; + dateRange: [Date, Date]; +}) { + const catalog = useMetricCatalog({ source, dateRange }); + const stats = useMetricWallStats({ source, dateRange }); + + const metrics = useMemo( + () => + catalog.entries.map(entry => { + const id = wallMetricId(entry.type, entry.name); + const entryStats = stats.data?.get(id); + return { + id, + name: entry.name, + type: entry.type, + unit: entry.unit, + description: entry.description, + classification: classifyMetric(entry), + keys: new Set(entryStats?.keys ?? []), + entities: entryStats?.entities ?? [], + }; + }), + [catalog.entries, stats.data], + ); + + return { + metrics, + failedKinds: catalog.failedKinds, + isLoading: catalog.isLoading, + isLoadingStats: stats.isLoading, + error: catalog.error, + }; +} + +/** How many metrics carry each attribute key, most first. */ +export function rankAttributeKeys( + metrics: WallMetric[], +): { key: string; count: number }[] { + const counts = new Map(); + for (const metric of metrics) { + for (const key of metric.keys) { + counts.set(key, (counts.get(key) ?? 0) + 1); + } + } + return Array.from(counts, ([key, count]) => ({ key, count })).sort( + (a, b) => b.count - a.count || a.key.localeCompare(b.key), + ); +} diff --git a/packages/app/src/components/MetricWall/useMetricWallState.ts b/packages/app/src/components/MetricWall/useMetricWallState.ts new file mode 100644 index 0000000000..bbe4869a67 --- /dev/null +++ b/packages/app/src/components/MetricWall/useMetricWallState.ts @@ -0,0 +1,42 @@ +import { useCallback, useMemo } from 'react'; +import { parseAsJson, useQueryState } from 'nuqs'; + +import { MetricFilterClause } from './tileDefaults'; +import { WallGrouping } from './wallGrouping'; + +export type ExpandedTile = { + metricId: string; + entity?: MetricFilterClause; + /** Aggregation override; only lives as long as the drill-down. */ + agg?: string; + splitBy?: string; + multiples?: boolean; +}; + +export type MetricWallState = { + query?: string; + grouping?: WallGrouping; + /** Attribute whose values the rail is listing. */ + railKey?: string; + tile?: ExpandedTile; +}; + +export function useMetricWallState() { + const [raw, setRaw] = useQueryState( + 'metricWall', + parseAsJson(), + ); + const state = useMemo(() => raw ?? {}, [raw]); + const update = useCallback( + (patch: Partial) => { + setRaw(prev => { + const next = { ...(prev ?? {}), ...patch }; + return Object.values(next).some(v => v != null && v !== '') + ? next + : null; + }); + }, + [setRaw], + ); + return [state, update] as const; +} diff --git a/packages/app/src/components/MetricWall/wallGrouping.ts b/packages/app/src/components/MetricWall/wallGrouping.ts new file mode 100644 index 0000000000..9e13344705 --- /dev/null +++ b/packages/app/src/components/MetricWall/wallGrouping.ts @@ -0,0 +1,129 @@ +import { METRIC_QUANTITIES, MetricQuantity } from './classifyMetric'; +import { + matchesMetricQuery, + MetricQueryToken, + ParsedMetricQuery, +} from './parseMetricQuery'; +import { MetricFilterClause } from './tileDefaults'; +import { ENTITY_KEYS, WallMetric } from './useMetricWallCatalog'; + +export type WallGrouping = 'entity' | 'quantity' | 'flat'; + +export type WallItem = { + /** Unique within the wall: the metric plus who it is scoped to. */ + key: string; + metric: WallMetric; + entity?: MetricFilterClause; +}; + +export type WallBand = { + quantity: MetricQuantity | 'all'; + items: WallItem[]; +}; + +export type WallSection = { + id: string; + entity?: MetricFilterClause; + metricCount: number; + bands: WallBand[]; +}; + +export function wallItemKey(metricId: string, entity?: MetricFilterClause) { + return entity ? `${metricId}|${entity.key}=${entity.value}` : metricId; +} + +function isEntityToken( + token: MetricQueryToken, +): token is Extract { + return ( + token.type === 'attr' && + (ENTITY_KEYS as readonly string[]).includes(token.key) + ); +} + +function band(items: WallItem[]): WallBand[] { + return METRIC_QUANTITIES.flatMap(quantity => { + const inBand = items + .filter(i => i.metric.classification.quantity === quantity) + .sort((a, b) => a.metric.name.localeCompare(b.metric.name)); + return inBand.length > 0 ? [{ quantity, items: inBand }] : []; + }); +} + +/** + * Lay the matching metrics out as the wall: by entity then quantity band, by + * band alone, or one flat list. Entity filters in the query pick sections, + * since the wall already knows who reports what. + */ +export function groupWallMetrics( + metrics: WallMetric[], + query: ParsedMetricQuery, + grouping: WallGrouping, +): WallSection[] { + const matching = metrics.filter(metric => + matchesMetricQuery( + { + ...metric, + quantity: metric.classification.quantity, + hasKey: key => + metric.keys.has(key) || metric.entities.some(e => e.key === key), + }, + query, + ), + ); + + if (grouping === 'flat') { + const items = matching + .map(metric => ({ key: metric.id, metric })) + .sort((a, b) => a.metric.name.localeCompare(b.metric.name)); + return items.length > 0 + ? [ + { + id: 'all', + metricCount: items.length, + bands: [{ quantity: 'all', items }], + }, + ] + : []; + } + + if (grouping === 'quantity') { + const bands = band(matching.map(metric => ({ key: metric.id, metric }))); + return bands.length > 0 + ? [{ id: 'all', metricCount: matching.length, bands }] + : []; + } + + const entityTokens = query.tokens.filter(isEntityToken); + const byEntity = new Map(); + const unscoped: WallItem[] = []; + for (const metric of matching) { + const entities = metric.entities.filter(entity => + entityTokens.every(t => t.key !== entity.key || t.value === entity.value), + ); + if (metric.entities.length === 0 && entityTokens.length === 0) { + unscoped.push({ key: metric.id, metric }); + } + for (const entity of entities) { + const id = `${entity.key}=${entity.value}`; + const list = byEntity.get(id) ?? []; + list.push({ key: wallItemKey(metric.id, entity), metric, entity }); + byEntity.set(id, list); + } + } + + const sections: WallSection[] = Array.from(byEntity, ([id, items]) => ({ + id, + entity: items[0].entity, + metricCount: items.length, + bands: band(items), + })).sort((a, b) => b.metricCount - a.metricCount || a.id.localeCompare(b.id)); + if (unscoped.length > 0) { + sections.push({ + id: 'unscoped', + metricCount: unscoped.length, + bands: band(unscoped), + }); + } + return sections; +} diff --git a/packages/app/src/components/PatternTable.tsx b/packages/app/src/components/PatternTable.tsx index bbecce94ab..d012342d82 100644 --- a/packages/app/src/components/PatternTable.tsx +++ b/packages/app/src/components/PatternTable.tsx @@ -13,10 +13,8 @@ import { useSearchTotalCount } from '@/components/SearchTotalCountChart'; import { Pattern, useGroupedPatterns } from '@/hooks/usePatterns'; import { getLevelExpression } from '@/source'; -import { - buildPatternColumnExpression, - PatternColumnSelector, -} from './Patterns/PatternColumnSelector'; +import { buildPatternColumnExpression } from './Patterns/patternColumn'; +import { PatternColumnSelector } from './Patterns/PatternColumnSelector'; import PatternSidePanel from './PatternSidePanel'; const emptyMap = new Map(); diff --git a/packages/app/src/components/Patterns/PatternColumnSelector.tsx b/packages/app/src/components/Patterns/PatternColumnSelector.tsx index b5ac47cf39..31a033500b 100644 --- a/packages/app/src/components/Patterns/PatternColumnSelector.tsx +++ b/packages/app/src/components/Patterns/PatternColumnSelector.tsx @@ -4,17 +4,6 @@ import { Box } from '@mantine/core'; import SQLInlineEditor from '@/components/SQLEditor/SQLInlineEditor'; import { useSource } from '@/source'; -export function buildPatternColumnExpression({ - patternColumn, - fallback, -}: { - patternColumn: string | null | undefined; - fallback: string; -}): string { - if (!patternColumn) return fallback; - return `toString(${patternColumn})`; -} - export function PatternColumnSelector({ sourceId, value, diff --git a/packages/app/src/components/Patterns/PatternTemplate.module.scss b/packages/app/src/components/Patterns/PatternTemplate.module.scss new file mode 100644 index 0000000000..f1d4b3d8e1 --- /dev/null +++ b/packages/app/src/components/Patterns/PatternTemplate.module.scss @@ -0,0 +1,10 @@ +.slot { + display: inline; + white-space: nowrap; + margin: 0 0.1em; + padding: 0 0.2em; + border-radius: 3px; + background: var(--color-bg-info-subtle); + color: var(--color-text-info); + font-weight: 600; +} diff --git a/packages/app/src/components/Patterns/PatternTemplate.tsx b/packages/app/src/components/Patterns/PatternTemplate.tsx new file mode 100644 index 0000000000..4360960060 --- /dev/null +++ b/packages/app/src/components/Patterns/PatternTemplate.tsx @@ -0,0 +1,55 @@ +import { Fragment } from 'react'; + +import styles from './PatternTemplate.module.scss'; + +export const PATTERN_WILDCARD = '<*>'; + +export type PatternTemplatePart = + | { type: 'text'; value: string; at: number } + | { type: 'slot'; at: number }; + +/** Split a Drain template so the UI can paint `<*>` as a hole, not more log text. */ +export function splitPatternTemplate(text: string): PatternTemplatePart[] { + if (!text.includes(PATTERN_WILDCARD)) { + return text ? [{ type: 'text', value: text, at: 0 }] : []; + } + const parts: PatternTemplatePart[] = []; + const chunks = text.split(PATTERN_WILDCARD); + let at = 0; + chunks.forEach((chunk, i) => { + if (chunk) { + parts.push({ type: 'text', value: chunk, at }); + at += chunk.length; + } + if (i < chunks.length - 1) { + parts.push({ type: 'slot', at }); + at += PATTERN_WILDCARD.length; + } + }); + return parts; +} + +/** Drain template with varying tokens drawn as holes, so the stable shape can be read. */ +export function PatternTemplate({ text }: { text: string }) { + const parts = splitPatternTemplate(text); + if (parts.length === 0) { + return null; + } + return ( + + {parts.map(part => + part.type === 'slot' ? ( + + {PATTERN_WILDCARD} + + ) : ( + {part.value} + ), + )} + + ); +} diff --git a/packages/app/src/components/Patterns/__tests__/PatternColumnSelector.test.tsx b/packages/app/src/components/Patterns/__tests__/PatternColumnSelector.test.tsx index 1956329772..7330ae6471 100644 --- a/packages/app/src/components/Patterns/__tests__/PatternColumnSelector.test.tsx +++ b/packages/app/src/components/Patterns/__tests__/PatternColumnSelector.test.tsx @@ -1,42 +1,83 @@ -import { buildPatternColumnExpression } from '@/components/Patterns/PatternColumnSelector'; - -describe('buildPatternColumnExpression', () => { - const fallback = 'Body'; - - it('returns the fallback when no expression is provided', () => { - expect( - buildPatternColumnExpression({ patternColumn: null, fallback }), - ).toBe(fallback); - expect( - buildPatternColumnExpression({ patternColumn: undefined, fallback }), - ).toBe(fallback); - expect(buildPatternColumnExpression({ patternColumn: '', fallback })).toBe( - fallback, +import { JSDataType } from '@hyperdx/common-utils/dist/clickhouse'; +import { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import { PatternColumnSelector } from '@/components/Explore/ExplorePatternColumnSelector'; +import { useMultipleAllFields } from '@/hooks/useMetadata'; + +jest.mock('@/hooks/useMetadata', () => ({ + useMultipleAllFields: jest.fn(() => ({ data: [] })), +})); + +const mockedUseMultipleAllFields = useMultipleAllFields as jest.Mock; + +describe('PatternColumnSelector', () => { + beforeEach(() => { + mockedUseMultipleAllFields.mockReturnValue({ + data: [ + { path: ['Body'], type: 'String', jsType: JSDataType.String }, + { path: ['ServiceName'], type: 'String', jsType: JSDataType.String }, + ], + }); + }); + + it('names the source default on the trigger so clustering is not a blank box', () => { + renderWithMantine( + , + ); + + expect(screen.getByTestId('explore-pattern-field')).toHaveTextContent( + 'on Body', + ); + }); + + it('names a field the reader picked', () => { + renderWithMantine( + , + ); + + expect(screen.getByTestId('explore-pattern-field')).toHaveTextContent( + 'on ServiceName', ); }); - it('wraps a plain column reference in toString()', () => { - expect( - buildPatternColumnExpression({ - patternColumn: 'ResourceAttributes', - fallback, - }), - ).toBe('toString(ResourceAttributes)'); + it('applies a field on click, without a SQL editor', async () => { + const user = userEvent.setup(); + const onApply = jest.fn(); + renderWithMantine( + , + ); + + await user.click(screen.getByTestId('explore-pattern-field')); + await user.click(await screen.findByRole('radio', { name: 'ServiceName' })); + + expect(onApply).toHaveBeenCalledWith('ServiceName'); }); - it('wraps an arbitrary SQL expression in toString()', () => { - expect( - buildPatternColumnExpression({ - patternColumn: "concatWithSeparator(' ', Body, LogAttributes)", - fallback, - }), - ).toBe("toString(concatWithSeparator(' ', Body, LogAttributes))"); - - expect( - buildPatternColumnExpression({ - patternColumn: "JSONExtractString(Body, 'message')", - fallback, - }), - ).toBe("toString(JSONExtractString(Body, 'message'))"); + it('stores the default as empty so a later source body change is tracked', async () => { + const user = userEvent.setup(); + const onApply = jest.fn(); + renderWithMantine( + , + ); + + await user.click(screen.getByTestId('explore-pattern-field')); + await user.click( + await screen.findByRole('button', { name: 'Use default' }), + ); + + expect(onApply).toHaveBeenCalledWith(''); }); }); diff --git a/packages/app/src/components/Patterns/__tests__/PatternTemplate.test.ts b/packages/app/src/components/Patterns/__tests__/PatternTemplate.test.ts new file mode 100644 index 0000000000..0f9eecd758 --- /dev/null +++ b/packages/app/src/components/Patterns/__tests__/PatternTemplate.test.ts @@ -0,0 +1,31 @@ +import { + PATTERN_WILDCARD, + splitPatternTemplate, +} from '@/components/Patterns/PatternTemplate'; + +describe('splitPatternTemplate', () => { + it('is a single text part when nothing varied', () => { + expect(splitPatternTemplate('connection refused')).toEqual([ + { type: 'text', value: 'connection refused', at: 0 }, + ]); + }); + + it('marks each Drain placeholder as a slot', () => { + expect(splitPatternTemplate('GET /api/products/<*> HTTP/1.1')).toEqual([ + { type: 'text', value: 'GET /api/products/', at: 0 }, + { type: 'slot', at: 18 }, + { type: 'text', value: ' HTTP/1.1', at: 21 }, + ]); + }); + + it('keeps adjacent slots when two placeholders sit together', () => { + expect(splitPatternTemplate(`<*>${PATTERN_WILDCARD}`)).toEqual([ + { type: 'slot', at: 0 }, + { type: 'slot', at: 3 }, + ]); + }); + + it('is empty for empty input', () => { + expect(splitPatternTemplate('')).toEqual([]); + }); +}); diff --git a/packages/app/src/components/Patterns/__tests__/patternColumn.test.ts b/packages/app/src/components/Patterns/__tests__/patternColumn.test.ts new file mode 100644 index 0000000000..7d327d9e4c --- /dev/null +++ b/packages/app/src/components/Patterns/__tests__/patternColumn.test.ts @@ -0,0 +1,141 @@ +import type { Filter } from '@hyperdx/common-utils/dist/types'; + +import { + andSqlWhere, + buildPatternColumnExpression, + drainTemplateToLikePattern, + nextSearchForPatternMatch, + patternMatchSqlCondition, +} from '@/components/Patterns/patternColumn'; + +describe('buildPatternColumnExpression', () => { + const fallback = 'Body'; + + it('returns the fallback when no expression is provided', () => { + expect( + buildPatternColumnExpression({ patternColumn: null, fallback }), + ).toBe(fallback); + expect( + buildPatternColumnExpression({ patternColumn: undefined, fallback }), + ).toBe(fallback); + expect(buildPatternColumnExpression({ patternColumn: '', fallback })).toBe( + fallback, + ); + }); + + it('wraps a plain column reference in toString()', () => { + expect( + buildPatternColumnExpression({ + patternColumn: 'ResourceAttributes', + fallback, + }), + ).toBe('toString(ResourceAttributes)'); + }); + + it('wraps an arbitrary SQL expression in toString()', () => { + expect( + buildPatternColumnExpression({ + patternColumn: "concatWithSeparator(' ', Body, LogAttributes)", + fallback, + }), + ).toBe("toString(concatWithSeparator(' ', Body, LogAttributes))"); + + expect( + buildPatternColumnExpression({ + patternColumn: "JSONExtractString(Body, 'message')", + fallback, + }), + ).toBe("toString(JSONExtractString(Body, 'message'))"); + }); +}); + +describe('drainTemplateToLikePattern', () => { + it('replaces each Drain placeholder with %', () => { + expect(drainTemplateToLikePattern('POST /users/<*> failed')).toBe( + 'POST /users/% failed', + ); + }); + + it('escapes LIKE wildcards in the literal parts', () => { + expect(drainTemplateToLikePattern('100% done id=_<*>')).toBe( + '100\\% done id=\\_%', + ); + }); + + it('leaves a template with no placeholders as an exact pattern', () => { + expect(drainTemplateToLikePattern('connection refused')).toBe( + 'connection refused', + ); + }); +}); + +describe('patternMatchSqlCondition', () => { + it('builds a LIKE predicate against the clustered expression', () => { + expect(patternMatchSqlCondition('Body', 'POST /users/<*> failed')).toBe( + "Body LIKE 'POST /users/% failed'", + ); + }); + + it('SQL-escapes quotes in the template', () => { + expect(patternMatchSqlCondition('Body', "can't <*>")).toBe( + "Body LIKE 'can''t %'", + ); + }); + + it('skips an empty template or expression', () => { + expect(patternMatchSqlCondition('Body', '')).toBeNull(); + expect(patternMatchSqlCondition('', 'foo <*>')).toBeNull(); + }); + + it('skips a template that is only wildcards', () => { + expect(patternMatchSqlCondition('Body', '<*>')).toBeNull(); + expect(patternMatchSqlCondition('Body', '<*><*>')).toBeNull(); + }); +}); + +describe('andSqlWhere', () => { + it('returns the extra clause when nothing was typed', () => { + expect(andSqlWhere('', "Body LIKE 'a%'")).toBe("Body LIKE 'a%'"); + }); + + it('ANDs onto an existing WHERE', () => { + expect(andSqlWhere("ServiceName = 'api'", "Body LIKE 'a%'")).toBe( + "(ServiceName = 'api') AND (Body LIKE 'a%')", + ); + }); +}); + +describe('nextSearchForPatternMatch', () => { + const sqlCondition = "Body LIKE 'POST /users/% failed'"; + const filters: Filter[] = [ + { type: 'sql', condition: "ServiceName IN ('api')" }, + ]; + + it('folds the LIKE into a SQL WHERE', () => { + expect( + nextSearchForPatternMatch({ + where: "SeverityText = 'error'", + whereLanguage: 'sql', + filters, + sqlCondition, + }), + ).toEqual({ + where: "(SeverityText = 'error') AND (Body LIKE 'POST /users/% failed')", + filters, + }); + }); + + it('keeps Lucene WHERE and appends a SQL filter instead', () => { + expect( + nextSearchForPatternMatch({ + where: 'level:error', + whereLanguage: 'lucene', + filters, + sqlCondition, + }), + ).toEqual({ + where: 'level:error', + filters: [...filters, { type: 'sql', condition: sqlCondition }], + }); + }); +}); diff --git a/packages/app/src/components/Patterns/patternColumn.ts b/packages/app/src/components/Patterns/patternColumn.ts new file mode 100644 index 0000000000..9241a9738d --- /dev/null +++ b/packages/app/src/components/Patterns/patternColumn.ts @@ -0,0 +1,77 @@ +import { escapeSqlString } from '@hyperdx/common-utils/dist/core/utils'; +import type { Filter } from '@hyperdx/common-utils/dist/types'; + +/** Drain3's placeholder for a token that varied across clustered events. */ +const DRAIN_WILDCARD = '<*>'; + +export function buildPatternColumnExpression({ + patternColumn, + fallback, +}: { + patternColumn: string | null | undefined; + fallback: string; +}): string { + if (!patternColumn) return fallback; + return `toString(${patternColumn})`; +} + +/** + * Turn a Drain template into a ClickHouse LIKE pattern. Literal `%`, `_` and + * `\` are escaped so they cannot act as wildcards; each `<*>` becomes `%`. + */ +export function drainTemplateToLikePattern(template: string): string { + return template.split(DRAIN_WILDCARD).map(escapeLikeLiteral).join('%'); +} + +function escapeLikeLiteral(value: string): string { + return value + .replaceAll('\\', '\\\\') + .replaceAll('%', '\\%') + .replaceAll('_', '\\_'); +} + +export function patternMatchSqlCondition( + bodyValueExpression: string, + template: string, +): string | null { + const expr = bodyValueExpression.trim(); + const pattern = template.trim(); + if (!expr || !pattern) return null; + const like = drainTemplateToLikePattern(pattern); + // A template that is only wildcards would match every row. + if (like.replaceAll('%', '') === '') return null; + return `${expr} LIKE '${escapeSqlString(like)}'`; +} + +export function andSqlWhere(existing: string, extra: string): string { + const a = existing.trim(); + const b = extra.trim(); + if (!a) return b; + if (!b) return a; + return `(${a}) AND (${b})`; +} + +/** + * Fold a pattern-match predicate into the current search. SQL WHERE is the + * typed box, so a LIKE belongs there. Lucene WHERE cannot carry it, so it + * rides along as an extra SQL filter instead of replacing the Lucene text. + */ +export function nextSearchForPatternMatch({ + where, + whereLanguage, + filters, + sqlCondition, +}: { + where: string; + whereLanguage?: 'sql' | 'lucene'; + filters: Filter[]; + sqlCondition: string; +}): { where: string; filters: Filter[] } { + if (whereLanguage === 'lucene') { + return { + where, + filters: [...filters, { type: 'sql', condition: sqlCondition }], + }; + } + return { where: andSqlWhere(where, sqlCondition), filters }; +} diff --git a/packages/app/src/components/SavedSearches/SavedSearchesFlyout.module.scss b/packages/app/src/components/SavedSearches/SavedSearchesFlyout.module.scss new file mode 100644 index 0000000000..b10af11678 --- /dev/null +++ b/packages/app/src/components/SavedSearches/SavedSearchesFlyout.module.scss @@ -0,0 +1,29 @@ +.row { + border-radius: 4px; + + &:hover { + background: var(--color-bg-sidenav-link-active); + } +} + +.rowLink { + display: flex; + align-items: center; + gap: 6px; + flex: 1; + min-width: 0; + padding: 4px 6px; + border-radius: 4px; + color: var(--color-text); + text-decoration: none; + + &:focus-visible { + outline: none; + background: var(--color-bg-sidenav-link-active); + } + + &[data-active] { + color: var(--color-text-sidenav-link-active); + background: var(--color-bg-sidenav-link-active); + } +} diff --git a/packages/app/src/components/SavedSearches/SavedSearchesFlyout.tsx b/packages/app/src/components/SavedSearches/SavedSearchesFlyout.tsx new file mode 100644 index 0000000000..27c1a44708 --- /dev/null +++ b/packages/app/src/components/SavedSearches/SavedSearchesFlyout.tsx @@ -0,0 +1,186 @@ +import { useMemo, useState } from 'react'; +import Link from 'next/link'; +import { SavedSearchListApiResponse } from '@hyperdx/common-utils/dist/types'; +import { + Drawer, + Group, + ScrollArea, + Stack, + Text, + TextInput, + UnstyledButton, +} from '@mantine/core'; +import { IconSearch, IconTable } from '@tabler/icons-react'; + +import { AlertStatusIcon } from '@/components/AlertStatusIcon'; +import EmptyState from '@/components/EmptyState'; +import { FavoriteButton } from '@/components/FavoriteButton'; +import { useFavorites } from '@/favorites'; +import { useSavedSearches } from '@/savedSearch'; + +import classes from './SavedSearchesFlyout.module.scss'; + +function SavedSearchRow({ + savedSearch, + isActive, + onNavigate, + linkPrefix, +}: { + savedSearch: SavedSearchListApiResponse; + isActive: boolean; + onNavigate: () => void; + linkPrefix: string; +}) { + return ( + + + + + {savedSearch.name} + + + + + ); +} + +export function SavedSearchesFlyout({ + opened, + onClose, + currentSavedSearchId, + linkPrefix = '/search', +}: { + opened: boolean; + onClose: () => void; + currentSavedSearchId?: string | null; + /** Base path for saved-search links (e.g. `/search` or `/explore`). */ + linkPrefix?: string; +}) { + const { data: savedSearches, isLoading, isError } = useSavedSearches(); + const { data: favorites } = useFavorites(); + const [search, setSearch] = useState(''); + + const favoritedIds = useMemo( + () => + new Set( + (favorites ?? []) + .filter(f => f.resourceType === 'savedSearch') + .map(f => f.resourceId), + ), + [favorites], + ); + + const filtered = useMemo(() => { + const all = savedSearches ?? []; + const q = search.trim().toLowerCase(); + const matched = q + ? all.filter( + s => + s.name.toLowerCase().includes(q) || + s.tags.some(t => t.toLowerCase().includes(q)), + ) + : all; + return matched.slice().sort((a, b) => a.name.localeCompare(b.name)); + }, [savedSearches, search]); + + const favorited = useMemo( + () => filtered.filter(s => favoritedIds.has(s.id)), + [filtered, favoritedIds], + ); + const others = useMemo( + () => filtered.filter(s => !favoritedIds.has(s.id)), + [filtered, favoritedIds], + ); + + return ( + + + } + value={search} + onChange={e => setSearch(e.currentTarget.value)} + size="xs" + data-testid="saved-searches-flyout-search" + /> + + {isLoading ? ( + + Loading saved searches... + + ) : isError ? ( + + Failed to load saved searches. + + ) : filtered.length === 0 ? ( + } + title={ + search ? 'No matching saved searches' : 'No saved searches yet' + } + /> + ) : ( + + + {favorited.length > 0 && ( +
+ + Favorites + + + {favorited.map(s => ( + + ))} + +
+ )} + +
+ {favorited.length > 0 && ( + + All Saved Searches + + )} + + {others.map(s => ( + + ))} + +
+
+
+ )} +
+
+ ); +} diff --git a/packages/app/src/components/Search/SearchAggControls.tsx b/packages/app/src/components/Search/SearchAggControls.tsx new file mode 100644 index 0000000000..de6f5953c4 --- /dev/null +++ b/packages/app/src/components/Search/SearchAggControls.tsx @@ -0,0 +1,448 @@ +import { useCallback, useMemo } from 'react'; +import { + parseAsBoolean, + parseAsInteger, + parseAsString, + parseAsStringEnum, + useQueryStates, +} from 'nuqs'; +import { Granularity } from '@hyperdx/common-utils/dist/core/utils'; +import { + Filter, + MetricsDataType, + NumberFormat, + NumberFormatSchema, +} from '@hyperdx/common-utils/dist/types'; + +import { ChartEditorSeries } from '@/components/ChartEditor/types'; +import { parseAsJsonEncoded } from '@/utils/queryParsers'; + +import { type ExploreFormula, parseExploreFormulas } from './exploreFormulas'; +import type { SearchView } from './searchViews'; + +const LEGACY_AGG_FNS = new Set([ + 'count', + 'count_distinct', + 'sum', + 'avg', + 'min', + 'max', + 'p50', + 'p90', + 'p95', + 'p99', +]); + +/** + * Translate a UI agg-fn (which includes percentile shorthands like `p95`) + * into the `select[]` shape understood by the chart config renderer. The + * percentile options map onto `{ aggFn: 'quantile', level }`. + */ +export function aggFnToSelectFields( + aggFn: string, +): { aggFn: string } | { aggFn: 'quantile'; level: number } { + if (['p50', 'p90', 'p95', 'p99'].includes(aggFn)) { + return { + aggFn: 'quantile', + level: Number.parseFloat(aggFn.replace('p', '0.')), + }; + } + return { aggFn }; +} + +const DEFAULT_AGG_LIMIT = 20; + +export type AggSortField = 'value' | 'name'; +type AggSortDirection = 'asc' | 'desc'; +type TimeseriesChartType = 'line' | 'bar'; + +export type ExploreSeries = ChartEditorSeries; + +/** + * A series' effective WHERE: whatever is still typed in its field, ANDed with + * the clauses that have been promoted into pills. Each clause is parenthesised + * so an OR-set pill (`x IN (...)`) cannot swallow the ones beside it. + */ +export function seriesAggCondition(series: ExploreSeries): string { + const clauses = [ + series.aggCondition, + ...(series.filters ?? []).map(f => ('condition' in f ? f.condition : '')), + ] + .map(clause => clause?.trim()) + .filter((clause): clause is string => Boolean(clause)); + + return clauses.length > 1 + ? clauses.map(clause => `(${clause})`).join(' AND ') + : (clauses[0] ?? ''); +} + +const DEFAULT_EXPLORE_SERIES: ExploreSeries = { + aggFn: 'count', + aggCondition: '', + aggConditionLanguage: 'sql', + valueExpression: '', +}; + +export interface SearchAggConfig { + series: ExploreSeries[]; + groupBy: string; + limit: number; + sort: AggSortField; + sortDir: AggSortDirection; + /** Line vs. bar for the Time series view. */ + chartType: TimeseriesChartType; + formulas: ExploreFormula[]; + /** When formulas exist: show operand series in the chart. Default on. */ + showOperandSeries: boolean; + granularity: Granularity | 'auto'; + alignDateRangeToGranularity: boolean; + fillNulls: 0 | false; + compareToPreviousPeriod: boolean; + fitYAxisToData: boolean; + numberFormat?: NumberFormat; +} + +/** + * Always SQL: these controls are Explore-only, and Explore authors WHERE + * clauses in SQL. Taking the language from the cross-page stored preference + * meant switching the Search page to Lucene silently changed what a new + * Explore series expected you to type. + */ +export function createEmptyExploreSeries(): ExploreSeries { + return { + aggFn: 'count', + aggCondition: '', + aggConditionLanguage: 'sql', + valueExpression: '', + }; +} + +/** True when every series on a metric source has a metric name to query. */ +export function exploreSeriesHaveMetricNames(series: ExploreSeries[]): boolean { + return series.length > 0 && series.every(s => Boolean(s.metricName)); +} + +/** + * True when every series has the column its aggregation needs. + * + * `count` is the only aggregation that takes no argument; the rest reduce over + * an expression. Explore commits a series edit the moment it is made, so a + * half-built series reaches the query layer as an empty expression and renders + * to `toString()` with no argument — a ClickHouse arity error that never + * mentions the column the user has yet to pick. + * + * Metric sources are exempt and shouldn't be passed here: they always reduce + * over `Value`, and the field is hidden for them. + */ +export function exploreSeriesHaveValueExpressions( + series: ExploreSeries[], +): boolean { + return series.every( + s => s.aggFn === 'count' || Boolean(s.valueExpression?.trim()), + ); +} + +export function canAddExploreSeries( + view: SearchView, + seriesCount: number, + hasFormulas = false, +): boolean { + if (view === 'pie' || view === 'bar') return false; + // Number is capped at 2 series unless a formula needs extra operands + // (e.g. A / (A + B + C)), matching Chart Explorer. + if (view === 'number' && !hasFormulas) return seriesCount < 2; + return true; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value != null && !Array.isArray(value); +} + +function optionalString(value: unknown): string | undefined { + return typeof value === 'string' ? value : undefined; +} + +function optionalBoolean(value: unknown): boolean | undefined { + return typeof value === 'boolean' ? value : undefined; +} + +/** + * A series' promoted pills. Anything malformed drops the whole list rather + * than half of it: a partially applied filter set silently widens the query. + */ +function parseSeriesFilters(value: unknown): Filter[] | undefined { + if (!Array.isArray(value) || value.length === 0 || value.length > 50) { + return undefined; + } + const filters: Filter[] = []; + for (const item of value) { + if (!isRecord(item)) return undefined; + if (item.type !== 'sql' && item.type !== 'lucene') return undefined; + if (typeof item.condition !== 'string') return undefined; + filters.push({ type: item.type, condition: item.condition }); + } + return filters; +} + +/** Parse the `series` URL param into chart `select[]` items, or null if invalid. */ +export function parseExploreSeries(value: unknown): ExploreSeries[] | null { + if (!Array.isArray(value) || value.length < 1 || value.length > 20) { + return null; + } + const series: ExploreSeries[] = []; + for (const item of value) { + if (!isRecord(item)) return null; + const aggFn = + typeof item.aggFn === 'string' && item.aggFn.length > 0 + ? item.aggFn + : 'count'; + const valueExpression = + typeof item.valueExpression === 'string' ? item.valueExpression : ''; + const aggCondition = + typeof item.aggCondition === 'string' ? item.aggCondition : ''; + // Lucene only when a link says so outright, so old shared URLs still + // render; anything unset is SQL, which is what Explore now authors. + const aggConditionLanguage = + item.aggConditionLanguage === 'lucene' ? 'lucene' : 'sql'; + const alias = optionalString(item.alias); + const metricName = optionalString(item.metricName); + const metricType = optionalString(item.metricType); + const color = optionalString(item.color); + const isDelta = optionalBoolean(item.isDelta); + const filters = parseSeriesFilters(item.filters); + + if (aggFn === 'quantile') { + series.push({ + aggFn: 'quantile', + level: typeof item.level === 'number' ? item.level : 0.95, + valueExpression, + aggCondition, + aggConditionLanguage, + ...(alias != null ? { alias } : {}), + ...(metricName != null ? { metricName } : {}), + ...(metricType != null + ? { metricType: metricType as MetricsDataType } + : {}), + ...(color != null ? { color: color as ExploreSeries['color'] } : {}), + ...(isDelta != null ? { isDelta } : {}), + ...(filters != null ? { filters } : {}), + }); + continue; + } + + series.push({ + aggFn, + valueExpression, + aggCondition, + aggConditionLanguage, + ...(typeof item.level === 'number' ? { level: item.level } : {}), + ...(alias != null ? { alias } : {}), + ...(metricName != null ? { metricName } : {}), + ...(metricType != null + ? { metricType: metricType as MetricsDataType } + : {}), + ...(color != null ? { color: color as ExploreSeries['color'] } : {}), + ...(isDelta != null ? { isDelta } : {}), + ...(filters != null ? { filters } : {}), + } as ExploreSeries); + } + return series; +} + +/** Turn the old `agg` / `aggExpr` / `metric` URL scalars into one series. */ +export function migrateLegacyAggToSeries(params: { + agg?: string | null; + aggExpr?: string | null; + metric?: string | null; + metricType?: string | null; +}): ExploreSeries { + const agg = + params.agg && LEGACY_AGG_FNS.has(params.agg) ? params.agg : 'count'; + const selectFields = aggFnToSelectFields(agg); + const isMetric = Boolean(params.metric); + return { + ...selectFields, + // The legacy params carried no condition, so there is no old syntax to + // preserve here and the series may as well start where Explore does. + aggCondition: '', + aggConditionLanguage: 'sql', + valueExpression: isMetric ? 'Value' : (params.aggExpr ?? ''), + ...(isMetric + ? { + metricName: params.metric ?? undefined, + metricType: (params.metricType || MetricsDataType.Gauge) as + | MetricsDataType + | undefined, + } + : {}), + } as ExploreSeries; +} + +function hasLegacyAggParams(state: { + agg: string | null; + aggExpr: string | null; + metric: string | null; + metricType: string | null; +}): boolean { + return ( + (state.agg != null && state.agg.length > 0) || + (state.aggExpr != null && state.aggExpr.length > 0) || + (state.metric != null && state.metric.length > 0) || + (state.metricType != null && state.metricType.length > 0) + ); +} + +/** URL-backed aggregation config for Explore chart views. */ +export function useSearchAggConfig(): [ + SearchAggConfig, + (patch: Partial) => void, +] { + const [state, setState] = useQueryStates({ + series: parseAsJsonEncoded(parseExploreSeries), + formulas: parseAsJsonEncoded(parseExploreFormulas), + showOperandSeries: parseAsBoolean.withDefault(true), + agg: parseAsString, + aggExpr: parseAsString, + metric: parseAsString, + metricType: parseAsString, + groupBy: parseAsString.withDefault(''), + limit: parseAsInteger.withDefault(DEFAULT_AGG_LIMIT), + sort: parseAsString.withDefault('value'), + sortDir: parseAsString.withDefault('desc'), + ts: parseAsString.withDefault('bar'), + granularity: parseAsStringEnum([ + 'auto', + ...Object.values(Granularity), + ]).withDefault('auto'), + alignIntervals: parseAsBoolean.withDefault(false), + fillNulls: parseAsBoolean.withDefault(true), + comparePrevious: parseAsBoolean.withDefault(false), + fitYAxis: parseAsBoolean.withDefault(false), + numberFormat: parseAsJsonEncoded(NumberFormatSchema.parse), + }); + + const { + series: seriesParam, + formulas: formulasParam, + showOperandSeries, + agg, + aggExpr, + metric, + metricType, + groupBy, + limit, + sort, + sortDir, + ts, + granularity, + alignIntervals, + fillNulls, + comparePrevious, + fitYAxis, + numberFormat, + } = state; + + const series = useMemo(() => { + if (seriesParam != null && seriesParam.length > 0) { + return seriesParam; + } + if (hasLegacyAggParams({ agg, aggExpr, metric, metricType })) { + return [migrateLegacyAggToSeries({ agg, aggExpr, metric, metricType })]; + } + return [DEFAULT_EXPLORE_SERIES]; + }, [seriesParam, agg, aggExpr, metric, metricType]); + + const formulas = useMemo( + () => formulasParam ?? [], + [formulasParam], + ); + + const config = useMemo( + () => ({ + series, + groupBy, + limit, + sort: sort as AggSortField, + sortDir: sortDir as AggSortDirection, + chartType: ts as TimeseriesChartType, + formulas, + showOperandSeries, + granularity, + alignDateRangeToGranularity: alignIntervals, + fillNulls: fillNulls ? 0 : false, + compareToPreviousPeriod: comparePrevious, + fitYAxisToData: fitYAxis, + numberFormat: numberFormat ?? undefined, + }), + [ + series, + groupBy, + limit, + sort, + sortDir, + ts, + formulas, + showOperandSeries, + granularity, + alignIntervals, + fillNulls, + comparePrevious, + fitYAxis, + numberFormat, + ], + ); + + const setConfig = useCallback( + (patch: Partial) => { + setState({ + ...(patch.series != null + ? { + series: patch.series, + // Drop the pre-series URL scalars so a migrated link doesn't + // fight the new `series` param on the next load. + agg: null, + aggExpr: null, + metric: null, + metricType: null, + } + : {}), + ...(patch.formulas != null + ? { + formulas: patch.formulas.length > 0 ? patch.formulas : null, + } + : {}), + ...(patch.showOperandSeries != null + ? { showOperandSeries: patch.showOperandSeries } + : {}), + ...(patch.groupBy != null ? { groupBy: patch.groupBy } : {}), + ...(patch.limit != null ? { limit: patch.limit } : {}), + ...(patch.sort != null ? { sort: patch.sort } : {}), + ...(patch.sortDir != null ? { sortDir: patch.sortDir } : {}), + ...(patch.chartType != null ? { ts: patch.chartType } : {}), + ...(patch.granularity != null + ? { granularity: patch.granularity } + : {}), + ...(patch.alignDateRangeToGranularity != null + ? { alignIntervals: patch.alignDateRangeToGranularity } + : {}), + ...(patch.fillNulls != null + ? { fillNulls: patch.fillNulls !== false } + : {}), + ...(patch.compareToPreviousPeriod != null + ? { comparePrevious: patch.compareToPreviousPeriod } + : {}), + ...(patch.fitYAxisToData != null + ? { fitYAxis: patch.fitYAxisToData } + : {}), + ...('numberFormat' in patch + ? { numberFormat: patch.numberFormat ?? null } + : {}), + }); + }, + [setState], + ); + + return [config, setConfig]; +} + +export { DEFAULT_AGG_LIMIT }; diff --git a/packages/app/src/components/Search/SearchColumnPicker.tsx b/packages/app/src/components/Search/SearchColumnPicker.tsx new file mode 100644 index 0000000000..8f31df50b8 --- /dev/null +++ b/packages/app/src/components/Search/SearchColumnPicker.tsx @@ -0,0 +1,248 @@ +import { useMemo, useState } from 'react'; +import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; +import { TSource } from '@hyperdx/common-utils/dist/types'; +import { + Badge, + Box, + Button, + Checkbox, + Group, + Loader, + Popover, + Text, + TextInput, + Tooltip, +} from '@mantine/core'; +import { IconColumns } from '@tabler/icons-react'; + +import { + useColumns, + useJsonColumns, + useMapColumns, + useMultipleAllFields, +} from '@/hooks/useMetadata'; +import { mergePath } from '@/utils'; + +function useColumnOptions( + source: TSource | undefined, + dateRange: [Date, Date] | undefined, + enabled: boolean, +) { + const tableConnection = useMemo( + () => (source ? tcFromSource(source) : undefined), + [source], + ); + const hasTable = enabled && tableConnection != null; + const { data: columns, isLoading: isColumnsLoading } = useColumns( + tableConnection ?? { databaseName: '', tableName: '', connectionId: '' }, + { enabled: hasTable }, + ); + const { data: jsonColumns } = useJsonColumns(tableConnection, { + enabled: hasTable, + }); + const { data: mapColumns } = useMapColumns(tableConnection, { + enabled: hasTable, + }); + const { data: fields, isLoading: isFieldsLoading } = useMultipleAllFields( + tableConnection ? [tableConnection] : [], + { + dateRange, + timestampValueExpression: source?.timestampValueExpression, + enabled: hasTable, + }, + ); + + const options = useMemo(() => { + // Top-level columns come first; the field list adds map/JSON sub-keys + // and is empty when the team has field metadata disabled. + const all = [ + ...(columns ?? []).map(c => c.name), + ...(fields ?? []).map(f => + mergePath(f.path, jsonColumns ?? [], mapColumns ?? []), + ), + ]; + return Array.from(new Set(all)); + }, [columns, fields, jsonColumns, mapColumns]); + + return { + options, + isLoading: hasTable && (isColumnsLoading || isFieldsLoading), + }; +} + +/** + * Columns checklist for the raw List view. Selections are staged locally and + * committed on Apply, mirroring the comma-separated `select` string the search + * page already uses. Hand-written SELECTs belong to the SQL editor. + */ +export function SearchColumnPicker({ + source, + dateRange, + selectedColumns, + onApply, + disabled, + disabledReason, +}: { + source?: TSource; + dateRange?: [Date, Date]; + selectedColumns: string[]; + onApply: (columns: string[]) => void; + disabled?: boolean; + disabledReason?: string; +}) { + const [opened, setOpened] = useState(false); + const [search, setSearch] = useState(''); + const [draft, setDraft] = useState(selectedColumns); + + const { options: availableColumns, isLoading } = useColumnOptions( + source, + dateRange, + opened, + ); + + // Reopening starts from the committed columns rather than an abandoned draft. + const handleOpenChange = (next: boolean) => { + if (next) { + setDraft(selectedColumns); + setSearch(''); + } + setOpened(next); + }; + + const filtered = useMemo(() => { + const q = search.trim().toLowerCase(); + // Selected columns stay listed even when absent from the schema, e.g. an + // expression written before the picker existed. + const union = Array.from( + new Set([...selectedColumns, ...availableColumns]), + ); + return q ? union.filter(c => c.toLowerCase().includes(q)) : union; + }, [search, availableColumns, selectedColumns]); + + const toggle = (column: string) => { + setDraft(prev => + prev.includes(column) + ? prev.filter(c => c !== column) + : [...prev, column], + ); + }; + + const apply = () => { + onApply(draft); + setOpened(false); + }; + + const countBadge = ( + + {selectedColumns.length} + + ); + + return ( + + + {/* A disabled button gets no pointer events, so the tooltip hangs off + a wrapper instead. */} + + + + + + + + setSearch(e.currentTarget.value)} + mb="xs" + autoFocus + rightSection={isLoading ? : undefined} + /> + + {/* Only offered while searching: unfiltered, "all" would add every + map sub-key on the table. */} + {search.trim() !== '' && ( + + )} + + + + {filtered.length === 0 ? ( + + {isLoading ? 'Loading fields' : 'No fields found'} + + ) : ( + filtered.map(column => ( + toggle(column)} + /> + )) + )} + + + + + ); +} diff --git a/packages/app/src/components/Search/SearchSortMenu.tsx b/packages/app/src/components/Search/SearchSortMenu.tsx new file mode 100644 index 0000000000..333463f10a --- /dev/null +++ b/packages/app/src/components/Search/SearchSortMenu.tsx @@ -0,0 +1,158 @@ +import { + Box, + Button, + Divider, + Popover, + Stack, + Text, + Tooltip, +} from '@mantine/core'; +import { + IconArrowDown, + IconArrowsSort, + IconArrowUp, + IconCheck, +} from '@tabler/icons-react'; + +export type SortDirection = 'asc' | 'desc'; + +export type SortFieldOption = { + value: string; + label: string; + icon?: React.ReactNode; +}; + +function OptionRow({ + icon, + label, + active, + onClick, +}: { + icon?: React.ReactNode; + label: React.ReactNode; + active?: boolean; + onClick: () => void; +}) { + return ( + + ); +} + +/** + * Structured sort control that replaces the free-form ORDER BY editor. For + * aggregated views the options are Value / Name (the metric vs. the group key); + * for the List view they are the displayed columns. Hand-written ORDER BY + * clauses belong to the SQL editor. + */ +export function SearchSortMenu({ + groupLabel = 'Sort by', + options, + activeField, + direction, + onChange, + onRevert, + canRevert, + disabled, + disabledReason, +}: { + groupLabel?: string; + options: SortFieldOption[]; + activeField?: string; + direction: SortDirection; + onChange: (field: string, direction: SortDirection) => void; + onRevert?: () => void; + canRevert?: boolean; + disabled?: boolean; + disabledReason?: string; +}) { + const setField = (field: string) => onChange(field, direction); + const setDirection = (dir: SortDirection) => + onChange(activeField ?? options[0]?.value ?? '', dir); + + return ( + + + {/* A disabled button gets no pointer events, so the tooltip hangs off + a wrapper instead. */} + + + + + + + + + + {groupLabel} + + {options.map(option => ( + setField(option.value)} + /> + ))} + + + Direction + + } + label="Ascending" + active={direction === 'asc'} + onClick={() => setDirection('asc')} + /> + } + label="Descending" + active={direction === 'desc'} + onClick={() => setDirection('desc')} + /> + {onRevert && ( + <> + + + + )} + + + + ); +} diff --git a/packages/app/src/components/Search/SearchViewSwitcher.module.scss b/packages/app/src/components/Search/SearchViewSwitcher.module.scss new file mode 100644 index 0000000000..0dc1a3f992 --- /dev/null +++ b/packages/app/src/components/Search/SearchViewSwitcher.module.scss @@ -0,0 +1,71 @@ +/* The switcher and the "As " picker are two pills that have to read + as one control, so both are built from the same three measurements. */ +$pill-padding: 4px; +$segment-height: 24px; +$pill-height: $segment-height + 2 * $pill-padding; + +.switcherRoot { + --sc-padding: #{$pill-padding}; + + flex-shrink: 0; + border-radius: var(--mantine-radius-default); +} + +.switcherLabel { + display: flex; + align-items: center; + height: $segment-height; + padding-block: 0; + padding-inline: 8px; +} + +/* Built as a field with a left addon rather than a second segmented control: + "As" is a fixed part of the frame, not a thing you can press, and the addon + colour is what says so. + + On the surface rather than the field fill, matching Group by beside it: + neither is typed into, and the input colour promised an editable box. */ +.asControl { + display: flex; + align-items: center; + flex-shrink: 0; + overflow: hidden; + background: var(--color-bg-surface); + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-default); +} + +/* Full-contrast text, not dimmed: the addon fill already sets this apart from + the value, so the label only has to be legible. Weight carries the hierarchy + instead — the chart type beside it is the heavier of the two. */ +.asLabel { + align-self: stretch; + display: flex; + align-items: center; + padding-inline: 8px; + border-right: 1px solid var(--color-border); + background: var(--color-bg-field-addon); + color: var(--color-text); + white-space: nowrap; +} + +.asTarget { + display: flex; + align-items: center; + gap: 6px; + height: $pill-height; + padding-inline: 8px; + white-space: nowrap; + + /* A shade short of the addon, matching Group by beside it. */ + &:hover { + background: var(--color-bg-control-hover); + } +} + +.segment { + display: flex; + align-items: center; + gap: 6px; + white-space: nowrap; +} diff --git a/packages/app/src/components/Search/SearchViewSwitcher.tsx b/packages/app/src/components/Search/SearchViewSwitcher.tsx new file mode 100644 index 0000000000..7a01ed231a --- /dev/null +++ b/packages/app/src/components/Search/SearchViewSwitcher.tsx @@ -0,0 +1,172 @@ +import { useMemo, useState } from 'react'; +import { SourceKind } from '@hyperdx/common-utils/dist/types'; +import { + Center, + Group, + Menu, + SegmentedControl, + Text, + Tooltip, + UnstyledButton, + VisuallyHidden, +} from '@mantine/core'; +import { IconChartLine, IconCheck, IconChevronDown } from '@tabler/icons-react'; + +import { + DEFAULT_CHART_VIEW, + getSearchViewMeta, + getVisibleSearchViews, + isAggregatedSearchView, + type SearchView, +} from './searchViews'; + +import classes from './SearchViewSwitcher.module.scss'; + +/** + * Stands in for whichever chart view is current, so the six chart types occupy + * one segment instead of six. Not a `SearchView`. + */ +const CHART_SEGMENT = 'chart'; + +/** + * Only the active segment spends width on its label; the rest stay icon-only + * with the name in a tooltip, so the row pays for one label rather than one per + * view. + */ +function segmentLabel( + label: string, + icon: React.ReactNode, + active: boolean, +): React.ReactNode { + if (active) { + return ( + + {icon} + {label} + + ); + } + return ( + +
+ {label} + {icon} +
+
+ ); +} + +export function SearchViewSwitcher({ + value, + onChange, + sourceKind, + sqlMode = false, +}: { + value: SearchView; + onChange: (view: SearchView) => void; + sourceKind?: SourceKind; + /** Offer only the views that can show a hand-written statement's result. */ + sqlMode?: boolean; +}) { + const options = useMemo( + () => getVisibleSearchViews({ sourceKind, sqlMode }), + [sourceKind, sqlMode], + ); + const eventViews = options.filter(o => !o.aggregated); + const chartViews = options.filter(o => o.aggregated); + const isChart = isAggregatedSearchView(value); + const [lastChartView, setLastChartView] = useState(() => + isAggregatedSearchView(value) ? value : DEFAULT_CHART_VIEW, + ); + if (isAggregatedSearchView(value) && lastChartView !== value) { + setLastChartView(value); + } + + // Both the chart segment and the As control show the current type's own + // glyph, so switching type is visible even while the segment is icon-only. + const chartMeta = getSearchViewMeta(isChart ? value : lastChartView); + const chartIcon = chartMeta?.icon ?? ; + + const segments: { value: string; label: React.ReactNode }[] = eventViews.map( + option => ({ + value: option.value, + label: segmentLabel( + option.shortLabel ?? option.label, + option.icon, + value === option.value, + ), + }), + ); + if (chartViews.length > 0) { + segments.push({ + value: CHART_SEGMENT, + label: segmentLabel('Charts', chartIcon, isChart), + }); + } + + return ( + + {eventViews.length > 0 && ( + { + if (next === CHART_SEGMENT) { + if (!isChart) onChange(lastChartView); + return; + } + const picked = eventViews.find(option => option.value === next); + if (picked) onChange(picked.value); + }} + data={segments} + /> + )} + {/* Only while a chart is on screen: picking a chart type is a question + about the current view, and asking it beside a table of events offers + a setting that changes nothing you can see. With the event views + hidden it is the whole switcher, so it stays even for a moment when + the view has not yet moved onto a chart. */} + {chartViews.length > 0 && (isChart || eventViews.length === 0) && ( + +
+ + As + + + + {chartIcon} + + {chartMeta?.label} + + + + +
+ + {chartViews.map(option => ( + : undefined + } + onClick={() => onChange(option.value)} + > + {option.label} + + ))} + +
+ )} +
+ ); +} diff --git a/packages/app/src/components/Search/TableDisplaySettings.tsx b/packages/app/src/components/Search/TableDisplaySettings.tsx new file mode 100644 index 0000000000..c5edf8c5e4 --- /dev/null +++ b/packages/app/src/components/Search/TableDisplaySettings.tsx @@ -0,0 +1,95 @@ +import { + Button, + Group, + Popover, + SegmentedControl, + Stack, + Text, +} from '@mantine/core'; +import { IconAdjustmentsHorizontal } from '@tabler/icons-react'; + +function SettingRow({ + label, + children, +}: { + label: string; + children: React.ReactNode; +}) { + return ( + + + {label} + + {children} + + ); +} + +export function TableDisplaySettings({ + rowSelection, + wrapLines, + onWrapLinesChange, + onResetColumnWidths, +}: { + /** Omit when the table doesn't support selecting rows. */ + rowSelection?: { enabled: boolean; onChange: (enabled: boolean) => void }; + wrapLines: boolean; + onWrapLinesChange: (wrap: boolean) => void; + /** Undefined while the columns have their default widths. */ + onResetColumnWidths?: () => void; +}) { + return ( + + + + + + + {rowSelection && ( + + rowSelection.onChange(v === 'show')} + data={[ + { label: 'Hide', value: 'hide' }, + { label: 'Show', value: 'show' }, + ]} + data-testid="row-selection-setting" + /> + + )} + + onWrapLinesChange(v === 'wrap')} + data={[ + { label: 'Single line', value: 'single' }, + { label: 'Wrap', value: 'wrap' }, + ]} + data-testid="wrap-lines-setting" + /> + + + + + + + + ); +} diff --git a/packages/app/src/components/Search/__tests__/SearchColumnPicker.test.tsx b/packages/app/src/components/Search/__tests__/SearchColumnPicker.test.tsx new file mode 100644 index 0000000000..308d61adde --- /dev/null +++ b/packages/app/src/components/Search/__tests__/SearchColumnPicker.test.tsx @@ -0,0 +1,120 @@ +import { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import { SearchColumnPicker } from '@/components/Search/SearchColumnPicker'; +import { makeLogSource } from '@/llm/__fixtures__/sources'; + +jest.mock('@/hooks/useMetadata', () => ({ + useColumns: jest.fn(() => ({ + data: [{ name: 'Timestamp' }, { name: 'Body' }, { name: 'ServiceName' }], + isLoading: false, + })), + useJsonColumns: jest.fn(() => ({ data: [] })), + useMapColumns: jest.fn(() => ({ data: ['LogAttributes'] })), + useMultipleAllFields: jest.fn(() => ({ + data: [ + { path: ['Body'], type: 'String', jsType: 'string' }, + { path: ['LogAttributes', 'user.id'], type: 'String', jsType: 'string' }, + ], + isLoading: false, + })), +})); + +const source = makeLogSource(); + +describe('SearchColumnPicker', () => { + it('shows the column count on the button', () => { + renderWithMantine( + , + ); + + expect(screen.getByTestId('search-column-picker')).toHaveTextContent( + 'Columns2', + ); + }); + + it('lists columns and map sub-keys, and applies several at once', async () => { + const onApply = jest.fn(); + renderWithMantine( + , + ); + + await userEvent.click(screen.getByTestId('search-column-picker')); + await userEvent.click(await screen.findByLabelText('ServiceName')); + await userEvent.click(screen.getByLabelText("LogAttributes['user.id']")); + expect(onApply).not.toHaveBeenCalled(); + + await userEvent.click(screen.getByText('Apply')); + + expect(onApply).toHaveBeenCalledWith([ + 'Timestamp', + 'ServiceName', + "LogAttributes['user.id']", + ]); + }); + + it('discards an unapplied draft when reopened', async () => { + renderWithMantine( + , + ); + + const trigger = screen.getByTestId('search-column-picker'); + await userEvent.click(trigger); + await userEvent.click(await screen.findByLabelText('Body')); + await userEvent.click(trigger); + await userEvent.click(trigger); + + expect(await screen.findByLabelText('Body')).not.toBeChecked(); + expect(screen.getByLabelText('Timestamp')).toBeChecked(); + }); + + it('filters the list by search', async () => { + renderWithMantine( + , + ); + + await userEvent.click(screen.getByTestId('search-column-picker')); + await userEvent.type( + await screen.findByPlaceholderText('Search fields'), + 'serv', + ); + + expect(screen.getByLabelText('ServiceName')).toBeInTheDocument(); + expect(screen.queryByLabelText('Body')).not.toBeInTheDocument(); + }); + + it('does not open while disabled', async () => { + renderWithMantine( + , + ); + + const trigger = screen.getByTestId('search-column-picker'); + expect(trigger).toBeDisabled(); + await userEvent.click(trigger); + expect( + screen.queryByTestId('search-column-picker-dropdown'), + ).not.toBeInTheDocument(); + }); +}); diff --git a/packages/app/src/components/Search/__tests__/SearchViewSwitcher.test.tsx b/packages/app/src/components/Search/__tests__/SearchViewSwitcher.test.tsx new file mode 100644 index 0000000000..6dd88ec47f --- /dev/null +++ b/packages/app/src/components/Search/__tests__/SearchViewSwitcher.test.tsx @@ -0,0 +1,177 @@ +import { useState } from 'react'; +import { SourceKind } from '@hyperdx/common-utils/dist/types'; +import { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import type { SearchView } from '@/components/Search/searchViews'; +import { SearchViewSwitcher } from '@/components/Search/SearchViewSwitcher'; + +describe('SearchViewSwitcher', () => { + function renderSwitcher({ + value = 'list', + sourceKind = SourceKind.Log, + sqlMode = false, + onChange = jest.fn(), + }: { + value?: SearchView; + sourceKind?: SourceKind; + sqlMode?: boolean; + onChange?: jest.Mock; + } = {}) { + renderWithMantine( + , + ); + return onChange; + } + + it('switches to time series when Charts is selected from Events', async () => { + const user = userEvent.setup(); + const onChange = renderSwitcher(); + + await user.click(screen.getByRole('radio', { name: 'Charts' })); + + expect(onChange).toHaveBeenCalledWith('timeseries'); + }); + + it('picks pie from the chart type menu', async () => { + const user = userEvent.setup(); + const onChange = renderSwitcher({ value: 'timeseries' }); + + await user.click(screen.getByRole('button', { name: /Chart as/ })); + await user.click(await screen.findByRole('menuitem', { name: 'Pie' })); + expect(onChange).toHaveBeenCalledWith('pie'); + }); + + it('picks number from the chart type menu', async () => { + const user = userEvent.setup(); + const onChange = renderSwitcher({ value: 'timeseries' }); + + await user.click(screen.getByRole('button', { name: /Chart as/ })); + await user.click(await screen.findByRole('menuitem', { name: 'Number' })); + expect(onChange).toHaveBeenCalledWith('number'); + }); + + it('returns to the last chart type after switching back to Events', async () => { + const user = userEvent.setup(); + const onChange = jest.fn(); + + function Harness() { + const [value, setValue] = useState('pie'); + return ( + { + onChange(next); + setValue(next); + }} + sourceKind={SourceKind.Log} + /> + ); + } + + renderWithMantine(); + + await user.click(screen.getByRole('radio', { name: 'Events' })); + await user.click(screen.getByRole('radio', { name: 'Charts' })); + expect(onChange).toHaveBeenLastCalledWith('pie'); + }); + + it('labels only the active segment, leaving the rest icon-only', () => { + renderSwitcher({ value: 'list' }); + + expect(screen.getByRole('radio', { name: 'Events' })).toBeChecked(); + // The inactive segments keep their names for screen readers, but the text + // is visually hidden so the row only pays for one label. + expect(screen.getByRole('radio', { name: 'Charts' })).not.toBeChecked(); + expect(screen.getByRole('radio', { name: 'Patterns' })).not.toBeChecked(); + }); + + it('names the current chart type in the As control', () => { + renderSwitcher({ value: 'pie' }); + + expect(screen.getByRole('radio', { name: 'Charts' })).toBeChecked(); + expect(screen.getByTestId('visualize-as-button')).toHaveTextContent('Pie'); + }); + + it('withholds the As control until a chart is on screen', () => { + renderSwitcher({ value: 'list' }); + + expect(screen.queryByTestId('visualize-as-button')).not.toBeInTheDocument(); + }); + + it('brings the As control back with the chart it belongs to', async () => { + const user = userEvent.setup(); + + function Harness() { + const [value, setValue] = useState('list'); + return ( + + ); + } + + renderWithMantine(); + expect(screen.queryByTestId('visualize-as-button')).not.toBeInTheDocument(); + + await user.click(screen.getByRole('radio', { name: 'Charts' })); + + expect(screen.getByTestId('visualize-as-button')).toHaveTextContent( + 'Time series', + ); + }); + + it('offers Browse instead of the event views for metric sources', async () => { + const user = userEvent.setup(); + const onChange = renderSwitcher({ + sourceKind: SourceKind.Metric, + value: 'timeseries', + }); + + expect( + screen.queryByRole('radio', { name: 'Events' }), + ).not.toBeInTheDocument(); + expect(screen.getByTestId('visualize-as-button')).toHaveTextContent( + 'Time series', + ); + + await user.click(screen.getByRole('radio', { name: 'Browse' })); + expect(onChange).toHaveBeenCalledWith('browse'); + }); + + it('keeps Browse to metric sources', () => { + renderSwitcher({ sourceKind: SourceKind.Log }); + + expect( + screen.queryByRole('radio', { name: 'Browse' }), + ).not.toBeInTheDocument(); + }); + + it('keeps Events but drops heatmap and patterns in SQL mode', async () => { + const user = userEvent.setup(); + const onChange = renderSwitcher({ sqlMode: true, value: 'timeseries' }); + + await user.click(screen.getByRole('radio', { name: 'Events' })); + + expect(onChange).toHaveBeenCalledWith('list'); + expect( + screen.queryByRole('menuitem', { name: /pattern|delta/i }), + ).not.toBeInTheDocument(); + }); + + it('offers chart types in SQL mode from the List view', async () => { + const user = userEvent.setup(); + const onChange = renderSwitcher({ sqlMode: true, value: 'list' }); + + await user.click(screen.getByRole('radio', { name: 'Charts' })); + + expect(onChange).toHaveBeenCalledWith('timeseries'); + }); +}); diff --git a/packages/app/src/components/Search/__tests__/searchAggConfig.test.ts b/packages/app/src/components/Search/__tests__/searchAggConfig.test.ts new file mode 100644 index 0000000000..0fd72cc75a --- /dev/null +++ b/packages/app/src/components/Search/__tests__/searchAggConfig.test.ts @@ -0,0 +1,331 @@ +import { validateFormula } from '@hyperdx/common-utils/dist/core/formula'; +import { SourceKind } from '@hyperdx/common-utils/dist/types'; + +import { insertSeriesRefAtCursor } from '@/components/Explore/ExploreFormulaCard'; +import { + canAddExploreFormula, + parseExploreFormulas, +} from '@/components/Search/exploreFormulas'; +import { + aggFnToSelectFields, + canAddExploreSeries, + type ExploreSeries, + exploreSeriesHaveValueExpressions, + migrateLegacyAggToSeries, + parseExploreSeries, + seriesAggCondition, +} from '@/components/Search/SearchAggControls'; + +describe('seriesAggCondition', () => { + const series = (patch: Partial): ExploreSeries => + ({ + aggFn: 'count', + valueExpression: '', + aggCondition: '', + aggConditionLanguage: 'sql', + ...patch, + }) as ExploreSeries; + + it('is the typed condition when nothing has been promoted', () => { + expect(seriesAggCondition(series({ aggCondition: 'Duration > 100' }))).toBe( + 'Duration > 100', + ); + }); + + it('is the pills when the field has been emptied into them', () => { + expect( + seriesAggCondition( + series({ + filters: [{ type: 'sql', condition: "level IN ('error')" }], + }), + ), + ).toBe("level IN ('error')"); + }); + + it('parenthesises so an OR-set pill cannot swallow what sits beside it', () => { + expect( + seriesAggCondition( + series({ + aggCondition: 'Duration > 100', + filters: [ + { type: 'sql', condition: "level IN ('error', 'fatal')" }, + { type: 'sql', condition: "service NOT IN ('cron')" }, + ], + }), + ), + ).toBe( + "(Duration > 100) AND (level IN ('error', 'fatal')) AND (service NOT IN ('cron'))", + ); + }); + + it('ignores whitespace left behind when a clause is promoted', () => { + expect( + seriesAggCondition( + series({ + aggCondition: ' ', + filters: [{ type: 'sql', condition: "level IN ('error')" }], + }), + ), + ).toBe("level IN ('error')"); + }); + + it('is empty for a series that filters nothing', () => { + expect(seriesAggCondition(series({}))).toBe(''); + }); +}); + +describe('parseExploreSeries', () => { + it('accepts a count series', () => { + expect( + parseExploreSeries([ + { + aggFn: 'count', + valueExpression: '', + aggCondition: 'level:error', + aggConditionLanguage: 'lucene', + alias: 'errors', + }, + ]), + ).toEqual([ + { + aggFn: 'count', + valueExpression: '', + aggCondition: 'level:error', + aggConditionLanguage: 'lucene', + alias: 'errors', + }, + ]); + }); + + it('restores the pills a shared link carries', () => { + expect( + parseExploreSeries([ + { + aggFn: 'count', + valueExpression: '', + aggCondition: '', + filters: [{ type: 'sql', condition: "level IN ('error')" }], + }, + ])?.[0].filters, + ).toEqual([{ type: 'sql', condition: "level IN ('error')" }]); + }); + + it('drops a malformed pill list rather than applying part of it', () => { + expect( + parseExploreSeries([ + { + aggFn: 'count', + valueExpression: '', + aggCondition: '', + filters: [ + { type: 'sql', condition: "level IN ('error')" }, + { type: 'sql' }, + ], + }, + ])?.[0].filters, + ).toBeUndefined(); + }); + + it('keeps an explicit lucene condition so old links still render', () => { + expect( + parseExploreSeries([ + { + aggFn: 'count', + aggCondition: 'level:error', + aggConditionLanguage: 'lucene', + }, + ])?.[0], + ).toMatchObject({ aggConditionLanguage: 'lucene' }); + }); + + it('reads an unset condition language as SQL', () => { + expect( + parseExploreSeries([{ aggFn: 'count', valueExpression: '' }])?.[0], + ).toMatchObject({ aggConditionLanguage: 'sql' }); + }); + + it('defaults quantile level when missing', () => { + const parsed = parseExploreSeries([ + { aggFn: 'quantile', valueExpression: 'Duration' }, + ]); + expect(parsed?.[0]).toMatchObject({ + aggFn: 'quantile', + level: 0.95, + valueExpression: 'Duration', + }); + }); + + it('rejects an empty array', () => { + expect(parseExploreSeries([])).toBeNull(); + }); + + it('rejects non-objects', () => { + expect(parseExploreSeries(['count'])).toBeNull(); + }); +}); + +describe('migrateLegacyAggToSeries', () => { + it('maps p95 to quantile + level', () => { + expect( + migrateLegacyAggToSeries({ agg: 'p95', aggExpr: 'Duration' }), + ).toEqual({ + aggFn: 'quantile', + level: 0.95, + aggCondition: '', + aggConditionLanguage: 'sql', + valueExpression: 'Duration', + }); + }); + + it('maps metric scalars onto a Value series', () => { + expect( + migrateLegacyAggToSeries({ + agg: 'avg', + metric: 'http.server.duration', + metricType: 'gauge', + }), + ).toMatchObject({ + aggFn: 'avg', + valueExpression: 'Value', + metricName: 'http.server.duration', + metricType: 'gauge', + }); + }); +}); + +describe('aggFnToSelectFields', () => { + it('passes count through', () => { + expect(aggFnToSelectFields('count')).toEqual({ aggFn: 'count' }); + }); +}); + +describe('parseExploreFormulas', () => { + it('accepts an empty array', () => { + expect(parseExploreFormulas([])).toEqual([]); + }); + + it('accepts expression and optional alias', () => { + expect( + parseExploreFormulas([{ expression: 'A / B', alias: 'ratio' }]), + ).toEqual([{ expression: 'A / B', alias: 'ratio' }]); + }); + + it('keeps an unknown series ref for live validation', () => { + expect(parseExploreFormulas([{ expression: 'A / C' }])).toEqual([ + { expression: 'A / C' }, + ]); + const result = validateFormula('A / C', { seriesCount: 2 }); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.errors[0]?.code).toBe('unknown-series-ref'); + expect(result.errors[0]?.message).toContain('Unknown series "C"'); + } + }); + + it('rejects a non-array', () => { + expect(parseExploreFormulas({ expression: 'A' })).toBeNull(); + }); +}); + +describe('canAddExploreFormula', () => { + it('allows time series and table formulas on log sources', () => { + expect(canAddExploreFormula('timeseries', 0, SourceKind.Log)).toBe(true); + expect(canAddExploreFormula('table', 1, SourceKind.Trace)).toBe(true); + }); + + it('caps number at one formula', () => { + expect(canAddExploreFormula('number', 0, SourceKind.Metric)).toBe(true); + expect(canAddExploreFormula('number', 1, SourceKind.Metric)).toBe(false); + }); + + it('rejects pie, bar, and session sources', () => { + expect(canAddExploreFormula('pie', 0, SourceKind.Log)).toBe(false); + expect(canAddExploreFormula('bar', 0, SourceKind.Log)).toBe(false); + expect(canAddExploreFormula('timeseries', 0, SourceKind.Session)).toBe( + false, + ); + }); +}); + +describe('canAddExploreSeries', () => { + it('lifts the number series cap when a formula exists', () => { + expect(canAddExploreSeries('number', 2)).toBe(false); + expect(canAddExploreSeries('number', 2, true)).toBe(true); + }); +}); + +describe('insertSeriesRefAtCursor', () => { + it('inserts a letter into an empty expression', () => { + expect(insertSeriesRefAtCursor('', 'A', 0, 0)).toEqual({ + next: 'A', + cursor: 1, + }); + }); + + it('pads a letter when appending after another ref', () => { + expect(insertSeriesRefAtCursor('A', 'B', 1, 1)).toEqual({ + next: 'A B', + cursor: 3, + }); + }); + + it('replaces the current selection', () => { + expect(insertSeriesRefAtCursor('A / X', 'B', 4, 5)).toEqual({ + next: 'A / B', + cursor: 5, + }); + }); +}); + +describe('exploreSeriesHaveValueExpressions', () => { + const series = (over: Partial): ExploreSeries => ({ + aggFn: 'count', + aggCondition: '', + aggConditionLanguage: 'sql', + valueExpression: '', + ...over, + }); + + it('lets count through, the one aggregation taking no argument', () => { + expect(exploreSeriesHaveValueExpressions([series({})])).toBe(true); + }); + + it('holds a percentile that has no column yet', () => { + expect( + exploreSeriesHaveValueExpressions([ + series({ aggFn: 'quantile', level: 0.99 }), + ]), + ).toBe(false); + }); + + it('is not fooled by whitespace', () => { + expect( + exploreSeriesHaveValueExpressions([ + series({ aggFn: 'avg', valueExpression: ' ' }), + ]), + ).toBe(false); + }); + + it('passes once the column is there', () => { + expect( + exploreSeriesHaveValueExpressions([ + series({ aggFn: 'avg', valueExpression: 'Duration' }), + ]), + ).toBe(true); + }); + + it('holds when any one series of several is incomplete', () => { + expect( + exploreSeriesHaveValueExpressions([ + series({ aggFn: 'avg', valueExpression: 'Duration' }), + series({ aggFn: 'max' }), + ]), + ).toBe(false); + }); + + it('treats a Custom expression as the column it stands in for', () => { + expect(exploreSeriesHaveValueExpressions([series({ aggFn: 'none' })])).toBe( + false, + ); + }); +}); diff --git a/packages/app/src/components/Search/exploreFormulas.ts b/packages/app/src/components/Search/exploreFormulas.ts new file mode 100644 index 0000000000..b658ad8dea --- /dev/null +++ b/packages/app/src/components/Search/exploreFormulas.ts @@ -0,0 +1,52 @@ +import { isFormulaSourceKind } from '@hyperdx/common-utils/dist/core/utils'; +import { SourceKind } from '@hyperdx/common-utils/dist/types'; + +import type { SearchView } from './searchViews'; + +export type ExploreFormula = { + expression: string; + alias?: string; +}; + +/** Views that can compile letter-ref formulas. */ +export function exploreViewSupportsFormulas(view: SearchView): boolean { + return view === 'timeseries' || view === 'table' || view === 'number'; +} + +export function canAddExploreFormula( + view: SearchView, + formulaCount: number, + sourceKind?: SourceKind, +): boolean { + if (!isFormulaSourceKind(sourceKind)) return false; + if (!exploreViewSupportsFormulas(view)) return false; + if (view === 'number' && formulaCount >= 1) return false; + return true; +} + +export function createEmptyExploreFormula(): ExploreFormula { + return { expression: '', alias: '' }; +} + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value != null && !Array.isArray(value); +} + +/** Parse the `formulas` URL param, or null if invalid. Empty array is valid. */ +export function parseExploreFormulas(value: unknown): ExploreFormula[] | null { + if (!Array.isArray(value) || value.length > 20) { + return null; + } + const formulas: ExploreFormula[] = []; + for (const item of value) { + if (!isRecord(item)) return null; + const expression = + typeof item.expression === 'string' ? item.expression : ''; + const alias = typeof item.alias === 'string' ? item.alias : undefined; + formulas.push({ + expression, + ...(alias != null && alias.length > 0 ? { alias } : {}), + }); + } + return formulas; +} diff --git a/packages/app/src/components/Search/searchViews.tsx b/packages/app/src/components/Search/searchViews.tsx new file mode 100644 index 0000000000..dc1a736327 --- /dev/null +++ b/packages/app/src/components/Search/searchViews.tsx @@ -0,0 +1,222 @@ +import { useCallback, useMemo } from 'react'; +import { parseAsString, useQueryState } from 'nuqs'; +import { DisplayType, SourceKind } from '@hyperdx/common-utils/dist/types'; +import { + IconBracketsContain, + IconChartBar, + IconChartLine, + IconChartPie, + IconChartTreemap, + IconGrid3x3, + IconLayoutGrid, + IconList, + IconNumbers, + IconTable, +} from '@tabler/icons-react'; + +import { IS_CLICKHOUSE_BUILD } from '@/config'; + +export type SearchView = + | 'list' + | 'timeseries' + | 'number' + | 'table' + | 'bar' + | 'pie' + | 'treemap' + | 'heatmap' + | 'patterns' + | 'browse'; + +const DEFAULT_SEARCH_VIEW: SearchView = 'list'; +export const DEFAULT_CHART_VIEW: SearchView = 'timeseries'; + +export type SearchViewMeta = { + value: SearchView; + label: string; + /** + * Shown in the view switcher when this view is active, where the label sits + * beside the icon and every character costs width the results row needs. + * Falls back to `label`. + */ + shortLabel?: string; + icon: React.ReactNode; + /** Requires an aggregation (agg fn + optional group by). */ + aggregated: boolean; + /** When set, the view is only offered for these source kinds. */ + sourceKinds?: SourceKind[]; + /** Hidden in the ClickHouse OSS build (matches existing pattern gating). */ + hiddenInClickhouseBuild?: boolean; +}; + +const SEARCH_VIEWS: SearchViewMeta[] = [ + { + value: 'list', + label: 'Events', + icon: , + aggregated: false, + }, + { + value: 'timeseries', + label: 'Time series', + icon: , + aggregated: true, + }, + { + value: 'number', + label: 'Number', + icon: , + aggregated: true, + }, + { + value: 'table', + label: 'Grouped table', + icon: , + aggregated: true, + }, + { + value: 'bar', + label: 'Bar', + icon: , + aggregated: true, + }, + { + value: 'pie', + label: 'Pie', + icon: , + aggregated: true, + }, + { + value: 'treemap', + label: 'Treemap', + icon: , + aggregated: true, + }, + { + value: 'heatmap', + label: 'Event deltas', + shortLabel: 'Deltas', + icon: , + aggregated: false, + sourceKinds: [SourceKind.Trace], + }, + { + value: 'patterns', + label: 'Event patterns', + shortLabel: 'Patterns', + icon: , + aggregated: false, + hiddenInClickhouseBuild: true, + }, + { + value: 'browse', + label: 'Browse', + icon: , + aggregated: false, + sourceKinds: [SourceKind.Metric], + }, +]; + +const VALID_VIEWS = new Set(SEARCH_VIEWS.map(v => v.value)); + +// Old links / saved searches used mode = results | delta | pattern. +const LEGACY_VIEW_ALIASES: Record = { + results: 'list', + delta: 'heatmap', + pattern: 'patterns', +}; + +function normalizeSearchView(raw: string | null | undefined): SearchView { + if (raw == null) return DEFAULT_SEARCH_VIEW; + if (VALID_VIEWS.has(raw)) return raw as SearchView; + return LEGACY_VIEW_ALIASES[raw] ?? DEFAULT_SEARCH_VIEW; +} + +export function isAggregatedSearchView(view: SearchView): boolean { + return SEARCH_VIEWS.find(v => v.value === view)?.aggregated ?? false; +} + +export function getSearchViewMeta( + view: SearchView, +): SearchViewMeta | undefined { + return SEARCH_VIEWS.find(v => v.value === view); +} + +/** + * Views that can show the result of a hand-written statement: the row list and + * the charts, which map 1:1 to the raw-SQL display types. Heatmap and patterns + * are built from builder queries of their own. + */ +export function isSqlModeSearchView(view: SearchView): boolean { + return view === 'list' || isAggregatedSearchView(view); +} + +/** Event vs chart views offered for this source (and SQL mode). */ +export function getVisibleSearchViews({ + sourceKind, + sqlMode = false, +}: { + sourceKind?: SourceKind; + sqlMode?: boolean; +}): SearchViewMeta[] { + return SEARCH_VIEWS.filter(v => { + if (v.hiddenInClickhouseBuild && IS_CLICKHOUSE_BUILD) return false; + if (v.sourceKinds && (!sourceKind || !v.sourceKinds.includes(sourceKind))) { + return false; + } + // Metric sources have no raw rows, so besides the metric wall only the + // aggregated chart views (time series / number / table / bar / pie / + // treemap) make sense. + if ( + sourceKind === SourceKind.Metric && + !v.aggregated && + v.value !== 'browse' + ) { + return false; + } + if (sqlMode && !isSqlModeSearchView(v.value)) return false; + return true; + }); +} + +/** Views that keep the top time histogram above the results. */ +export function viewShowsHistogram(view: SearchView): boolean { + return view === 'list' || view === 'patterns'; +} + +export function searchViewToDisplayType(view: SearchView): DisplayType { + switch (view) { + case 'timeseries': + return DisplayType.StackedBar; + case 'number': + return DisplayType.Number; + case 'table': + return DisplayType.Table; + case 'bar': + return DisplayType.Bar; + case 'pie': + return DisplayType.Pie; + case 'treemap': + return DisplayType.Treemap; + default: + return DisplayType.Search; + } +} + +/** URL-backed search view state, keeping the legacy `mode` param name. */ +export function useSearchView(): [SearchView, (view: SearchView) => void] { + const [raw, setRaw] = useQueryState( + 'mode', + parseAsString.withDefault(DEFAULT_SEARCH_VIEW), + ); + const view = useMemo(() => normalizeSearchView(raw), [raw]); + const setView = useCallback( + (next: SearchView) => { + setRaw(next === DEFAULT_SEARCH_VIEW ? null : next); + }, + [setRaw], + ); + return [view, setView]; +} + +export { SearchViewSwitcher } from './SearchViewSwitcher'; diff --git a/packages/app/src/components/Sparkline.tsx b/packages/app/src/components/Sparkline.tsx index 52bc6132c5..c7e28c3351 100644 --- a/packages/app/src/components/Sparkline.tsx +++ b/packages/app/src/components/Sparkline.tsx @@ -6,6 +6,7 @@ import { Line, LineChart, ResponsiveContainer, + YAxis, } from 'recharts'; type SparklineType = 'line' | 'area' | 'bar'; @@ -37,6 +38,7 @@ export function Sparkline({ type, color, height = '100%', + yDomain, }: { points: SparklinePoint[]; type: SparklineType; @@ -44,13 +46,18 @@ export function Sparkline({ // Matches ResponsiveContainer's accepted height: a pixel number or a // percentage string (e.g. "100%"). height?: number | `${number}%`; + /** Fixed value range, so sparklines drawn side by side share one scale. */ + yDomain?: [number, number]; }) { if (points.length < 2) return null; + const yAxis = yDomain ? : null; + return ( {type === 'bar' ? ( + {yAxis} ) : type === 'area' ? ( + {yAxis} ) : ( + {yAxis} + + , + ); + + await userEvent.click(screen.getByText('checkout')); + return screen.findByRole('link', { hidden: true }); +} + +describe('DBHighlightedAttributesList', () => { + it('uses the Lucene expression a source defines for the attribute', async () => { + const generateSearchUrl = jest.fn(() => '/search'); + + await openSearchLink({ generateSearchUrl }); + + expect(generateSearchUrl).toHaveBeenCalledWith( + expect.objectContaining({ + where: 'service:"checkout"', + whereLanguage: 'lucene', + }), + ); + }); + + // Explore discards a Lucene WHERE, so sending one would make the action a + // no-op. The SQL form of the same attribute is already to hand. + it('sends the SQL expression to a page that only takes SQL', async () => { + const generateSearchUrl = jest.fn(() => '/explore'); + + await openSearchLink({ generateSearchUrl, sqlOnlySearchUrl: true }); + + expect(generateSearchUrl).toHaveBeenCalledWith( + expect.objectContaining({ + where: `ServiceName = 'checkout'`, + whereLanguage: 'sql', + }), + ); + }); +}); diff --git a/packages/app/src/components/__tests__/Table.test.tsx b/packages/app/src/components/__tests__/Table.test.tsx new file mode 100644 index 0000000000..3c1ef10987 --- /dev/null +++ b/packages/app/src/components/__tests__/Table.test.tsx @@ -0,0 +1,39 @@ +import { ColumnDef } from '@tanstack/react-table'; +import { render } from '@testing-library/react'; + +import { Table } from '@/components/Table'; +import { UNDEFINED_WIDTH } from '@/tableUtils'; + +type Row = { label: string; value: string }; + +const columns: ColumnDef[] = [ + { + accessorKey: 'label', + header: 'Label', + size: 260, + cell: ({ row }) => {row.original.label}, + }, + { + accessorKey: 'value', + header: 'Value', + size: UNDEFINED_WIDTH, + cell: ({ row }) => {row.original.value}, + }, +]; + +const data: Row[] = [{ label: 'URL', value: 'https://example.com/a/b/c' }]; + +const cells = (container: HTMLElement) => + Array.from(container.querySelectorAll('tbody td')); + +describe('Table', () => { + it('pins sized columns to their pixel width in the default fixed layout', () => { + const { container } = render( + , + ); + + const [label, value] = cells(container); + expect(label).toHaveStyle({ width: '260px' }); + expect(value).toHaveStyle({ width: '100%' }); + }); +}); diff --git a/packages/app/src/hooks/useQueriedDashboardFilterValues.tsx b/packages/app/src/hooks/useQueriedDashboardFilterValues.tsx index 9023b91ac7..8f8f092a99 100644 --- a/packages/app/src/hooks/useQueriedDashboardFilterValues.tsx +++ b/packages/app/src/hooks/useQueriedDashboardFilterValues.tsx @@ -7,6 +7,7 @@ import { } from '@hyperdx/common-utils/dist/core/materializedViews'; import { FilterSelection } from '@hyperdx/common-utils/dist/dashboardFilterValues'; import { + FilterRange, FilterState, ResolvedFilterValuesQuery, resolveFilterValuesWhere, @@ -65,6 +66,43 @@ const resolvedFor = ( whereLanguage: filter.whereLanguage ?? 'sql', }; +/** + * Tighter of two ranges. Either bound may be absent now that a filter can be + * one-sided (`duration > 1000`), and absent means unbounded, so the side that + * states a bound wins outright. Where both state the same edge at the same + * value, the strict operator is the tighter one; `>=` and `<=` are the + * defaults and stay implicit. + */ +const intersectRange = (a: FilterRange, b: FilterRange): FilterRange => { + const range: FilterRange = {}; + + const min = + a.min != null && b.min != null ? Math.max(a.min, b.min) : (a.min ?? b.min); + if (min != null) { + range.min = min; + if ( + (a.min === min && a.minOp === '>') || + (b.min === min && b.minOp === '>') + ) { + range.minOp = '>'; + } + } + + const max = + a.max != null && b.max != null ? Math.min(a.max, b.max) : (a.max ?? b.max); + if (max != null) { + range.max = max; + if ( + (a.max === max && a.maxOp === '<') || + (b.max === max && b.maxOp === '<') + ) { + range.maxOp = '<'; + } + } + + return range; +}; + /** * AND two constraints on the same column together. Reached when two filter * definitions share an expression but hold independent selections: both narrow @@ -85,10 +123,7 @@ const intersectSelections = ( excluded: new Set([...a.excluded, ...b.excluded]), range: a.range && b.range - ? { - min: Math.max(a.range.min, b.range.min), - max: Math.min(a.range.max, b.range.max), - } + ? intersectRange(a.range, b.range) : (a.range ?? b.range), }); diff --git a/packages/app/src/theme/SemanticVariants.stories.tsx b/packages/app/src/theme/SemanticVariants.stories.tsx index 736d4b4dab..5e77414faa 100644 --- a/packages/app/src/theme/SemanticVariants.stories.tsx +++ b/packages/app/src/theme/SemanticVariants.stories.tsx @@ -21,8 +21,8 @@ const story = { }; export default story; -// Variants exposed as Button / ActionIcon (only danger). -const CONTROL_VARIANTS = ['danger'] as const; +// Variants exposed as Button / ActionIcon (danger + warning). +const CONTROL_VARIANTS = ['danger', 'warning'] as const; // Variants supported by Text. const TEXT_VARIANTS = ['danger', 'warning', 'success'] as const; diff --git a/packages/app/src/theme/themes/clickstack/_tokens.scss b/packages/app/src/theme/themes/clickstack/_tokens.scss index b88e7a86d5..ee17264489 100644 --- a/packages/app/src/theme/themes/clickstack/_tokens.scss +++ b/packages/app/src/theme/themes/clickstack/_tokens.scss @@ -135,6 +135,12 @@ --color-bg-field: var(--click-field-color-background-default); --color-bg-field-highlighted: var(--palette-neutral-650); --color-bg-field-addon: var(--palette-neutral-712); + + /* Hover for a control that sits on the surface rather than inside a field — + the "Group by " pickers. Dark mode lifts off the surface where + light mode steps down from white, so this lands on the addon here and + stays above it there; both end up a shade short of the label. */ + --color-bg-control-hover: var(--palette-neutral-725); --color-bg-neutral: #4d4f66; --color-bg-success: #22c55e; --color-bg-danger: #ef4444; @@ -358,9 +364,16 @@ (Select/Combobox options, Menu items, popover/dropdown lists) when it is hovered or is the currently-selected entry. */ --color-bg-option-active: var(--click-global-color-background-muted); + --color-bg-field: var(--click-field-color-background-default); --color-bg-field-highlighted: var(--click-global-color-background-default); --color-bg-field-addon: var(--click-global-color-background-default); + + /* Hover for a control that sits on the surface rather than inside a field — + the "Group by " pickers. Stops short of the addon so the two halves + stay legible when hovered: the value is dimmed whenever it is showing a + default, and dimmed text on the addon grey is a poor read. */ + --color-bg-control-hover: var(--palette-slate-50); --color-bg-neutral: #adb5bd; --color-bg-success: #16a34a; --color-bg-danger: #dc2626; diff --git a/packages/app/src/theme/themes/hyperdx/_tokens.scss b/packages/app/src/theme/themes/hyperdx/_tokens.scss index d185840999..1ec0db38fd 100644 --- a/packages/app/src/theme/themes/hyperdx/_tokens.scss +++ b/packages/app/src/theme/themes/hyperdx/_tokens.scss @@ -176,6 +176,7 @@ (Select/Combobox options, Menu items, popover/dropdown lists) when it is hovered or is the currently-selected entry. */ --color-bg-option-active: var(--color-bg-sidenav-link-active); + --color-bg-field: #f9f9fc; --color-bg-field-highlighted: var(--mantine-color-white); --color-bg-field-addon: var(--mantine-color-white); diff --git a/packages/app/src/theme/themes/semanticVariants.ts b/packages/app/src/theme/themes/semanticVariants.ts index d1c03ab262..d693c28247 100644 --- a/packages/app/src/theme/themes/semanticVariants.ts +++ b/packages/app/src/theme/themes/semanticVariants.ts @@ -20,7 +20,7 @@ export const SEMANTIC_TEXT_COLORS: Record = { * tokens so the control reads as a soft, accessible tint rather than a solid * fill, with the semantic text token as the foreground. * - * Only `danger` is exposed as a control variant; `warning`/`success` remain + * `danger` and `warning` are exposed as control variants; `success` remains * available for `Text` and `Alert` but not as buttons. */ export const SEMANTIC_CONTROL_COLORS: Record< @@ -32,6 +32,11 @@ export const SEMANTIC_CONTROL_COLORS: Record< hover: 'var(--color-bg-danger-subtle-hover)', color: 'var(--color-text-danger)', }, + warning: { + bg: 'var(--color-bg-warning-subtle)', + hover: 'var(--color-bg-warning-subtle-hover)', + color: 'var(--color-text-warning)', + }, }; /** diff --git a/packages/app/src/utils/__tests__/metricPivot.test.ts b/packages/app/src/utils/__tests__/metricPivot.test.ts new file mode 100644 index 0000000000..5b0021b920 --- /dev/null +++ b/packages/app/src/utils/__tests__/metricPivot.test.ts @@ -0,0 +1,154 @@ +import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; + +import { translateMetricGroupFilters } from '@/utils/metricPivot'; + +const metricSource = { + id: 'metrics', + kind: SourceKind.Metric, + name: 'Metrics', + connection: 'conn', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + metricTables: { gauge: 'otel_metrics_gauge' }, + resourceAttributesExpression: 'ResourceAttributes', +} as unknown as TSource; + +const logSource = { + id: 'logs', + kind: SourceKind.Log, + name: 'Logs', + connection: 'conn', + from: { databaseName: 'default', tableName: 'otel_logs' }, + timestampValueExpression: 'Timestamp', + resourceAttributesExpression: 'ResourceAttributes', +} as unknown as TSource; + +describe('translateMetricGroupFilters', () => { + it('carries a resource attribute across to the target source', () => { + const { filters, droppedColumns } = translateMetricGroupFilters({ + groupFilters: [ + { + column: "ResourceAttributes['k8s.pod.name']", + value: 'payment-7d9f4', + }, + ], + metricSource, + targetSource: logSource, + }); + + expect(filters).toEqual([ + { column: "ResourceAttributes['k8s.pod.name']", value: 'payment-7d9f4' }, + ]); + expect(droppedColumns).toEqual([]); + }); + + it('rewrites the map expression when the two sources name it differently', () => { + const { filters } = translateMetricGroupFilters({ + groupFilters: [ + { column: "ResourceAttributes['service.name']", value: 'checkout' }, + ], + metricSource, + targetSource: { + ...logSource, + resourceAttributesExpression: 'resource_attributes', + } as TSource, + }); + + expect(filters).toEqual([ + { column: "resource_attributes['service.name']", value: 'checkout' }, + ]); + }); + + it('drops a metric data-point attribute rather than guessing a counterpart', () => { + const { filters, droppedColumns } = translateMetricGroupFilters({ + groupFilters: [ + { column: "Attributes['http.route']", value: '/checkout' }, + { column: "ResourceAttributes['service.name']", value: 'checkout' }, + ], + metricSource, + targetSource: logSource, + }); + + expect(filters).toEqual([ + { column: "ResourceAttributes['service.name']", value: 'checkout' }, + ]); + expect(droppedColumns).toEqual(["Attributes['http.route']"]); + }); + + it('drops a bare column, which has no key to re-address', () => { + const { filters, droppedColumns } = translateMetricGroupFilters({ + groupFilters: [{ column: 'ServiceName', value: 'checkout' }], + metricSource, + targetSource: logSource, + }); + + expect(filters).toEqual([]); + expect(droppedColumns).toEqual(['ServiceName']); + }); + + it('drops everything when the target declares no resource attributes', () => { + const { filters, droppedColumns } = translateMetricGroupFilters({ + groupFilters: [ + { column: "ResourceAttributes['service.name']", value: 'checkout' }, + ], + metricSource, + targetSource: { + ...logSource, + resourceAttributesExpression: undefined, + } as TSource, + }); + + expect(filters).toEqual([]); + expect(droppedColumns).toEqual(["ResourceAttributes['service.name']"]); + }); + + it('drops everything when there is no target source at all', () => { + const { filters, droppedColumns } = translateMetricGroupFilters({ + groupFilters: [ + { column: "ResourceAttributes['service.name']", value: 'checkout' }, + ], + metricSource, + targetSource: undefined, + }); + + expect(filters).toEqual([]); + expect(droppedColumns).toEqual(["ResourceAttributes['service.name']"]); + }); + + it('skips a series whose group value is null', () => { + const { filters, droppedColumns } = translateMetricGroupFilters({ + groupFilters: [ + { column: "ResourceAttributes['service.name']", value: null }, + ], + metricSource, + targetSource: logSource, + }); + + expect(filters).toEqual([]); + expect(droppedColumns).toEqual([]); + }); + + it('tolerates whitespace and double-quoted keys', () => { + const { filters } = translateMetricGroupFilters({ + groupFilters: [ + { column: ' ResourceAttributes [ "host.name" ] ', value: 'node-1' }, + ], + metricSource, + targetSource: logSource, + }); + + expect(filters).toEqual([ + { column: 'ResourceAttributes["host.name"]', value: 'node-1' }, + ]); + }); + + it('returns empty for no group filters', () => { + expect( + translateMetricGroupFilters({ + groupFilters: undefined, + metricSource, + targetSource: logSource, + }), + ).toEqual({ filters: [], droppedColumns: [] }); + }); +}); diff --git a/packages/app/src/utils/__tests__/metricTableConnections.test.ts b/packages/app/src/utils/__tests__/metricTableConnections.test.ts new file mode 100644 index 0000000000..bb6332f4c2 --- /dev/null +++ b/packages/app/src/utils/__tests__/metricTableConnections.test.ts @@ -0,0 +1,90 @@ +import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; + +import { metricTableConnections } from '@/utils/metricTableConnections'; + +const metricSource = { + id: 'metrics', + kind: SourceKind.Metric, + name: 'Metrics', + connection: 'conn', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + metricTables: { + gauge: 'otel_metrics_gauge', + sum: 'otel_metrics_sum', + }, + resourceAttributesExpression: 'ResourceAttributes', +} as unknown as TSource; + +const logSource = { + id: 'logs', + kind: SourceKind.Log, + name: 'Logs', + connection: 'conn', + from: { databaseName: 'default', tableName: 'otel_logs' }, + timestampValueExpression: 'Timestamp', +} as unknown as TSource; + +describe('metricTableConnections', () => { + it('resolves one connection per series, carrying the metric name', () => { + expect( + metricTableConnections(metricSource, [ + { metricType: 'gauge', metricName: 'k8s.pod.cpu' }, + { metricType: 'sum', metricName: 'http.requests' }, + ]), + ).toEqual([ + { + databaseName: 'default', + tableName: 'otel_metrics_gauge', + connectionId: 'conn', + metricName: 'k8s.pod.cpu', + }, + { + databaseName: 'default', + tableName: 'otel_metrics_sum', + connectionId: 'conn', + metricName: 'http.requests', + }, + ]); + }); + + it('dedupes series that resolve to the same table and metric', () => { + const connections = metricTableConnections(metricSource, [ + { metricType: 'gauge', metricName: 'k8s.pod.cpu' }, + { metricType: 'gauge', metricName: 'k8s.pod.cpu' }, + ]); + + expect(connections).toHaveLength(1); + }); + + it('skips series with no metric picked yet', () => { + expect( + metricTableConnections(metricSource, [ + { metricType: 'gauge' }, + { metricName: 'k8s.pod.cpu' }, + {}, + ]), + ).toEqual([]); + }); + + it('skips a metric type the source has no table for', () => { + expect( + metricTableConnections(metricSource, [ + { metricType: 'histogram', metricName: 'http.duration' }, + ]), + ).toEqual([]); + }); + + it('returns nothing for a non-metric source, so callers fall back', () => { + expect( + metricTableConnections(logSource, [ + { metricType: 'gauge', metricName: 'k8s.pod.cpu' }, + ]), + ).toEqual([]); + }); + + it('returns nothing when there is no source or no series', () => { + expect(metricTableConnections(undefined, [])).toEqual([]); + expect(metricTableConnections(metricSource, undefined)).toEqual([]); + }); +}); diff --git a/packages/app/src/utils/metricPivot.ts b/packages/app/src/utils/metricPivot.ts new file mode 100644 index 0000000000..d1c5bc2793 --- /dev/null +++ b/packages/app/src/utils/metricPivot.ts @@ -0,0 +1,73 @@ +import { TSource } from '@hyperdx/common-utils/dist/types'; + +/** A group column / value pair decoded from a chart series key. */ +export type GroupFilter = { column: string; value: any }; + +export type TranslatedGroupFilters = { + /** Filters that address the same attribute on the target source. */ + filters: GroupFilter[]; + /** Columns with no counterpart on the target source; not carried over. */ + droppedColumns: string[]; +}; + +/** + * `ResourceAttributes['k8s.pod.name']` split into its map expression and its + * quoted key. Anything else — a bare column, a function call — has no key to + * re-address and so cannot be translated. + */ +const ATTRIBUTE_LOOKUP = /^\s*(.+?)\s*\[\s*('(?:[^']|'')*'|"[^"]*")\s*\]\s*$/; + +/** Not every source kind has resource attributes — PromQL sources have none. */ +function resourceAttributesExpression( + source: TSource | undefined, +): string | undefined { + if (!source || !('resourceAttributesExpression' in source)) return undefined; + return source.resourceAttributesExpression?.trim() || undefined; +} + +/** + * Re-address a metric chart's group-by columns against the source that holds + * the rows. + * + * Clicking one series of a metric chart asks "what was this pod doing?", so the + * pod has to survive the jump to logs or traces. The two sources name their + * attribute maps independently — one may call it `ResourceAttributes` and the + * other `resource_attributes` — so the map expression is swapped while the key + * is kept. + * + * Only resource attributes are carried. They identify the entity that emitted + * the telemetry and OpenTelemetry keeps them consistent across signals, so a + * pod name means the same thing on both sides. A metric's own data-point + * attributes have no such guarantee: a key that exists on the metric and not on + * the log table yields a filter that matches nothing, which reads as "this pod + * logged nothing" rather than "that filter was nonsense". Those are dropped and + * reported instead. + */ +export function translateMetricGroupFilters({ + groupFilters, + metricSource, + targetSource, +}: { + groupFilters: GroupFilter[] | undefined; + metricSource: TSource | undefined; + targetSource: TSource | undefined; +}): TranslatedGroupFilters { + const from = resourceAttributesExpression(metricSource); + const to = resourceAttributesExpression(targetSource); + + const filters: GroupFilter[] = []; + const droppedColumns: string[] = []; + + for (const filter of groupFilters ?? []) { + if (!filter?.column || filter.value == null) continue; + + const match = from && to ? ATTRIBUTE_LOOKUP.exec(filter.column) : null; + if (match && match[1].trim() === from) { + filters.push({ column: `${to}[${match[2]}]`, value: filter.value }); + } else { + droppedColumns.push(filter.column); + } + } + + return { filters, droppedColumns }; +} diff --git a/packages/app/src/utils/metricTableConnections.ts b/packages/app/src/utils/metricTableConnections.ts new file mode 100644 index 0000000000..6bc37a3438 --- /dev/null +++ b/packages/app/src/utils/metricTableConnections.ts @@ -0,0 +1,49 @@ +import type { TableConnection } from '@hyperdx/common-utils/dist/core/metadata'; +import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; + +import { getMetricTableName } from '@/utils'; + +/** The bit of a chart series this needs: which metric, of which type. */ +export type MetricSeriesRef = { + metricType?: string; + metricName?: string; +}; + +/** + * The tables backing a metric source's currently selected series. + * + * A metric source has no usable `from.tableName` — it fans out to one table per + * metric type — so anything that reads schema from the source (field + * autocomplete, the filter sidebar) has to ask the per-series tables instead. + * Each connection carries its `metricName` as well, because one metric table + * holds every metric of that type and the attributes differ per metric. + * + * Returns an empty array for non-metric sources, and for metric sources whose + * series have no metric picked yet; callers fall back to `tcFromSource`. + */ +export function metricTableConnections( + source: TSource | undefined, + series: MetricSeriesRef[] | undefined, +): TableConnection[] { + if (source?.kind !== SourceKind.Metric || !Array.isArray(series)) { + return []; + } + + const seen = new Set(); + const connections: TableConnection[] = []; + for (const s of series) { + if (!s?.metricType || !s?.metricName) continue; + const tableName = getMetricTableName(source, s.metricType); + if (!tableName) continue; + const key = `${tableName}::${s.metricName}`; + if (seen.has(key)) continue; + seen.add(key); + connections.push({ + databaseName: source.from.databaseName, + tableName, + connectionId: source.connection, + metricName: s.metricName, + }); + } + return connections; +} diff --git a/packages/app/styles/LogTable.module.scss b/packages/app/styles/LogTable.module.scss index af448de981..7a458ba774 100644 --- a/packages/app/styles/LogTable.module.scss +++ b/packages/app/styles/LogTable.module.scss @@ -32,6 +32,28 @@ $button-height: 18px; height: 24px; } +.framed { + border: 1px solid var(--color-border); + border-radius: var(--mantine-radius-sm); + overflow: hidden; + + .tableHead { + background: var(--color-bg-muted); + + th { + border-bottom: 1px solid var(--color-border); + } + } +} + +.tableBar { + flex-shrink: 0; + min-height: 34px; + padding: 4px var(--mantine-spacing-xs); + background: var(--color-bg-muted); + border-bottom: 1px solid var(--color-border); +} + .tableRow { position: relative; @@ -90,6 +112,12 @@ $button-height: 18px; opacity: 1; } +// A table with a bar has an explicit on/off for checkboxes, so when they are +// on they stay on rather than waiting for hover. +.framed .selectCell { + opacity: 1; +} + // Touch has no hover to reveal with. @media (hover: none) { .selectCell { diff --git a/packages/app/tests/e2e/features/explore-metrics.spec.ts b/packages/app/tests/e2e/features/explore-metrics.spec.ts new file mode 100644 index 0000000000..0c2bcbccba --- /dev/null +++ b/packages/app/tests/e2e/features/explore-metrics.spec.ts @@ -0,0 +1,91 @@ +/** + * Grouped time-series tests for the Explore page with a metric source. + * + * Seed: "E2E Metrics" gauge table with k8s pod/namespace metrics, grouped by + * namespace → produces ≥ 2 series. Correlations: E2E Metrics → E2E Logs → + * E2E Traces (via logSourceId / traceSourceId in the fixture). + */ +import { ExplorePage } from '../page-objects/ExplorePage'; +import { expect, test } from '../utils/base-test'; +import { DEFAULT_METRICS_SOURCE_NAME } from '../utils/constants'; + +const METRIC_NAME = 'k8s.pod.phase'; +// Value format mirrors MetricNameSelect's internal SEPARATOR (7 colons + type). +const METRIC_VALUE = 'k8s.pod.phase:::::::gauge'; +const GROUP_BY_FIELD = "ResourceAttributes['k8s.namespace.name']"; + +test.describe( + 'Explore metrics grouped time-series', + { tag: ['@full-stack', '@explore'] }, + () => { + let explorePage: ExplorePage; + + test.beforeEach(async ({ page }) => { + explorePage = new ExplorePage(page); + // Navigate via UI, not URL params: in full-stack mode the source URL + // param expects a MongoDB ObjectId, not the human-readable name. + await explorePage.goto(); + await explorePage.selectSource(DEFAULT_METRICS_SOURCE_NAME); + await explorePage.selectMetric(METRIC_NAME, METRIC_VALUE); + await explorePage.setGroupBy(GROUP_BY_FIELD); + await explorePage.runQuery(); + }); + + test('renders a grouped time-series chart with at least two series', async () => { + await expect(explorePage.getChartContainer()).toBeVisible(); + await expect + .poll(() => explorePage.countChartSeries(), { timeout: 10000 }) + .toBeGreaterThanOrEqual(2); + }); + + test('chart settings drawer exposes the alignment control and accepts a change', async () => { + await explorePage.openChartSettings(); + await expect(explorePage.getChartSettingsDrawer()).toBeVisible({ + timeout: 5000, + }); + await expect(explorePage.getAlignmentControl()).toBeVisible(); + await explorePage.selectGranularity('1 hour'); + await explorePage.applyChartSettings(); + await expect(explorePage.getChartSettingsDrawer()).toBeHidden({ + timeout: 5000, + }); + }); + + test('pinned tooltip shows correlation links and a series filter action', async () => { + await explorePage.clickChartToPin(); + await expect(explorePage.getPinnedTooltip()).toBeVisible({ + timeout: 5000, + }); + + await expect(explorePage.getFirstSeriesActionsButton()).toBeVisible({ + timeout: 5000, + }); + await expect + .poll(() => explorePage.countSeriesActionButtons(), { timeout: 5000 }) + .toBeGreaterThanOrEqual(2); + + const dataKey = await explorePage.openFirstSeriesActionsMenu(); + expect(dataKey).toBeTruthy(); + + await expect(explorePage.getViewRelatedLogsLink(dataKey)).toBeVisible({ + timeout: 3000, + }); + const logsUrl = await explorePage.getRelatedLogsUrl(dataKey); + expect(logsUrl?.pathname).toBe('/explore'); + // source is a MongoDB ObjectId in full-stack mode, not the source name + expect(logsUrl?.searchParams.get('source')).toBeTruthy(); + + await expect(explorePage.getViewRelatedTracesLink(dataKey)).toBeVisible({ + timeout: 3000, + }); + const tracesUrl = await explorePage.getRelatedTracesUrl(dataKey); + expect(tracesUrl?.pathname).toBe('/explore'); + expect(tracesUrl?.searchParams.get('source')).toBeTruthy(); + + await explorePage.filterToSeries(dataKey); + await expect(explorePage.getFirstActiveFilterPill()).toBeVisible({ + timeout: 8000, + }); + }); + }, +); diff --git a/packages/app/tests/e2e/features/lucene-autocomplete.spec.ts b/packages/app/tests/e2e/features/lucene-autocomplete.spec.ts index 275ce498a2..31ee2a599e 100644 --- a/packages/app/tests/e2e/features/lucene-autocomplete.spec.ts +++ b/packages/app/tests/e2e/features/lucene-autocomplete.spec.ts @@ -6,7 +6,7 @@ * now passes a source id; the dashboard-wide WHERE spans every tile and has * none, so it still leans on the connection. Both are covered here because * this exercises the real query path end to end, which the unit tests can't: - * they mock out the very `useAllFields` call that was never running. + * they mock out the very `useMultipleAllFields` call that was never running. * * The other inputs the same fix touched are covered where they live: the * services dashboard, sessions, the trace waterfall's span/log filters and the diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index c4a0035379..eeba701d77 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -57,7 +57,8 @@ type SeriesType = | 'pie' | 'event_patterns' | 'bar' - | 'heatmap'; + | 'heatmap' + | 'treemap'; /** * Series data structure for chart verification diff --git a/packages/app/tests/e2e/page-objects/ExplorePage.ts b/packages/app/tests/e2e/page-objects/ExplorePage.ts new file mode 100644 index 0000000000..80dd627f9e --- /dev/null +++ b/packages/app/tests/e2e/page-objects/ExplorePage.ts @@ -0,0 +1,181 @@ +import type { Locator, Page } from '@playwright/test'; + +import { ChartEditorComponent } from '../components/ChartEditorComponent'; + +/** + * Page object for the /explore route. + * + * Delegates source/metric selection to ChartEditorComponent (same test-IDs as + * the dashboard tile editor) and adds explore-specific methods. + */ +export class ExplorePage { + readonly page: Page; + readonly chartEditor: ChartEditorComponent; + + constructor(page: Page) { + this.page = page; + this.chartEditor = new ChartEditorComponent(page); + } + + async goto(params?: Record) { + const search = params ? '?' + new URLSearchParams(params).toString() : ''; + await this.page.goto(`/explore${search}`); + } + + async selectSource(name: string) { + await this.chartEditor.selectSource(name); + } + + async selectMetric(metricName: string, metricValue?: string) { + await this.chartEditor.selectMetric(metricName, metricValue); + } + + /** + * Set the group-by via the ExploreGroupByControl FieldPicker. + * Opens the popover, switches to SQL mode, types the expression, then applies. + */ + async setGroupBy(expression: string) { + await this.page.getByTestId('explore-group-by-target').click(); + // Scope to the group-by dialog by role; the accessible name starts with + // "Group by" and may change as a value is selected, so match with a regex. + const dropdown = this.page.getByRole('dialog', { name: /^Group by/i }); + await dropdown.waitFor({ state: 'visible', timeout: 5000 }); + // Mantine SegmentedControl hides the actual radio inputs; click the label. + await dropdown.getByText('SQL', { exact: true }).click(); + const sqlEditor = dropdown.locator('.cm-content'); + await sqlEditor.waitFor({ state: 'visible', timeout: 5000 }); + await sqlEditor.click(); + await this.page.keyboard.press('ControlOrMeta+A'); + await this.page.keyboard.press('Delete'); + await this.page.keyboard.type(expression); + await dropdown.getByRole('button', { name: 'Apply', exact: true }).click(); + await dropdown.waitFor({ state: 'hidden', timeout: 5000 }); + } + + /** Uses search-submit-button, not the chart-run-query-button from the tile editor. */ + async runQuery() { + await this.page.getByTestId('search-submit-button').click(); + await this.page + .locator('.recharts-responsive-container') + .first() + .waitFor({ state: 'visible', timeout: 15000 }); + } + + getChartContainer(): Locator { + return this.page.locator('.recharts-responsive-container').first(); + } + + async countChartSeries(): Promise { + return this.page + .locator('.recharts-line, .recharts-area, .recharts-bar') + .count(); + } + + async clickChartToPin() { + const surface = this.page.locator('.recharts-surface').first(); + await surface.waitFor({ state: 'visible', timeout: 10000 }); + const box = await surface.boundingBox(); + if (!box) throw new Error('recharts-surface bounding box is null'); + const x = box.x + box.width * 0.5; + const y = box.y + box.height * 0.4; + // Move first so recharts processes the mousemove and records an active + // coordinate; wait for the hover tooltip to confirm it before clicking. + await this.page.mouse.move(x, y); + await this.page + .getByTestId('chart-tooltip') + .waitFor({ state: 'visible', timeout: 5000 }); + await this.page.mouse.click(x, y); + } + + /** Only rendered when the active source is a metric source. */ + async openChartSettings() { + await this.page.getByTestId('explore-chart-settings-button').click(); + } + + getChartSettingsDrawer(): Locator { + return this.page.getByRole('dialog'); + } + + /** "Alignment" is the label on the GranularityPicker Select, not a checkbox. */ + getAlignmentControl(): Locator { + return this.getChartSettingsDrawer().getByText('Alignment', { + exact: true, + }); + } + + async selectGranularity(label: string) { + await this.getChartSettingsDrawer() + .getByTestId('granularity-picker') + .click(); + await this.page.getByRole('option', { name: label, exact: true }).click(); + } + + async applyChartSettings() { + await this.getChartSettingsDrawer() + .getByTestId('display-settings-apply-button') + .click(); + } + + getPinnedTooltip(): Locator { + return this.page.getByTestId('chart-tooltip'); + } + + private getSeriesActionsButtons(): Locator { + return this.getPinnedTooltip().locator( + '[data-testid^="chart-series-actions-"]', + ); + } + + getFirstSeriesActionsButton(): Locator { + return this.getSeriesActionsButtons().first(); + } + + async countSeriesActionButtons(): Promise { + return this.getSeriesActionsButtons().count(); + } + + /** + * Open the first series action menu and return the dataKey from its test-id. + * Leaves the menu open for the caller to interact with. + */ + async openFirstSeriesActionsMenu(): Promise { + const btn = this.getFirstSeriesActionsButton(); + await btn.waitFor({ state: 'visible', timeout: 5000 }); + const testId = (await btn.getAttribute('data-testid')) ?? ''; + const dataKey = testId.replace('chart-series-actions-', ''); + await btn.click(); + return dataKey; + } + + getViewRelatedLogsLink(dataKey: string): Locator { + return this.page.getByTestId(`chart-view-events-link-${dataKey}`); + } + + getViewRelatedTracesLink(dataKey: string): Locator { + return this.page.getByTestId(`chart-view-traces-link-${dataKey}`); + } + + /** + * Parse the "View related logs" href into a URL. + * `source` is a MongoDB ObjectId in full-stack mode, not a human-readable name. + */ + async getRelatedLogsUrl(dataKey: string): Promise { + const href = + await this.getViewRelatedLogsLink(dataKey).getAttribute('href'); + return href ? new URL(href, 'http://localhost') : null; + } + + async getRelatedTracesUrl(dataKey: string): Promise { + const href = + await this.getViewRelatedTracesLink(dataKey).getAttribute('href'); + return href ? new URL(href, 'http://localhost') : null; + } + + async filterToSeries(dataKey: string) { + await this.page.getByTestId(`chart-focus-series-${dataKey}`).click(); + } + + getFirstActiveFilterPill(): Locator { + return this.page.locator('[data-testid^="active-filter-pill-"]').first(); + } +} diff --git a/packages/cli/src/shared/tileConfig.ts b/packages/cli/src/shared/tileConfig.ts index b8b1fea940..e7f624c55c 100644 --- a/packages/cli/src/shared/tileConfig.ts +++ b/packages/cli/src/shared/tileConfig.ts @@ -380,6 +380,7 @@ export function convertTileConfigForQuery( return convertToTableChartConfig(config); case DisplayType.Pie: case DisplayType.Bar: + case DisplayType.Treemap: // Raw SQL configs pass through untouched (matches CategoricalChart) return isBuilderChartConfig(config) ? (convertToCategoricalChartConfig(config) as ChartConfigWithDateRange & diff --git a/packages/common-utils/package.json b/packages/common-utils/package.json index 4731a91a6f..e076b0db18 100644 --- a/packages/common-utils/package.json +++ b/packages/common-utils/package.json @@ -50,7 +50,7 @@ "dev:build": "tsup && tsc --emitDeclarationOnly --declaration && tsc-alias", "build": "tsup", "ci:build": "tsup", - "lint": "npx eslint . --ext .ts --max-warnings 80", + "lint": "npx eslint . --ext .ts --max-warnings 81", "lint:fix": "npx eslint . --ext .ts --fix", "ci:lint": "yarn lint && yarn tsc --noEmit", "ci:unit": "jest --ci --coverage", diff --git a/packages/common-utils/src/__tests__/builderToRawSql.test.ts b/packages/common-utils/src/__tests__/builderToRawSql.test.ts index aa4d24d7d2..e1d82c227c 100644 --- a/packages/common-utils/src/__tests__/builderToRawSql.test.ts +++ b/packages/common-utils/src/__tests__/builderToRawSql.test.ts @@ -111,6 +111,30 @@ describe('renderBuilderConfigAsSqlTemplate', () => { expect(sql).not.toContain('HYPERDX_PARAM_'); }); + it('renders plain, unaggregated columns with their sort for a row list', async () => { + const sql = await renderBuilderConfigAsSqlTemplate( + { + ...baseLineConfig, + displayType: DisplayType.Table, + granularity: undefined, + groupBy: undefined, + select: [ + { aggCondition: '', valueExpression: 'timestamp' }, + { aggCondition: '', valueExpression: 'ServiceName as service' }, + ], + orderBy: 'timestamp DESC', + }, + mockMetadata, + ); + + expect(sql).not.toBeNull(); + expect(sql).toMatch(/SELECT\s+timestamp,\s+ServiceName AS `service`/); + expect(sql).not.toContain('count()'); + expect(sql).not.toMatch(/GROUP BY/); + expect(sql).toMatch(/ORDER BY\s+timestamp DESC/); + expect(sql).toMatch(/LIMIT\s+200/); + }); + it('strips granularity for a table chart even when the form carries one', async () => { const sql = await renderBuilderConfigAsSqlTemplate( { ...baseLineConfig, displayType: DisplayType.Table }, diff --git a/packages/common-utils/src/__tests__/filters.test.ts b/packages/common-utils/src/__tests__/filters.test.ts index 5cfe4683cd..6ca023f84b 100644 --- a/packages/common-utils/src/__tests__/filters.test.ts +++ b/packages/common-utils/src/__tests__/filters.test.ts @@ -140,6 +140,36 @@ describe('filters', () => { ]); }); + it('emits one-sided comparisons for open-ended ranges', () => { + expect( + filtersToQuery({ + Duration: { + included: new Set(), + excluded: new Set(), + range: { min: 1_000_000_000, minOp: '>' }, + }, + }), + ).toEqual([{ type: 'sql', condition: 'Duration > 1000000000' }]); + expect( + filtersToQuery({ + status: { + included: new Set(), + excluded: new Set(), + range: { min: 500, minOp: '>=' }, + }, + }), + ).toEqual([{ type: 'sql', condition: 'status >= 500' }]); + expect( + filtersToQuery({ + Duration: { + included: new Set(), + excluded: new Set(), + range: { max: 100, maxOp: '<' }, + }, + }), + ).toEqual([{ type: 'sql', condition: 'Duration < 100' }]); + }); + it('should escape single quotes in excluded filter values', () => { const filters = { message: { @@ -1228,6 +1258,28 @@ describe('filters', () => { }); }); + it('parses one-sided numeric comparisons into a range', () => { + expect( + parseQuery([{ type: 'sql', condition: 'Duration > 1000000000' }]) + .filters, + ).toEqual({ + Duration: { + included: new Set(), + excluded: new Set(), + range: { min: 1000000000, minOp: '>' }, + }, + }); + expect( + parseQuery([{ type: 'sql', condition: 'status >= 500' }]).filters, + ).toEqual({ + status: { + included: new Set(), + excluded: new Set(), + range: { min: 500, minOp: '>=' }, + }, + }); + }); + it('drops a BETWEEN with quoted / non-numeric bounds instead of emitting NaN', () => { expect( parseQuery([ @@ -1260,6 +1312,8 @@ describe('filters', () => { ["ServiceName IN ('checkout', 'payments')", 'IN'], ["SeverityText NOT IN ('debug', 'trace')", 'NOT IN'], ['Duration BETWEEN 100 AND 5000', 'BETWEEN (numeric)'], + ['Duration > 1000000000', 'one-sided greater-than'], + ['status >= 500', 'one-sided greater-or-equal'], ["LogAttributes['x'] IN ('y')", 'map-access column'], ["Body IN ('a AND b')", 'value containing AND'], ])('accepts a single renderable predicate: %s (%s)', condition => { diff --git a/packages/common-utils/src/__tests__/renderChartConfig.test.ts b/packages/common-utils/src/__tests__/renderChartConfig.test.ts index 41aea39ed9..6f510af3f0 100644 --- a/packages/common-utils/src/__tests__/renderChartConfig.test.ts +++ b/packages/common-utils/src/__tests__/renderChartConfig.test.ts @@ -928,6 +928,73 @@ describe('renderChartConfig', () => { }); describe('histogram metric queries', () => { + describe('a series with no column picked', () => { + const configWithValueExpression = ( + valueExpression: string, + ): ChartConfigWithOptDateRange => ({ + displayType: DisplayType.Line, + connection: 'test-connection', + from: { databaseName: 'default', tableName: 'otel_logs' }, + select: [{ aggFn: 'quantile', level: 0.99, valueExpression }], + where: '', + whereLanguage: 'sql', + timestampValueExpression: 'Timestamp', + dateRange: [new Date('2025-01-01'), new Date('2025-01-02')], + }); + + it('is rejected rather than sent as `toString()` with no argument', async () => { + await expect( + renderChartConfig( + configWithValueExpression(''), + mockMetadata, + querySettings, + ), + ).rejects.toThrow( + 'Column is required for all non-count aggregation functions', + ); + }); + + it('is still rejected when the column is only whitespace', async () => { + await expect( + renderChartConfig( + configWithValueExpression(' '), + mockMetadata, + querySettings, + ), + ).rejects.toThrow( + 'Column is required for all non-count aggregation functions', + ); + }); + + it('renders once a column is there', async () => { + const sql = parameterizedQueryToSql( + await renderChartConfig( + configWithValueExpression('Duration'), + mockMetadata, + querySettings, + ), + ); + + expect(sql).toContain('quantile(0.99)'); + expect(sql).toContain('Duration'); + }); + + it('leaves count alone, which takes no argument', async () => { + const sql = parameterizedQueryToSql( + await renderChartConfig( + { + ...configWithValueExpression(''), + select: [{ aggFn: 'count', valueExpression: '' }], + }, + mockMetadata, + querySettings, + ), + ); + + expect(sql).toContain('count()'); + }); + }); + describe('quantile', () => { it('should generate a whole-range query without a time dimension', async () => { const config: ChartConfigWithOptDateRange = { diff --git a/packages/common-utils/src/core/builderToRawSql.ts b/packages/common-utils/src/core/builderToRawSql.ts index ce3320fe77..5dfbe232d3 100644 --- a/packages/common-utils/src/core/builderToRawSql.ts +++ b/packages/common-utils/src/core/builderToRawSql.ts @@ -24,6 +24,7 @@ const RAW_SQL_DISPLAY_TYPES = new Set([ DisplayType.StackedLine, DisplayType.Pie, DisplayType.Bar, + DisplayType.Treemap, DisplayType.Number, ]); @@ -140,7 +141,9 @@ export async function renderBuilderConfigAsSqlTemplate( dateRange: [new Date(0), new Date(0)], }; const renderConfig = - displayType === DisplayType.Pie || displayType === DisplayType.Bar + displayType === DisplayType.Pie || + displayType === DisplayType.Bar || + displayType === DisplayType.Treemap ? convertToCategoricalChartConfig(templateConfig) : displayType === DisplayType.Number ? convertToNumberChartConfig(templateConfig) diff --git a/packages/common-utils/src/core/renderChartConfig.ts b/packages/common-utils/src/core/renderChartConfig.ts index 8de7551337..128bc6c1b9 100644 --- a/packages/common-utils/src/core/renderChartConfig.ts +++ b/packages/common-utils/src/core/renderChartConfig.ts @@ -550,6 +550,17 @@ const aggFnExpr = ({ const isNone = fn === 'none'; const isCount = fn.startsWith('count'); const isWhereUsed = isNonEmptyWhereExpr(where); + + // A blank expression is a half-built series, not an argument. Left alone it + // interpolates to `toString()` and ClickHouse rejects the whole statement + // with an arity error that never mentions the column the user forgot. Treat + // it as the missing column it is — `count` is the one aggregation that takes + // no argument. + if (!isCount && typeof expr === 'string' && expr.trim() === '') { + throw new Error( + 'Column is required for all non-count aggregation functions', + ); + } // Cast to float64 because the expr might not be a number const unsafeExpr = { UNSAFE_RAW_SQL: diff --git a/packages/common-utils/src/filters.ts b/packages/common-utils/src/filters.ts index 78e3d09c66..d03ef32326 100644 --- a/packages/common-utils/src/filters.ts +++ b/packages/common-utils/src/filters.ts @@ -27,14 +27,39 @@ import { substituteVariables, } from '@/variables'; +export type FilterRangeMinOp = '>' | '>='; +export type FilterRangeMaxOp = '<' | '<='; + +/** Numeric range on a column. Two bounds emit BETWEEN; one bound emits `>`/`>=`/`<`/`<=`. */ +export type FilterRange = { + min?: number; + max?: number; + minOp?: FilterRangeMinOp; + maxOp?: FilterRangeMaxOp; +}; + export type FilterState = { [key: string]: { included: Set; excluded: Set; - range?: { min: number; max: number }; // For BETWEEN conditions + range?: FilterRange; }; }; +function rangeToSqlCondition(key: string, range: FilterRange): string | null { + const { min, max, minOp = '>=', maxOp = '<=' } = range; + if (min != null && max != null) { + return `${key} BETWEEN ${min} AND ${max}`; + } + if (min != null) { + return `${key} ${minOp} ${min}`; + } + if (max != null) { + return `${key} ${maxOp} ${max}`; + } + return null; +} + // Wrap a quoted string literal in a ClickHouse expression whose result type // matches the date column's type. const dateTimeValueExpr = (chType: string, quotedValue: string): string => { @@ -110,10 +135,13 @@ export const filtersToQuery = ( }); } if (values.range != null) { - conditions.push({ - type: 'sql' as const, - condition: `${actualKey} BETWEEN ${values.range.min} AND ${values.range.max}`, - }); + const rangeCondition = rangeToSqlCondition(actualKey, values.range); + if (rangeCondition != null) { + conditions.push({ + type: 'sql' as const, + condition: rangeCondition, + }); + } } return conditions; }); @@ -502,7 +530,7 @@ export const parseQuery = ( { included: Set; excluded: Set; - range?: { min: number; max: number }; + range?: FilterRange; } >(); for (const filter of q) { @@ -541,6 +569,35 @@ export const parseQuery = ( } } + const comparisonMatch = filter.condition.match( + /^(.+?)\s*(>=|<=|>|<)\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)\s*$/, + ); + if (comparisonMatch) { + const keyStr = comparisonMatch[1].trim(); + const op = comparisonMatch[2]; + const bound = Number(comparisonMatch[3]); + if ( + keyStr && + Number.isFinite(bound) && + (op === '>' || op === '>=' || op === '<' || op === '<=') + ) { + const range: FilterRange = + op === '>' || op === '>=' + ? { min: bound, minOp: op } + : { max: bound, maxOp: op }; + if (!state.has(keyStr)) { + state.set(keyStr, { + included: new Set(), + excluded: new Set(), + range, + }); + } else { + state.get(keyStr)!.range = range; + } + } + continue; + } + // Extract all simple IN/NOT IN clauses from the condition // This handles both simple conditions and compound conditions with AND/OR const inClauses = extractInClauses(filter.condition); @@ -606,7 +663,8 @@ function countTopLevelAnd(condition: string): number { * * The sidebar only reads `type: 'sql'` conditions in the exact pinned-filter * form filtersToQuery produces — a single `IN (...)`, `NOT IN (...)`, - * or `BETWEEN AND ` predicate. `parseQuery` is deliberately + * `BETWEEN AND `, or one-sided `>/=/<= ` predicate. + * `parseQuery` is deliberately * lenient (it extracts what it can and ignores the rest), so "parses to a * non-empty state" is *not* enough: `col IN ('x') AND foo = 1` would render the * `IN` facet while still executing `AND foo = 1` at query time, so the displayed @@ -642,9 +700,10 @@ export function isRenderablePinnedFilter(filter: Filter): boolean { // a compound), which is not a single renderable facet. if (filtersToQuery(state).length !== 1) return false; - // Catch conjuncts the parser dropped: a single IN/NOT IN has no top-level - // AND, a single BETWEEN has exactly one (its own `min AND max`). - const expectedAnds = state[keys[0]].range ? 1 : 0; + // Catch conjuncts the parser dropped: a single IN/NOT IN or one-sided + // comparison has no top-level AND; a BETWEEN has exactly one (`min AND max`). + const range = state[keys[0]].range; + const expectedAnds = range?.min != null && range?.max != null ? 1 : 0; return countTopLevelAnd(filter.condition) === expectedAnds; } diff --git a/packages/common-utils/src/rawSqlParams.ts b/packages/common-utils/src/rawSqlParams.ts index 91fd1eaf23..47b4bc8fa9 100644 --- a/packages/common-utils/src/rawSqlParams.ts +++ b/packages/common-utils/src/rawSqlParams.ts @@ -90,6 +90,10 @@ export const QUERY_PARAMS_BY_DISPLAY_TYPE: Record< QUERY_PARAMS.startDateMilliseconds, QUERY_PARAMS.endDateMilliseconds, ], + [DisplayType.Treemap]: [ + QUERY_PARAMS.startDateMilliseconds, + QUERY_PARAMS.endDateMilliseconds, + ], [DisplayType.Number]: [ QUERY_PARAMS.startDateMilliseconds, QUERY_PARAMS.endDateMilliseconds, @@ -121,6 +125,7 @@ export const QUERY_PARAM_EXAMPLES: Record = { [DisplayType.Table]: DATE_RANGE_WHERE_EXAMPLE_SQL, [DisplayType.Pie]: DATE_RANGE_WHERE_EXAMPLE_SQL, [DisplayType.Bar]: DATE_RANGE_WHERE_EXAMPLE_SQL, + [DisplayType.Treemap]: DATE_RANGE_WHERE_EXAMPLE_SQL, [DisplayType.Number]: DATE_RANGE_WHERE_EXAMPLE_SQL, [DisplayType.Search]: '', [DisplayType.Heatmap]: '', diff --git a/packages/common-utils/src/types.ts b/packages/common-utils/src/types.ts index f1c006a299..dbeb438027 100644 --- a/packages/common-utils/src/types.ts +++ b/packages/common-utils/src/types.ts @@ -43,6 +43,7 @@ export enum DisplayType { Heatmap = 'heatmap', Markdown = 'markdown', EventPatterns = 'event_patterns', + Treemap = 'treemap', } export type KeyValue = { key: Key; value: Value }; diff --git a/scripts/ci/ratchet-baseline.json b/scripts/ci/ratchet-baseline.json index 21457c9e1e..bbf15c266f 100644 --- a/scripts/ci/ratchet-baseline.json +++ b/scripts/ci/ratchet-baseline.json @@ -1,14 +1,14 @@ { "api": { - "as-any": 89, + "as-any": 88, "ts-ignore": 0, "eslint-disable": 36, "@source": 0 }, "app": { - "as-any": 213, + "as-any": 221, "ts-ignore": 0, - "eslint-disable": 142, + "eslint-disable": 154, "@source": 0 }, "cli": {