From e9f41332e82cf5c20980b3b63da07c8e88f7baaf Mon Sep 17 00:00:00 2001
From: Nico Vermaas <vermaas@astron.nl>
Date: Mon, 7 Feb 2022 14:32:42 +0100
Subject: [PATCH] apply filter to dashboard add 'clear filter' button

---
 atdb/taskdatabase/services/algorithms.py      | 27 ++++++++++++-------
 .../templates/dashboard/toggles.html          |  2 +-
 atdb/taskdatabase/templates/query/index.html  |  4 +--
 .../filter/clear_filter_button.html           |  1 +
 .../taskdatabase/filter/filter_buttons.html   |  2 +-
 .../filter/{filter.html => filter_xxx.html}   |  0
 .../templates/taskdatabase/index.html         |  2 +-
 atdb/taskdatabase/urls.py                     |  2 ++
 atdb/taskdatabase/views.py                    | 14 +++++++++-
 9 files changed, 39 insertions(+), 15 deletions(-)
 create mode 100644 atdb/taskdatabase/templates/taskdatabase/filter/clear_filter_button.html
 rename atdb/taskdatabase/templates/taskdatabase/filter/{filter.html => filter_xxx.html} (100%)

diff --git a/atdb/taskdatabase/services/algorithms.py b/atdb/taskdatabase/services/algorithms.py
index 45d14adc..0489a857 100644
--- a/atdb/taskdatabase/services/algorithms.py
+++ b/atdb/taskdatabase/services/algorithms.py
@@ -274,13 +274,13 @@ def convert_monitor_to_html(request, monitor_data):
 
 
 # aggregate information from the tasks table per workflow per status
-def aggregate_resources_tasks(selection):
+def aggregate_resources_tasks(request, selection, filtered_tasks):
     workflow_results = []
     my_workflows = []
 
     # get all active tasks
     if 'active' in selection:
-        active_tasks = Task.objects.filter(status__in=settings.ACTIVE_STATUSSES).filter(task_type='regular')
+        active_tasks = filtered_tasks.filter(status__in=settings.ACTIVE_STATUSSES).filter(task_type='regular')
 
         # retrieve all unique workflows from the active tasks
         active_workflows = active_tasks.values('workflow').distinct()
@@ -303,7 +303,7 @@ def aggregate_resources_tasks(selection):
         # get the numbers for this workflow
 
         # all tasks for this workflow for the 'grand total'
-        tasks_per_workflow = Task.objects.filter(workflow=workflow).filter(task_type='regular')
+        tasks_per_workflow = filtered_tasks.filter(workflow=workflow).filter(task_type='regular')
         nr_of_tasks_per_workflow = tasks_per_workflow.count()
 
         sum_size_to_process = tasks_per_workflow.aggregate(Sum('size_to_process'))
@@ -471,24 +471,27 @@ def highlight_value(values, value_to_highlight):
 
 
 def construct_tasks_per_workflow_html(request, workflow_results):
+
+    try:
+        query_list_of_ids = request.session['query_list_of_ids']
+        current_query_params = request.session['current_query_params']
+    except:
+        pass
+
+
     # --- Progress of tasks per active workflow ---
     results_tasks = "<p>Progress of tasks per workflow</p>"
 
     # construct the header
-    ###header = ""
     header = '<th class="aggregate_failed">failed</th><th class="aggregate">active</th><th class="aggregate">total</th>'
     for status in settings.ALL_STATUSSES:
         header += "<th>" + status + "</th>"
 
-    ###header += '<th class="failed">failed</th><th class="active">active</th><th>total</th>'
-
     results_tasks += header
 
     for workflow_result in workflow_results:
 
         link = construct_link_to_workflow_api(request, workflow_result)
-
-        # values = "<tr><td colspan='5'><b>" + link + "</b></td></tr><tr>"
         values = "<tr class='info'><td colspan='6'><b>" + link + "</b></td>"
 
         # add sizes
@@ -615,8 +618,14 @@ def construct_logs_per_workflow_html(request, workflow_results):
 def construct_dashboard_html(request, selection):
     # gather and construct the dashboard based on the requested selection
 
+    try:
+        filtered_tasks_as_list = request.session['filtered_tasks_as_list']
+        filtered_tasks = Task.objects.filter(id__in=filtered_tasks_as_list)
+    except:
+        filtered_tasks = Task.objects.all()
+
     # --- Progress of tasks per active workflow ---
-    workflow_results = aggregate_resources_tasks(selection)
+    workflow_results = aggregate_resources_tasks(request, selection, filtered_tasks)
     results_tasks = construct_tasks_per_workflow_html(request, workflow_results)
 
     # --- logentries ---
diff --git a/atdb/taskdatabase/templates/dashboard/toggles.html b/atdb/taskdatabase/templates/dashboard/toggles.html
index 5b2a7850..1cdf43c1 100644
--- a/atdb/taskdatabase/templates/dashboard/toggles.html
+++ b/atdb/taskdatabase/templates/dashboard/toggles.html
@@ -9,7 +9,7 @@
            data-onstyle="warning"
            data-offstyle="primary"
            data-style
-           {% if not 'active' in selection %} checked {% endif %}
+           {% if not 'active' in toggle_selection %} checked {% endif %}
     >&nbsp;
 
     <input type="checkbox"
diff --git a/atdb/taskdatabase/templates/query/index.html b/atdb/taskdatabase/templates/query/index.html
index 8bff93a4..a37ce609 100644
--- a/atdb/taskdatabase/templates/query/index.html
+++ b/atdb/taskdatabase/templates/query/index.html
@@ -15,8 +15,8 @@
         <form action="" method="get" class="form form-inline">
             {% bootstrap_form filter.form layout='inline' %}
             &nbsp;<button type="submit" class="btn-success  btn-sm" title="Filter"><i class="fas fa-filter"></i> Filter</button>
-
-        </form>
+            {% include "taskdatabase/filter/clear_filter_button.html" %}
+         </form>
     </div>
     {% endif %}
 
diff --git a/atdb/taskdatabase/templates/taskdatabase/filter/clear_filter_button.html b/atdb/taskdatabase/templates/taskdatabase/filter/clear_filter_button.html
new file mode 100644
index 00000000..3f103a32
--- /dev/null
+++ b/atdb/taskdatabase/templates/taskdatabase/filter/clear_filter_button.html
@@ -0,0 +1 @@
+<a href="{% url 'clear-filter' %}" class="btn btn-warning btn-sm" role="button"><i class="fas fa-window-close"></i> Clear Filter</a>
diff --git a/atdb/taskdatabase/templates/taskdatabase/filter/filter_buttons.html b/atdb/taskdatabase/templates/taskdatabase/filter/filter_buttons.html
index 8bb3b20f..cd5dcca5 100644
--- a/atdb/taskdatabase/templates/taskdatabase/filter/filter_buttons.html
+++ b/atdb/taskdatabase/templates/taskdatabase/filter/filter_buttons.html
@@ -6,7 +6,7 @@
              <tr><td>Click to Filter</td></tr>
              <tr>
                <td>
-                <a href="{% url 'task-set-filter' 'all' %}" class="btn btn-success btn-sm" role="button"><i class="fas fa-layer-group"></i> All</a>
+                   {% include "taskdatabase/filter/clear_filter_button.html" %}
                 <a href="{% url 'task-set-active-filter' %}" class="btn btn-success btn-sm" role="button"><i class="fas fa-layer-group"></i> Active</a>
                 <a href="{% url 'task-set-filter' 'failed' %}" class="btn btn-danger btn-sm" role="button"><i class="fas fa-layer-group"></i> Failed</a>
                  <a href="{% url 'task-set-onhold-filter' True %}" class="btn btn-warning btn-sm" role="button"><i class="fas fa-layer-group"></i> On Hold</a>
diff --git a/atdb/taskdatabase/templates/taskdatabase/filter/filter.html b/atdb/taskdatabase/templates/taskdatabase/filter/filter_xxx.html
similarity index 100%
rename from atdb/taskdatabase/templates/taskdatabase/filter/filter.html
rename to atdb/taskdatabase/templates/taskdatabase/filter/filter_xxx.html
diff --git a/atdb/taskdatabase/templates/taskdatabase/index.html b/atdb/taskdatabase/templates/taskdatabase/index.html
index 592574f4..1abdb6fe 100644
--- a/atdb/taskdatabase/templates/taskdatabase/index.html
+++ b/atdb/taskdatabase/templates/taskdatabase/index.html
@@ -34,7 +34,7 @@
         {% include 'taskdatabase/pagination.html' %}
        </div>
     </div>
-    <p class="footer"> Version 1.0.0 (4 feb 2021 - 16:00)
+    <p class="footer"> Version 1.0.0 (7 feb 2021 - 14:00)
 
 </div>
 
diff --git a/atdb/taskdatabase/urls.py b/atdb/taskdatabase/urls.py
index bddabc88..47ab03e1 100644
--- a/atdb/taskdatabase/urls.py
+++ b/atdb/taskdatabase/urls.py
@@ -83,6 +83,8 @@ urlpatterns = [
     path('tasks/set_filter/<filter>', views.TaskSetFilter, name='task-set-filter'),
     path('tasks/set_active_filter', views.TaskSetActiveFilter, name='task-set-active-filter'),
     path('tasks/task-set-onhold-filter/<onhold>', views.TaskSetOnHoldFilter, name='task-set-onhold-filter'),
+    path('tasks/clear_filter/', views.TaskClearFilter, name='clear-filter'),
+
     path('tasks/<int:pk>/set_status/<new_status>/<query_params>', views.TaskSetStatusTables2, name = 'task-setstatus'),
     path('tasks/set_status_multi/<new_status>/<query_params>', views.TaskMultiStatus, name='task-multi-setstatus'),
     path('tasks/set_multi_hold/<onhold>/<query_params>', views.TaskMultiHold, name='task-multi-hold'),
diff --git a/atdb/taskdatabase/views.py b/atdb/taskdatabase/views.py
index 191b504e..203c6361 100644
--- a/atdb/taskdatabase/views.py
+++ b/atdb/taskdatabase/views.py
@@ -172,7 +172,12 @@ class QueryView(SingleTableMixin, FilterView):
 
         query_list_of_ids = list(self.object_list.values_list('id'))[:limit]
 
+        filtered_tasks_as_list = []
+        for id in query_list_of_ids:
+            filtered_tasks_as_list.append(id[0])
+
         # store on the session
+        self.request.session['filtered_tasks_as_list'] = filtered_tasks_as_list
         self.request.session['query_list_of_ids'] = query_list_of_ids
         return self.object_list
 
@@ -483,7 +488,7 @@ def ShowDashboard(request, selection):
     return render(request, "dashboard/dashboard.html",
                   {'results_tasks': results_tasks,
                    'results_logs': results_logs,
-                   'selection': selection})
+                   'toggle_selection': selection})
 
 
 def WorkflowDetails(request, id):
@@ -803,6 +808,13 @@ def TaskSetOnHoldFilter(request, onhold):
     request.session['task_onhold_filter'] = onhold
     return redirect('/atdb/?page=1')
 
+def TaskClearFilter(request):
+    request.session['task_filter'] = 'all'
+    request.session['task_onhold_filter'] = None
+    request.session['filtered_tasks_as_list'] = None
+    request.session['query_list_of_ids'] = None
+    request.session['search_box'] = ''
+    return redirect('/atdb/?page=1')
 
 @login_required
 def ChangePriority(request, pk, priority_change, page=0):
-- 
GitLab