mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 01:47:22 +08:00
d91456fd53
This adds the ability to collect stats without exposing them
among other stats via API.
The most important thing I wanted to achieve is to provide
an API where stats are not exposed by default, and a developer
has to explicitly specify that they should be
exposed (`expose_via_api: true`). Implementing an opposite
solution would be simpler, but that's less safe in terms of
potential security issues.
When working on this, I had to refactor the current solution.
I would go even further with the refactoring, but the next steps
seem to be going too far in changing the solution we have,
and that would also take more time. Two things that can be
improved in the future:
1. Data structures for holding stats can be further improved
2. Core stats are hard-coded in the About template (it's hard
to fix it without correcting data structures first, see point 1):
63a0700d45/app/views/about/index.html.erb (L61-L101)
The most significant refactorings are:
1. Introducing the `Stat` model
2. Aligning the way the core and the plugin stats' are registered
51 lines
1.6 KiB
Ruby
51 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Statistics
|
|
def self.active_users
|
|
{
|
|
last_day: User.where("last_seen_at > ?", 1.days.ago).count,
|
|
"7_days": User.where("last_seen_at > ?", 7.days.ago).count,
|
|
"30_days": User.where("last_seen_at > ?", 30.days.ago).count,
|
|
}
|
|
end
|
|
|
|
def self.likes
|
|
{
|
|
last_day:
|
|
UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 1.days.ago).count,
|
|
"7_days":
|
|
UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 7.days.ago).count,
|
|
"30_days":
|
|
UserAction.where(action_type: UserAction::LIKE).where("created_at > ?", 30.days.ago).count,
|
|
count: UserAction.where(action_type: UserAction::LIKE).count,
|
|
}
|
|
end
|
|
|
|
def self.posts
|
|
{
|
|
last_day: Post.where("created_at > ?", 1.days.ago).count,
|
|
"7_days": Post.where("created_at > ?", 7.days.ago).count,
|
|
"30_days": Post.where("created_at > ?", 30.days.ago).count,
|
|
count: Post.count,
|
|
}
|
|
end
|
|
|
|
def self.topics
|
|
{
|
|
last_day: Topic.listable_topics.where("created_at > ?", 1.days.ago).count,
|
|
"7_days": Topic.listable_topics.where("created_at > ?", 7.days.ago).count,
|
|
"30_days": Topic.listable_topics.where("created_at > ?", 30.days.ago).count,
|
|
count: Topic.listable_topics.count,
|
|
}
|
|
end
|
|
|
|
def self.users
|
|
{
|
|
last_day: User.real.where("created_at > ?", 1.days.ago).count,
|
|
"7_days": User.real.where("created_at > ?", 7.days.ago).count,
|
|
"30_days": User.real.where("created_at > ?", 30.days.ago).count,
|
|
count: User.real.count,
|
|
}
|
|
end
|
|
end
|