Skip to content

Commit dd1c6d6

Browse files
committed
Refactor the Scan status system for dynamic UI
Signed-off-by: tdruez <[email protected]>
1 parent 015fc0c commit dd1c6d6

File tree

5 files changed

+79
-20
lines changed

5 files changed

+79
-20
lines changed

component_catalog/templates/component_catalog/includes/scan_actions_list.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55
<i class="far fa-file-alt"></i>{% if show_btn_label %} View Scan results{% endif %}
66
</a>
77
</li>
8+
{% else %}
9+
<li class="list-inline-item" style="width:36px;"></li>
810
{% endif %}
911
{% if scan.download_result_url %}
1012
<li class="list-inline-item"{% if not show_btn_label %} data-bs-toggle="tooltip"{% endif %} data-bs-title="Download Scan results">
1113
<a class="btn btn-outline-dark btn-sm" href="{{ scan.download_result_url }}" target="_blank">
1214
<i class="fas fa-download"></i>{% if show_btn_label %} Download Scan results{% endif %}
1315
</a>
1416
</li>
17+
{% else %}
18+
<li class="list-inline-item" style="width:36px;"></li>
1519
{% endif %}
1620
{% if scan.delete_url %}
1721
<li class="list-inline-item"{% if not show_btn_label %} data-bs-toggle="tooltip"{% endif %} data-bs-title="Delete Scan">
1822
<a class="btn btn-outline-danger btn-sm scan_delete_link" href="#scan-delete-modal" role="button" data-delete-url="{{ scan.delete_url }}" data-bs-toggle="modal">
1923
<i class="far fa-trash-alt"></i>{% if show_btn_label %} Delete Scan{% endif %}
2024
</a>
2125
</li>
26+
{% else %}
27+
<li class="list-inline-item" style="width:36px;"></li>
2228
{% endif %}
2329
{% if scan.refresh_url %}
2430
<li class="list-inline-item"{% if not show_btn_label %} data-bs-toggle="tooltip"{% endif %} data-bs-title="Refresh Scan">
2531
<a class="btn btn-outline-dark btn-sm scan_refresh_link" href="#scan-refresh-modal" role="button" data-refresh-url="{{ scan.refresh_url }}" data-bs-toggle="modal">
2632
<i class="fa-solid fa-rotate-right"></i>{% if show_btn_label %} Refresh Scan{% endif %}
2733
</a>
2834
</li>
35+
{% else %}
36+
<li class="list-inline-item" style="width:36px;"></li>
2937
{% endif %}
3038
</ul>
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
<strong class="ms-1">{{ scan.runs.0.status|title }}</strong>
2-
{% include 'component_catalog/includes/scan_status.html' with status=scan.runs.0.status only %}
1+
<strong class="ms-1">{{ scan.status_for_display }}</strong>
2+
{% include 'component_catalog/includes/scan_status.html' with status=scan.status only %}
33
<div class="text-center mt-2">
4-
{% if scan.runs.0.status != "running" %}
5-
{% include 'component_catalog/includes/scan_actions_list.html' with scan=scan only %}
6-
{% endif %}
4+
{% include 'component_catalog/includes/scan_actions_list.html' with scan=scan only %}
75
</div>

component_catalog/templates/component_catalog/includes/scan_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="progress" style="width: 100%; margin-bottom: 0; height: .5rem;">
1+
<div class="progress" style="width: 100%; min-width:190px ;margin-bottom: 0; height: .5rem;">
22
{% if status == 'success' %}
33
<div class="progress-bar {% if has_errors %}bg-warning{% else %}bg-success{% endif %}" title="{{ status|title }}" role="progressbar" style="width: 100%" aria-label="Scan progress" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></div>
44
{% elif status == 'failure' or status == "stopped" or status == "stale" %}

component_catalog/views.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from dejacode_toolkit.download import DataCollectionException
7474
from dejacode_toolkit.purldb import PurlDB
7575
from dejacode_toolkit.scancodeio import ScanCodeIO
76+
from dejacode_toolkit.scancodeio import ScanStatus
7677
from dejacode_toolkit.scancodeio import get_package_download_url
7778
from dejacode_toolkit.scancodeio import get_scan_results_as_file_url
7879
from dje import tasks
@@ -1689,6 +1690,32 @@ def refresh_scan_view(request, project_uuid):
16891690
return redirect("component_catalog:scan_list")
16901691

16911692

1693+
def get_scan_context_data(scan, package=None):
1694+
scan_uuid = scan.get("uuid")
1695+
1696+
status = ""
1697+
if scan_run := scan.get("runs"):
1698+
status = scan_run[-1].get("status")
1699+
1700+
context_data = {
1701+
"status": status,
1702+
"status_for_display": status.replace("_", " ").capitalize(),
1703+
"created_date": parse_datetime(scan.get("created_date")),
1704+
"package": package,
1705+
}
1706+
1707+
scan_status = ScanStatus(status)
1708+
if package and scan_status.is_completed:
1709+
context_data["view_url"] = f"{package.details_url}#scan" if package else None
1710+
if scan_status.is_completed:
1711+
context_data["download_result_url"] = get_scan_results_as_file_url(scan)
1712+
if not scan_status.is_in_progress:
1713+
context_data["delete_url"] = reverse("component_catalog:scan_delete", args=[scan_uuid])
1714+
context_data["refresh_url"] = reverse("component_catalog:scan_refresh", args=[scan_uuid])
1715+
1716+
return context_data
1717+
1718+
16921719
class ScanListView(
16931720
LoginRequiredMixin,
16941721
AddPackagePermissionMixin,
@@ -1738,23 +1765,12 @@ def get_context_data(self, **kwargs):
17381765

17391766
scans = []
17401767
for scan in context_data["object_list"]:
1741-
scan_uuid = scan.get("uuid")
17421768
package_download_url = get_package_download_url(scan)
17431769
package = packages_by_url.get(package_download_url)
1744-
scan["package"] = package
1745-
scan["created_date"] = parse_datetime(scan.get("created_date"))
1746-
scan["view_url"] = f"{package.details_url}#scan" if package else None
1747-
scan["download_result_url"] = get_scan_results_as_file_url(scan)
1748-
scan["delete_url"] = reverse("component_catalog:scan_delete", args=[scan_uuid])
1749-
scan["refresh_url"] = reverse("component_catalog:scan_refresh", args=[scan_uuid])
1750-
scans.append(scan)
1751-
1752-
context_data.update(
1753-
{
1754-
"scans": scans,
1755-
}
1756-
)
1770+
scan_context_data = get_scan_context_data(scan, package)
1771+
scans.append(scan_context_data)
17571772

1773+
context_data["scans"] = scans
17581774
return context_data
17591775

17601776

dejacode_toolkit/scancodeio.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,43 @@ def map_detected_package_data(cls, detected_package):
449449
return package_data_for_model
450450

451451

452+
class ScanStatus:
453+
"""List of ScanCode.io Run status."""
454+
455+
NOT_STARTED = "not_started"
456+
QUEUED = "queued"
457+
RUNNING = "running"
458+
SUCCESS = "success"
459+
FAILURE = "failure"
460+
STOPPED = "stopped"
461+
STALE = "stale"
462+
463+
# Status groupings
464+
IN_PROGRESS = [QUEUED, RUNNING]
465+
COMPLETED = [SUCCESS, FAILURE, STOPPED, STALE]
466+
ISSUES = [FAILURE, STOPPED, STALE]
467+
PENDING = [NOT_STARTED]
468+
469+
def __init__(self, status):
470+
self.value = status
471+
472+
@property
473+
def is_in_progress(self):
474+
return self.value in self.IN_PROGRESS
475+
476+
@property
477+
def is_completed(self):
478+
return self.value in self.COMPLETED
479+
480+
@property
481+
def has_issues(self):
482+
return self.value in self.ISSUES
483+
484+
@property
485+
def is_pending(self):
486+
return self.value in self.PENDING
487+
488+
452489
def get_hash_uid(value):
453490
"""Return a Unique ID based on a 10 characters hash of the provided `value`."""
454491
return md5(str(value).encode("utf-8"), usedforsecurity=False).hexdigest()[:10]

0 commit comments

Comments
 (0)