about summary refs log tree commit diff
path: root/uploader/genotypes
diff options
context:
space:
mode:
Diffstat (limited to 'uploader/genotypes')
-rw-r--r--uploader/genotypes/models.py24
-rw-r--r--uploader/genotypes/views.py50
2 files changed, 42 insertions, 32 deletions
diff --git a/uploader/genotypes/models.py b/uploader/genotypes/models.py
index 4c3e634..34d2cfe 100644
--- a/uploader/genotypes/models.py
+++ b/uploader/genotypes/models.py
@@ -31,16 +31,28 @@ def genotype_markers(
         species_id: int,
         offset: int = 0,
         limit: Optional[int] = None
-) -> tuple[dict, ...]:
+) -> tuple[tuple[dict, ...], int]:
     """Retrieve markers from the database."""
-    _query = "SELECT * FROM Geno WHERE SpeciesId=%s"
-    if bool(limit) and limit > 0:# type: ignore[operator]
-        _query = _query + f" LIMIT {limit} OFFSET {offset}"
+    _query_template = (
+        "SELECT %%COLS%% FROM Geno AS gno "
+        "WHERE gno.SpeciesId=%s "
+        "%%LIMIT%%")
 
     with conn.cursor(cursorclass=DictCursor) as cursor:
-        cursor.execute(_query, (species_id,))
+        cursor.execute(
+            _query_template.replace("%%LIMIT%%", "").replace(
+                "%%COLS%%", "COUNT(gno.Id) AS total_records"),
+            (species_id,))
+        _total_records = cursor.fetchone()["total_records"]
+        cursor.execute(
+            _query_template.replace("%%COLS%%", "gno.*").replace(
+                "%%LIMIT%%",
+                (f"LIMIT {int(limit)} OFFSET {int(offset)}"
+                 if bool(limit) and limit > 0
+                 else "")),
+            (species_id,))
         debug_query(cursor, app.logger)
-        return tuple(dict(row) for row in cursor.fetchall())
+        return tuple(dict(row) for row in cursor.fetchall()), _total_records
 
 
 def genotype_dataset(
diff --git a/uploader/genotypes/views.py b/uploader/genotypes/views.py
index 3fa2131..f27671c 100644
--- a/uploader/genotypes/views.py
+++ b/uploader/genotypes/views.py
@@ -6,6 +6,7 @@ from pymonad.either import Left, Right, Either
 from gn_libs.mysqldb import database_connection
 from flask import (flash,
                    request,
+                   jsonify,
                    redirect,
                    Blueprint,
                    render_template,
@@ -19,8 +20,8 @@ from uploader.route_utils import generic_select_population
 from uploader.datautils import safe_int, enumerate_sequence
 from uploader.species.models import all_species, species_by_id
 from uploader.monadic_requests import make_either_error_handler
-from uploader.request_checks import with_species, with_population
 from uploader.population.models import population_by_species_and_id
+from uploader.request_checks import with_species, with_dataset, with_population
 
 from .models import (genotype_markers,
                      genotype_dataset,
@@ -56,34 +57,31 @@ def list_genotypes(species: dict, population: dict, **kwargs):# pylint: disable=
 
 
 @genotypesbp.route(
-    "/<int:species_id>/populations/<int:population_id>/genotypes/list-markers",
+    "/<int:species_id>/populations/<int:population_id>/genotypes/<int:dataset_id>/list-markers",
     methods=["GET"])
 @require_login
-@with_population(species_redirect_uri="species.list_species",
-                 redirect_uri="species.populations.list_species_populations")
-def list_markers(
-        species: dict,
-        population: dict,
-        **kwargs
-):# pylint: disable=[unused-argument]
-    """List a species' genetic markers."""
+@with_species(redirect_uri="species.populations.genotypes.list_genotypes")
+def list_markers(species: dict, **_kwargs):
+    """List the markers that exist for this species."""
+    args = request.args
+    offset = int(args.get("start") or 0)
     with database_connection(app.config["SQL_URI"]) as conn:
-        start_from = max(safe_int(request.args.get("start_from") or 0), 0)
-        count = safe_int(request.args.get("count") or 20)
-        return render_template("genotypes/list-markers.html",
-                               species=species,
-                               population=population,
-                               total_markers=genotype_markers_count(
-                                   conn, species["SpeciesId"]),
-                               start_from=start_from,
-                               count=count,
-                               markers=enumerate_sequence(
-                                   genotype_markers(conn,
-                                                    species["SpeciesId"],
-                                                    offset=start_from,
-                                                    limit=count),
-                                   start=start_from+1),
-                               activelink="list-markers")
+        markers, total_records = genotype_markers(
+            conn,
+            species["SpeciesId"],
+            offset=offset,
+            limit=int(args.get("length") or 0))
+        return jsonify({
+            **({"draw": int(args.get("draw"))}
+               if bool(args.get("draw") or False)
+               else {}),
+            "recordsTotal": total_records,
+            "recordsFiltered": len(markers),
+            "markers": tuple({**marker, "index": idx}
+                             for idx, marker in
+                             enumerate(markers, start=offset+1))
+        })
+
 
 @genotypesbp.route(
     "/<int:species_id>/populations/<int:population_id>/genotypes/datasets/"