Add emails_sent metric

This commit is contained in:
rubenwardy 2023-06-19 23:34:49 +01:00
parent 3992b30cc2
commit edce45f71a
4 changed files with 25 additions and 6 deletions

@ -54,7 +54,7 @@ app.config["LANGUAGES"] = {
app.config.from_pyfile(os.environ["FLASK_CONFIG"]) 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) github = GitHub(app)
csrf = CSRFProtect(app) csrf = CSRFProtect(app)

@ -18,6 +18,7 @@ from flask import Blueprint, make_response
from sqlalchemy.sql.expression import func from sqlalchemy.sql.expression import func
from app.models import Package, db, User, UserRank, PackageState from app.models import Package, db, User, UserRank, PackageState
from app.rediscache import get_key
bp = Blueprint("metrics", __name__) 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_packages", "Total packages", "gauge", packages)
ret += write_single_stat("contentdb_users", "Number of registered users", "gauge", users) 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_downloads", "Total downloads", "gauge", downloads)
ret += write_single_stat("contentdb_emails", "Number of emails sent", "counter", int(get_key("emails_sent", "0")))
if full: if full:
scores = Package.query.join(User).with_entities(User.username, Package.name, Package.score) \ scores = Package.query.join(User).with_entities(User.username, Package.name, Package.score) \

@ -14,16 +14,27 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
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): def make_download_key(ip, package):
return "{}/{}/{}".format(ip, package.author.username, package.name) return "{}/{}/{}".format(ip, package.author.username, package.name)
def set_key(key, v): def set_key(key, v):
r.set(key, v) redis_client.set(key, v)
def has_key(key): 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

@ -22,6 +22,7 @@ from flask_babel import force_locale, gettext, lazy_gettext, LazyString
from flask_mail import Message from flask_mail import Message
from app import mail from app import mail
from app.models import Notification, db, EmailSubscription, User from app.models import Notification, db, EmailSubscription, User
from app.rediscache import increment_key
from app.tasks import celery from app.tasks import celery
from app.utils import abs_url_for, abs_url, random_string 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) msg.html = render_template("emails/verify.html", token=token, sub=sub)
mail.send(msg) mail.send(msg)
increment_key("emails_sent")
@celery.task() @celery.task()
@ -88,6 +90,7 @@ def send_unsubscribe_verify(email, locale):
msg.html = render_template("emails/verify_unsubscribe.html", sub=sub) msg.html = render_template("emails/verify_unsubscribe.html", sub=sub)
mail.send(msg) mail.send(msg)
increment_key("emails_sent")
@celery.task(rate_limit="25/m") @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) conn.send(msg)
else: else:
mail.send(msg) mail.send(msg)
increment_key("emails_sent")
@celery.task(rate_limit="25/m") @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) msg.html = render_template("emails/notification.html", notification=notification, sub=sub)
mail.send(msg) mail.send(msg)
increment_key("emails_sent")
def send_notification_digest(notifications: [Notification], locale): 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) msg.html = render_template("emails/notification_digest.html", notifications=notifications, user=user, sub=sub)
mail.send(msg) mail.send(msg)
increment_key("emails_sent")
@celery.task() @celery.task()