about summary refs log tree commit diff
path: root/src/db.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-08 16:23:03 +0000
committerPjotr <wrk@bosa.lan>2026-11-08 16:23:14 +0000
commite934e755f14648fff6ebac07e9cf9e5b37cb3fe4 (patch)
treeb7b921fe7e83bed348d65f3ecda3cb284479cd19 /src/db.rs
parent0769a0ea0a8654e5e50e03365545c8ccf58d6b91 (diff)
downloadgenenetwork4-e934e755f14648fff6ebac07e9cf9e5b37cb3fe4.tar.gz
api: describe case attributes, and gate large computations only
Two things that came out of reading a real response rather than a test.

**Say what a case attribute means.** `GET /groups/{group}/case-attributes`
returned names and values, and the per-trait attribute endpoint returned
values, so a caller had to guess that `SeqCvge` is a sequencing depth.
MariaDB has had the answer all along in `CaseAttribute.Description`:

  SeqCvge   Depth of DNA sequencing for 1 male of each strain in 2016
  Status    Live= Available at JAX, Cryo=Cryopreserved only, Extinct
  Epoch     BXD family subgroups. Each number with common parents. ...
  Sex       F= female, M= male

Both endpoints now carry it -- a `descriptions` map on the group listing, a
`description` field on the per-trait one -- and the covariate notes quote it,
so `/compute/gemma` answers "Covariate SeqCvge (Depth of DNA sequencing for
1 male of each strain in 2016): categorical ..." instead of leaving the name
to be interpreted. It matters most for covariates: `SeqCvge` is a property
of the *genotype* data, its values include compound ones (`40+31L`, two
sequencing runs), and the server codes those as 20-odd levels rather than as
a depth -- a covariate fitted for the wrong reason, which the description is
the first hint against.

**Gate large computations, not every computation.** The gate was applied to
every job, so a 93-strain test request could be refused because somebody
else's 6 000-individual run held the slot -- which is how the test suite
started serialising itself behind a mutex, a workaround for the wrong
problem. What the gate is for is *large* work: a BXD panel of ~200 finishes
in seconds and two at once cost nothing, while UM-HET3's 6 382 write 207 MB
of kinship per chromosome. It now starts at `LARGE_PANEL` (1 000
individuals): below that no slot is taken, nothing waits and nothing is
refused; above it the old policy holds unchanged (1 slot for gemma, 1 for
ravanan, `RQTL_MAX_CONCURRENT` for rqtl, one large job per caller, 409
naming the run in the way).

The size is not known when the request arrives -- the panel is resolved
server-side -- so the decorator now makes a *light* claim and the handler
promotes it (`__gn4_claim.claim_slot_or_busy(individuals, rid)`) at the
point where it knows, then hands it to `Gate::run` around the call that
computes, so the slot still outlives a client disconnect. A light claim
registers nothing and its drop removes nothing: only a held claim may touch
the claimant map.

* src/compute_gate.rs: `LARGE_PANEL`/`is_large`, `Gate::claim_light`,
  `Claim::claim_slot_or_busy`/`is_held`, `busy_error` (the 409 policy, moved
  out of `claim_or_busy`), and `Gate::run` unchanged in spirit.
* gnauth-macros: binds the light claim; the body keeps one gate call.
* src/routes/{gemma,rqtl}.rs: promotion after the panel/sample count is
  known, and the compute call wrapped in `run`; error mapping moved inside
  the job so the future returns `ApiError`.
* src/rqtl.rs: `Sample` is `Clone` (the job owns its copy).
* tests: the gemma suite loses its lock entirely (the fixtures and BXD.4 are
  under the threshold); rqtl keeps its lock only for the machine's sake --
  concurrent `Rscript` scans would compete for cores and scratch, not for a
  slot. Four new gate tests pin the threshold, that a small job never takes
  the slot, that one caller cannot hold two large jobs, and that another
  caller waits rather than fails.
* README.md, docs/www/ai-instruction.md, docs/www/operator-checklist.md:
  what the threshold is and why it is 1 000.

Measured on :3001 after the change: two concurrent small-panel GEMMA
requests from one token both answer 200 (one was 409 before), while two
6 382-individual requests answer 502/409 -- the second gated with the usual
`compute_busy` naming the first.

277 tests pass; the same four as before do not.
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs72
1 files changed, 52 insertions, 20 deletions
diff --git a/src/db.rs b/src/db.rs
index 4919264..283f4bf 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -576,6 +576,15 @@ pub struct TraitCaseAttribute {
     pub value: Option<String>,
 }
 
+/// One attribute of one trait: its values on that trait's samples, and
+/// what the attribute means (`CaseAttribute.Description`, advertised
+/// because the names are not self-explanatory -- `SeqCvge` is "Depth of
+/// DNA sequencing for 1 male of each strain in 2016").
+pub struct TraitCaseAttributeLookup {
+    pub description: Option<String>,
+    pub values: Vec<TraitCaseAttribute>,
+}
+
 /// A trait's sample names, in the order its values come back.
 pub fn get_group_trait_samples(group_id: u16, trait_id: u32) -> Result<Vec<String>, DbError> {
     Ok(get_group_trait_values(group_id, trait_id)?
@@ -593,17 +602,18 @@ pub fn get_trait_case_attribute(
     group_id: u16,
     trait_id: u32,
     attribute: &str,
-) -> Result<Option<Vec<TraitCaseAttribute>>, DbError> {
+) -> Result<Option<TraitCaseAttributeLookup>, DbError> {
     let pool = pool()?;
     let mut conn = pool.get_conn()?;
-    let defined: Option<String> = conn.exec_first(
-        "SELECT Name FROM CaseAttribute \
+    let defined: Option<(String, String)> = conn.exec_first(
+        "SELECT Name, Description FROM CaseAttribute \
          WHERE InbredSetId = :group_id AND Name = :attribute LIMIT 1",
         mysql::params! { "group_id" => group_id, "attribute" => attribute },
     )?;
-    if defined.is_none() {
+    let Some((_, description)) = defined else {
         return Ok(None);
-    }
+    };
+    let description = description.trim().to_string();
     let samples = get_group_trait_samples(group_id, trait_id)?;
     // The attribute's values for the whole group, then aligned onto the
     // trait's samples: the case-attribute table is keyed by
@@ -626,15 +636,16 @@ pub fn get_trait_case_attribute(
             (strain, value)
         })
         .collect();
-    Ok(Some(
-        samples
+    Ok(Some(TraitCaseAttributeLookup {
+        description: (!description.is_empty()).then_some(description),
+        values: samples
             .into_iter()
             .map(|name| {
                 let value = by_name.get(&name).cloned().filter(|v| !v.is_empty());
                 TraitCaseAttribute { name, value }
             })
             .collect(),
-    ))
+    }))
 }
 
 /// One published (phenotype) trait's metadata: its PublishXRef mapping
@@ -985,26 +996,36 @@ pub struct StrainCaseAttributes {
 }
 
 /// A group's case attributes: every attribute name defined for it
-/// (`CaseAttribute.Name`) and, for each strain that has at least one
-/// value set, that strain's values -- combined into one response
-/// rather than GN3's separate .../names and .../values endpoints,
-/// since neither list is large enough on its own to need splitting.
+/// (`CaseAttribute.Name`), what each one means (`CaseAttribute.Description`),
+/// and, for each strain that has at least one value set, that strain's
+/// values -- combined into one response rather than GN3's separate
+/// .../names and .../values endpoints, since neither list is large enough
+/// on its own to need splitting. The descriptions are what make the names
+/// usable: `SeqCvge` is unguessable, and its description -- "Depth of DNA
+/// sequencing for 1 male of each strain in 2016" -- says both what it is
+/// and why it is not a phenotype.
 #[derive(Serialize, utoipa::ToSchema)]
 pub struct CaseAttributes {
     pub names: Vec<String>,
+    /// Attribute name -> description, for the names that have one. A
+    /// description can be empty in the database; those are left out
+    /// rather than served as "".
+    pub descriptions: std::collections::BTreeMap<String, String>,
     pub values: Vec<StrainCaseAttributes>,
 }
 
-/// Fetch every case-attribute name defined for a group, matching
-/// GeneNetwork3's gn3.api.case_attributes.__case_attribute_labels_by_inbred_set__
-/// (trimmed to just the name -- Description isn't used by genenetwork4
-/// yet, and CaseAttributeId/InbredSetId are already implied by this
-/// being scoped to one group).
-fn get_group_case_attribute_names(group_id: u16) -> Result<Vec<String>, DbError> {
+/// Fetch every case-attribute name and description defined for a group,
+/// matching GeneNetwork3's
+/// gn3.api.case_attributes.__case_attribute_labels_by_inbred_set__ (which
+/// selects the name only; `CaseAttribute.Description` is what genenetwork4
+/// adds -- CaseAttributeId/InbredSetId are already implied by this being
+/// scoped to one group).
+fn get_group_case_attribute_definitions(group_id: u16) -> Result<Vec<(String, String)>, DbError> {
     let pool = pool()?;
     let mut conn = pool.get_conn()?;
     conn.exec(
-        "SELECT Name FROM CaseAttribute WHERE InbredSetId = :group_id ORDER BY Name",
+        "SELECT Name, Description FROM CaseAttribute \
+         WHERE InbredSetId = :group_id ORDER BY Name",
         mysql::params! { "group_id" => group_id },
     )
     .map_err(DbError::from)
@@ -1063,8 +1084,19 @@ fn get_group_case_attribute_values(group_id: u16) -> Result<Vec<StrainCaseAttrib
 /// Fetch a group's case attributes: every attribute name defined for
 /// it, and every strain's values for them. See `CaseAttributes`.
 pub fn get_group_case_attributes(group_id: u16) -> Result<CaseAttributes, DbError> {
+    let definitions = get_group_case_attribute_definitions(group_id)?;
+    let mut names = Vec::with_capacity(definitions.len());
+    let mut descriptions = std::collections::BTreeMap::new();
+    for (name, description) in definitions {
+        let description = description.trim().to_string();
+        if !description.is_empty() {
+            descriptions.insert(name.clone(), description);
+        }
+        names.push(name);
+    }
     Ok(CaseAttributes {
-        names: get_group_case_attribute_names(group_id)?,
+        names,
+        descriptions,
         values: get_group_case_attribute_values(group_id)?,
     })
 }