64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
from prometheus_client import start_http_server, Counter
|
|
from bitvend.models import User, Transaction
|
|
from sqlalchemy import func, text
|
|
import cachetools
|
|
import datetime
|
|
|
|
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(window=None):
|
|
balance = func.sum(Transaction.amount).label("balance")
|
|
candidates = (
|
|
Transaction.query.with_entities(
|
|
Transaction.uid,
|
|
func.sum(Transaction.amount * -1),
|
|
func.count(),
|
|
)
|
|
.group_by(Transaction.uid)
|
|
.filter(Transaction.amount < 0)
|
|
.filter(Transaction.type == "purchase")
|
|
.having(balance < 0)
|
|
.order_by(balance.asc())
|
|
)
|
|
if window is not None:
|
|
start_time = datetime.date.today() - datetime.timedelta(hours=window)
|
|
candidates = candidates.filter(Transaction.created >= start_time)
|
|
return (
|
|
candidates
|
|
.limit(5)
|
|
.all()
|
|
)
|
|
|
|
|
|
@cachetools.cached(cachetools.TTLCache(32, 600))
|
|
def bottles_purchased():
|
|
return Transaction.query.filter(
|
|
Transaction.amount.in_([-500, -600]), Transaction.type == "purchase"
|
|
).count()
|