Skip to content

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

Add some better defaults & limits to agent API (#70033)

Noah Mossf4bbf2984fca
diff --git a/enterprise/backend/src/metabase_enterprise/agent_api/api.clj b/enterprise/backend/src/metabase_enterprise/agent_api/api.clj
index c8b33ea38f5..3cf10bc6c5f 100644
--- a/enterprise/backend/src/metabase_enterprise/agent_api/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/agent_api/api.clj
@@ -25,6 +25,16 @@
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
+;;; --------------------------------------------------- Defaults ------------------------------------------------------
+
+(def ^:private ^:const default-field-values-limit
+ "Default number of field values to return when no limit is specified."
+ 30)
+
+(def ^:private ^:const default-query-row-limit
+ "Default row limit for table queries when no limit is specified."
+ 100)
+
;;; ---------------------------------------------------- Helpers ------------------------------------------------------
(defn- check-tool-result
@@ -187,7 +197,7 @@
"Get details for a table by ID."
[{:keys [id]} :- [:map [:id ms/PositiveInt]]
{:keys [with-fields with-field-values with-related-tables with-metrics with-measures with-segments]
- :or {with-fields true, with-field-values true, with-related-tables true,
+ :or {with-fields true, with-field-values false, with-related-tables true,
with-metrics true, with-measures false, with-segments false}}
:- [:map
[:with-field-values {:optional true} [:maybe :boolean]]
@@ -216,13 +226,13 @@
{:entity-type "table"
:entity-id id
:field-id field-id
- :limit (request/limit)})))
+ :limit (or (request/limit) default-field-values-limit)})))
(api.macros/defendpoint :get "/v1/metric/:id" :- ::metric
"Get details for a metric by ID."
[{:keys [id]} :- [:map [:id ms/PositiveInt]]
{:keys [with-default-temporal-breakout with-field-values with-queryable-dimensions with-segments]
- :or {with-default-temporal-breakout true, with-field-values true,
+ :or {with-default-temporal-breakout true, with-field-values false,
with-queryable-dimensions true, with-segments false}}
:- [:map
[:with-default-temporal-breakout {:optional true} [:maybe :boolean]]
@@ -247,7 +257,7 @@
{:entity-type "metric"
:entity-id id
:field-id field-id
- :limit (request/limit)})))
+ :limit (or (request/limit) default-field-values-limit)})))
(api.macros/defendpoint :post "/v1/search" :- ::search-response
"Search for tables and metrics.
@@ -320,7 +330,9 @@
(defn- construct-table-query
"Build a query from a table using the provided query components."
[body]
- (let [args (mc/encode ::construct-query-table-request body deftool/request-transformer)
+ (let [body (cond-> body
+ (not (:limit body)) (assoc :limit default-query-row-limit))
+ args (mc/encode ::construct-query-table-request body deftool/request-transformer)
data (check-tool-result (metabot-filters/query-datasource args))]
{:query (-> (:query data)
json/encode
diff --git a/enterprise/backend/src/metabase_enterprise/agent_api/reference.md b/enterprise/backend/src/metabase_enterprise/agent_api/reference.md
index bd5ae46cb48..e8da424aab4 100644
--- a/enterprise/backend/src/metabase_enterprise/agent_api/reference.md
+++ b/enterprise/backend/src/metabase_enterprise/agent_api/reference.md
@@ -115,7 +115,7 @@ Query parameters (all boolean, all optional):
| Parameter | Default | Description |
|---------------------|---------|-----------------------------------------|
| with-fields | true | Include field metadata |
-| with-field-values | true | Include sample values on each field |
+| with-field-values | false | Include sample values on each field |
| with-related-tables | true | Include FK-related tables |
| with-metrics | true | Include metrics defined on this table |
| with-measures | false | Include measures |
@@ -180,7 +180,7 @@ Query parameters (all boolean, all optional):
| Parameter | Default | Description |
|---------------------------------|---------|---------------------------------------|
| with-default-temporal-breakout | true | Include default time dimension |
-| with-field-values | true | Include sample values on dimensions |
+| with-field-values | false | Include sample values on dimensions |
| with-queryable-dimensions | true | Include dimensions for group_by |
| with-segments | false | Include applicable segments |
@@ -209,7 +209,7 @@ Response:
### GET /v1/metric/{id}/field/{field-id}/values
Get statistics and sample values for a field. Accepts optional `limit` query
-parameter.
+parameter (default: 30).
Response:
@@ -546,7 +546,8 @@ On failure:
{"status": "failed", "error": "Query error message"}
```
-Row limits are enforced automatically:
+Row limits:
+- Default: 100 rows (applied when no `limit` is specified in construct-query for tables)
- Simple queries (no aggregation): 2000 rows max
- Aggregated queries: 10000 rows max
diff --git a/enterprise/backend/test/metabase_enterprise/agent_api/api_test.clj b/enterprise/backend/test/metabase_enterprise/agent_api/api_test.clj
index 3f22fa0b35f..56c506bfbce 100644
--- a/enterprise/backend/test/metabase_enterprise/agent_api/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/agent_api/api_test.clj
@@ -212,7 +212,17 @@
:id table-id
:fields empty?}
(agent-client :rasta :get 200
- (str "agent/v1/table/" table-id "?with-fields=false&with-related-tables=false"))))))))
+ (str "agent/v1/table/" table-id "?with-fields=false&with-related-tables=false"))))))
+
+ (testing "Field values are excluded by default"
+ (let [table-id (mt/id :orders)
+ table (agent-client :rasta :get 200 (str "agent/v1/table/" table-id))]
+ (is (every? #(nil? (:field_values %)) (:fields table)))))
+
+ (testing "Field values are included when explicitly requested"
+ (let [table-id (mt/id :orders)
+ table (agent-client :rasta :get 200 (str "agent/v1/table/" table-id "?with-field-values=true"))]
+ (is (some #(seq (:field_values %)) (:fields table)))))))
(deftest get-metric-details-test
(with-agent-api-setup!
@@ -262,16 +272,18 @@
;; Ensure field values exist for the field we'll test
(ensure-fresh-field-values! (mt/id :people :state))
- (testing "Returns field statistics and values for a table field"
+ (testing "Returns field statistics and values with default limit of 30"
(let [table-id (mt/id :people)
field-id (visible-field-id table-id "State")]
(is (some? field-id) "Should find the State field")
- (is (=? {:statistics {:distinct_count 49}
- :values sequential?}
- (agent-client :crowberto :get 200
- (format "agent/v1/table/%d/field/%s/values" table-id field-id))))))
-
- (testing "Respects limit parameter"
+ (let [result (agent-client :crowberto :get 200
+ (format "agent/v1/table/%d/field/%s/values" table-id field-id))]
+ (is (=? {:statistics {:distinct_count 49}
+ :values sequential?}
+ result))
+ (is (<= (count (:values result)) 30) "Should apply default limit of 30"))))
+
+ (testing "Respects explicit limit parameter"
(let [table-id (mt/id :people)
field-id (visible-field-id table-id "State")
result (agent-client :crowberto :get 200
@@ -315,7 +327,14 @@
(is (= (mt/id) (lib/database-id decoded)))
(is (= (mt/id :orders) (lib/source-table-id decoded))))))
- (testing "Constructs a query with a limit"
+ (testing "Applies default limit of 100 when no limit is specified"
+ (let [table-id (mt/id :orders)
+ response (agent-client :rasta :post 200 "agent/v1/construct-query"
+ {:table_id table-id})
+ decoded (decode-query response)]
+ (is (= 100 (lib/current-limit decoded)))))
+
+ (testing "Respects explicit limit"
(let [table-id (mt/id :orders)
response (agent-client :rasta :post 200 "agent/v1/construct-query"
{:table_id table-id