From a9cf00f75ec9ddbcd825f0e847105d8e6dbc5438 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Sun, 26 Mar 2023 10:31:49 +0200 Subject: [PATCH 1/2] metrics: add counter for created/destroyed users --- .rubocop.yml | 3 +++ app/models/user.rb | 6 ++++++ lib/retrospring/metrics.rb | 10 ++++++++++ spec/models/user_spec.rb | 39 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index acd3db74..ed74d1c9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -134,3 +134,6 @@ Style/EndlessMethod: Style/TrailingCommaInHashLiteral: EnforcedStyleForMultiline: consistent_comma + +Style/TrailingCommaInArguments: + EnforcedStyleForMultiline: consistent_comma diff --git a/app/models/user.rb b/app/models/user.rb index 92427a5d..2b2241e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/lib/retrospring/metrics.rb b/lib/retrospring/metrics.rb index 7c52dcb6..3c715e5c 100644 --- a/lib/retrospring/metrics.rb +++ b/lib/retrospring/metrics.rb @@ -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( diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2981df53..b3fe0836 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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] From 3508f846e7c598ccab4f0b088e212ff1456a7055 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Wed, 29 Mar 2023 08:03:13 +0200 Subject: [PATCH 2/2] pet the cyber canine 9000 --- lib/retrospring/metrics.rb | 26 +++++++++++++------------- spec/models/user_spec.rb | 6 +++--- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/retrospring/metrics.rb b/lib/retrospring/metrics.rb index 3c715e5c..9c5ea3bb 100644 --- a/lib/retrospring/metrics.rb +++ b/lib/retrospring/metrics.rb @@ -24,65 +24,65 @@ module Retrospring labels: [:version], preset_labels: { version: Retrospring::Version.to_s, - } + }, ).tap { _1.set 1 } QUESTIONS_ASKED = counter( :retrospring_questions_asked_total, docstring: "How many questions got asked", - labels: %i[anonymous followers generated] + labels: %i[anonymous followers generated], ) QUESTIONS_ANSWERED = counter( :retrospring_questions_answered_total, - docstring: "How many questions got answered" + docstring: "How many questions got answered", ) COMMENTS_CREATED = counter( :retrospring_comments_created_total, - docstring: "How many comments got created" + docstring: "How many comments got created", ) USERS_CREATED = counter( :retrospring_users_created_total, - docstring: "How many users got created" + docstring: "How many users got created", ) USERS_DESTROYED = counter( :retrospring_users_destroyed_total, - docstring: "How many users deleted their accounts" + docstring: "How many users deleted their accounts", ) # metrics from Sidekiq::Stats.new SIDEKIQ = { processed: gauge( :sidekiq_processed, - docstring: "Number of jobs processed by Sidekiq" + docstring: "Number of jobs processed by Sidekiq", ), failed: gauge( :sidekiq_failed, - docstring: "Number of jobs that failed" + docstring: "Number of jobs that failed", ), scheduled_size: gauge( :sidekiq_scheduled_jobs, - docstring: "Number of jobs that are enqueued" + docstring: "Number of jobs that are enqueued", ), retry_size: gauge( :sidekiq_retried_jobs, - docstring: "Number of jobs that are being retried" + docstring: "Number of jobs that are being retried", ), dead_size: gauge( :sidekiq_dead_jobs, - docstring: "Number of jobs that are dead" + docstring: "Number of jobs that are dead", ), processes_size: gauge( :sidekiq_processes, - docstring: "Number of active Sidekiq processes" + docstring: "Number of active Sidekiq processes", ), queue_enqueued: gauge( :sidekiq_queues_enqueued, docstring: "Number of enqueued jobs per queue", - labels: %i[queue] + labels: %i[queue], ), }.freeze end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b3fe0836..6a938a48 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -10,7 +10,7 @@ RSpec.describe User, type: :model do @user = User.new( screen_name: "FunnyMeme2004", password: "y_u_no_secure_password?", - email: "nice.meme@nsa.gov" + email: "nice.meme@nsa.gov", ) Profile.new(user: @user) end @@ -108,7 +108,7 @@ RSpec.describe User, type: :model do describe "email validation" do subject do - FactoryBot.build(:user, email: email).tap(&:validate).errors[:email] + FactoryBot.build(:user, email:).tap(&:validate).errors[:email] end shared_examples_for "valid email" do |example_email| @@ -255,7 +255,7 @@ RSpec.describe User, type: :model do describe "#cursored_timeline" do let(:last_id) { nil } - subject { me.cursored_timeline(last_id: last_id, size: 3) } + subject { me.cursored_timeline(last_id:, size: 3) } context "user answered nothing and is not following anyone" do include_examples "result is blank"