def total_relation(self, table_name, key, value=None, all_time=None): """Return totals based on relationship data.""" tables = tdb_sql.get_rel_table("%s_account_link" % table_name) t1, t2 = tables[0], tables[3] s = sa.select([sa.func.count(t1.c.date)], sa.and_(t1.c.rel_id == t2.c.thing_id, t2.c.key == key)) if value: s.append_whereclause(t2.c.value == value) s = self.append_date_clause(t1, s, all_time=all_time) return s.execute().fetchone()[0]
def total_relation(self, table_name, key, value=None, all_time=None): """Return totals based on relationship data.""" tables = tdb_sql.get_rel_table('%s_account_link' % table_name) t1, t2 = tables[0], tables[3] s = sa.select([sa.func.count(t1.c.date)], sa.and_(t1.c.rel_id == t2.c.thing_id, t2.c.key == key)) if value: s.append_whereclause(t2.c.value == value) s = self.append_date_clause(t1, s, all_time=all_time) return s.execute().fetchone()[0]
def vote_stats(config): stats = {} link_votes = Vote.rel(Account, Link) comment_votes = Vote.rel(Account, Comment) for name, rel in (('link', link_votes), ('comment', comment_votes)): table = get_rel_table(rel._type_id)[0] q = table.count(table.c.date > timeago('1 day')) stats[name+'_vote_count_past_day'] = q.execute().fetchone()[0] stats['vote_count_past_day'] = stats['link_vote_count_past_day'] + stats['comment_vote_count_past_day'] return stats
def vote_stats(config, ranges): stats = {} link_votes = Vote.rel(Account, Link) comment_votes = Vote.rel(Account, Comment) for name, rel in (('link', link_votes), ('comment', comment_votes)): table = get_rel_table(rel._type_id)[0] q = table.count( (table.c.date > ranges['yesterday'][0]) & (table.c.date < ranges['yesterday'][1])) stats[name+'_vote_count_yesterday'] = q.execute().fetchone()[0] stats['vote_count_yesterday'] = stats['link_vote_count_yesterday'] + stats['comment_vote_count_yesterday'] return stats
def vote_stats(config, ranges): stats = {} link_votes = Vote.rel(Account, Link) comment_votes = Vote.rel(Account, Comment) for name, rel in (('link', link_votes), ('comment', comment_votes)): table = get_rel_table(rel._type_id)[0] q = table.count((table.c.date > ranges['yesterday'][0]) & (table.c.date < ranges['yesterday'][1])) stats[name + '_vote_count_yesterday'] = q.execute().fetchone()[0] stats['vote_count_yesterday'] = stats['link_vote_count_yesterday'] + stats[ 'comment_vote_count_yesterday'] return stats
def top_user_change(period = '1 day'): rel = Vote.rel(Account, Link) rt, account, link, dt = tdb.get_rel_table(rel._type_id) author = dt.alias() date = utils.timeago(period) s = sa.select([author.c.value, sa.func.sum(sa.cast(rt.c.name, sa.Integer))], sa.and_(rt.c.date > date, author.c.thing_id == rt.c.rel_id, author.c.key == 'author_id'), group_by = author.c.value, order_by = sa.desc(sa.func.sum(sa.cast(rt.c.name, sa.Integer))), limit = 10) rows = s.execute().fetchall() return [(int(r.value), r.sum) for r in rows]