Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Log conversation-title calls under their own source (#83868)

Andrei Simionescue77f96619b42
diff --git a/enterprise/backend/src/metabase_enterprise/metabot/usage.clj b/enterprise/backend/src/metabase_enterprise/metabot/usage.clj
index 34e55dfd264..08c83a3e552 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot/usage.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot/usage.clj
@@ -16,14 +16,15 @@
(set! *warn-on-reflection* true)
;; Keep these in sync with the CASE branches in the LATEST version of:
-;; resources/migrations/instance_analytics_views/ai_usage_log/v4/{h2,mysql,postgres}-ai_usage_log.sql
-;; resources/migrations/instance_analytics_views/metabot_conversations/v6/{h2,mysql,postgres}-metabot_conversations.sql
+;; resources/migrations/instance_analytics_views/ai_usage_log/v6/{h2,mysql,postgres}-ai_usage_log.sql
+;; resources/migrations/instance_analytics_views/metabot_conversations/v8/{h2,mysql,postgres}-metabot_conversations.sql
;; When adding a value here, create a new view version (vN+1) with the extra CASE branches plus a migration
;; that reinstalls the views, so users see a nicer name in ai analytics. Parity is enforced by
;; `metabase-enterprise.metabot.usage-test/known-sources-and-profiles-have-view-case-branches-test`.
(def ^:private known-sources
#{"metabot_agent"
"contextual_interestingness"
+ "conversation_title"
"document_generate_content"
"example_question_generation_batch"
"exploration"
diff --git a/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj
index deae30fdecd..2f59a0a25e6 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot/usage_test.clj
@@ -5,6 +5,11 @@
[clojure.test :refer [deftest is testing]]
[metabase-enterprise.metabot.usage :as ee.usage]
[metabase.api.common :as api]
+ [metabase.llm.test-util :as llm.tu]
+ [metabase.metabot.conversation-title :as conversation-title]
+ [metabase.metabot.scope :as scope]
+ [metabase.metabot.self.openrouter :as openrouter]
+ [metabase.metabot.test-util :as mut]
[metabase.metabot.usage :as usage]
[metabase.test :as mt]
[toucan2.core :as t2]))
@@ -220,6 +225,26 @@
(finally
(t2/delete! :model/AiUsageLog :source "metabot_agent" :model model))))))))
+(deftest conversation-title-call-logs-its-own-source-test
+ (ee.usage/clear-limit-cache!)
+ (mt/with-temporary-setting-values [llm-providers llm.tu/default-connections
+ llm-metabot-provider "openrouter/anthropic/claude-haiku-4-5"]
+ (binding [scope/*current-user-metabot-permissions* scope/all-yes-permissions]
+ (mt/with-temp [:model/MetabotConversation {conversation-id :id} {:user_id (mt/user->id :rasta)}]
+ (mt/with-dynamic-fn-redefs [openrouter/openrouter (constantly (mut/mock-llm-response
+ [{:type :tool-input :id "call-1" :function "json"
+ :arguments {:title "Orders by month"}}
+ {:type :usage :id "msg-1"
+ :usage {:promptTokens 900
+ :completionTokens 30}}]))]
+ (try
+ (is (= "Orders by month"
+ (#'conversation-title/generate! conversation-id "internal" "Show orders by month")))
+ (is (= ["conversation_title"]
+ (t2/select-fn-vec :source :model/AiUsageLog :conversation_id conversation-id)))
+ (finally
+ (t2/delete! :model/AiUsageLog :conversation_id conversation-id))))))))
+
;;; ------------------------------------------ check-usage-limits! ------------------------------------------
(deftest no-limits-configured-returns-nil-test
@@ -479,8 +504,9 @@
sources (disj @#'ee.usage/known-sources "user-intent-classification")
profiles @#'ee.usage/known-profile-ids]
(doseq [[view expected] {"ai_usage_log" sources
- ;; v_metabot_conversations maps both the source and the profile id
- "metabot_conversations" (into sources profiles)}
+ ;; v_metabot_conversations maps both the source and the profile id, and never takes
+ ;; a conversation's source from its title call
+ "metabot_conversations" (into (disj sources "conversation_title") profiles)}
:let [dir (latest-view-version-dir view)
sql-files (filter #(str/ends-with? (.getName ^java.io.File %) ".sql")
(.listFiles dir))]]
diff --git a/enterprise/backend/test/metabase_enterprise/metabot_analytics/v_metabot_conversations_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_analytics/v_metabot_conversations_test.clj
index 4eaa4cdca54..05a2c390216 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot_analytics/v_metabot_conversations_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot_analytics/v_metabot_conversations_test.clj
@@ -283,6 +283,32 @@
:total_tokens 15}]
(is (=? {:model "anthropic/claude-haiku-4-5-20251001"} (first (query-view [convo-id]))))))))
+(deftest title-call-sets-neither-source-nor-model-test
+ (testing "a title call logged before the agent's first call counts toward the tokens, not the source or model"
+ (let [convo-id (str (random-uuid))
+ now (t/offset-date-time)]
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/MetabotConversation _ {:id convo-id :user_id user-id}
+ :model/AiUsageLog _ {:source "conversation_title"
+ :model "anthropic/claude-haiku-4-5"
+ :conversation_id convo-id
+ :prompt_tokens 900
+ :completion_tokens 30
+ :total_tokens 930
+ :created_at (t/minus now (t/seconds 5))}
+ :model/AiUsageLog _ {:source "metabot_agent"
+ :model default-model
+ :conversation_id convo-id
+ :prompt_tokens 9000
+ :completion_tokens 300
+ :total_tokens 9300
+ :created_at now}]
+ (is (=? {:source "metabot_agent"
+ :source_name "Metabot"
+ :model default-model
+ :total_tokens 10230}
+ (first (query-view [convo-id]))))))))
+
(deftest ip-address-test
(testing "ip_address is surfaced from the conversation row"
(let [convo-with-ip (str (random-uuid))
diff --git a/resources/instance_analytics/collections/main/usage_analytics/metabot_conversations.yaml b/resources/instance_analytics/collections/main/usage_analytics/metabot_conversations.yaml
index 4991361402e..3094795a2b2 100644
--- a/resources/instance_analytics/collections/main/usage_analytics/metabot_conversations.yaml
+++ b/resources/instance_analytics/collections/main/usage_analytics/metabot_conversations.yaml
@@ -247,7 +247,7 @@ result_metadata:
visibility_type: normal
- base_type: type/Text
coercion_strategy: null
- description: Surface that originated the conversation (e.g. web, slack, embed); taken from the first ai_usage_log entry.
+ description: Surface that originated the conversation (e.g. web, slack, embed); taken from the earliest ai_usage_log entry that is not a conversation title call.
display_name: Source
effective_type: type/Text
field_ref:
@@ -322,7 +322,7 @@ result_metadata:
visibility_type: normal
- base_type: type/Text
coercion_strategy: null
- description: LLM model used for the conversation; taken from the first ai_usage_log entry.
+ description: LLM model used for the conversation; taken from the earliest ai_usage_log entry that is not a conversation title call.
display_name: Model
effective_type: type/Text
field_ref:
diff --git a/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/model.yaml b/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/model.yaml
index fa4af7f8c45..b2434b884b8 100644
--- a/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/model.yaml
+++ b/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/model.yaml
@@ -1,6 +1,6 @@
name: model
display_name: Model
-description: LLM model used for the conversation; taken from the first ai_usage_log entry.
+description: LLM model used for the conversation; taken from the earliest ai_usage_log entry that is not a conversation title call.
created_at: '2026-04-24T19:39:29.987038Z'
visibility_type: normal
table_id: [Internal Metabase Database, public, v_metabot_conversations]
diff --git a/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/source.yaml b/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/source.yaml
index 60904dc6a60..ecd7e6fa027 100644
--- a/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/source.yaml
+++ b/resources/instance_analytics/databases/internal_metabase_database/schemas/public/tables/v_metabot_conversations/fields/source.yaml
@@ -1,6 +1,6 @@
name: source
display_name: Source
-description: Surface that originated the conversation (e.g. web, slack, embed); taken from the first ai_usage_log entry.
+description: Surface that originated the conversation (e.g. web, slack, embed); taken from the earliest ai_usage_log entry that is not a conversation title call.
created_at: '2026-04-24T19:39:29.987038Z'
visibility_type: normal
table_id: [Internal Metabase Database, public, v_metabot_conversations]
diff --git a/resources/migrations/065/20261007_ai_analytics_views_conversation_title_source.yaml b/resources/migrations/065/20261007_ai_analytics_views_conversation_title_source.yaml
new file mode 100644
index 00000000000..7a56eef7395
--- /dev/null
+++ b/resources/migrations/065/20261007_ai_analytics_views_conversation_title_source.yaml
@@ -0,0 +1,64 @@
+## Name conversation-title calls in AI usage analytics, and keep them from deciding a conversation's source and model
+
+databaseChangeLog:
+ - changeSet:
+ id: v65.2026-10-07T12:00:00
+ author: andreis
+ comment: Updated view v_ai_usage_log v6 - name the conversation_title source
+ changes:
+ - sqlFile:
+ dbms: postgresql
+ path: ../instance_analytics_views/ai_usage_log/v6/postgres-ai_usage_log.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: mysql,mariadb
+ path: ../instance_analytics_views/ai_usage_log/v6/mysql-ai_usage_log.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: h2
+ path: ../instance_analytics_views/ai_usage_log/v6/h2-ai_usage_log.sql
+ relativeToChangelogFile: true
+ rollback:
+ - sqlFile:
+ dbms: postgresql
+ path: ../instance_analytics_views/ai_usage_log/v5/postgres-ai_usage_log.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: mysql,mariadb
+ path: ../instance_analytics_views/ai_usage_log/v5/mysql-ai_usage_log.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: h2
+ path: ../instance_analytics_views/ai_usage_log/v5/h2-ai_usage_log.sql
+ relativeToChangelogFile: true
+
+ - changeSet:
+ id: v65.2026-10-07T12:00:01
+ author: andreis
+ comment: Updated view v_metabot_conversations v8 - take source and model from calls other than the title
+ changes:
+ - sqlFile:
+ dbms: postgresql
+ path: ../instance_analytics_views/metabot_conversations/v8/postgres-metabot_conversations.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: mysql,mariadb
+ path: ../instance_analytics_views/metabot_conversations/v8/mysql-metabot_conversations.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: h2
+ path: ../instance_analytics_views/metabot_conversations/v8/h2-metabot_conversations.sql
+ relativeToChangelogFile: true
+ rollback:
+ - sqlFile:
+ dbms: postgresql
+ path: ../instance_analytics_views/metabot_conversations/v7/postgres-metabot_conversations.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: mysql,mariadb
+ path: ../instance_analytics_views/metabot_conversations/v7/mysql-metabot_conversations.sql
+ relativeToChangelogFile: true
+ - sqlFile:
+ dbms: h2
+ path: ../instance_analytics_views/metabot_conversations/v7/h2-metabot_conversations.sql
+ relativeToChangelogFile: true
diff --git a/resources/migrations/instance_analytics_views/ai_usage_log/v6/h2-ai_usage_log.sql b/resources/migrations/instance_analytics_views/ai_usage_log/v6/h2-ai_usage_log.sql
new file mode 100644
index 00000000000..5ae03cfca5a
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/ai_usage_log/v6/h2-ai_usage_log.sql
@@ -0,0 +1,53 @@
+DROP VIEW IF EXISTS v_ai_usage_log;
+
+CREATE OR REPLACE VIEW v_ai_usage_log AS
+SELECT
+ a.id AS usage_log_id,
+ a.created_at,
+ a.source,
+ CASE a.source
+ WHEN 'metabot_agent' THEN 'Metabot'
+ WHEN 'agent' THEN 'Metabot'
+ WHEN 'contextual_interestingness' THEN 'Research'
+ WHEN 'exploration' THEN 'Research'
+ WHEN 'document_generate_content' THEN 'Documents'
+ WHEN 'example_question_generation_batch' THEN 'Suggested Prompts'
+ WHEN 'conversation_title' THEN 'Conversation titles'
+ WHEN 'slack' THEN 'Slackbot'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'oss-sql-gen' THEN 'SQL'
+ WHEN 'sql-gen' THEN 'SQL'
+ WHEN 'unknown' THEN 'Unknown'
+ ELSE a.source
+ END AS source_name,
+ a.model,
+ a.profile_id,
+ a.prompt_tokens,
+ a.completion_tokens,
+ a.total_tokens,
+ a.conversation_id,
+ a.user_id,
+ 'user_' || a.user_id AS user_qualified_id,
+ COALESCE(u.first_name || ' ' || u.last_name, u.email) AS user_display_name,
+ (SELECT pg.name
+ FROM permissions_group_membership pgm
+ JOIN permissions_group pg ON pg.id = pgm.group_id
+ WHERE pgm.user_id = a.user_id
+ AND pg.id != 1
+ ORDER BY pg.name
+ LIMIT 1) AS group_name,
+ c.ip_address AS ip_address,
+ c.embedding_hostname AS embedding_hostname,
+ c.embedding_path AS embedding_path,
+ c.user_agent AS user_agent,
+ c.sanitized_user_agent AS sanitized_user_agent,
+ c.forked_from_conversation_id AS forked_from_conversation_id,
+ a.tenant_id,
+ a.request_id,
+ a.cache_creation_tokens,
+ a.cache_read_tokens
+FROM ai_usage_log a
+LEFT JOIN core_user u
+ ON u.id = a.user_id
+LEFT JOIN metabot_conversation c
+ ON c.id = a.conversation_id;
diff --git a/resources/migrations/instance_analytics_views/ai_usage_log/v6/mysql-ai_usage_log.sql b/resources/migrations/instance_analytics_views/ai_usage_log/v6/mysql-ai_usage_log.sql
new file mode 100644
index 00000000000..ef6d675fb32
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/ai_usage_log/v6/mysql-ai_usage_log.sql
@@ -0,0 +1,53 @@
+DROP VIEW IF EXISTS v_ai_usage_log;
+
+CREATE OR REPLACE VIEW v_ai_usage_log AS
+SELECT
+ a.id AS usage_log_id,
+ a.created_at,
+ a.source,
+ CASE a.source
+ WHEN 'metabot_agent' THEN 'Metabot'
+ WHEN 'agent' THEN 'Metabot'
+ WHEN 'contextual_interestingness' THEN 'Research'
+ WHEN 'exploration' THEN 'Research'
+ WHEN 'document_generate_content' THEN 'Documents'
+ WHEN 'example_question_generation_batch' THEN 'Suggested Prompts'
+ WHEN 'conversation_title' THEN 'Conversation titles'
+ WHEN 'slack' THEN 'Slackbot'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'oss-sql-gen' THEN 'SQL'
+ WHEN 'sql-gen' THEN 'SQL'
+ WHEN 'unknown' THEN 'Unknown'
+ ELSE a.source
+ END AS source_name,
+ a.model,
+ a.profile_id,
+ a.prompt_tokens,
+ a.completion_tokens,
+ a.total_tokens,
+ a.conversation_id,
+ a.user_id,
+ CONCAT('user_', a.user_id) AS user_qualified_id,
+ COALESCE(CONCAT(u.first_name, ' ', u.last_name), u.email) AS user_display_name,
+ (SELECT pg.name
+ FROM permissions_group_membership pgm
+ JOIN permissions_group pg ON pg.id = pgm.group_id
+ WHERE pgm.user_id = a.user_id
+ AND pg.id != 1
+ ORDER BY pg.name
+ LIMIT 1) AS group_name,
+ c.ip_address AS ip_address,
+ c.embedding_hostname AS embedding_hostname,
+ c.embedding_path AS embedding_path,
+ c.user_agent AS user_agent,
+ c.sanitized_user_agent AS sanitized_user_agent,
+ c.forked_from_conversation_id AS forked_from_conversation_id,
+ a.tenant_id,
+ a.request_id,
+ a.cache_creation_tokens,
+ a.cache_read_tokens
+FROM ai_usage_log a
+LEFT JOIN core_user u
+ ON u.id = a.user_id
+LEFT JOIN metabot_conversation c
+ ON c.id = a.conversation_id;
diff --git a/resources/migrations/instance_analytics_views/ai_usage_log/v6/postgres-ai_usage_log.sql b/resources/migrations/instance_analytics_views/ai_usage_log/v6/postgres-ai_usage_log.sql
new file mode 100644
index 00000000000..5ae03cfca5a
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/ai_usage_log/v6/postgres-ai_usage_log.sql
@@ -0,0 +1,53 @@
+DROP VIEW IF EXISTS v_ai_usage_log;
+
+CREATE OR REPLACE VIEW v_ai_usage_log AS
+SELECT
+ a.id AS usage_log_id,
+ a.created_at,
+ a.source,
+ CASE a.source
+ WHEN 'metabot_agent' THEN 'Metabot'
+ WHEN 'agent' THEN 'Metabot'
+ WHEN 'contextual_interestingness' THEN 'Research'
+ WHEN 'exploration' THEN 'Research'
+ WHEN 'document_generate_content' THEN 'Documents'
+ WHEN 'example_question_generation_batch' THEN 'Suggested Prompts'
+ WHEN 'conversation_title' THEN 'Conversation titles'
+ WHEN 'slack' THEN 'Slackbot'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'oss-sql-gen' THEN 'SQL'
+ WHEN 'sql-gen' THEN 'SQL'
+ WHEN 'unknown' THEN 'Unknown'
+ ELSE a.source
+ END AS source_name,
+ a.model,
+ a.profile_id,
+ a.prompt_tokens,
+ a.completion_tokens,
+ a.total_tokens,
+ a.conversation_id,
+ a.user_id,
+ 'user_' || a.user_id AS user_qualified_id,
+ COALESCE(u.first_name || ' ' || u.last_name, u.email) AS user_display_name,
+ (SELECT pg.name
+ FROM permissions_group_membership pgm
+ JOIN permissions_group pg ON pg.id = pgm.group_id
+ WHERE pgm.user_id = a.user_id
+ AND pg.id != 1
+ ORDER BY pg.name
+ LIMIT 1) AS group_name,
+ c.ip_address AS ip_address,
+ c.embedding_hostname AS embedding_hostname,
+ c.embedding_path AS embedding_path,
+ c.user_agent AS user_agent,
+ c.sanitized_user_agent AS sanitized_user_agent,
+ c.forked_from_conversation_id AS forked_from_conversation_id,
+ a.tenant_id,
+ a.request_id,
+ a.cache_creation_tokens,
+ a.cache_read_tokens
+FROM ai_usage_log a
+LEFT JOIN core_user u
+ ON u.id = a.user_id
+LEFT JOIN metabot_conversation c
+ ON c.id = a.conversation_id;
diff --git a/resources/migrations/instance_analytics_views/metabot_conversations/v8/h2-metabot_conversations.sql b/resources/migrations/instance_analytics_views/metabot_conversations/v8/h2-metabot_conversations.sql
new file mode 100644
index 00000000000..0c99b0c122d
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/metabot_conversations/v8/h2-metabot_conversations.sql
@@ -0,0 +1,103 @@
+DROP VIEW IF EXISTS v_metabot_conversations;
+
+CREATE OR REPLACE VIEW v_metabot_conversations AS
+SELECT
+ c.id AS conversation_id,
+ c.created_at,
+ c.user_id,
+ c.title,
+ COALESCE(u.first_name || ' ' || u.last_name, u.email) AS user_display_name,
+ COUNT(m.id) AS message_count,
+ COUNT(CASE WHEN m.role = 'user' THEN 1 END) AS user_message_count,
+ COUNT(CASE WHEN m.role = 'assistant' THEN 1 END) AS assistant_message_count,
+ COUNT(CASE WHEN m.forked_from_message_id IS NULL THEN 1 END) AS new_message_count,
+ COALESCE(MAX(a.total_tokens), 0) AS total_tokens,
+ COALESCE(MAX(a.prompt_tokens), 0) AS prompt_tokens,
+ COALESCE(MAX(a.completion_tokens), 0) AS completion_tokens,
+ MAX(m.created_at) AS last_message_at,
+ (SELECT mm.profile_id
+ FROM metabot_message mm
+ WHERE mm.conversation_id = c.id
+ AND mm.role = 'assistant'
+ ORDER BY mm.created_at, mm.id
+ LIMIT 1) AS profile_id,
+ (SELECT CASE mm.profile_id
+ WHEN 'internal' THEN 'Internal'
+ WHEN 'embedding_next' THEN 'Embedding'
+ WHEN 'nlq' THEN 'NLQ'
+ WHEN 'sql' THEN 'SQL'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'transforms_codegen' THEN 'Transforms codegen'
+ WHEN 'document-generate-content' THEN 'Documents'
+ WHEN 'explorations' THEN 'Research'
+ ELSE mm.profile_id
+ END
+ FROM metabot_message mm
+ WHERE mm.conversation_id = c.id
+ AND mm.role = 'assistant'
+ ORDER BY mm.created_at, mm.id
+ LIMIT 1) AS profile_name,
+ (SELECT pg.name
+ FROM permissions_group_membership pgm
+ JOIN permissions_group pg ON pg.id = pgm.group_id
+ WHERE pgm.user_id = c.user_id
+ AND pg.id != 1
+ ORDER BY pg.name
+ LIMIT 1) AS group_name,
+ (SELECT aul.source
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS source,
+ (SELECT CASE aul.source
+ WHEN 'metabot_agent' THEN 'Metabot'
+ WHEN 'agent' THEN 'Metabot'
+ WHEN 'document_generate_content' THEN 'Documents'
+ WHEN 'example_question_generation_batch' THEN 'Suggested Prompts'
+ WHEN 'slack' THEN 'Slackbot'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'oss-sql-gen' THEN 'SQL'
+ WHEN 'sql-gen' THEN 'SQL'
+ WHEN 'unknown' THEN 'Unknown'
+ WHEN 'contextual_interestingness' THEN 'Research'
+ WHEN 'exploration' THEN 'Research'
+ ELSE aul.source
+ END
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS source_name,
+ c.ip_address AS ip_address,
+ c.embedding_hostname AS embedding_hostname,
+ c.embedding_path AS embedding_path,
+ c.user_agent AS user_agent,
+ c.sanitized_user_agent AS sanitized_user_agent,
+ c.forked_from_conversation_id AS forked_from_conversation_id,
+ MAX(a.tenant_id) AS tenant_id,
+ MAX(t.name) AS tenant_name,
+ (SELECT aul.model
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS model
+FROM metabot_conversation c
+LEFT JOIN core_user u
+ ON u.id = c.user_id
+LEFT JOIN metabot_message m
+ ON m.conversation_id = c.id
+LEFT JOIN (
+ SELECT
+ conversation_id,
+ COALESCE(SUM(prompt_tokens), 0) AS prompt_tokens,
+ COALESCE(SUM(completion_tokens), 0) AS completion_tokens,
+ COALESCE(SUM(total_tokens), 0) AS total_tokens,
+ MIN(tenant_id) AS tenant_id
+ FROM ai_usage_log
+ WHERE conversation_id IS NOT NULL
+ GROUP BY conversation_id
+) a ON a.conversation_id = c.id
+LEFT JOIN tenant t ON t.id = a.tenant_id
+GROUP BY c.id, c.created_at, c.user_id, c.title, u.first_name, u.last_name, u.email;
diff --git a/resources/migrations/instance_analytics_views/metabot_conversations/v8/mysql-metabot_conversations.sql b/resources/migrations/instance_analytics_views/metabot_conversations/v8/mysql-metabot_conversations.sql
new file mode 100644
index 00000000000..d614e3bdee5
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/metabot_conversations/v8/mysql-metabot_conversations.sql
@@ -0,0 +1,103 @@
+DROP VIEW IF EXISTS v_metabot_conversations;
+
+CREATE OR REPLACE VIEW v_metabot_conversations AS
+SELECT
+ c.id AS conversation_id,
+ c.created_at,
+ c.user_id,
+ c.title,
+ COALESCE(CONCAT(u.first_name, ' ', u.last_name), u.email) AS user_display_name,
+ COUNT(m.id) AS message_count,
+ COUNT(CASE WHEN m.role = 'user' THEN 1 END) AS user_message_count,
+ COUNT(CASE WHEN m.role = 'assistant' THEN 1 END) AS assistant_message_count,
+ COUNT(CASE WHEN m.forked_from_message_id IS NULL THEN 1 END) AS new_message_count,
+ COALESCE(MAX(a.total_tokens), 0) AS total_tokens,
+ COALESCE(MAX(a.prompt_tokens), 0) AS prompt_tokens,
+ COALESCE(MAX(a.completion_tokens), 0) AS completion_tokens,
+ MAX(m.created_at) AS last_message_at,
+ (SELECT mm.profile_id
+ FROM metabot_message mm
+ WHERE mm.conversation_id = c.id
+ AND mm.role = 'assistant'
+ ORDER BY mm.created_at, mm.id
+ LIMIT 1) AS profile_id,
+ (SELECT CASE mm.profile_id
+ WHEN 'internal' THEN 'Internal'
+ WHEN 'embedding_next' THEN 'Embedding'
+ WHEN 'nlq' THEN 'NLQ'
+ WHEN 'sql' THEN 'SQL'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'transforms_codegen' THEN 'Transforms codegen'
+ WHEN 'document-generate-content' THEN 'Documents'
+ WHEN 'explorations' THEN 'Research'
+ ELSE mm.profile_id
+ END
+ FROM metabot_message mm
+ WHERE mm.conversation_id = c.id
+ AND mm.role = 'assistant'
+ ORDER BY mm.created_at, mm.id
+ LIMIT 1) AS profile_name,
+ (SELECT pg.name
+ FROM permissions_group_membership pgm
+ JOIN permissions_group pg ON pg.id = pgm.group_id
+ WHERE pgm.user_id = c.user_id
+ AND pg.id != 1
+ ORDER BY pg.name
+ LIMIT 1) AS group_name,
+ (SELECT aul.source
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS source,
+ (SELECT CASE aul.source
+ WHEN 'metabot_agent' THEN 'Metabot'
+ WHEN 'agent' THEN 'Metabot'
+ WHEN 'document_generate_content' THEN 'Documents'
+ WHEN 'example_question_generation_batch' THEN 'Suggested Prompts'
+ WHEN 'slack' THEN 'Slackbot'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'oss-sql-gen' THEN 'SQL'
+ WHEN 'sql-gen' THEN 'SQL'
+ WHEN 'unknown' THEN 'Unknown'
+ WHEN 'contextual_interestingness' THEN 'Research'
+ WHEN 'exploration' THEN 'Research'
+ ELSE aul.source
+ END
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS source_name,
+ c.ip_address AS ip_address,
+ c.embedding_hostname AS embedding_hostname,
+ c.embedding_path AS embedding_path,
+ c.user_agent AS user_agent,
+ c.sanitized_user_agent AS sanitized_user_agent,
+ c.forked_from_conversation_id AS forked_from_conversation_id,
+ MAX(a.tenant_id) AS tenant_id,
+ MAX(t.name) AS tenant_name,
+ (SELECT aul.model
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS model
+FROM metabot_conversation c
+LEFT JOIN core_user u
+ ON u.id = c.user_id
+LEFT JOIN metabot_message m
+ ON m.conversation_id = c.id
+LEFT JOIN (
+ SELECT
+ conversation_id,
+ COALESCE(SUM(prompt_tokens), 0) AS prompt_tokens,
+ COALESCE(SUM(completion_tokens), 0) AS completion_tokens,
+ COALESCE(SUM(total_tokens), 0) AS total_tokens,
+ MIN(tenant_id) AS tenant_id
+ FROM ai_usage_log
+ WHERE conversation_id IS NOT NULL
+ GROUP BY conversation_id
+) a ON a.conversation_id = c.id
+LEFT JOIN tenant t ON t.id = a.tenant_id
+GROUP BY c.id, c.created_at, c.user_id, c.title, u.first_name, u.last_name, u.email;
diff --git a/resources/migrations/instance_analytics_views/metabot_conversations/v8/postgres-metabot_conversations.sql b/resources/migrations/instance_analytics_views/metabot_conversations/v8/postgres-metabot_conversations.sql
new file mode 100644
index 00000000000..dc570e2cad0
--- /dev/null
+++ b/resources/migrations/instance_analytics_views/metabot_conversations/v8/postgres-metabot_conversations.sql
@@ -0,0 +1,103 @@
+DROP VIEW IF EXISTS v_metabot_conversations;
+
+CREATE OR REPLACE VIEW v_metabot_conversations AS
+SELECT
+ c.id AS conversation_id,
+ c.created_at,
+ c.user_id,
+ c.title,
+ COALESCE(u.first_name || ' ' || u.last_name, u.email) AS user_display_name,
+ COUNT(m.id) AS message_count,
+ COUNT(CASE WHEN m.role = 'user' THEN 1 END) AS user_message_count,
+ COUNT(CASE WHEN m.role = 'assistant' THEN 1 END) AS assistant_message_count,
+ COUNT(CASE WHEN m.forked_from_message_id IS NULL THEN 1 END) AS new_message_count,
+ COALESCE(MAX(a.total_tokens), 0) AS total_tokens,
+ COALESCE(MAX(a.prompt_tokens), 0) AS prompt_tokens,
+ COALESCE(MAX(a.completion_tokens), 0) AS completion_tokens,
+ MAX(m.created_at) AS last_message_at,
+ (SELECT mm.profile_id
+ FROM metabot_message mm
+ WHERE mm.conversation_id = c.id
+ AND mm.role = 'assistant'
+ ORDER BY mm.created_at, mm.id
+ LIMIT 1) AS profile_id,
+ (SELECT CASE mm.profile_id
+ WHEN 'internal' THEN 'Internal'
+ WHEN 'embedding_next' THEN 'Embedding'
+ WHEN 'nlq' THEN 'NLQ'
+ WHEN 'sql' THEN 'SQL'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'transforms_codegen' THEN 'Transforms codegen'
+ WHEN 'document-generate-content' THEN 'Documents'
+ WHEN 'explorations' THEN 'Research'
+ ELSE mm.profile_id
+ END
+ FROM metabot_message mm
+ WHERE mm.conversation_id = c.id
+ AND mm.role = 'assistant'
+ ORDER BY mm.created_at, mm.id
+ LIMIT 1) AS profile_name,
+ (SELECT pg.name
+ FROM permissions_group_membership pgm
+ JOIN permissions_group pg ON pg.id = pgm.group_id
+ WHERE pgm.user_id = c.user_id
+ AND pg.id != 1
+ ORDER BY pg.name
+ LIMIT 1) AS group_name,
+ (SELECT aul.source
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS source,
+ (SELECT CASE aul.source
+ WHEN 'metabot_agent' THEN 'Metabot'
+ WHEN 'agent' THEN 'Metabot'
+ WHEN 'document_generate_content' THEN 'Documents'
+ WHEN 'example_question_generation_batch' THEN 'Suggested Prompts'
+ WHEN 'slack' THEN 'Slackbot'
+ WHEN 'slackbot' THEN 'Slackbot'
+ WHEN 'oss-sql-gen' THEN 'SQL'
+ WHEN 'sql-gen' THEN 'SQL'
+ WHEN 'unknown' THEN 'Unknown'
+ WHEN 'contextual_interestingness' THEN 'Research'
+ WHEN 'exploration' THEN 'Research'
+ ELSE aul.source
+ END
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS source_name,
+ c.ip_address AS ip_address,
+ c.embedding_hostname AS embedding_hostname,
+ c.embedding_path AS embedding_path,
+ c.user_agent AS user_agent,
+ c.sanitized_user_agent AS sanitized_user_agent,
+ c.forked_from_conversation_id AS forked_from_conversation_id,
+ MAX(a.tenant_id) AS tenant_id,
+ MAX(t.name) AS tenant_name,
+ (SELECT aul.model
+ FROM ai_usage_log aul
+ WHERE aul.conversation_id = c.id
+ AND aul.source <> 'conversation_title'
+ ORDER BY aul.created_at
+ LIMIT 1) AS model
+FROM metabot_conversation c
+LEFT JOIN core_user u
+ ON u.id = c.user_id
+LEFT JOIN metabot_message m
+ ON m.conversation_id = c.id
+LEFT JOIN (
+ SELECT
+ conversation_id,
+ COALESCE(SUM(prompt_tokens), 0) AS prompt_tokens,
+ COALESCE(SUM(completion_tokens), 0) AS completion_tokens,
+ COALESCE(SUM(total_tokens), 0) AS total_tokens,
+ MIN(tenant_id) AS tenant_id
+ FROM ai_usage_log
+ WHERE conversation_id IS NOT NULL
+ GROUP BY conversation_id
+) a ON a.conversation_id = c.id
+LEFT JOIN tenant t ON t.id = a.tenant_id
+GROUP BY c.id, c.created_at, c.user_id, c.title, u.first_name, u.last_name, u.email;
diff --git a/src/metabase/metabot/conversation_title.clj b/src/metabase/metabot/conversation_title.clj
index 3a1105d17a3..a95cb0d7561 100644
--- a/src/metabase/metabot/conversation_title.clj
+++ b/src/metabase/metabot/conversation_title.clj
@@ -131,7 +131,7 @@
{:request-id (str (random-uuid))
:session-id conversation-id
:profile-id profile-id
- :source "metabot_agent"
+ :source "conversation_title"
:tag "conversation-title"})
title (clean-title (:title response))]
(when title