Skip to content

Commit

Permalink
Add file type parameter
Browse files Browse the repository at this point in the history
This commit adds a file_type parameter to the new Data and API
endpoints and explicitly adds original and preservation versions of
each new report to the report selection UI.
  • Loading branch information
tw4l committed Nov 17, 2020
1 parent b3e6410 commit 778e924
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 55 deletions.
41 changes: 29 additions & 12 deletions AIPscan/API/namespace_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
from flask_restx import Namespace, Resource
from AIPscan.Data import data


FILE_FORMAT_FIELD = "file_format"
FILE_TYPE_FIELD = "file_type"
PUID_FIELD = "puid"

api = Namespace("data", description="Retrieve data from AIPscan to shape as you desire")

"""
Expand Down Expand Up @@ -91,7 +96,7 @@ class LargestFileList(Resource):
@api.doc(
"list_formats",
params={
"file_type": {
FILE_TYPE_FIELD: {
"description": "Optional file type filter (original or preservation)",
"in": "query",
"type": "str",
Expand All @@ -105,7 +110,7 @@ class LargestFileList(Resource):
)
def get(self, storage_service_id, file_type=None, limit=20):
"""Largest files"""
file_type = request.args.get("file_type", None)
file_type = request.args.get(FILE_TYPE_FIELD)
try:
limit = int(request.args.get("limit", 20))
except ValueError:
Expand All @@ -118,43 +123,55 @@ def get(self, storage_service_id, file_type=None, limit=20):

@api.route("/storage_service/<storage_service_id>/file-format")
class AIPsByFormatList(Resource):
FILE_FORMAT_FIELD = "file_format"

@api.doc(
"list_aips_by_format",
params={
FILE_FORMAT_FIELD: {
"description": "File format name (must be exact match)",
"in": "query",
"type": "str",
}
},
FILE_TYPE_FIELD: {
"description": "Optional file type filter (original or preservation)",
"in": "query",
"type": "str",
},
},
)
def get(self, storage_service_id):
"""AIPs containing file format"""
file_format = request.args.get(self.FILE_FORMAT_FIELD, "")
file_format = request.args.get(FILE_FORMAT_FIELD, "")
file_type = request.args.get(FILE_TYPE_FIELD)
aip_data = data.aips_by_file_format(
storage_service_id=storage_service_id, file_format=file_format
storage_service_id=storage_service_id,
file_format=file_format,
file_type=file_type,
)
return aip_data


@api.route("/storage_service/<storage_service_id>/puid")
class AIPsByPUIDList(Resource):
PUID_FIELD = "puid"

@api.doc(
"list_aips_by_puid",
params={
PUID_FIELD: {
"description": "PRONOM ID (PUID)",
"in": "query",
"type": "str",
}
},
FILE_TYPE_FIELD: {
"description": "Optional file type filter (original or preservation)",
"in": "query",
"type": "str",
},
},
)
def get(self, storage_service_id):
"""AIPs containing PUID"""
puid = request.args.get(self.PUID_FIELD, "")
aip_data = data.aips_by_puid(storage_service_id=storage_service_id, puid=puid)
puid = request.args.get(PUID_FIELD, "")
file_type = request.args.get(FILE_TYPE_FIELD)
aip_data = data.aips_by_puid(
storage_service_id=storage_service_id, puid=puid, file_type=file_type
)
return aip_data
38 changes: 28 additions & 10 deletions AIPscan/Data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from AIPscan import db
from AIPscan.models import AIP, File, FileType, StorageService

VALID_FILE_TYPES = set(item.value for item in FileType)

FIELD_AIP = "AIP"
FIELD_AIP_ID = "AIPID"
Expand Down Expand Up @@ -228,7 +229,6 @@ def _largest_files_query(storage_service_id, file_type, limit):
This is separated into its own helper function to aid in testing.
"""
VALID_FILE_TYPES = set(item.value for item in FileType)
if file_type is not None and file_type in VALID_FILE_TYPES:
files = (
File.query.join(AIP)
Expand Down Expand Up @@ -302,14 +302,16 @@ def largest_files(storage_service_id, file_type=None, limit=20):


def _query_aips_by_file_format_or_puid(
storage_service_id, search_string, file_format=True
storage_service_id, search_string, file_format=True, file_type=None
):
"""Fetch information on all AIPs with given format or PUID from db.
:param storage_service_id: Storage Service ID (int)
:param search_string: File format or PUID (str)
:param file_format: Boolean indicating whether to filter on file
format or PUID.
format (default) or PUID
:param file_type: Optional filter for type of file to return (str,
used only if value matches FileType enum options)
:returns: SQLAlchemy query results
"""
Expand All @@ -327,18 +329,23 @@ def _query_aips_by_file_format_or_puid(
.group_by(AIP.id)
.order_by(db.func.count(File.id).desc(), db.func.sum(File.size).desc())
)
if file_type is not None and file_type in VALID_FILE_TYPES:
aips = aips.filter(File.file_type == file_type)
if file_format:
return aips.filter(File.file_format == search_string)
return aips.filter(File.puid == search_string)


def _aips_by_file_format_or_puid(storage_service_id, search_string, file_format=True):
def _aips_by_file_format_or_puid(
storage_service_id, search_string, file_type, file_format=True
):
"""Return overview of all AIPs containing original files in format
:param storage_service_id: Storage Service ID (int)
:param search_string: File format name or PUID (str)
:param file_type: Optional filter for type of file to return (str)
:param file_format: Boolean indicating whether to filter on file
format or PUID.
format or PUID (defaults to True)
:returns: "report" dict containing following fields:
report["StorageName"]: Name of Storage Service queried
Expand All @@ -360,7 +367,7 @@ def _aips_by_file_format_or_puid(storage_service_id, search_string, file_format=

report[FIELD_AIPS] = []
results = _query_aips_by_file_format_or_puid(
storage_service_id, search_string, file_format
storage_service_id, search_string, file_format, file_type
)
for result in results:
aip_info = {}
Expand All @@ -376,23 +383,34 @@ def _aips_by_file_format_or_puid(storage_service_id, search_string, file_format=
return report


def aips_by_file_format(storage_service_id, file_format):
def aips_by_file_format(storage_service_id, file_format, file_type=None):
"""Return overview of AIPs containing original files in format.
:param storage_service_id: Storage Service ID (int)
:param file_format: File format name (str)
:param file_type: Optional filter for type of file to return (str)
:returns: Report dict provided by _aips_by_file_format_or_puid
"""
return _aips_by_file_format_or_puid(storage_service_id, file_format)
return _aips_by_file_format_or_puid(
storage_service_id=storage_service_id,
search_string=file_format,
file_type=file_type,
)


def aips_by_puid(storage_service_id, puid):
def aips_by_puid(storage_service_id, puid, file_type=None):
"""Return overview of AIPs containing original files in format.
:param storage_service_id: Storage Service ID (int)
:param puid: PUID (str)
:param file_type: Optional filter for type of file to return (str)
:returns: Report dict provided by _aips_by_file_format_or_puid
"""
return _aips_by_file_format_or_puid(storage_service_id, puid, False)
return _aips_by_file_format_or_puid(
storage_service_id=storage_service_id,
search_string=puid,
file_type=file_type,
file_format=False,
)
9 changes: 7 additions & 2 deletions AIPscan/Reporter/report_aips_by_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flask import render_template, request

from AIPscan.models import FileType
from AIPscan.Data import data
from AIPscan.Reporter import reporter, translate_headers, request_params

Expand All @@ -11,15 +12,19 @@ def aips_by_format():
"""Return AIPs containing file format, sorted by count and total size."""
storage_service_id = request.args.get(request_params["storage_service_id"])
file_format = request.args.get(request_params["file_format"])
file_type = request.args.get(request_params["file_type"], FileType.original.value)
aip_data = data.aips_by_file_format(
storage_service_id=storage_service_id, file_format=file_format
storage_service_id=storage_service_id,
file_format=file_format,
file_type=file_type,
)
headers = [data.FIELD_AIP_NAME, data.FIELD_UUID, data.FIELD_COUNT, data.FIELD_SIZE]
return render_template(
"report_aips_by_format.html",
storage_service_id=storage_service_id,
storage_service_name=aip_data.get(data.FIELD_STORAGE_NAME),
file_format=file_format,
file_type=file_type,
columns=translate_headers(headers),
aips=aip_data[data.FIELD_AIPS],
aips=aip_data.get(data.FIELD_AIPS),
)
10 changes: 7 additions & 3 deletions AIPscan/Reporter/report_aips_by_puid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from flask import render_template, request

from AIPscan.models import File, FileType
from AIPscan.Data import data
from AIPscan.Reporter import reporter, translate_headers, request_params
from AIPscan.models import File


def get_format_string_from_puid(puid):
Expand Down Expand Up @@ -35,14 +35,18 @@ def aips_by_puid():
"""Return AIPs containing PUID, sorted by count and total size."""
storage_service_id = request.args.get(request_params["storage_service_id"])
puid = request.args.get(request_params["puid"])
aip_data = data.aips_by_puid(storage_service_id=storage_service_id, puid=puid)
file_type = request.args.get(request_params["file_type"], FileType.original.value)
aip_data = data.aips_by_puid(
storage_service_id=storage_service_id, puid=puid, file_type=file_type
)
headers = [data.FIELD_AIP_NAME, data.FIELD_UUID, data.FIELD_COUNT, data.FIELD_SIZE]
return render_template(
"report_aips_by_puid.html",
storage_service_id=storage_service_id,
storage_service_name=aip_data.get(data.FIELD_STORAGE_NAME),
puid=puid,
file_format=get_format_string_from_puid(puid),
file_type=file_type,
columns=translate_headers(headers),
aips=aip_data[data.FIELD_AIPS],
aips=aip_data.get(data.FIELD_AIPS),
)
4 changes: 3 additions & 1 deletion AIPscan/Reporter/templates/report_aips_by_format.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}

<div class="alert alert-secondary" style="height: 100px;">
<div class="alert alert-secondary">
<a class="noprint" onClick="window.print();return false">
<button type="button" class="btn btn-info" style="float:right;">Print</button>
</a>
Expand All @@ -11,6 +11,8 @@
<strong>Storage Service:</strong> {{ storage_service_name }}</strong>
<br>
<strong>File format:</strong> {{ file_format }}
<br>
<strong>File type:</strong> {{ file_type }} files
</div>

{% if aips %}
Expand Down
6 changes: 4 additions & 2 deletions AIPscan/Reporter/templates/report_aips_by_puid.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}

<div class="alert alert-secondary" style="height: 125px;">
<div class="alert alert-secondary">
<a class="noprint" onClick="window.print();return false">
<button type="button" class="btn btn-info" style="float:right;">Print</button>
</a>
Expand All @@ -13,8 +13,10 @@
<strong>PUID:</strong> {{ puid }}
{% if file_format %}
<br>
<strong>File format/version:</strong> {{ file_format }}
<strong>File format and version:</strong> {{ file_format }}
{% endif %}
<br>
<strong>File type:</strong> {{ file_type }} files
</div>

{% if aips %}
Expand Down
Loading

0 comments on commit 778e924

Please sign in to comment.