• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rero / sonar / 17425918180

03 Sep 2025 07:11AM UTC coverage: 95.796% (-0.6%) from 96.378%
17425918180

push

github

PascalRepond
translations: extract messages

Co-Authored-by: Pascal Repond <pascal.repond@rero.ch>

7816 of 8159 relevant lines covered (95.8%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.95
/sonar/monitoring/views/__init__.py
1
# Swiss Open Access Repository
2
# Copyright (C) 2021 RERO
3
#
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU Affero General Public License as published by
6
# the Free Software Foundation, version 3 of the License.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU Affero General Public License for more details.
12
#
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15

16
"""Monitoring views."""
17

18
from functools import wraps
1✔
19

20
from flask import Blueprint, current_app, jsonify, request
1✔
21
from flask_security import current_user
1✔
22
from invenio_pidstore.models import PIDStatus
1✔
23
from invenio_search import current_search_client
1✔
24
from redis import Redis
1✔
25

26
from sonar.modules.documents.urn import Urn
1✔
27
from sonar.modules.permissions import monitoring_access_permission
1✔
28
from sonar.monitoring.api.data_integrity import DataIntegrityMonitoring
1✔
29
from sonar.monitoring.api.database import DatabaseMonitoring
1✔
30

31
api_blueprint = Blueprint("monitoring_api", __name__, url_prefix="/monitoring")
1✔
32

33

34
def is_monitoring_user(func):
1✔
35
    """Decorator checking if a user is logged and has `monitoring` rights."""
36

37
    @wraps(func)
1✔
38
    def decorated_view(*args, **kwargs):
1✔
39
        if not current_user.is_authenticated:
1✔
40
            return jsonify({"error": "Unauthorized"}), 401
1✔
41
        if not monitoring_access_permission.require().can():
1✔
42
            return jsonify({"error": "Forbidden"}), 403
1✔
43
        return func(*args, **kwargs)
1✔
44

45
    return decorated_view
1✔
46

47

48
@api_blueprint.before_request
1✔
49
@is_monitoring_user
1✔
50
def check_for_monitoring_user():
1✔
51
    """Check if user is superuser or monitoring before each request."""
52

53

54
@api_blueprint.route("/db_connection_counts")
1✔
55
def db_connection_count():
1✔
56
    """Information about current database connections."""
57
    try:
1✔
58
        db_monitoring = DatabaseMonitoring()
1✔
59
        return jsonify({"data": db_monitoring.count_connections()})
1✔
60
    except Exception as exception:
×
61
        return jsonify({"error": str(exception)}), 500
×
62

63

64
@api_blueprint.route("/db_connections")
1✔
65
def db_activity():
1✔
66
    """Current database activity."""
67
    try:
1✔
68
        db_monitoring = DatabaseMonitoring()
1✔
69
        return jsonify({"data": db_monitoring.activity()})
1✔
70
    except Exception as exception:
×
71
        return jsonify({"error": str(exception)}), 500
×
72

73

74
@api_blueprint.route("/es_db_status")
1✔
75
def data_status():
1✔
76
    """Status of data integrity."""
77
    try:
1✔
78
        data_monitoring = DataIntegrityMonitoring()
1✔
79
        return jsonify({"data": {"status": "red" if data_monitoring.has_error() else "green"}})
1✔
80
    except Exception as exception:
1✔
81
        return jsonify({"error": str(exception)}), 500
1✔
82

83

84
@api_blueprint.route("/es_db_counts")
1✔
85
def data_info():
1✔
86
    """Info of data integrity."""
87
    try:
1✔
88
        data_monitoring = DataIntegrityMonitoring()
1✔
89
        return jsonify({"data": data_monitoring.info(with_detail=("detail" in request.args))})
1✔
90
    except Exception as exception:
1✔
91
        return jsonify({"error": str(exception)}), 500
1✔
92

93

94
@api_blueprint.route("/redis")
1✔
95
def redis():
1✔
96
    """Displays redis info.
97

98
    :return: jsonified redis info.
99
    """
100
    url = current_app.config.get("ACCOUNTS_SESSION_REDIS_URL", "redis://localhost:6379")
×
101
    redis = Redis.from_url(url)
×
102
    info = redis.info()
×
103
    return jsonify({"data": info})
×
104

105

106
@api_blueprint.route("/es")
1✔
107
def elastic_search():
1✔
108
    """Displays elastic search cluster info.
109

110
    :return: jsonified elastic search cluster info.
111
    """
112
    try:
1✔
113
        info = current_search_client.cluster.health()
1✔
114
        return jsonify({"data": info})
1✔
115
    except Exception as exception:
1✔
116
        return jsonify({"error": str(exception)}), 500
1✔
117

118

119
@api_blueprint.route("/urn")
1✔
120
def urn():
1✔
121
    """Count of unregistered urn pids.
122

123
    :return: jsonified count information.
124
    """
125
    data = {}
1✔
126
    try:
1✔
127
        days = int(args.get("days", 0)) if (args := request.args) else 0
1✔
128
        count, pids = Urn.get_urn_pids(days=days)
1✔
129
        data["reserved"] = {"count": count, "pids": list(pids)}
1✔
130
        count, pids = Urn.get_urn_pids(days=days, status=PIDStatus.REGISTERED)
1✔
131
        data["registered"] = {"count": count}
1✔
132
        return jsonify({"data": data})
1✔
133
    except Exception as exception:
×
134
        return jsonify({"error": str(exception)}), 500
×
135

136

137
@api_blueprint.route("/es_indices")
1✔
138
def elastic_search_indices():
1✔
139
    """Displays Elasticsearch indices info.
140

141
    :return: jsonified Elasticsearch indices info.
142
    """
143
    info = current_search_client.cat.indices(bytes="b", format="json", s="index")
×
144
    info = {data["index"]: data for data in info}
×
145
    return jsonify({"data": info})
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc