diff --git a/sonar/monitoring/api/data_integrity.py b/sonar/monitoring/api/data_integrity.py index 6642ce33..61346eb7 100644 --- a/sonar/monitoring/api/data_integrity.py +++ b/sonar/monitoring/api/data_integrity.py @@ -36,7 +36,7 @@ def get_db_count(self, doc_type, with_deleted=False): :param with_deleted: Count also deleted items. :returns: Items count. """ - if not sonar.endpoints.get(doc_type): + if not sonar.endpoints.get(doc_type) or doc_type == 'proj': raise Exception( 'No endpoint configured for "{type}"'.format(type=doc_type)) diff --git a/sonar/monitoring/api/database.py b/sonar/monitoring/api/database.py index 7cf9bd01..a6699d17 100644 --- a/sonar/monitoring/api/database.py +++ b/sonar/monitoring/api/database.py @@ -75,13 +75,14 @@ def format_row(row): """Format returned row from DB.""" return { 'application_name': row['application_name'], - 'client_address': row['client_addr'], + 'client_addr': row['client_addr'], 'client_port': row['client_port'], - 'query': row['left'], + 'backend_start': row['backend_start'], + 'xact_start': row['xact_start'], 'query_start': row['query_start'], - 'state': row['state'], 'wait_event': row['wait_event'], - 'transaction_start': row['xact_start'] + 'state': row['state'], + 'left': row['left'], } return list(map(format_row, db.session.execute(query).fetchall())) diff --git a/sonar/monitoring/views/__init__.py b/sonar/monitoring/views/__init__.py index ca1996ea..30a0e569 100644 --- a/sonar/monitoring/views/__init__.py +++ b/sonar/monitoring/views/__init__.py @@ -19,10 +19,11 @@ from functools import wraps -from flask import Blueprint, abort, jsonify, request +from flask import Blueprint, current_app, jsonify, request from flask_security import current_user from invenio_pidstore.models import PIDStatus from invenio_search import current_search_client +from redis import Redis from sonar.modules.documents.urn import Urn from sonar.modules.permissions import superuser_access_permission @@ -54,7 +55,7 @@ def check_for_superuser(): """Check if user is superuser before each request, with decorator.""" -@api_blueprint.route('/db/connections/count') +@api_blueprint.route('/db_connection_counts') def db_connection_count(): """Information about current database connections.""" try: @@ -64,7 +65,7 @@ def db_connection_count(): return jsonify({'error': str(exception)}), 500 -@api_blueprint.route('/db/activity') +@api_blueprint.route('db_connections') def db_activity(): """Current database activity.""" try: @@ -74,21 +75,21 @@ def db_activity(): return jsonify({'error': str(exception)}), 500 -@api_blueprint.route('/data/status') +@api_blueprint.route('/es_db_status') def data_status(): """Status of data integrity.""" try: data_monitoring = DataIntegrityMonitoring() return jsonify({ 'data': { - 'status': 'green' if not data_monitoring.has_error() else 'red' + 'status': 'red' if data_monitoring.has_error() else 'green' } }) except Exception as exception: return jsonify({'error': str(exception)}), 500 -@api_blueprint.route('/data/info') +@api_blueprint.route('/es_db_counts') def data_info(): """Info of data integrity.""" try: @@ -101,6 +102,18 @@ def data_info(): return jsonify({'error': str(exception)}), 500 +@api_blueprint.route('/redis') +def redis(): + """Displays redis info. + + :return: jsonified redis info. + """ + url = current_app.config.get('ACCOUNTS_SESSION_REDIS_URL', + 'redis://localhost:6379') + redis = Redis.from_url(url) + info = redis.info() + return jsonify({'data': info}) + @api_blueprint.route('/es') def elastic_search(): """Displays elastic search cluster info. @@ -130,3 +143,15 @@ def urn(): return jsonify(dict(data=data)) except Exception as exception: return jsonify({'error': str(exception)}), 500 + + +@api_blueprint.route('/es_indices') +def elastic_search_indices(): + """Displays Elasticsearch indices info. + + :return: jsonified Elasticsearch indices info. + """ + info = current_search_client.cat.indices( + bytes='b', format='json', s='index') + info = {data['index']: data for data in info} + return jsonify({'data': info}) diff --git a/tests/api/monitoring/test_monitoring_views.py b/tests/api/monitoring/test_monitoring_views.py index 79f043a3..c648a942 100644 --- a/tests/api/monitoring/test_monitoring_views.py +++ b/tests/api/monitoring/test_monitoring_views.py @@ -98,14 +98,15 @@ def fetchall(self): assert response.json == { 'data': [{ 'application_name': '', - 'client_address': '10.233.92.25', + 'backend_start': 'Mon, 08 Feb 2021 10:46:55 GMT', + 'client_addr': '10.233.92.25', 'client_port': 33382, - 'query': + 'left': '\n SELECT\n pid, application_name, client', 'query_start': 'Mon, 08 Feb 2021 10:46:55 GMT', 'state': 'active', - 'transaction_start': 'Mon, 08 Feb 2021 10:46:55 GMT', - 'wait_event': None + 'wait_event': None, + 'xact_start': 'Mon, 08 Feb 2021 10:46:55 GMT' }] } diff --git a/tests/unit/monitoring/test_api_db_connection.py b/tests/unit/monitoring/test_api_db_connection.py index 36fc681b..bc10c698 100644 --- a/tests/unit/monitoring/test_api_db_connection.py +++ b/tests/unit/monitoring/test_api_db_connection.py @@ -73,20 +73,14 @@ def fetchall(self): monkeypatch.setattr(db.session, 'execute', lambda *args: MockActivityQuery()) assert db_monitoring.activity() == [{ - 'application_name': - '', - 'client_address': - '10.233.92.25', - 'client_port': - 33382, - 'query': + 'application_name': '', + 'backend_start': 'Mon, 08 Feb 2021 10:46:55 GMT', + 'client_addr': '10.233.92.25', + 'client_port': 33382, + 'left': '\n SELECT\n pid, application_name, client', - 'query_start': - 'Mon, 08 Feb 2021 10:46:55 GMT', - 'state': - 'active', - 'wait_event': - None, - 'transaction_start': - 'Mon, 08 Feb 2021 10:46:55 GMT' + 'query_start': 'Mon, 08 Feb 2021 10:46:55 GMT', + 'state': 'active', + 'wait_event': None, + 'xact_start': 'Mon, 08 Feb 2021 10:46:55 GMT' }]