Skip to content

Commit

Permalink
WIP: Demo report
Browse files Browse the repository at this point in the history
  • Loading branch information
ross-spencer committed Oct 1, 2020
1 parent 591a773 commit 51dd51c
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 1 deletion.
9 changes: 9 additions & 0 deletions AIPscan/API/namespace_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,12 @@ def get(self, storage_service_id):
"""AIP overview two"""
aip_data = data.derivative_overview(storage_service_id=storage_service_id)
return aip_data


@api.route("/agent-data/<storage_service_id>")
class AgentData(Resource):
@api.doc("agent info")
def get(self, storage_service_id):
"""Filter and return information about agents"""
aip_data = data.agent_event_report(storage_service_id=storage_service_id)
return aip_data
49 changes: 48 additions & 1 deletion AIPscan/Data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

from datetime import datetime

from AIPscan.models import aips as aip_model, originals, copies, storage_services
from AIPscan.models import (
aips as aip_model,
originals,
copies,
events,
storage_services,
)


FIELD_AIP_NAME = "AipName"
Expand Down Expand Up @@ -201,3 +207,44 @@ def derivative_overview(storage_service_id):
report[FIELD_ALL_AIPS] = all_aips
report[FIELD_STORAGE_NAME] = storage_service.name
return report


def agent_event_report(storage_service_id):
"""Return information about agents based on event or events supplied
to the function.
"""
report = []
storage_service = _get_storage_service(storage_service_id)
aips = aip_model.query.filter_by(storage_service_id=storage_service.id).all()

EVENT_TYPE = "ingestion"
AGENT_TYPE = "Archivematica user"

AIP = "aip"
EVENT = "event"
INGEST_DATE = "ingest_date"
FINISH_DATE = "finish_date"

USERNAME = "username="
USER = "user"

for aip in aips:
files = originals.query.filter_by(aip_id=aip.id)
for file in files[:1]:
event = events.query.filter_by(original_id=file.id, type=EVENT_TYPE).all()
for e in event:
log_line = {}
log_line[AIP] = aip.uuid
log_line[EVENT] = EVENT_TYPE
log_line[INGEST_DATE] = "{}".format(e.date).rsplit(":", 1)[0]
log_line[FINISH_DATE] = "{}".format(aip.create_date).rsplit(":", 1)[0]
for f in e.event_agents:
if f.agent_type == AGENT_TYPE:
user = (
f.agent_value.split(",", 1)[0]
.replace(USERNAME, "")
.replace('"', "")
)
log_line[USER] = user
report.append(log_line)
return report
43 changes: 43 additions & 0 deletions AIPscan/Reporter/report_ingest_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-

"""Report on who ingested AIPs and when and an indication about how
long they took.
"""

from flask import render_template, request
import pandas as pd
import plotly.express as px


from AIPscan.Data import data
from AIPscan.Reporter import reporter


@reporter.route("/ingest_log/", methods=["GET"])
def ingest_log():
"""Return the information needed to present an ingest gantt chart.
"""
storage_service_id = request.args.get("amss_id")
ingests = data.agent_event_report(storage_service_id)
pd_list = []
for ingest in ingests:
try:
pd_list.append(
dict(
User=ingest["user"],
Start=ingest["ingest_date"],
Finish=ingest["finish_date"],
)
)
except KeyError:
pass
df = pd.DataFrame(pd_list)
fig = px.timeline(df, x_start="Start", x_end="Finish", y="User")
fig.update_yaxes(autorange="reversed")

return render_template(
"report_ingest_log.html",
storage_service_name="Demo",
number_of_transfers="12",
plot=fig.to_html(full_html=False),
)
29 changes: 29 additions & 0 deletions AIPscan/Reporter/templates/report_ingest_log.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ storage_service_name }}: Ingest log</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/custom.css') }}">
<script type="text/javascript" language="javascript" src="{{ url_for('static', filename='js/plotly-latest.min.js') }}"></script>

</head>
<body>

<div class="container-fluid" style="margin: 20px 0;">

<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>
<strong>{{ storage_service_name }}: Ingest log</strong><br />
{{ number_of_transfers }} transfers total<br />
</div>

<!-- Add plotly.express here -->

{{ plot|safe }}

</div>

</div>
</body>
</html>
19 changes: 19 additions & 0 deletions AIPscan/Reporter/templates/reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
<td><a href="#"><button type="button" id="report4a" class="btn btn-info" style="margin-left:5px; margin-bottom:5px;">Tabular</button></a></td>
</tr>

<tr>
<td>Transfers log</td>
<td></td>
<td></td>
<td><a href="#"><button type="button" id="report5a" class="btn btn-info" style="margin-left:5px; margin-bottom:5px;">Gantt Chart</button></a></td>
</tr>

</table>

</div>
Expand Down Expand Up @@ -192,6 +199,18 @@
);
window.open(url);
});
$("#report5a").on("click", function() {
bootstrap_alert('This is a demo of report three!', WARNINGS.ALERT_SECONDARY);
const URL_AIP_CONTENTS = "/reporter/ingest_log/";
var storageServiceID = $('#ss').val();
var url = (
window.location.origin +
URL_AIP_CONTENTS +
'?amss_id=' +
storageServiceID
);
window.open(url);
});
});
</script>

Expand Down
1 change: 1 addition & 0 deletions AIPscan/Reporter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from AIPscan.Reporter import ( # noqa: F401
report_aip_contents,
report_formats_count,
report_ingest_log,
report_originals_with_derivatives,
)

Expand Down
1 change: 1 addition & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ kombu==4.6.10
lxml==4.5.0
MarkupSafe==1.1.1
metsrw==0.3.15
plotly-express==0.4.1
pytz==2020.1
requests==2.23.0
six==1.14.0
Expand Down

0 comments on commit 51dd51c

Please sign in to comment.