From 49e8989917eb4fce6c84d3d14eaeb8de47f12f60 Mon Sep 17 00:00:00 2001 From: Vermaas <vermaas@astron.nl> Date: Fri, 14 Apr 2023 14:24:51 +0200 Subject: [PATCH] making plots collapsable adding status_code check refactor to prevent double code --- atdb/taskdatabase/services/algorithms.py | 111 +++++++++--------- .../taskdatabase/validation/tasks.html | 2 +- 2 files changed, 58 insertions(+), 55 deletions(-) diff --git a/atdb/taskdatabase/services/algorithms.py b/atdb/taskdatabase/services/algorithms.py index f170ed76..f9d94082 100644 --- a/atdb/taskdatabase/services/algorithms.py +++ b/atdb/taskdatabase/services/algorithms.py @@ -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())) +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> 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'): - #results = "<h4>Inspection Plots (for SAS_ID " + task.sas_id + ")</h4>" # translate the path to a url try: @@ -688,8 +742,6 @@ def construct_inspectionplots(task,source='task_id'): except: 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) translation = srm_to_url.split("::") @@ -697,37 +749,13 @@ def construct_inspectionplots(task,source='task_id'): if source == 'task_id': results = "<h4>Inspection Plots and Summary Logs</h4>" results += "<p>Clicking a link will redirect to SURF SARA in a new browser window. </p>" - - 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) + results = add_plots(task, token, translation, results) elif source == 'sas_id': sas_id = task.sas_id 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>" - # keep a temporary list of filenames to check uniqueness - plot_files = [] - tasks = Task.objects.filter(sas_id=sas_id) for task in tasks: @@ -738,32 +766,7 @@ def construct_inspectionplots(task,source='task_id'): try: results += '<tr style="background-color:#7EB1C4"><td colspan="3"><b>Task ' + str(task.id) + '</b></td></tr>' - plots = task.quality_json["plots"] - - 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) + results = add_plots(task,token,translation,results) except Exception as error: logger.error(error) diff --git a/atdb/taskdatabase/templates/taskdatabase/validation/tasks.html b/atdb/taskdatabase/templates/taskdatabase/validation/tasks.html index d5e1eaab..cb47196b 100644 --- a/atdb/taskdatabase/templates/taskdatabase/validation/tasks.html +++ b/atdb/taskdatabase/templates/taskdatabase/validation/tasks.html @@ -18,7 +18,7 @@ {% if task.has_plots %} <td> <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 %}"> <img src="{% static 'taskdatabase/surfsara.jpg' %}" height="20" alt="inspection plots"> </a> -- GitLab