about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-11-17 12:25:06 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-11-17 12:25:06 +0000
commitd480e89997eb44c7867756ce2d8716a63fe2feaa (patch)
treeb54eb53a25a2e2d4da6426009fcbbf23c0a86092
parent9be4832dd827a3486848bd45960e51111e2e589d (diff)
downloadgenenetwork4-d480e89997eb44c7867756ce2d8716a63fe2feaa.tar.gz
groups/{group}/traits: limit_to / ids_only / names_only, old shape by default
Fourth of the six, and the first with a payload worth trimming: BXDPublish has
15 060 traits, so `names_only` takes the answer from megabytes of objects to a
list a dropdown can use.

Verified on the deployed build:

  GET /groups/BXD/traits                        15 060 objects, keys as before
  GET ...?ids_only=true&limit_to=10001,NOPE     ["10001"] + note: nope

The listing is parsed before the auth-server round trip, so a contradictory
request (ids_only + names_only) is refused without spending a gn-auth call.
Read tier: 24 ok, 0 failed.
-rw-r--r--src/routes/groups.rs57
1 files changed, 48 insertions, 9 deletions
diff --git a/src/routes/groups.rs b/src/routes/groups.rs
index 727013a..28e9c03 100644
--- a/src/routes/groups.rs
+++ b/src/routes/groups.rs
@@ -807,6 +807,16 @@ pub struct GroupTraitsParams {
     /// Only the traits with this short name (abbreviation), e.g.
     /// "5HT_MB_M" or "3NP_Tox".
     pub abbreviation: Option<String>,
+    /// Keep only these ids or trait names, comma-separated. Values that match
+    /// nothing are named in the reply's note.
+    #[serde(default)]
+    pub limit_to: Option<String>,
+    /// Answer with one id per trait instead of objects.
+    #[serde(default)]
+    pub ids_only: Option<bool>,
+    /// Answer with one name per trait instead of objects.
+    #[serde(default)]
+    pub names_only: Option<bool>,
 }
 
 enum GroupTraitsLookup {
@@ -844,7 +854,8 @@ fn get_group_traits_result(
     ("group" = String, Path, description = "Group InbredSetName, Name, FullName or numeric Id"),
     GroupTraitsParams
 ), responses(
-    (status = 200, description = "The published traits belonging to the group", body = Envelope<Vec<db::TraitInfo>>),
+    (status = 200, description = "The published traits belonging to the group; \
+`ids_only`/`names_only` answer one value per trait instead", body = Envelope<crate::listing::Rows>),
     Unauthorized,
     (status = 403, description = "The group's published data isn't public, and the caller isn't authorized for it", body = ApiError),
     (status = 404, description = "No matching group", body = ApiError),
@@ -856,8 +867,24 @@ pub async fn group_traits(
     Path(group): Path<String>,
     token: AuthToken,
     Query(params): Query<GroupTraitsParams>,
-) -> Result<Json<Envelope<Vec<db::TraitInfo>>>, ApiError> {
+) -> Result<Json<Envelope<crate::listing::Rows>>, ApiError> {
     let rid = ctx.request_info();
+    // No listing parameters means the old answer: `Rows::Objects`.
+    let listing = crate::listing::Listing::parse(&crate::listing::ListingParams {
+        limit_to: params.limit_to.clone(),
+        ids_only: params.ids_only,
+        names_only: params.names_only,
+    })
+    .map_err(|detail| {
+        ApiError::new(
+            StatusCode::UNPROCESSABLE_ENTITY,
+            "invalid_listing",
+            "Invalid listing parameters",
+            detail,
+            "group_traits",
+        )
+        .with_request_id(rid.clone())
+    })?;
     let not_found_name = group.clone();
     let auth_server_url = Config::load().auth_server_url.clone();
     tokio::task::spawn_blocking(move || {
@@ -893,15 +920,27 @@ pub async fn group_traits(
     .and_then(|lookup| match lookup {
         GroupTraitsLookup::Found(traits) => {
             let group = path_segment(&not_found_name);
-            Ok(Json(
-                ctx.envelope(traits)
-                    .with_note(db::LOD_NOTE)
-                    .with_links(crate::suggest::root_links())
-                    .with_next(
+            let rows: Vec<serde_json::Value> = traits
+                .iter()
+                .map(|trait_info| serde_json::to_value(trait_info).unwrap_or(serde_json::Value::Null))
+                .collect();
+            let (shaped, missing) = listing.shape(rows, params.limit_to.as_deref());
+            let mut envelope = ctx
+                .envelope(shaped)
+                .with_note(db::LOD_NOTE)
+                .with_links(crate::suggest::root_links())
+                .with_next(
                     &format!("/groups/{group}"),
                     "The group these traits belong to",
-                ),
-            ))
+                );
+            if !missing.is_empty() {
+                envelope = envelope.with_note(&format!(
+                    "limit_to named {} value(s) that matched no trait: {}",
+                    missing.len(),
+                    missing.join(", ")
+                ));
+            }
+            Ok(Json(envelope))
         }
         GroupTraitsLookup::Forbidden => Err(ApiError::new(
             StatusCode::FORBIDDEN,