# Different counting methods for Likes: user\_stats vs Badges \[maybe bug\]

**URL:** https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145
**Category:** UX
**Created:** [3월 1, 2017, 12:23오전 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145 "2017-03-01T00:23:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Lutz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lutz/32/65103_2.png) [@Lutz](https://meta.discourse.org/u/Lutz)
#### Post date: [3월 1, 2017, 12:23오전 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/1 "2017-03-01T00:23:07Z")

</div>

We have a user who has over 1400 submitted Likes according to the User Summary. And also over 600 received Likes. Both for several days. He still has not received the Badge Emphatic (500 Likes received and 1000 given)

After some searching I found out that the Likes are counted differently here. In Summary the query is:

`
SELECT COUNT(*) FROM "user_actions" WHERE "user_actions"."user_id" = 15 AND "user_actions"."action_type" = 2 `  
"Likes Revived: " 638

```plaintext
SELECT COUNT(*) FROM "user_actions" WHERE "user_actions"."user_id" = 15 AND "user_actions"."action_type" = 1   
```

“Likes given”: 1471

For the badge is the condition as follows, so he should get the badge.

```plaintext
      SELECT us.user_id, current_timestamp AS granted_at
      FROM user_stats AS us
      INNER JOIN posts AS p ON p.user_id = us.user_id
      WHERE p.like_count> 0
        AND us.likes_given> = 1000
        AND (: backfill OR us.user_id IN (: user_ids))
      GROUP BY us.user_id, us.likes_given
      HAVING COUNT (*)> 500
```

The query

```plaintext
 SELECT us.user_id, count (*) cnt_likes_received, us.likes_given
      FROM user_stats AS us
      INNER JOIN posts AS p ON p.user_id = us.user_id
      WHERE p.like_count> 0
        AND us.user_id = 15
        AND us.likes_given >= 1000
      GROUP BY us.user_id, us.likes_given;
```

```
user_id cnt_likes_received likes_given
15 393 1462

```

This suggests that either user\_stats is completely wrong, or that the Likes are counted differently.  
I always get reports about this, because badges seem very important to our users.

Either “fix” user\_stats or change the query for the Badge.

Edit: I have no pending or broken sidekiq jobs. We use the latest test-passed branch from today.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [3월 1, 2017, 3:53오전 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/2 "2017-03-01T03:53:12Z")

</div>

Any ideas here @sam?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 1, 2017, 2:23오후 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/3 "2017-03-01T14:23:52Z")

</div>

One moment, what is the user\_stat record for said user? keep in mind user\_stat is a cache table, it is not the source of truth.

---

<div class="post-metadata">

### Author: ![Lutz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lutz/32/65103_2.png) [@Lutz](https://meta.discourse.org/u/Lutz)
#### Post date: [3월 1, 2017, 2:34오후 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/4 "2017-03-01T14:34:52Z")

</div>

`SELECT * FROM user_stats where user_id = 15`  
gives  
 ![](https://global.discourse-cdn.com/meta/optimized/3X/7/9/7923c3f34864bc7d4b597a8cf72bdd7048392715_2_690x22.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 1, 2017, 2:36오후 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/5 "2017-03-01T14:36:27Z")

</div>

So wait … the issue is

1474 vs 1462  
641 vs 638

Seems fairly close and a consistency job should eventually repair it.

---

<div class="post-metadata">

### Author: ![Lutz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lutz/32/65103_2.png) [@Lutz](https://meta.discourse.org/u/Lutz)
#### Post date: [3월 1, 2017, 2:39오후 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/6 "2017-03-01T14:39:57Z")

</div>

Yes but the badge query gives an other number:

Original Query is

> ```
> SELECT us.user_id, current_timestamp AS granted_at
> FROM user_stats AS us
> INNER JOIN posts AS p ON p.user_id = us.user_id
> WHERE p.like_count > 0
> AND us.likes_given > = 1000
> AND (: backfill OR us.user_id IN (: user_ids))
> GROUP BY us.user_id, us.likes_given
> HAVING COUNT (*) > 500
> 
> ```

And

> ```
> SELECT us.user_id, count (*) cnt_likes_received, us.likes_given
> FROM user_stats AS us
> INNER JOIN posts AS p ON p.user_id = us.user_id
> WHERE p.like_count> 0
> AND us.user_id = 15
> AND us.likes_given >= 1000
> GROUP BY us.user_id, us.likes_given;
> 
> ```

gives  
 ![](https://global.discourse-cdn.com/meta/original/3X/3/3/33cd51f66424284b8569d2b36a4b9833c5516086.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [3월 1, 2017, 2:45오후 UTC](https://meta.discourse.org/t/different-counting-methods-for-likes-user-stats-vs-badges-maybe-bug/58145/7 "2017-03-01T14:45:14Z")

</div>

> [@Lutz](#):
>
> count (\*) cnt\_likes\_received

ahh well that would only be counting public posts, it is not counting PMs or posts in private categories
