Add top4 products

This commit is contained in:
Wout Schellaert 2016-09-10 23:01:13 +02:00
parent a6f71195fd
commit ba33a05f66
3 changed files with 56 additions and 16 deletions

View file

@ -1,20 +1,54 @@
from models import User, Location, Order, OrderItem from models import User, Location, Order, OrderItem, Product
from sqlalchemy.sql import func, desc
class CountableModel: class FatModel:
@classmethod
def all(cls):
return cls.query.all()
@classmethod @classmethod
def amount(cls): def amount(cls):
return cls.query.count() return cls.query.count()
class FatLocation(Location, CountableModel): pass class FatLocation(Location, FatModel): pass
class FatOrder(Order, CountableModel): pass class FatOrder(Order, FatModel):
# It's hard to add the unique user constraint,
# as DISTINCT seems to apply after a GROUP BY and aggregate
# So DISTINCT ... count(user_id) ... will count all users,
# even if they get reduced by the disctinct afterwards.
@classmethod
def items_per_order(cls):
return Order.query.\
join(OrderItem).\
group_by(Order.id).\
with_entities(Order.id, func.count(OrderItem.user_id).label('total'))
class FatUser(User, CountableModel): pass class FatUser(User, FatModel): pass
class FatOrderItem(OrderItem, CountableModel): pass class FatOrderItem(OrderItem, FatModel): pass
class FatProduct(Product, FatModel):
@classmethod
def top4(cls):
top4 = OrderItem.query\
.join(Product)\
.join(Location)\
.group_by(Product.id)\
.with_entities(Product.name,
Location.name,
func.count(Product.id).label('count')
)\
.order_by(desc('count'))\
.limit(4)
for top in top4:
print(top)

View file

@ -4,11 +4,11 @@
<h2>Stats bruh</h2> <h2>Stats bruh</h2>
<div class="jumbotron"> <div class="jumbotron">
<h5> <h5>
In Over
<strong>{{ data.order_amount }}</strong> orders, <strong>{{ data.amount.orders }}</strong> orders,
<strong>{{ data.user_amount }}</strong> users have ordered <strong>{{ data.amount.users }}</strong> users have ordered
<strong>{{ data.orderitem_amount }}</strong> items in <strong>{{ data.amount.orderitem }}</strong> items in
<strong>{{ data.location_amount }}</strong> locations. <strong>{{ data.amount.location }}</strong> locations.
</h5> </h5>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -1,15 +1,21 @@
from flask import render_template from flask import render_template
from fatmodels import FatLocation, FatOrder, FatOrderItem, FatUser from fatmodels import FatLocation, FatOrder, FatOrderItem, FatUser, FatProduct
from app import app from app import app
@app.route('/stats/') @app.route('/stats/')
def stats(): def stats():
data = { data = {
'order_amount': FatOrder.amount(), 'amount': {
'location_amount': FatLocation.amount(), 'orders': FatOrder.amount(),
'user_amount': FatUser.amount(), 'locations': FatLocation.amount(),
'orderitem_amount': FatOrderItem.amount() 'users': FatUser.amount(),
'orderitems': FatOrderItem.amount(),
'products': FatProduct.amount(),
},
'top4': {
'products': FatProduct.top4()
}
} }
return render_template('stats.html', data=data) return render_template('stats.html', data=data)