Skip to content

Commit

Permalink
stats: first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
shon committed Sep 6, 2019
1 parent 9431632 commit 201904a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/libs/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from peewee import fn, SQL
from app.models import Comment, RejectedComment


def total_comments():
approved = Comment.select(Comment.id).count()
rejected = RejectedComment.select(RejectedComment.id).count()
return {'approved': approved,
'rejected': rejected,
'total': approved + rejected}


def weekly_comments():
approved = Comment.select \
(fn.date_trunc('week', Comment.created), fn.COUNT(Comment.id)) \
.group_by(fn.date_trunc('week', Comment.created)) \
.order_by(SQL('date_trunc').asc())

rejected = RejectedComment.select \
(fn.date_trunc('week', RejectedComment.created), fn.COUNT(RejectedComment.id)) \
.group_by(fn.date_trunc('week', RejectedComment.created)) \
.order_by(SQL('date_trunc').asc())

data = zip(approved.tuples(), rejected.tuples())
return [(w1.date, c, (c + rc), round(c*100/(c+rc))) for (w1, c), (w2, rc) in data]


def hourly_comments():
q = Comment.select \
(fn.date_part('hour', Comment.created), fn.COUNT(Comment.id)) \
.group_by(fn.date_part('hour', Comment.created)) \
.order_by(SQL('date_part').asc()).tuples()
return tuple((h, c) for (h, c) in q)

0 comments on commit 201904a

Please sign in to comment.