diff --git a/app/__init__.py b/app/__init__.py index c19ed410..5b71e4d2 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -54,7 +54,7 @@ app.config["LANGUAGES"] = { app.config.from_pyfile(os.environ["FLASK_CONFIG"]) -r = redis.Redis.from_url(app.config["REDIS_URL"]) +redis_client = redis.Redis.from_url(app.config["REDIS_URL"]) github = GitHub(app) csrf = CSRFProtect(app) diff --git a/app/blueprints/metrics/__init__.py b/app/blueprints/metrics/__init__.py index 4760e5d4..3aee8f37 100644 --- a/app/blueprints/metrics/__init__.py +++ b/app/blueprints/metrics/__init__.py @@ -18,6 +18,7 @@ from flask import Blueprint, make_response from sqlalchemy.sql.expression import func from app.models import Package, db, User, UserRank, PackageState +from app.rediscache import get_key bp = Blueprint("metrics", __name__) @@ -53,6 +54,7 @@ def generate_metrics(full=False): 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) + ret += write_single_stat("contentdb_emails", "Number of emails sent", "counter", int(get_key("emails_sent", "0"))) if full: scores = Package.query.join(User).with_entities(User.username, Package.name, Package.score) \ diff --git a/app/rediscache.py b/app/rediscache.py index 869abb04..0dc458cc 100644 --- a/app/rediscache.py +++ b/app/rediscache.py @@ -14,16 +14,27 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from . import r +from . import redis_client + +# This file acts as a facade between the rest of the code and redis, +# and also means that the rest of the code avoids knowing about `app` -# This file acts as a facade between the releases code and redis, -# and also means that the releases code avoids knowing about `app` def make_download_key(ip, package): return "{}/{}/{}".format(ip, package.author.username, package.name) + def set_key(key, v): - r.set(key, v) + redis_client.set(key, v) + def has_key(key): - return r.exists(key) + return redis_client.exists(key) + + +def increment_key(key): + redis_client.incrby(key, 1) + + +def get_key(key, default=None): + return redis_client.get(key) or default diff --git a/app/tasks/emails.py b/app/tasks/emails.py index 85704926..6aed90e7 100644 --- a/app/tasks/emails.py +++ b/app/tasks/emails.py @@ -22,6 +22,7 @@ from flask_babel import force_locale, gettext, lazy_gettext, LazyString from flask_mail import Message from app import mail from app.models import Notification, db, EmailSubscription, User +from app.rediscache import increment_key from app.tasks import celery from app.utils import abs_url_for, abs_url, random_string @@ -69,6 +70,7 @@ def send_verify_email(email, token, locale): msg.html = render_template("emails/verify.html", token=token, sub=sub) mail.send(msg) + increment_key("emails_sent") @celery.task() @@ -88,6 +90,7 @@ def send_unsubscribe_verify(email, locale): msg.html = render_template("emails/verify_unsubscribe.html", sub=sub) mail.send(msg) + increment_key("emails_sent") @celery.task(rate_limit="25/m") @@ -107,6 +110,7 @@ def send_email_with_reason(email: str, locale: str, subject: str, text: str, htm conn.send(msg) else: mail.send(msg) + increment_key("emails_sent") @celery.task(rate_limit="25/m") @@ -142,6 +146,7 @@ def send_single_email(notification, locale): msg.html = render_template("emails/notification.html", notification=notification, sub=sub) mail.send(msg) + increment_key("emails_sent") def send_notification_digest(notifications: [Notification], locale): @@ -164,6 +169,7 @@ def send_notification_digest(notifications: [Notification], locale): msg.html = render_template("emails/notification_digest.html", notifications=notifications, user=user, sub=sub) mail.send(msg) + increment_key("emails_sent") @celery.task()