metrics: add counter for created/destroyed users

This commit is contained in:
Georg Gadinger 2023-03-26 10:31:49 +02:00
parent 429c82bd3c
commit a9cf00f75e
4 changed files with 58 additions and 0 deletions

View File

@ -134,3 +134,6 @@ Style/EndlessMethod:
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: consistent_comma
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: consistent_comma

View File

@ -85,8 +85,14 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength
end
end
after_destroy do
Retrospring::Metrics::USERS_DESTROYED.increment
end
after_create do
Profile.create(user_id: id) if Profile.where(user_id: id).count.zero?
Retrospring::Metrics::USERS_CREATED.increment
end
# use the screen name as parameter for url helpers

View File

@ -43,6 +43,16 @@ module Retrospring
docstring: "How many comments got created"
)
USERS_CREATED = counter(
:retrospring_users_created_total,
docstring: "How many users got created"
)
USERS_DESTROYED = counter(
:retrospring_users_destroyed_total,
docstring: "How many users deleted their accounts"
)
# metrics from Sidekiq::Stats.new
SIDEKIQ = {
processed: gauge(

View File

@ -33,6 +33,45 @@ RSpec.describe User, type: :model do
end
end
describe "callbacks" do
describe "before_destroy" do
it "marks reports about this user as deleted" do
other_user = FactoryBot.create(:user)
other_user.report me, "va tutto benissimo"
expect { me.destroy }
.to change { Reports::User.find_by(target_id: me.id).deleted? }
.from(false)
.to(true)
end
end
describe "after_destroy" do
it "increments the users_destroyed metric" do
expect { me.destroy }.to change { Retrospring::Metrics::USERS_DESTROYED.values.values.sum }.by(1)
end
end
describe "after_create" do
subject :user do
User.create!(
screen_name: "konqi",
email: "konqi@example.rrerr.net",
password: "dragonsRQt5",
)
end
it "creates a profile for the user" do
expect { user }.to change { Profile.count }.by(1)
expect(Profile.find_by(user:).user).to eq(user)
end
it "increments the users_created metric" do
expect { user }.to change { Retrospring::Metrics::USERS_CREATED.values.values.sum }.by(1)
end
end
end
describe "custom sharing url validation" do
subject do
FactoryBot.build(:user, sharing_custom_url: url).tap(&:validate).errors[:sharing_custom_url]