Why don't more people use the user_stats table?

badge

(Steven Slade) #1

I’ve been making badge queries and also looking at a lot of queries posted on What cool badge queries have you come up with? and it seems as though the user_stats table rarely gets used in favour of more complicated queries.

user_stats has the following columns:
user_id
topics_entered
time_read
days_visited
posts_read_count
likes_given
likes_received
topic_reply_count
new_since timestamp
read_faq timestamp
first_post_created_at
post_count
topic_count

Is there a downfall to using this table for things like giving a badge after X amount of likes given, etc? Is it too good to be true? Or is it a completely viable table to be querying for the obvious badges it could support?


(Mittineague) #2

One thing to consider is that some are “all time” and some are “within the last _ days” (default 100 days but can be changed by Admin)

So I guess it depends on what stat(s) you’re using to determine the Badge grant criteria.


(Steven Slade) #3

Is there a way to distinguish what intervals they are? I see that one column on the table is int4 …could this be connected to something in the Discourse core defining the interval?

EDIT: I see that int4 is a data type that is just beyond my knowledge and probably not anything to do with intervals. Nonetheless, anyway to tell what intervals the data in this table might calculated as? It would be terrible to use the likes_given for badges when it is only the most recent likes or something like that.


(Mittineague) #4

AFAIK the “all time” Likes are from the User Actions table

discourse/user.rb at master · discourse/discourse · GitHub

  def like_count
    UserAction.where(user_id: id, action_type: UserAction::WAS_LIKED).count
  end

  def like_given_count
    UserAction.where(user_id: id, action_type: UserAction::LIKE).count
  end