From d637dd1dc11288e6c8d0526a395bc9557bfe376a Mon Sep 17 00:00:00 2001 From: Alice Butcher Date: Fri, 14 Jun 2024 14:46:03 +0100 Subject: [PATCH] feat: add integration with ckanext-status so far this just checks the iiif server status --- ckanext/nhm/lib/utils.py | 27 ++++++++++++++++++ ckanext/nhm/plugin.py | 59 ++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 3 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 ckanext/nhm/lib/utils.py diff --git a/ckanext/nhm/lib/utils.py b/ckanext/nhm/lib/utils.py new file mode 100644 index 00000000..490a05c4 --- /dev/null +++ b/ckanext/nhm/lib/utils.py @@ -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 diff --git a/ckanext/nhm/plugin.py b/ckanext/nhm/plugin.py index 4466db24..757c780d 100644 --- a/ckanext/nhm/plugin.py +++ b/ckanext/nhm/plugin.py @@ -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__) @@ -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): @@ -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._('connected') + status_type = 'good' + elif iiif_health['ping'] and iiif_health['status'] != ':)': + status_text = toolkit._('connected (issues)') + status_type = 'ok' + else: + status_text = toolkit._('disconnected') + 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._('connected') + status_type = 'good' + else: + status_text = toolkit._('disconnected') + 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 in e.g. our Collection specimens dataset' + ), + 'state': status_type, + } + ) + + return status_reports diff --git a/pyproject.toml b/pyproject.toml index 28c9037c..8bdbf136 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]