aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2019-09-04 12:04:08 -0700
committerBryan Newbold <bnewbold@robocracy.org>2019-09-04 12:04:08 -0700
commitf4da02fae5ebd179a0b3af4ff179543813fa146b (patch)
tree3f14fba54e6279a1bc042831ad4b7e273dc71f15 /python
parentb0223b6e10706554e55e5d90f36d1946d285c3e2 (diff)
downloadfatcat-f4da02fae5ebd179a0b3af4ff179543813fa146b.tar.gz
fatcat-f4da02fae5ebd179a0b3af4ff179543813fa146b.zip
start container coverage page
Diffstat (limited to 'python')
-rw-r--r--python/fatcat_web/routes.py19
-rw-r--r--python/fatcat_web/search.py68
-rw-r--r--python/fatcat_web/templates/container_view_coverage.html15
-rw-r--r--python/fatcat_web/templates/entity_base.html4
4 files changed, 104 insertions, 2 deletions
diff --git a/python/fatcat_web/routes.py b/python/fatcat_web/routes.py
index f313fce0..3f5af621 100644
--- a/python/fatcat_web/routes.py
+++ b/python/fatcat_web/routes.py
@@ -230,6 +230,10 @@ def generic_editgroup_entity_view(editgroup_id, entity_type, ident, view_templat
def container_view(ident):
return generic_entity_view('container', ident, 'container_view.html')
+@app.route('/container/<ident>/coverage', methods=['GET'])
+def container_view_coverage(ident):
+ return generic_entity_view('container', ident, 'container_view_coverage.html')
+
@app.route('/container/<ident>/metadata', methods=['GET'])
def container_view_metadata(ident):
return generic_entity_view('container', ident, 'entity_view_metadata.html')
@@ -718,6 +722,21 @@ def container_ident_stats(ident):
abort(503)
return jsonify(stats)
+@app.route('/container/<ident>/ia_coverage_years.json', methods=['GET', 'OPTIONS'])
+@crossdomain(origin='*',headers=['access-control-allow-origin','Content-Type'])
+def container_ident_ia_coverage_years_json(ident):
+ try:
+ container = api.get_container(ident)
+ except ApiException as ae:
+ abort(ae.status)
+ try:
+ histogram = get_elastic_container_histogram(container.ident)
+ except Exception as ae:
+ app.log.error(ae)
+ abort(503)
+ histogram = [dict(year=h[0], in_ia=h[1], count=h[2]) for h in histogram]
+ return jsonify({'container_id': ident, "histogram": histogram})
+
@app.route('/release/<ident>.bib', methods=['GET'])
def release_bibtex(ident):
try:
diff --git a/python/fatcat_web/search.py b/python/fatcat_web/search.py
index 94246329..523269ce 100644
--- a/python/fatcat_web/search.py
+++ b/python/fatcat_web/search.py
@@ -274,7 +274,7 @@ def get_elastic_container_random_releases(ident, limit=5):
#print(resp.json())
resp.raise_for_status()
resp = resp.json()
- print(resp)
+ #print(resp)
hits = [h['_source'] for h in resp['hits']['hits']]
for h in hits:
# Handle surrogate strings that elasticsearch returns sometimes,
@@ -285,3 +285,69 @@ def get_elastic_container_random_releases(ident, limit=5):
h[key] = h[key].encode('utf8', 'ignore').decode('utf8')
return hits
+
+def get_elastic_container_histogram(ident):
+ """
+ Fetches a stacked histogram of
+
+ Filters to the past 500 years (at most), or about 1000 vaules.
+
+ Returns a list of tuples:
+ (year, in_ia, count)
+ """
+
+ query = {
+ "aggs": {
+ "year_in_ia": {
+ "composite": {
+ "size": 1000,
+ "sources": [
+ {"year": {
+ "histogram": {
+ "field": "release_year",
+ "interval": 1,
+ }}},
+ {"in_ia": {
+ "terms": {
+ "field": "in_ia",
+ }}},
+ ],
+ },
+ },
+ },
+ "size": 0,
+ "query": {
+ "bool": {
+ "must": [{
+ "range": {
+ "release_year": {
+ "gte": datetime.datetime.today().year - 499,
+ "lte": datetime.datetime.today().year,
+ }
+ }
+ }],
+ "filter": [{
+ "bool": {
+ "should": [{
+ "match": {
+ "container_id": ident
+ }
+ }],
+ "minimum_should_match": 1,
+ },
+ }],
+ }
+ }
+ }
+ resp = requests.get(
+ "{}/fatcat_release/_search".format(app.config['ELASTICSEARCH_BACKEND']),
+ json=query,
+ params=dict(request_cache="true"))
+ resp.raise_for_status()
+ # TODO: abort()
+ resp = resp.json()
+ print(resp)
+ vals = [(h['key']['year'], h['key']['in_ia'], h['doc_count'])
+ for h in resp['aggregations']['year_in_ia']['buckets']]
+ vals = sorted(vals)
+ return vals
diff --git a/python/fatcat_web/templates/container_view_coverage.html b/python/fatcat_web/templates/container_view_coverage.html
new file mode 100644
index 00000000..eb9dba8a
--- /dev/null
+++ b/python/fatcat_web/templates/container_view_coverage.html
@@ -0,0 +1,15 @@
+{% set container = entity %}
+{% set entity_view = "coverage" %}
+{% set entity_type = "container" %}
+{% import "entity_macros.html" as entity_macros %}
+{% extends "entity_base.html" %}
+
+{% block entity_main %}
+
+<h3>Preservation Coverage By Year</h3>
+
+<img src="/container/{{ container.ident }}/ia_coverage_years.svg">
+<br><a href="/container/{{ container.ident }}/ia_coverage_years.json">Download as JSON</a>
+
+{% endblock %}
+
diff --git a/python/fatcat_web/templates/entity_base.html b/python/fatcat_web/templates/entity_base.html
index bba95d9d..48f51ec6 100644
--- a/python/fatcat_web/templates/entity_base.html
+++ b/python/fatcat_web/templates/entity_base.html
@@ -78,7 +78,9 @@
<div style="min-width: 40em;">
<div class="ui small tabular compact menu">
{{ entity_tab("overview", "Overview", "") }}
- {% if entity_type == "release" %}
+ {% if entity_type == "container" and entity.state == 'active' and not editgroup %}
+ {{ entity_tab("coverage", "Coverage", "/coverage") }}
+ {% elif entity_type == "release" %}
{{ entity_tab("contribs", "Authors", "/contribs", entity._authors|count ) }}
{{ entity_tab("references", "References", "/references", entity.refs|count) }}
{% endif %}