Skip to content

Commit

Permalink
Add more instance stats APIs (mastodon#6125)
Browse files Browse the repository at this point in the history
* Add GET /api/v1/instance/peers API to reveal known domains

* Add GET /api/v1/instance/activity API

* Make new APIs disableable, exclude private statuses from activity stats

* Fix code style issue

* Fix week timestamps
  • Loading branch information
Gargron committed Dec 29, 2017
1 parent c7f0ce5 commit d79d1e4
Show file tree
Hide file tree
Showing 14 changed files with 144 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/controllers/admin/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ class SettingsController < BaseController
bootstrap_timeline_accounts
thumbnail
min_invite_role
activity_api_enabled
peers_api_enabled
).freeze

BOOLEAN_SETTINGS = %w(
open_registrations
open_deletion
timeline_preview
show_staff_badge
activity_api_enabled
peers_api_enabled
).freeze

UPLOAD_SETTINGS = %w(
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/api/v1/instances/activity_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

class Api::V1::Instances::ActivityController < Api::BaseController
before_action :require_enabled_api!

respond_to :json

def show
render_cached_json('api:v1:instances:activity:show', expires_in: 1.day) { activity }
end

private

def activity
weeks = []

12.times do |i|
day = i.weeks.ago.to_date
week_id = day.cweek
week = Date.commercial(day.cwyear, week_id)

weeks << {
week: week.to_time.to_i.to_s,
statuses: Redis.current.get("activity:statuses:local:#{week_id}") || 0,
logins: Redis.current.pfcount("activity:logins:#{week_id}"),
registrations: Redis.current.get("activity:accounts:local:#{week_id}") || 0,
}
end

weeks
end

def require_enabled_api!
head 404 unless Setting.activity_api_enabled
end
end
17 changes: 17 additions & 0 deletions app/controllers/api/v1/instances/peers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class Api::V1::Instances::PeersController < Api::BaseController
before_action :require_enabled_api!

respond_to :json

def index
render_cached_json('api:v1:instances:peers:index', expires_in: 1.day) { Account.remote.domains }
end

private

def require_enabled_api!
head 404 unless Setting.peers_api_enabled
end
end
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,13 @@ def respond_with_error(code)
end
end
end

def render_cached_json(cache_key, **options)
data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do
yield.to_json
end

expires_in options[:expires_in], public: true
render json: data
end
end
6 changes: 0 additions & 6 deletions app/controllers/auth/confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

class Auth::ConfirmationsController < Devise::ConfirmationsController
layout 'auth'

def show
super do |user|
BootstrapTimelineWorker.perform_async(user.account_id) if user.errors.empty?
end
end
end
1 change: 1 addition & 0 deletions app/controllers/concerns/user_tracking_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def set_user_activity

# Mark as signed-in today
current_user.update_tracked_fields!(request)
ActivityTracker.record('activity:logins', current_user.id)

# Regenerate feed if needed
regenerate_feed! if user_needs_feed_update?
Expand Down
31 changes: 31 additions & 0 deletions app/lib/activity_tracker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class ActivityTracker
EXPIRE_AFTER = 90.days.seconds

class << self
def increment(prefix)
key = [prefix, current_week].join(':')

redis.incrby(key, 1)
redis.expire(key, EXPIRE_AFTER)
end

def record(prefix, value)
key = [prefix, current_week].join(':')

redis.pfadd(key, value)
redis.expire(key, value)
end

private

def redis
Redis.current
end

def current_week
Time.zone.today.cweek
end
end
end
4 changes: 4 additions & 0 deletions app/models/form/admin_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Form::AdminSettings
:bootstrap_timeline_accounts=,
:min_invite_role,
:min_invite_role=,
:activity_api_enabled,
:activity_api_enabled=,
:peers_api_enabled,
:peers_api_enabled=,
to: Setting
)
end
6 changes: 6 additions & 0 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def emojis
end

after_create_commit :store_uri, if: :local?
after_create_commit :update_statistics, if: :local?

around_create Mastodon::Snowflake::Callbacks

Expand Down Expand Up @@ -308,4 +309,9 @@ def carried_over_reply_to_account_id
def set_local
self.local = account.local?
end

def update_statistics
return unless public_visibility? || unlisted_visibility?
ActivityTracker.increment('activity:statuses:local')
end
end
15 changes: 15 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,19 @@ def enable!
update!(disabled: false)
end

def confirm
return if confirmed?

super
update_statistics!
end

def confirm!
return if confirmed?

skip_confirmation!
save!
update_statistics!
end

def promote!
Expand Down Expand Up @@ -202,4 +212,9 @@ def send_devise_notification(notification, *args)
def sanitize_languages
filtered_languages.reject!(&:blank?)
end

def update_statistics!
BootstrapTimelineWorker.perform_async(account_id)
ActivityTracker.increment('activity:accounts:local')
end
end
8 changes: 8 additions & 0 deletions app/views/admin/settings/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@
.fields-group
= f.input :bootstrap_timeline_accounts, wrapper: :with_block_label, label: t('admin.settings.bootstrap_timeline_accounts.title'), hint: t('admin.settings.bootstrap_timeline_accounts.desc_html')

%hr/

.fields-group
= f.input :activity_api_enabled, as: :boolean, wrapper: :with_label, label: t('admin.settings.activity_api_enabled.title'), hint: t('admin.settings.activity_api_enabled.desc_html')

.fields-group
= f.input :peers_api_enabled, as: :boolean, wrapper: :with_label, label: t('admin.settings.peers_api_enabled.title'), hint: t('admin.settings.peers_api_enabled.desc_html')

.actions
= f.button :button, t('generic.save_changes'), type: :submit
6 changes: 6 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,18 @@ en:
unresolved: Unresolved
view: View
settings:
activity_api_enabled:
desc_html: Counts of locally posted statuses, active users, and new registrations in weekly buckets
title: Publish aggregate statistics about user activity
bootstrap_timeline_accounts:
desc_html: Separate multiple usernames by comma. Only local and unlocked accounts will work. Default when empty is all local admins.
title: Default follows for new users
contact_information:
email: Business e-mail
username: Contact username
peers_api_enabled:
desc_html: Domain names this instance has encountered in the fediverse
title: Publish list of discovered instances
registrations:
closed_message:
desc_html: Displayed on frontpage when registrations are closed. You can use HTML tags
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@

resources :apps, only: [:create]

resource :instance, only: [:show]
resource :instance, only: [:show] do
resources :peers, only: [:index], controller: 'instances/peers'
resource :activity, only: [:show], controller: 'instances/activity'
end

resource :domain_blocks, only: [:show, :create, :destroy]

resources :follow_requests, only: [:index] do
Expand Down
3 changes: 2 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ defaults: &defaults
- webmaster
- administrator
bootstrap_timeline_accounts: ''

activity_api_enabled: true
peers_api_enabled: true
development:
<<: *defaults

Expand Down

0 comments on commit d79d1e4

Please sign in to comment.