[IMP] gamification: improve 40e8437 to make only one global request

This commit is contained in:
Martin Trigaux 2014-09-02 11:25:02 +02:00
parent 40e843761f
commit b707719891
1 changed files with 14 additions and 11 deletions

View File

@ -95,18 +95,21 @@ class gamification_badge(osv.Model):
the total number of time this badge was granted the total number of time this badge was granted
the total number of users this badge was granted to the total number of users this badge was granted to
""" """
result = dict.fromkeys(ids, False) result = dict.fromkeys(ids, {'stat_count':0, 'stat_count_distinct':0, 'unique_owner_ids':[]})
for badge_id in ids:
cr.execute(""" cr.execute("""
SELECT user_id SELECT badge_id, count(user_id) as stat_count,
FROM gamification_badge_user count(distinct(user_id)) as stat_count_distinct,
WHERE badge_id = %s array_agg(distinct(user_id)) as unique_owner_ids
""", (badge_id,)) FROM gamification_badge_user
res = [user_id[0] for user_id in cr.fetchall()] WHERE badge_id in %s
GROUP BY badge_id
""", (tuple(ids),))
for (badge_id, stat_count, stat_count_distinct, unique_owner_ids) in cr.fetchall():
result[badge_id] = { result[badge_id] = {
'unique_owner_ids': list(set(res)), 'stat_count': stat_count,
'stat_count': len(res), 'stat_count_distinct': stat_count_distinct,
'stat_count_distinct': len(list(set(res))) 'unique_owner_ids': unique_owner_ids,
} }
return result return result