def get_monthly_top_commented_articles(top=10, since=None): months = Comment.select(fn.date_trunc('month', Comment.created)) if since: months = months.where(Comment.created >= since) months = months.group_by(fn.date_trunc('month', Comment.created)).order_by( SQL('date_trunc').asc()).tuples() month_top_commented_articles_map = {m[0]: [] for m in months} output_formatter = lambda mtc: (Asset.get_by_id(mtc[1]).url, mtc[2]) for month in months: month_top_commented_articles_map[month[0]].extend( list( map( output_formatter, Comment.select( fn.date_trunc('month', Comment.created), Comment.asset_id, fn.count(Comment.id)).group_by( fn.date_trunc('month', Comment.created), Comment.asset_id).where( fn.date_trunc('month', Comment.created) == month).order_by((SQL('date_trunc')), (SQL('count')).desc()).limit( int(top)).tuples()))) first_month = min(month_top_commented_articles_map.keys()) last_month = max(month_top_commented_articles_map.keys()) months = get_week_or_month_counter(metric='month', first_metric_value=first_month, last_metric_value=last_month) monthly_top_commented_articles = list( month_top_commented_articles_map.items()) ret = fill_output_with_default_values( metric_counter=months, output=monthly_top_commented_articles, default_value=[]) return ret
def get_last2days_top_commented_articles(top=10): last2days_top_commented_articles = Comment.select( Comment.asset_id, fn.count(Comment.id)).group_by( Comment.asset_id).where(Comment.created >= arrow.utcnow().shift( days=-int(2)).span('day')[0].date()).order_by( (SQL('count')).desc()).limit(int(top)) return [(Asset.get_by_id(asset_id).url, count) for asset_id, count in last2days_top_commented_articles.tuples()]
def get_pending_comments_by_asset(since=None): pending_comments_by_asset = PendingComment.select( PendingComment.asset_id, fn.count(PendingComment.id)).group_by(PendingComment.asset_id) if since: pending_comments_by_asset = pending_comments_by_asset.where( PendingComment.created >= since) total_pending = sum( [count for asset_id, count in pending_comments_by_asset.tuples()]) pending_comments_by_asset = [ (Asset.get_by_id(asset_id).url, count) for asset_id, count in pending_comments_by_asset.tuples() ] return { 'total_pending': total_pending, 'pending_comments_by_asset': pending_comments_by_asset }