aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2024-04-17 15:47:23 +0300
committerBonfaceKilz2024-04-30 12:18:58 +0300
commit4d0326e998136bc330171bf760bc1e8a0a6cccf4 (patch)
tree67c67c84d91818ea687eacfc04ef1ddaf1dfc8c6
parente4cc7ffce07f8c33f42e2ec0257277897217c98b (diff)
downloadgenenetwork3-4d0326e998136bc330171bf760bc1e8a0a6cccf4.tar.gz
Add an endpoint to query a dataset's git history from it's id.
* gn3/api/metadata.py: Import get_history. (view_history): New endpoint. * gn3/db/datasets.py (get_history): New function. Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
-rw-r--r--gn3/api/metadata.py23
-rw-r--r--gn3/db/datasets.py31
2 files changed, 53 insertions, 1 deletions
diff --git a/gn3/api/metadata.py b/gn3/api/metadata.py
index fe7e903..a6a88fc 100644
--- a/gn3/api/metadata.py
+++ b/gn3/api/metadata.py
@@ -7,7 +7,8 @@ from flask import request
from flask import current_app
from gn3.db.datasets import (retrieve_metadata,
- save_dataset_metadata)
+ save_metadata,
+ get_history)
from gn3.db.rdf import RDF_PREFIXES
from gn3.db.rdf import (query_frame_and_compact,
query_and_compact,
@@ -263,6 +264,26 @@ CONSTRUCT {
)
+@metadata.route("/datasets/<id_>/history")
+def view_history(id_):
+ history = get_history(
+ git_dir=Path(current_app.config.get("DATA_DIR"),
+ "gn-docs"),
+ name=id_,
+ ).either(
+ lambda error: {
+ "error": "Unable to fetch history",
+ "error_description": error,
+ },
+ lambda history: {
+ "id": id_,
+ "history": history,
+ })
+ if history.get("error"):
+ raise Exception(history.get("error_description"))
+ return history
+
+
@metadata.route("/datasets/search/<term>", methods=["GET"])
def search_datasets(term):
"""Search datasets"""
diff --git a/gn3/db/datasets.py b/gn3/db/datasets.py
index fef01a5..f3b4f9f 100644
--- a/gn3/db/datasets.py
+++ b/gn3/db/datasets.py
@@ -408,3 +408,34 @@ def save_metadata(
]))
.then(lambda _: monadic_run_cmd(f"git -C {git_dir} \
push origin master --dry-run".split(" "))))
+
+
+def get_history(git_dir: str, name: str) -> Either:
+ """Get the git history for a given dataset. This history is
+ returned as a HTML formatted text. Example of the formatted
+ output:
+
+ ```
+ <tr>
+ <td><i>3 Weeks Ago</i></td>
+ <td>
+ <a style='color:green;' href='..id=some-id' target='_blank'>
+ some-id
+ </a>
+ </td>
+ <td>commit header</td>
+ <td>Author Name</td>
+ </tr>
+ ```
+
+ """
+ pretty_format_str = "<tr><td><i>%cr</i></td>\
+<td>\
+<a style='color:green;' href='https://git.genenetwork.org/gn-docs/commit/general?id=%H' \
+target='_blank'>%h</a></td>\
+<td>%s</td><td>%an</td></tr>"
+ return monadic_run_cmd([
+ "git", "-C",
+ str(Path(git_dir) / "general/datasets/" / name),
+ "log", f"--pretty=format:{pretty_format_str}",
+ ])