The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[AI Analytics] Make conversation list sortable by more fields (#74380)
Sloan Sparger23d6dba5aa73
diff --git a/e2e/test/scenarios/metabot/usage-auditing.cy.spec.ts b/e2e/test/scenarios/metabot/usage-auditing.cy.spec.tsindex 0b4f0a38bad..331c87e1d84 100644--- a/e2e/test/scenarios/metabot/usage-auditing.cy.spec.ts+++ b/e2e/test/scenarios/metabot/usage-auditing.cy.spec.ts@@ -1023,6 +1023,30 @@ describe("scenarios > metabot > usage auditing", () => {});});+ it("sorts the conversations table by each sortable column", () => {+ const sortableColumns: Array<{ headerLabel: RegExp; sortBy: string }> = [+ { headerLabel: /^User/, sortBy: "user" },+ { headerLabel: /^Profile/, sortBy: "profile_id" },+ { headerLabel: /^Date/, sortBy: "created_at" },+ { headerLabel: /^Messages/, sortBy: "message_count" },+ { headerLabel: /^Tokens/, sortBy: "total_tokens" },+ { headerLabel: /^IP/, sortBy: "ip_address" },+ ];++ visitConversationsPage();++ for (const { headerLabel, sortBy } of sortableColumns) {+ H.main()+ .findByRole("table")+ .findByRole("button", { name: headerLabel })+ .click();++ cy.wait("@conversations")+ .its("request.url")+ .should("include", `sort_by=${sortBy}`);+ }+ });+it("shows message usage stats charts", () => {visitUsageStatsPage();H.main().findByRole("tab", { name: "Messages" }).realClick();diff --git a/enterprise/backend/src/metabase_enterprise/metabot_analytics/api.clj b/enterprise/backend/src/metabase_enterprise/metabot_analytics/api.cljindex 6ef759d6bb6..a0254b08209 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_analytics/api.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_analytics/api.clj@@ -49,7 +49,7 @@(def ^:private SortColumn"Allow-list of columns the list endpoint can sort by."- [:enum "created_at" "message_count" "total_tokens"])+ [:enum "created_at" "message_count" "total_tokens" "user" "profile_id" "ip_address"])(def ^:private SortDirection[:enum "asc" "desc"])diff --git a/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj b/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.cljindex 36313d38517..208450c79ac 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj@@ -71,11 +71,16 @@(some-> user (select-keys [:id :email :first_name :last_name :tenant_id])))(def ^:private sort-columns- "Allow-list of API sort keys → HoneySQL column refs, to keep user input out- of the `:order-by` clause."- {"created_at" :c.created_at- "message_count" :message_count- "total_tokens" :total_tokens})+ "Allow-list of API sort keys → vectors of HoneySQL ORDER BY expressions (sans+ direction). A vector lets a single sort key emit multiple ORDER BY terms that+ share the same direction (e.g. user sort orders by first_name then last_name)."+ {"created_at" [:c.created_at]+ "message_count" [:message_count]+ "total_tokens" [:total_tokens]+ "user" [[:lower [:min :u.first_name]]+ [:lower [:min :u.last_name]]]+ "profile_id" [:profile_id]+ "ip_address" [:c.ip_address]})(def ^:private list-query"HoneySQL query that selects one row per conversation with the aggregate@@ -102,7 +107,8 @@:from [[:metabot_conversation :c]]:left-join [[:metabot_message :m] [:and[:= :m.conversation_id :c.id]- [:= :m.deleted_at nil]]]+ [:= :m.deleted_at nil]]+ [:core_user :u] [:= :u.id :c.user_id]]:group-by [:c.id]})(defn- row->summary@@ -154,23 +160,25 @@and a serialized `date` parameter string, plus sorting by an allow-listed`sort-by` column in either direction (defaults to newest-first)."[{:keys [limit offset user-id group-id tenant-id date sort-by sort-dir]}]- (let [limit (or limit default-limit)- offset (or offset default-offset)- where (list-where-clause {:user-id user-id- :group-id group-id- :tenant-id tenant-id- :date date})- sort-col (get sort-columns sort-by :c.created_at)- direction (if (= sort-dir "asc") :asc :desc)- total (:count (t2/query-one (cond-> {:select [[[:count :*] :count]]- :from [[:metabot_conversation :c]]}- where (assoc :where where))))- rows (t2/select :model/MetabotConversation- (cond-> (assoc list-query- :order-by [[sort-col direction] [:c.id :asc]]- :limit limit- :offset offset)- where (assoc :where where)))]+ (let [limit (or limit default-limit)+ offset (or offset default-offset)+ where (list-where-clause {:user-id user-id+ :group-id group-id+ :tenant-id tenant-id+ :date date})+ sort-exprs (get sort-columns sort-by [:c.created_at])+ direction (if (= sort-dir "asc") :asc :desc)+ order-by (conj (mapv #(vector % direction) sort-exprs)+ [:c.id :asc])+ total (:count (t2/query-one (cond-> {:select [[[:count :*] :count]]+ :from [[:metabot_conversation :c]]}+ where (assoc :where where))))+ rows (t2/select :model/MetabotConversation+ (cond-> (assoc list-query+ :order-by order-by+ :limit limit+ :offset offset)+ where (assoc :where where)))]{:data (->> (t2/hydrate rows :user)hydrate-tool-counts(map row->summary))diff --git a/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.cljindex a78ca66a1be..154509e1c64 100644--- a/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj@@ -194,21 +194,88 @@(is (= 3 (:total response)))(is (= [] (:data response))))))))-(deftest list-conversations-sorting-test- (with-list-conversations-fixture!- (fn [{:keys [response-path convo-1 convo-2 convo-3]}]- (let [response (mt/user-http-request :crowberto :get 200- (format "%s&sort_by=message_count&sort_dir=asc" response-path))]- (is (= [convo-3 convo-1 convo-2] (map :conversation_id (:data response))))))))--(deftest list-conversations-sorting-desc-test- (testing "sort-dir=desc reverses the sort order — same fixture as the asc test"- (with-list-conversations-fixture!- (fn [{:keys [response-path convo-1 convo-2 convo-3]}]- (let [response (mt/user-http-request :crowberto :get 200- (format "%s&sort_by=message_count&sort_dir=desc" response-path))]- (is (= [convo-2 convo-1 convo-3]- (map :conversation_id (:data response)))))))))+(defn- with-sortable-conversations-fixture!+ "Seeds three conversations differentiated across every sortable column: user+ (first_name/last_name), profile_id, ip_address, created_at, message_count,+ and total_tokens. Tests pick a sort key and verify the order of the+ convo-a / convo-m / convo-z handles. Seeded into a private 2026 date window+ so a `date` filter isolates them from any other rows in the test app DB.++ Each sort key yields a distinct permutation, so a bug that ignored `sort_by`+ and always returned `created_at` order would fail five of the six cases.++ Designed orderings (asc):+ created_at: (a, m, z) jan-1 / jan-2 / jan-3+ user: (m, a, z) first_name Alice / Ben / Chloe+ profile_id: (z, a, m) 1-profile / 2-profile / 3-profile+ ip_address: (z, m, a) 10.0.0.1 / 10.0.0.2 / 10.0.0.3+ message_count: (m, z, a) counts 1, 2, 3+ total_tokens: (a, z, m) tokens 10, 20, 30"+ [thunk]+ (mt/with-premium-features #{:audit-app}+ (mt/with-temp [:model/User {user-a :id} {:email "[email protected]"+ :first_name "Ben"+ :last_name "Anderson"}+ :model/User {user-m :id} {:email "[email protected]"+ :first_name "Alice"+ :last_name "Mason"}+ :model/User {user-z :id} {:email "[email protected]"+ :first_name "Chloe"+ :last_name "Zane"}]+ (let [base-path "ee/metabot-analytics/conversations"+ response-path (str base-path "?date=2026-01-01~2026-01-31")+ convo-a (str (random-uuid))+ convo-m (str (random-uuid))+ convo-z (str (random-uuid))+ jan-1 (offset-date-time "2026-01-01T00:00:00Z")+ jan-2 (offset-date-time "2026-01-02T00:00:00Z")+ jan-3 (offset-date-time "2026-01-03T00:00:00Z")+ jan-4 (offset-date-time "2026-01-04T00:00:00Z")+ seed-msg! (fn [convo-id profile-id tokens]+ (insert-message! {:conversation-id convo-id+ :created-at jan-4+ :role "assistant"+ :profile-id profile-id+ :total-tokens tokens+ :data [{:role "assistant" :content "hi"}]}))]+ (try+ (insert-conversation! {:conversation-id convo-a :user-id user-a+ :created-at jan-1 :ip-address "10.0.0.3"})+ (insert-conversation! {:conversation-id convo-m :user-id user-m+ :created-at jan-2 :ip-address "10.0.0.2"})+ (insert-conversation! {:conversation-id convo-z :user-id user-z+ :created-at jan-3 :ip-address "10.0.0.1"})+ (seed-msg! convo-a "2-profile" 4)+ (seed-msg! convo-a "2-profile" 3)+ (seed-msg! convo-a "2-profile" 3)+ (seed-msg! convo-m "3-profile" 30)+ (seed-msg! convo-z "1-profile" 10)+ (seed-msg! convo-z "1-profile" 10)+ (thunk {:response-path response-path+ :convo-a convo-a+ :convo-m convo-m+ :convo-z convo-z})+ (finally+ (delete-conversations! [convo-a convo-m convo-z])))))))++(deftest list-conversations-sort-test+ (with-sortable-conversations-fixture!+ (fn [{:keys [response-path convo-a convo-m convo-z]}]+ (doseq [[sort-by asc-order]+ [["created_at" [convo-a convo-m convo-z]]+ ["user" [convo-m convo-a convo-z]]+ ["profile_id" [convo-z convo-a convo-m]]+ ["ip_address" [convo-z convo-m convo-a]]+ ["message_count" [convo-m convo-z convo-a]]+ ["total_tokens" [convo-a convo-z convo-m]]]]+ (testing (str "sort_by=" sort-by " &sort_dir=asc")+ (let [response (mt/user-http-request :crowberto :get 200+ (str response-path "&sort_by=" sort-by "&sort_dir=asc"))]+ (is (= asc-order (map :conversation_id (:data response))))))+ (testing (str "sort_by=" sort-by " &sort_dir=desc")+ (let [response (mt/user-http-request :crowberto :get 200+ (str response-path "&sort_by=" sort-by "&sort_dir=desc"))]+ (is (= (reverse asc-order) (map :conversation_id (:data response))))))))))(deftest list-conversations-invalid-sort-test(with-list-conversations-fixture!diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsTable.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsTable.tsxindex 59522ed2bd9..dbc7ee2fb94 100644--- a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsTable.tsx+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/ConversationsTable.tsx@@ -50,8 +50,16 @@ export function ConversationsTable({<table className={cx(AdminS.ContentTable, S.table)}><thead><tr>- <th>{t`User`}</th>- <th>{t`Profile`}</th>+ <SortableColumnHeader+ name="user"+ sortingOptions={sortingOptions}+ onSortingOptionsChange={onSortingOptionsChange}+ >{t`User`}</SortableColumnHeader>+ <SortableColumnHeader+ name="profile_id"+ sortingOptions={sortingOptions}+ onSortingOptionsChange={onSortingOptionsChange}+ >{t`Profile`}</SortableColumnHeader><SortableColumnHeadername="created_at"sortingOptions={sortingOptions}@@ -70,7 +78,11 @@ export function ConversationsTable({>{t`Tokens`}</SortableColumnHeader><th>{t`Queries`}</th><th>{t`Searches`}</th>- <th>{t`IP`}</th>+ <SortableColumnHeader+ name="ip_address"+ sortingOptions={sortingOptions}+ onSortingOptionsChange={onSortingOptionsChange}+ >{t`IP`}</SortableColumnHeader></tr></thead><tbody>diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/utils.ts b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/utils.tsindex 1a348c80cd8..89cfc279383 100644--- a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/components/ConversationsPage/utils.ts@@ -5,7 +5,10 @@ import {} from "metabase/common/hooks/use-url-state";import type { SortDirection } from "metabase-types/api";-import type { ConversationSortColumn } from "../../types";+import {+ CONVERSATION_SORT_COLUMNS,+ type ConversationSortColumn,+} from "../../types";import {type FilterUrlState,filterUrlStateConfig,@@ -56,7 +59,7 @@ function parseSortColumn(param: QueryParam): ConversationSortColumn {}function isSortColumn(value: string): value is ConversationSortColumn {- return ["created_at", "message_count", "total_tokens"].includes(value);+ return CONVERSATION_SORT_COLUMNS.some((col) => col === value);}function parseSortDirection(param: QueryParam): SortDirection {diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/types.ts b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/types.tsindex 2cddd19eac1..959a617cd92 100644--- a/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/types.ts+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/metabot-analytics/types.ts@@ -35,10 +35,16 @@ export type ConversationSummary = {user: MetabotUserInfo | null;};-export type ConversationSortColumn =- | "created_at"- | "message_count"- | "total_tokens";+export const CONVERSATION_SORT_COLUMNS = [+ "created_at",+ "message_count",+ "total_tokens",+ "user",+ "profile_id",+ "ip_address",+] as const;++export type ConversationSortColumn = (typeof CONVERSATION_SORT_COLUMNS)[number];export type ConversationsRequest = {limit?: number;