forked from cybrespace/mastodon
Add more instance stats APIs (#6125)
* 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
This commit is contained in:
parent
511c6f9625
commit
38fc1b498d
|
@ -17,6 +17,8 @@ module Admin
|
||||||
bootstrap_timeline_accounts
|
bootstrap_timeline_accounts
|
||||||
thumbnail
|
thumbnail
|
||||||
min_invite_role
|
min_invite_role
|
||||||
|
activity_api_enabled
|
||||||
|
peers_api_enabled
|
||||||
).freeze
|
).freeze
|
||||||
|
|
||||||
BOOLEAN_SETTINGS = %w(
|
BOOLEAN_SETTINGS = %w(
|
||||||
|
@ -24,6 +26,8 @@ module Admin
|
||||||
open_deletion
|
open_deletion
|
||||||
timeline_preview
|
timeline_preview
|
||||||
show_staff_badge
|
show_staff_badge
|
||||||
|
activity_api_enabled
|
||||||
|
peers_api_enabled
|
||||||
).freeze
|
).freeze
|
||||||
|
|
||||||
UPLOAD_SETTINGS = %w(
|
UPLOAD_SETTINGS = %w(
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -121,4 +121,13 @@ class ApplicationController < ActionController::Base
|
||||||
end
|
end
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -2,10 +2,4 @@
|
||||||
|
|
||||||
class Auth::ConfirmationsController < Devise::ConfirmationsController
|
class Auth::ConfirmationsController < Devise::ConfirmationsController
|
||||||
layout 'auth'
|
layout 'auth'
|
||||||
|
|
||||||
def show
|
|
||||||
super do |user|
|
|
||||||
BootstrapTimelineWorker.perform_async(user.account_id) if user.errors.empty?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,6 +17,7 @@ module UserTrackingConcern
|
||||||
|
|
||||||
# Mark as signed-in today
|
# Mark as signed-in today
|
||||||
current_user.update_tracked_fields!(request)
|
current_user.update_tracked_fields!(request)
|
||||||
|
ActivityTracker.record('activity:logins', current_user.id)
|
||||||
|
|
||||||
# Regenerate feed if needed
|
# Regenerate feed if needed
|
||||||
regenerate_feed! if user_needs_feed_update?
|
regenerate_feed! if user_needs_feed_update?
|
||||||
|
|
|
@ -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
|
|
@ -30,6 +30,10 @@ class Form::AdminSettings
|
||||||
:bootstrap_timeline_accounts=,
|
:bootstrap_timeline_accounts=,
|
||||||
:min_invite_role,
|
:min_invite_role,
|
||||||
:min_invite_role=,
|
:min_invite_role=,
|
||||||
|
:activity_api_enabled,
|
||||||
|
:activity_api_enabled=,
|
||||||
|
:peers_api_enabled,
|
||||||
|
:peers_api_enabled=,
|
||||||
to: Setting
|
to: Setting
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
@ -135,6 +135,7 @@ class Status < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
after_create_commit :store_uri, if: :local?
|
after_create_commit :store_uri, if: :local?
|
||||||
|
after_create_commit :update_statistics, if: :local?
|
||||||
|
|
||||||
around_create Mastodon::Snowflake::Callbacks
|
around_create Mastodon::Snowflake::Callbacks
|
||||||
|
|
||||||
|
@ -308,4 +309,9 @@ class Status < ApplicationRecord
|
||||||
def set_local
|
def set_local
|
||||||
self.local = account.local?
|
self.local = account.local?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_statistics
|
||||||
|
return unless public_visibility? || unlisted_visibility?
|
||||||
|
ActivityTracker.increment('activity:statuses:local')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -122,9 +122,19 @@ class User < ApplicationRecord
|
||||||
update!(disabled: false)
|
update!(disabled: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def confirm
|
||||||
|
return if confirmed?
|
||||||
|
|
||||||
|
super
|
||||||
|
update_statistics!
|
||||||
|
end
|
||||||
|
|
||||||
def confirm!
|
def confirm!
|
||||||
|
return if confirmed?
|
||||||
|
|
||||||
skip_confirmation!
|
skip_confirmation!
|
||||||
save!
|
save!
|
||||||
|
update_statistics!
|
||||||
end
|
end
|
||||||
|
|
||||||
def promote!
|
def promote!
|
||||||
|
@ -202,4 +212,9 @@ class User < ApplicationRecord
|
||||||
def sanitize_languages
|
def sanitize_languages
|
||||||
filtered_languages.reject!(&:blank?)
|
filtered_languages.reject!(&:blank?)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_statistics!
|
||||||
|
BootstrapTimelineWorker.perform_async(account_id)
|
||||||
|
ActivityTracker.increment('activity:accounts:local')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -46,5 +46,13 @@
|
||||||
.fields-group
|
.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')
|
= 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
|
.actions
|
||||||
= f.button :button, t('generic.save_changes'), type: :submit
|
= f.button :button, t('generic.save_changes'), type: :submit
|
||||||
|
|
|
@ -265,12 +265,18 @@ en:
|
||||||
unresolved: Unresolved
|
unresolved: Unresolved
|
||||||
view: View
|
view: View
|
||||||
settings:
|
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:
|
bootstrap_timeline_accounts:
|
||||||
desc_html: Separate multiple usernames by comma. Only local and unlocked accounts will work. Default when empty is all local admins.
|
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
|
title: Default follows for new users
|
||||||
contact_information:
|
contact_information:
|
||||||
email: Business e-mail
|
email: Business e-mail
|
||||||
username: Contact username
|
username: Contact username
|
||||||
|
peers_api_enabled:
|
||||||
|
desc_html: Domain names this instance has encountered in the fediverse
|
||||||
|
title: Publish list of discovered instances
|
||||||
registrations:
|
registrations:
|
||||||
closed_message:
|
closed_message:
|
||||||
desc_html: Displayed on frontpage when registrations are closed. You can use HTML tags
|
desc_html: Displayed on frontpage when registrations are closed. You can use HTML tags
|
||||||
|
|
|
@ -241,7 +241,11 @@ Rails.application.routes.draw do
|
||||||
|
|
||||||
resources :apps, only: [:create]
|
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]
|
resource :domain_blocks, only: [:show, :create, :destroy]
|
||||||
|
|
||||||
resources :follow_requests, only: [:index] do
|
resources :follow_requests, only: [:index] do
|
||||||
|
|
|
@ -47,7 +47,8 @@ defaults: &defaults
|
||||||
- webmaster
|
- webmaster
|
||||||
- administrator
|
- administrator
|
||||||
bootstrap_timeline_accounts: ''
|
bootstrap_timeline_accounts: ''
|
||||||
|
activity_api_enabled: true
|
||||||
|
peers_api_enabled: true
|
||||||
development:
|
development:
|
||||||
<<: *defaults
|
<<: *defaults
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue