Skip to content

Commit

Permalink
merge: #779 from ginger/status
Browse files Browse the repository at this point in the history
  • Loading branch information
alycejenni authored Jul 4, 2024
2 parents 53cab6c + 38dc575 commit 9c3c2b5
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
28 changes: 28 additions & 0 deletions ckanext/nhm/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,3 +1599,31 @@ def get_record_iiif_manifest_url(resource_id: str, record_id: int) -> str:
{'builder_id': 'record', 'resource_id': resource_id, 'record_id': record_id},
)
return toolkit.url_for('iiif.resource', identifier=manifest_id, _external=True)


def get_status_indicator():
"""
Check if we need to display a status indicator, and if so what type.
:return: 'red', 'amber', or None (if no alerts)
"""
# is there a status message?
status_message = toolkit.config.get('ckanext.status.message', None)
if status_message:
return 'red'

try:
status_reports = toolkit.get_action('status_list')({}, {})
except KeyError:
# if the action doesn't exist
status_reports = []

# are there any 'bad' items?
red_status = [r for r in status_reports if r['state'] == 'bad']
if len(red_status) > 0:
return 'red'

# are there any reports with small issues?
amber_status = [r for r in status_reports if r['state'] == 'ok']
if len(amber_status) > 0:
return 'amber'
27 changes: 27 additions & 0 deletions ckanext/nhm/lib/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# encoding: utf-8
#
# This file is part of ckanext-nhm
# Created by the Natural History Museum in London, UK

from ckan.plugins import toolkit
import requests


def get_iiif_status():
health = {}

url = toolkit.config.get('ckanext.iiif.image_server_url')
r = requests.get(url + '/status')
if r.ok:
health['ping'] = True
response_json = r.json()
else:
response_json = {}

health['status'] = response_json.get('status')
mss = response_json.get('profiles', {}).get('mss', {})
health['specimens'] = mss.get('mss_status', {}).get('status', ':(')
health['es'] = mss.get('es', {'status': 'red', 'response_time': None})

return health
59 changes: 59 additions & 0 deletions ckanext/nhm/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
IVersionedDatastore,
IVersionedDatastoreDownloads,
)
from ckanext.nhm.lib.utils import get_iiif_status

try:
from ckanext.status.interfaces import IStatus

status_available = True
except ImportError:
status_available = False

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -63,6 +71,8 @@ class NHMPlugin(SingletonPlugin, toolkit.DefaultDatasetForm):
implements(interfaces.IClick)
implements(interfaces.IConfigurable)
implements(IVersionedDatastoreDownloads, inherit=True)
if status_available:
implements(IStatus)

## IConfigurable
def configure(self, config):
Expand Down Expand Up @@ -710,3 +720,52 @@ def download_modify_eml(self, eml_dict, query):
)
eml_dict['creator'] = creators
return eml_dict

## IStatus
def modify_status_reports(self, status_reports):
iiif_health = get_iiif_status()

# overall image server status
if iiif_health['ping'] and iiif_health['status'] == ':)':
status_text = toolkit._('available')
status_type = 'good'
elif iiif_health['ping'] and iiif_health['status'] != ':)':
status_text = toolkit._('available (issues)')
status_type = 'ok'
else:
status_text = toolkit._('unavailable')
status_type = 'bad'

status_reports.append(
{
'label': toolkit._('Image server'),
'value': status_text,
'group': toolkit._('Images'),
'help': toolkit._(
'The IIIF server provides most of the images in datasets (some are externally hosted)'
),
'state': status_type,
}
)

# specimen images
if iiif_health['ping'] and iiif_health['specimens'] == ':)':
status_text = toolkit._('available')
status_type = 'good'
else:
status_text = toolkit._('unavailable')
status_type = 'bad'

status_reports.append(
{
'label': toolkit._('Specimen images'),
'value': status_text,
'group': toolkit._('Images'),
'help': toolkit._(
'Specimen images are a specific subset of images used primarily in the Collection specimens and Index lots datasets'
),
'state': status_type,
}
)

return status_reports
17 changes: 17 additions & 0 deletions ckanext/nhm/theme/assets/less/nhm.less
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,23 @@ body {
& .notifications {
padding-right: 10px;
}

& .status-indicator {
font-size: @font-size-body-s;
position: absolute;
top: 0;
margin-left: 1.3em;
padding: 0 2px;
border-radius: @rounding;

&.status-indicator-red {
background: @warning2;
}

&.status-indicator-amber {
background: orange;
}
}
}

.icon-pad() {
Expand Down
24 changes: 24 additions & 0 deletions ckanext/nhm/theme/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@

{# We do not want a search #}{% block header_site_search %}{% endblock %}

{% set status_indicator = h.get_status_indicator() %}
{% macro status_icon() %}
<div>
<a href="{{ h.url_for('status.index') }}"
title="{{ _('System status') }}">
<span class="sr-only">{{ _('System status') }}</span>
{% if status_indicator %}
<span class="status-indicator status-indicator-{{ status_indicator }}">
<span class="sr-only">{{ _('Status alert') }}</span>
<i class="fas fa-exclamation fa-xs"></i>
</span>
{% endif %}
<i class="fas fa-heartbeat fa-lg"></i>
</a>
</div>
{% endmacro %}


{% block header_wrapper %}
<header class="masthead" role="heading">
<div class="account-icons container">
Expand Down Expand Up @@ -71,6 +89,9 @@
</div>
{% endif %}

{# Status page #}
{{ status_icon() }}

{# Logout #}
{% block header_account_log_out_link %}
<a href="{{ h.url_for('user.logout') }}" title="{{ _('Log out') }}">
Expand All @@ -85,6 +106,9 @@
{# headway widget #}
<div class="headway-container"></div>

{# Status page #}
{{ status_icon() }}

{% block header_account_notlogged %}
<div>
<a href="{{ h.url_for('user.login') }}" title="{{ _('Log in') }}">
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ dependencies = [
"ckanext-ldap>=3.2.0",
"ckanext-query-dois>=4.0.0",
"ckanext-statistics>=3.1.0",
"ckanext-versioned-datastore>=5.1.0"
"ckanext-versioned-datastore>=5.1.0",
# this also depends on ckanext-dcat==1.3.0 (see readme)
"requests"
]

[project.optional-dependencies]
Expand Down

0 comments on commit 9c3c2b5

Please sign in to comment.