contentdb/app/blueprints/metrics/__init__.py

75 lines
2.8 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2020-04-23 23:52:03 +02:00
# Copyright (C) 2020 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
2021-01-30 17:59:42 +01:00
# it under the terms of the GNU Affero General Public License as published by
2020-04-23 23:52:03 +02:00
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021-01-30 17:59:42 +01:00
# GNU Affero General Public License for more details.
2020-04-23 23:52:03 +02:00
#
2021-01-30 17:59:42 +01:00
# You should have received a copy of the GNU Affero General Public License
2020-04-23 23:52:03 +02:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import Blueprint, make_response
from sqlalchemy.sql.expression import func
2020-12-04 03:23:04 +01:00
from app.models import Package, db, User, UserRank, PackageState
2020-04-23 23:52:03 +02:00
bp = Blueprint("metrics", __name__)
def generate_metrics(full=False):
def write_single_stat(name, help, type, value):
fmt = "# HELP {name} {help}\n# TYPE {name} {type}\n{name} {value}\n\n"
return fmt.format(name=name, help=help, type=type, value=value)
def gen_labels(labels):
pieces = [key + "=" + str(val) for key, val in labels.items()]
2020-12-04 03:23:04 +01:00
return ",".join(pieces)
2020-04-23 23:52:03 +02:00
def write_array_stat(name, help, type, data):
2020-12-04 03:23:04 +01:00
ret = "# HELP {name} {help}\n# TYPE {name} {type}\n" \
.format(name=name, help=help, type=type)
2020-04-23 23:52:03 +02:00
for entry in data:
assert(len(entry) == 2)
2020-12-04 03:23:04 +01:00
ret += "{name}{{{labels}}} {value}\n" \
.format(name=name, labels=gen_labels(entry[0]), value=entry[1])
2020-04-23 23:52:03 +02:00
return ret + "\n"
downloads_result = db.session.query(func.sum(Package.downloads)).one_or_none()
2020-04-23 23:52:03 +02:00
downloads = 0 if not downloads_result or not downloads_result[0] else downloads_result[0]
packages = Package.query.filter_by(state=PackageState.APPROVED).count()
2020-04-23 23:52:03 +02:00
users = User.query.filter(User.rank != UserRank.NOT_JOINED).count()
ret = ""
2021-11-25 00:01:42 +01:00
ret += write_single_stat("contentdb_packages", "Total packages", "gauge", packages)
ret += write_single_stat("contentdb_users", "Number of registered users", "gauge", users)
ret += write_single_stat("contentdb_downloads", "Total downloads", "gauge", downloads)
2020-04-23 23:52:03 +02:00
if full:
scores = Package.query.join(User).with_entities(User.username, Package.name, Package.score) \
.filter(Package.state==PackageState.APPROVED).all()
2020-04-23 23:52:03 +02:00
2020-12-04 03:23:04 +01:00
ret += write_array_stat("contentdb_package_score", "Package score", "gauge",
[({ "author": score[0], "name": score[1] }, score[2]) for score in scores])
2020-04-23 23:52:03 +02:00
else:
score_result = db.session.query(func.sum(Package.score)).one_or_none()
score = 0 if not score_result or not score_result[0] else score_result[0]
ret += write_single_stat("contentdb_score", "Total package score", "gauge", score)
return ret
@bp.route("/metrics")
def metrics():
2020-12-04 03:23:04 +01:00
response = make_response(generate_metrics(), 200)
response.mimetype = "text/plain"
return response