Skip to content
Snippets Groups Projects
Commit 49e89899 authored by Nico Vermaas's avatar Nico Vermaas
Browse files

making plots collapsable

adding status_code check
refactor to prevent double code
parent fd3e4cfc
No related branches found
No related tags found
1 merge request!291Sdc 935 better plots
Pipeline #47782 passed
...@@ -679,8 +679,62 @@ def unique_values_for_aggregation_key(queryset, aggregation_key): ...@@ -679,8 +679,62 @@ def unique_values_for_aggregation_key(queryset, aggregation_key):
return list(map(lambda x: x[aggregation_key], queryset.values(aggregation_key).distinct())) return list(map(lambda x: x[aggregation_key], queryset.values(aggregation_key).distinct()))
def add_plots(task, token, translation, results):
# keep a temporary list of filenames to check uniqueness
plot_files = []
plots = task.quality_json["plots"]
count = 0
for plot in plots:
basename = plot['basename']
checksum = plot['checksum']
# plot_file = basename + str(plot['size'])
plot_file = checksum
# only add unique files
if not plot_file in plot_files:
count = count + 1
surl = plot['surl'] + "?action=show&authz=" + str(token)
url = surl.replace(translation[0], translation[1])
if basename.endswith('png'):
# retrieve the url and add the binary data to the html
response = requests.get(url)
if response.status_code == 200:
content_as_string = base64.b64encode(response.content).decode("utf-8")
img_html = '<img width="800" src = "data:image/png;base64,' + content_as_string + '">'
collapseable = """
<p>
<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#replace_with_id" aria-expanded="false" aria-controls="replace_with_id">
<i class="fas fa-image"></i>&nbsp;&nbsp;replace_with_basename
</button>
</p>
<div class="collapse" id="replace_with_id">
<div class="card card-body">
<a href="replace_with_url" target='_blank'>replace_with_image</a>
<hr>
<i>(click on image for orignal)</i>
</div>
</div>
"""
collapseable = collapseable.replace('replace_with_basename', basename)
collapseable = collapseable.replace('replace_with_image', img_html)
collapseable = collapseable.replace('replace_with_url', url)
collapseable = collapseable.replace('replace_with_id', "plot" + str(count))
results += '<tr><td>' + collapseable + '</td></tr>'
else:
results += '<tr><td><a href="' + url + '" target="_blank">' + basename + '</a></td></tr>'
plot_files.append(plot_file)
return results
def construct_inspectionplots(task,source='task_id'): def construct_inspectionplots(task,source='task_id'):
#results = "<h4>Inspection Plots (for SAS_ID " + task.sas_id + ")</h4>"
# translate the path to a url # translate the path to a url
try: try:
...@@ -688,8 +742,6 @@ def construct_inspectionplots(task,source='task_id'): ...@@ -688,8 +742,6 @@ def construct_inspectionplots(task,source='task_id'):
except: except:
srm_to_url = "srm://srm.grid.sara.nl/pnfs/grid.sara.nl/data/lofar/ops/disk/ldv/::https://webdav.grid.surfsara.nl/" srm_to_url = "srm://srm.grid.sara.nl/pnfs/grid.sara.nl/data/lofar/ops/disk/ldv/::https://webdav.grid.surfsara.nl/"
# https://webdav.grid.surfsara.nl/pnfs/grid.sara.nl/data/lofar/ops/disk/ldv::https://webdav.grid.surfsara.nl
token = str(Configuration.objects.get(key='dcache:token').value) token = str(Configuration.objects.get(key='dcache:token').value)
translation = srm_to_url.split("::") translation = srm_to_url.split("::")
...@@ -697,37 +749,13 @@ def construct_inspectionplots(task,source='task_id'): ...@@ -697,37 +749,13 @@ def construct_inspectionplots(task,source='task_id'):
if source == 'task_id': if source == 'task_id':
results = "<h4>Inspection Plots and Summary Logs</h4>" results = "<h4>Inspection Plots and Summary Logs</h4>"
results += "<p>Clicking a link will redirect to SURF SARA in a new browser window. </p>" results += "<p>Clicking a link will redirect to SURF SARA in a new browser window. </p>"
results = add_plots(task, token, translation, results)
plots = task.quality_json["plots"]
# keep a temporary list of filenames to check uniqueness
plot_files = []
for plot in plots:
basename = plot['basename']
if not basename in plot_files:
surl = plot['surl'] + "?action=show&authz=" + str(token)
url = surl.replace(translation[0],translation[1])
results += '<tr><td><a href="' + url + '" target="_blank">'+ basename + '</a></td></tr>'
if basename.endswith('png'):
# retrieve the url and add the binary data to the html
# response = requests.get(url)
# content_as_string = base64.b64encode(response.content).decode("utf-8")
# img_html = '<img width="800" src = "data:image/png;base64,' + content_as_string + '">'
#results += '<tr><td><a href="' + url + '" alt="' + basename + '" target="_blank">' + img_html + '</a></td></tr>'
results += '<tr><td><a href="' + url + '" target="_blank"><img width="800" src="' + url + '" alt="' + basename + '"/></a></td></tr>'
plot_files.append(basename)
elif source == 'sas_id': elif source == 'sas_id':
sas_id = task.sas_id sas_id = task.sas_id
results = "<h4>(Unique) Inspection Plots and Summary Logs for SAS_ID" + str(sas_id) + "</h4>" results = "<h4>(Unique) Inspection Plots and Summary Logs for SAS_ID" + str(sas_id) + "</h4>"
results += "<p>Clicking a link will redirect to SURF SARA in a new browser window. </p>" results += "<p>Clicking a link will redirect to SURF SARA in a new browser window. </p>"
# keep a temporary list of filenames to check uniqueness
plot_files = []
tasks = Task.objects.filter(sas_id=sas_id) tasks = Task.objects.filter(sas_id=sas_id)
for task in tasks: for task in tasks:
...@@ -738,32 +766,7 @@ def construct_inspectionplots(task,source='task_id'): ...@@ -738,32 +766,7 @@ def construct_inspectionplots(task,source='task_id'):
try: try:
results += '<tr style="background-color:#7EB1C4"><td colspan="3"><b>Task ' + str(task.id) + '</b></td></tr>' results += '<tr style="background-color:#7EB1C4"><td colspan="3"><b>Task ' + str(task.id) + '</b></td></tr>'
plots = task.quality_json["plots"] results = add_plots(task,token,translation,results)
for plot in plots:
basename = plot['basename']
checksum = plot['checksum']
#plot_file = basename + str(plot['size'])
plot_file = checksum
# only add unique files
if not plot_file in plot_files:
surl = plot['surl'] + "?action=show&authz=" + str(token)
url = surl.replace(translation[0], translation[1])
results += '<tr><td><a href="' + url + '" target="_blank">' + basename + '</a></td></tr>'
if basename.endswith('png'):
# retrieve the url and add the binary data to the html
# response = requests.get(url)
# content_as_string = base64.b64encode(response.content).decode("utf-8")
# img_html = '<img width="800" src = "data:image/png;base64,' + content_as_string + '">'
# results += '<tr><td><a href="' + url + '" alt="'+basename+'" target="_blank">' + img_html+ '</a></td></tr>'
results += '<tr><td><a href="' + url + '" target="_blank"><img width="800" src="' + url + '" alt="'+basename+'"/></a></td></tr>'
plot_files.append(plot_file)
except Exception as error: except Exception as error:
logger.error(error) logger.error(error)
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
{% if task.has_plots %} {% if task.has_plots %}
<td> <td>
<a class="open-modal btn btn-primary btn-sm" <a class="open-modal btn btn-primary btn-sm"
href="{% url 'inspection-plots' task.id my_tasks.number %}" href="{% url 'inspection-plots-sasid' task.id my_tasks.number %}"
data-popup-url="{% url 'inspection-plots-sasid' task.id my_tasks.number %}"> data-popup-url="{% url 'inspection-plots-sasid' task.id my_tasks.number %}">
<img src="{% static 'taskdatabase/surfsara.jpg' %}" height="20" alt="inspection plots"> <img src="{% static 'taskdatabase/surfsara.jpg' %}" height="20" alt="inspection plots">
</a>&nbsp; </a>&nbsp;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment