bitvend: move stats calculation, preliminary optimization

master
informatic 2023-07-15 01:18:00 +02:00
parent d5896d5d16
commit fc4e256acb
2 changed files with 56 additions and 22 deletions

View File

@ -1,7 +1,59 @@
from prometheus_client import start_http_server, Counter
from bitvend.models import User, Transaction
from sqlalchemy import func, text
import cachetools
coin_counter = Counter("coins_inserted", "Number of coins inserted into machine")
purchase_counter = Counter("purchases", "Number of purchases")
cashless_purchase_counter = Counter(
"cashless_purchases", "Number of cashless (BTC) purchases"
)
# @cachetools.cached(cachetools.TTLCache(32, 600))
def hall_of_shame():
balance = func.sum(Transaction.amount).label("balance")
candidates = (
Transaction.query.with_entities(Transaction.uid)
.group_by(Transaction.uid)
.having(balance < 0)
.order_by(balance.asc())
.limit(5)
.all()
)
return (
User.query.with_entities(User, User.balance)
.filter(User.uid.in_([c for (c,) in candidates]))
.order_by(User.balance.asc())
.all()
)
# @cachetools.cached(cachetools.TTLCache(32, 600))
def hall_of_addicts():
balance = func.sum(Transaction.amount).label("balance")
candidates = (
Transaction.query.with_entities(Transaction.uid)
.group_by(Transaction.uid)
.filter(Transaction.amount < 0)
.filter(Transaction.type == "purchase")
.having(balance < 0)
.order_by(balance.asc())
.limit(5)
.all()
)
return (
User.query.with_entities(User, User.purchase_amount, User.purchase_count)
.filter(User.uid.in_([c for (c,) in candidates]))
.order_by(User.purchase_amount.desc())
.all()
)
# @cachetools.cached(cachetools.TTLCache(32, 600))
def bottles_purchased():
return Transaction.query.filter(
Transaction.amount.in_([-500, -600]), Transaction.type == "purchase"
).count()

View File

@ -10,6 +10,7 @@ from bitvend.models import db, User, Transaction, NoFunds
from bitvend.forms import TransferForm
from bitvend.graphs import gen_main_graph
from bitvend.stats import hall_of_shame, hall_of_addicts, bottles_purchased
from spaceauth import login_required, current_user, cap_required
bp = Blueprint("bitvend", __name__, template_folder="templates")
@ -18,25 +19,6 @@ bp = Blueprint("bitvend", __name__, template_folder="templates")
@bp.route("/")
def index():
transactions = []
hallofshame = (
User.query.with_entities(User, User.balance)
.order_by(User.balance.asc())
.filter(User.balance < 0)
.limit(5)
.all()
)
hallofaddicts = (
User.query.with_entities(User, User.purchase_amount, User.purchase_count)
.order_by(User.purchase_amount.desc())
.filter(User.purchase_amount > 0)
.limit(5)
.all()
)
bottles_purchased = Transaction.query.filter(
Transaction.amount.in_([-500, -600]), Transaction.type == "purchase"
).count()
if current_user.is_authenticated:
transactions = current_user.transactions.order_by(
@ -48,9 +30,9 @@ def index():
items=app.config["ITEMS"],
transactions=transactions,
transfer_form=TransferForm(),
hallofshame=hallofshame,
hallofaddicts=hallofaddicts,
bottles_purchased=bottles_purchased,
hallofshame=hall_of_shame(),
hallofaddicts=hall_of_addicts(),
bottles_purchased=bottles_purchased(),
)