Skip to content

Commit 285c4bc

Browse files
committed
only associate one jupyter notebook per run
Signed-off-by: Lance-Drane <[email protected]>
1 parent 2a252e9 commit 285c4bc

File tree

4 files changed

+27
-40
lines changed

4 files changed

+27
-40
lines changed

ipsportal/data_api.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
from flask import Blueprint, jsonify, request, Response
5+
from pymongo.errors import PyMongoError
56
from typing import Any, Tuple
67
from .db import db, get_runid, get_data_tags
78
from .minio import put_minio_object
@@ -109,14 +110,6 @@ def add() -> Tuple[Response, int]:
109110
if not portal_runid:
110111
return jsonify("Missing value for HTTP Header X-Ips-Portal-Runid"), 400
111112

112-
# Jupyter links are optional to associate with a data event.
113-
# If they DO exist, we use a non-printable delimiter to separate links in the header value
114-
juypter_links_header = request.headers.get("X-Ips-Jupyter-Links")
115-
if juypter_links_header:
116-
jupyter_links = juypter_links_header.split("\x01")
117-
else:
118-
jupyter_links = []
119-
120113
# runid is needed because the portal_runid is not a valid bucket name for MINIO
121114
runid = get_runid(portal_runid)
122115
if runid is None:
@@ -147,7 +140,6 @@ def add() -> Tuple[Response, int]:
147140
"tags": {
148141
"tag": resolved_tag,
149142
"data_location_url": data_location_url,
150-
"jupyter_urls": jupyter_links,
151143
}
152144
},
153145
},
@@ -182,14 +174,6 @@ def add_url() -> Tuple[Response, int]:
182174
if not isinstance(url, str):
183175
errors.append({"url": "Must be provided and a string"})
184176

185-
if not isinstance(data.get('tags'), list):
186-
errors.append({"tags": "Must be provided and a non-empty list"})
187-
else:
188-
try:
189-
tag_lookups = set(map(float, data.get('tags')))
190-
except ValueError:
191-
errors.append({"tags": "Must be floating point values"})
192-
193177
portal_runid = data.get('portal_runid')
194178
if not isinstance(portal_runid, str):
195179
errors.append({"portal_runid": "Must be provided"})
@@ -203,16 +187,14 @@ def add_url() -> Tuple[Response, int]:
203187
if errors:
204188
return jsonify(errors), 400
205189

206-
# TODO figure out how to do this entirely in Mongo
207-
for tags_prop in result['tags']: # type: ignore[index]
208-
if tags_prop['tag'] in tag_lookups and url not in tags_prop['jupyter_urls']:
209-
tags_prop['jupyter_urls'].append(url)
210-
db.data.replace_one(
211-
{'_id': result['_id']}, # type: ignore[index]
212-
result # type: ignore[arg-type]
213-
)
214-
215-
return jsonify([]), 200
190+
try:
191+
db.data.update_one(
192+
{"portal_runid": portal_runid},
193+
{"$set": {"jupyter_url": url}},
194+
)
195+
return jsonify([]), 200
196+
except PyMongoError:
197+
return jsonify([{"portal_runid", "does not exist"}]), 400
216198

217199

218200
@bp.route("/api/data/query", methods=["POST"])

ipsportal/db.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,12 @@ def get_data_tags(portal_runid: str) -> Optional[dict[str, Any]]:
178178
if result:
179179
return result.get('tags')
180180
return None
181+
182+
183+
def get_data_information(portal_runid: str) -> tuple[Optional[dict[str, Any]], Optional[str]]:
184+
result = db.data.find_one(
185+
{"portal_runid": portal_runid}, projection={"_id": False, "tags": True, "jupyter_url": True}
186+
)
187+
if result:
188+
return result.get('tags'), result.get('jupyter_url')
189+
return None, None

ipsportal/run.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Tuple
22
from flask import Blueprint, render_template
3-
from ipsportal.db import get_run, get_runid, get_data_tags
3+
from ipsportal.db import get_run, get_runid, get_data_information
44

55
bp = Blueprint('index', __name__)
66

@@ -20,5 +20,5 @@ def run(runid: int) -> Tuple[str, int]:
2020
run['parent_runid'] = get_runid(str(run.get('parent_portal_runid')))
2121
else:
2222
run['parent_runid'] = None
23-
data_info = get_data_tags(run['portal_runid'])
24-
return render_template("events.html", run=run, data_info=data_info), 200
23+
data_info, jupyter_url = get_data_information(run['portal_runid'])
24+
return render_template("events.html", run=run, data_info=data_info, jupyter_url=jupyter_url), 200

ipsportal/templates/events.html

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ <h4>{% block title %}Run - {{ run.runid }}{% endblock %}</h4>
100100
<td><a href="{{run.parent_runid}}">Run {{ run.parent_runid }}</a></td>
101101
</tr>
102102
{% endif %}
103+
{% if jupyter_url %}
104+
<tr>
105+
<th>Associated JupyterHub Notebook</th>
106+
<td><a href="{{jupyter_url}}">Explore JupyterHub Notebook</a></td>
107+
</tr>
108+
{% endif %}
103109
</tbody>
104110
</table>
105111

@@ -112,16 +118,6 @@ <h4>{% block title %}Run - {{ run.runid }}{% endblock %}</h4>
112118
<details>
113119
<summary>{{item.tag}}</summary>
114120
<p><a class="raw-data-uri" href="{{item.data_location_url}}" download="{{run.portal_runid}}__{{item.tag}}">Download raw data</a></p>
115-
{% if item.jupyter_urls %}
116-
<details>
117-
<summary>Associated JupyterHub Links</summary>
118-
<ul>
119-
{% for url in item.jupyter_urls %}
120-
<li><a href="{{url}}">{{url}}</a></li>
121-
{% endfor %}
122-
</ul>
123-
</details>
124-
{% endif %}
125121
</details>
126122
</li>
127123
{% endfor %}

0 commit comments

Comments
 (0)