Attempt to get model from datagrid to include pivot and filters
diff --git a/ui/src/components/aggregation_panel.ts b/ui/src/components/aggregation_panel.ts
index cd45ca8..53c4f42 100644
--- a/ui/src/components/aggregation_panel.ts
+++ b/ui/src/components/aggregation_panel.ts
@@ -34,7 +34,11 @@
   CellRenderer,
   ColumnType,
 } from './widgets/datagrid/datagrid_schema';
-import type {DataSource} from './widgets/datagrid/data_source';
+import type {
+  DataSource,
+  DataSourceModel,
+  FlatModel,
+} from './widgets/datagrid/data_source';
 import {Icons} from '../base/semantic_icons';
 
 export interface AggregationPanelAttrs {
@@ -47,9 +51,22 @@
   readonly controls?: m.Children;
   readonly trace?: Trace;
   readonly query?: string;
+  /**
+   * Called when the grid is ready, providing the data source and a function
+   * to retrieve the current model (filters, sort, pagination, pivot state).
+   * Use this to build a query that reflects the current filtered result set,
+   * e.g. for creating a debug track from filtered data.
+   */
+  readonly onDataGridReady?: (
+    dataSource: DataSource,
+    getModel: () => DataSourceModel,
+  ) => void;
 }
 
 export class AggregationPanel implements m.ClassComponent<AggregationPanelAttrs> {
+  // Stores the getModel function from DataGridApi, set when the grid is ready.
+  private getModel?: () => DataSourceModel;
+
   view({attrs}: m.CVnode<AggregationPanelAttrs>) {
     const {
       dataSource,
@@ -61,6 +78,7 @@
       controls,
       trace,
       query,
+      onDataGridReady,
     } = attrs;
 
     return m(Stack, {fillHeight: true, spacing: 'none'}, [
@@ -76,6 +94,7 @@
           onClearGridState,
           trace,
           query,
+          onDataGridReady,
         ),
       ),
     ]);
@@ -90,9 +109,29 @@
     onClearGridState?: () => void,
     trace?: Trace,
     query?: string,
+    onDataGridReady?: (
+      dataSource: DataSource,
+      getModel: () => DataSourceModel,
+    ) => void,
   ) {
+    // Use the filtered query if available, otherwise fall back to the static query.
+    const filteredQuery = query;
+
+    const gridModel = this.getModel?.();
+
+    const debugModel: FlatModel = {
+      mode: 'flat',
+      columns: gridModel.map((field) => ({
+        field,
+        alias: field,
+      })),
+      filters: gridModel?.filters,
+      pagination: undefined,
+      sort: undefined,
+    };
+
     const debugTrackButton =
-      trace && query
+      trace && filteredQuery
         ? m(
             Popup,
             {
@@ -104,7 +143,7 @@
             },
             m(AddDebugTrackMenu, {
               trace,
-              query,
+              query: filteredQuery,
               availableColumns: getDefaultVisibleColumns(gridConfig.schema),
               onAdd: () => trace.navigate('#!/viewer'),
             }),
@@ -115,7 +154,11 @@
       fillHeight: true,
       schema: gridConfig.schema,
       data: dataSource,
-      onReady,
+      onReady: (api: DataGridApi) => {
+        onReady?.(api);
+        this.getModel = api.getModel;
+        onDataGridReady?.(dataSource, api.getModel);
+      },
       // Spread controlled state props (columns, filters, pivot and callbacks)
       ...dataGridState,
       toolbarItemsLeft: [
diff --git a/ui/src/components/tracks/add_debug_track_menu.ts b/ui/src/components/tracks/add_debug_track_menu.ts
index e2339e2..e1e353c 100644
--- a/ui/src/components/tracks/add_debug_track_menu.ts
+++ b/ui/src/components/tracks/add_debug_track_menu.ts
@@ -108,6 +108,7 @@
         onSubmit: () => {
           attrs.onAdd?.();
           this.createTracks(attrs);
+          console.log(attrs.query);
         },
         submitLabel: 'Add Track',
         cancelLabel: 'Cancel',
diff --git a/ui/src/components/widgets/datagrid/data_source.ts b/ui/src/components/widgets/datagrid/data_source.ts
index 069d0ad..3d635c2 100644
--- a/ui/src/components/widgets/datagrid/data_source.ts
+++ b/ui/src/components/widgets/datagrid/data_source.ts
@@ -58,6 +58,14 @@
    * Returns a promise that resolves to all filtered and sorted rows.
    */
   exportData(model: DataSourceModel): Promise<readonly Row[]>;
+
+  /**
+   * Returns the SQL query that would be executed for the given model,
+   * without running it. Useful for debugging or passing to external tools
+   * (e.g., creating a debug track from the filtered result set).
+   * Optional — only SQL-backed data sources implement this.
+   */
+  getQuery?(model: DataSourceModel): string;
 }
 
 // Common fields shared across all data source modes