From ccd357f6794fcffbe9d01b7ed7019dabd6e13de9 Mon Sep 17 00:00:00 2001 From: Daniil Mironenko Date: Wed, 24 Jul 2024 11:27:30 +0300 Subject: [PATCH] Update metrics format (#54) * update format of engagement rate metric * update format of comments/likes ratio metrics * update code style * add public_suffix gem to dependencies * fix: update Gemfile.lock * fix: remove unnecessary dependency * update Gemfile.lock * transfer changes to migrations * update code style --- Gemfile.lock | 768 +++++++++--------- app/services/metrics_calculator.rb | 40 +- ...ame_engagement_score_to_engagement_rate.rb | 8 + ...40724080223_change_ratio_type_in_metric.rb | 8 + db/schema.rb | 12 +- 5 files changed, 429 insertions(+), 407 deletions(-) create mode 100644 db/migrate/20240724075401_rename_engagement_score_to_engagement_rate.rb create mode 100644 db/migrate/20240724080223_change_ratio_type_in_metric.rb diff --git a/Gemfile.lock b/Gemfile.lock index 7b0152f..fb0cc76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,380 +1,388 @@ -GEM - remote: https://rubygems.org/ - specs: - actioncable (7.1.3.4) - actionpack (= 7.1.3.4) - activesupport (= 7.1.3.4) - nio4r (~> 2.0) - websocket-driver (>= 0.6.1) - zeitwerk (~> 2.6) - actionmailbox (7.1.3.4) - actionpack (= 7.1.3.4) - activejob (= 7.1.3.4) - activerecord (= 7.1.3.4) - activestorage (= 7.1.3.4) - activesupport (= 7.1.3.4) - mail (>= 2.7.1) - net-imap - net-pop - net-smtp - actionmailer (7.1.3.4) - actionpack (= 7.1.3.4) - actionview (= 7.1.3.4) - activejob (= 7.1.3.4) - activesupport (= 7.1.3.4) - mail (~> 2.5, >= 2.5.4) - net-imap - net-pop - net-smtp - rails-dom-testing (~> 2.2) - actionpack (7.1.3.4) - actionview (= 7.1.3.4) - activesupport (= 7.1.3.4) - nokogiri (>= 1.8.5) - racc - rack (>= 2.2.4) - rack-session (>= 1.0.1) - rack-test (>= 0.6.3) - rails-dom-testing (~> 2.2) - rails-html-sanitizer (~> 1.6) - actiontext (7.1.3.4) - actionpack (= 7.1.3.4) - activerecord (= 7.1.3.4) - activestorage (= 7.1.3.4) - activesupport (= 7.1.3.4) - globalid (>= 0.6.0) - nokogiri (>= 1.8.5) - actionview (7.1.3.4) - activesupport (= 7.1.3.4) - builder (~> 3.1) - erubi (~> 1.11) - rails-dom-testing (~> 2.2) - rails-html-sanitizer (~> 1.6) - activejob (7.1.3.4) - activesupport (= 7.1.3.4) - globalid (>= 0.3.6) - activemodel (7.1.3.4) - activesupport (= 7.1.3.4) - activerecord (7.1.3.4) - activemodel (= 7.1.3.4) - activesupport (= 7.1.3.4) - timeout (>= 0.4.0) - activestorage (7.1.3.4) - actionpack (= 7.1.3.4) - activejob (= 7.1.3.4) - activerecord (= 7.1.3.4) - activesupport (= 7.1.3.4) - marcel (~> 1.0) - activesupport (7.1.3.4) - base64 - bigdecimal - concurrent-ruby (~> 1.0, >= 1.0.2) - connection_pool (>= 2.2.5) - drb - i18n (>= 1.6, < 2) - minitest (>= 5.1) - mutex_m - tzinfo (~> 2.0) - addressable (2.8.7) - public_suffix (>= 2.0.2, < 7.0) - administrate (0.20.1) - actionpack (>= 6.0, < 8.0) - actionview (>= 6.0, < 8.0) - activerecord (>= 6.0, < 8.0) - jquery-rails (~> 4.6.0) - kaminari (~> 1.2.2) - sassc-rails (~> 2.1) - selectize-rails (~> 0.6) - ast (2.4.2) - base64 (0.2.0) - bcrypt (3.1.20) - bigdecimal (3.1.8) - bindex (0.8.1) - bootsnap (1.18.3) - msgpack (~> 1.2) - browser (6.0.0) - builder (3.3.0) - capybara (3.40.0) - addressable - matrix - mini_mime (>= 0.1.3) - nokogiri (~> 1.11) - rack (>= 1.6.0) - rack-test (>= 0.6.3) - regexp_parser (>= 1.5, < 3.0) - xpath (~> 3.2) - concurrent-ruby (1.3.3) - connection_pool (2.4.1) - crass (1.0.6) - cssbundling-rails (1.4.0) - railties (>= 6.0.0) - csv (3.3.0) - date (3.3.4) - debug (1.9.2) - irb (~> 1.10) - reline (>= 0.3.8) - devise (4.9.4) - bcrypt (~> 3.0) - orm_adapter (~> 0.1) - railties (>= 4.1.0) - responders - warden (~> 1.2.3) - digest (3.1.1) - drb (2.2.1) - erubi (1.13.0) - faraday (2.10.0) - faraday-net_http (>= 2.0, < 3.2) - logger - faraday-net_http (3.1.0) - net-http - ffi (1.16.3) - ffi (1.16.3-x64-mingw-ucrt) - globalid (1.2.1) - activesupport (>= 6.1) - httparty (0.22.0) - csv - mini_mime (>= 1.0.0) - multi_xml (>= 0.5.2) - i18n (1.14.5) - concurrent-ruby (~> 1.0) - importmap-rails (2.0.1) - actionpack (>= 6.0.0) - activesupport (>= 6.0.0) - railties (>= 6.0.0) - io-console (0.7.2) - irb (1.14.0) - rdoc (>= 4.0.0) - reline (>= 0.4.2) - jbuilder (2.12.0) - actionview (>= 5.0.0) - activesupport (>= 5.0.0) - jquery-rails (4.6.0) - rails-dom-testing (>= 1, < 3) - railties (>= 4.2.0) - thor (>= 0.14, < 2.0) - json (2.7.2) - kaminari (1.2.2) - activesupport (>= 4.1.0) - kaminari-actionview (= 1.2.2) - kaminari-activerecord (= 1.2.2) - kaminari-core (= 1.2.2) - kaminari-actionview (1.2.2) - actionview - kaminari-core (= 1.2.2) - kaminari-activerecord (1.2.2) - activerecord - kaminari-core (= 1.2.2) - kaminari-core (1.2.2) - language_server-protocol (3.17.0.3) - logger (1.6.0) - loofah (2.22.0) - crass (~> 1.0.2) - nokogiri (>= 1.12.0) - mail (2.8.1) - mini_mime (>= 0.1.1) - net-imap - net-pop - net-smtp - marcel (1.0.4) - matrix (0.4.2) - mini_mime (1.1.5) - minitest (5.24.1) - msgpack (1.7.2) - multi_xml (0.7.1) - bigdecimal (~> 3.1) - mutex_m (0.2.0) - net-http (0.4.1) - uri - net-imap (0.4.14) - date - net-protocol - net-pop (0.1.2) - net-protocol - net-protocol (0.2.2) - timeout - net-smtp (0.5.0) - net-protocol - nio4r (2.7.3) - nokogiri (1.16.6-x64-mingw-ucrt) - racc (~> 1.4) - nokogiri (1.16.6-x86_64-linux) - racc (~> 1.4) - orm_adapter (0.5.0) - parallel (1.25.1) - parser (3.3.4.0) - ast (~> 2.4.1) - racc - pg (1.5.6) - pg (1.5.6-x64-mingw-ucrt) - pkce_challenge (1.0.0) - psych (5.1.2) - stringio - public_suffix (6.0.0) - puma (6.4.2) - nio4r (~> 2.0) - racc (1.8.0) - rack (3.1.7) - rack-session (2.0.0) - rack (>= 3.0.0) - rack-test (2.1.0) - rack (>= 1.3) - rackup (2.1.0) - rack (>= 3) - webrick (~> 1.8) - rails (7.1.3.4) - actioncable (= 7.1.3.4) - actionmailbox (= 7.1.3.4) - actionmailer (= 7.1.3.4) - actionpack (= 7.1.3.4) - actiontext (= 7.1.3.4) - actionview (= 7.1.3.4) - activejob (= 7.1.3.4) - activemodel (= 7.1.3.4) - activerecord (= 7.1.3.4) - activestorage (= 7.1.3.4) - activesupport (= 7.1.3.4) - bundler (>= 1.15.0) - railties (= 7.1.3.4) - rails-dom-testing (2.2.0) - activesupport (>= 5.0.0) - minitest - nokogiri (>= 1.6) - rails-html-sanitizer (1.6.0) - loofah (~> 2.21) - nokogiri (~> 1.14) - rails_performance (1.2.2) - browser - railties - redis - redis-namespace - railties (7.1.3.4) - actionpack (= 7.1.3.4) - activesupport (= 7.1.3.4) - irb - rackup (>= 1.0.0) - rake (>= 12.2) - thor (~> 1.0, >= 1.2.2) - zeitwerk (~> 2.6) - rainbow (3.1.1) - rake (13.2.1) - rdoc (6.7.0) - psych (>= 4.0.0) - redis (5.2.0) - redis-client (>= 0.22.0) - redis-client (0.22.2) - connection_pool - redis-namespace (1.11.0) - redis (>= 4) - regexp_parser (2.9.2) - reline (0.5.9) - io-console (~> 0.5) - responders (3.1.1) - actionpack (>= 5.2) - railties (>= 5.2) - rexml (3.3.2) - strscan - rubocop (1.65.0) - json (~> 2.3) - language_server-protocol (>= 3.17.0) - parallel (~> 1.10) - parser (>= 3.3.0.2) - rainbow (>= 2.2.2, < 4.0) - regexp_parser (>= 2.4, < 3.0) - rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.31.1, < 2.0) - ruby-progressbar (~> 1.7) - unicode-display_width (>= 2.4.0, < 3.0) - rubocop-ast (1.31.3) - parser (>= 3.3.1.0) - ruby-progressbar (1.13.0) - rubyzip (2.3.2) - sassc (2.4.0) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt - selectize-rails (0.12.6) - selenium-webdriver (4.23.0) - base64 (~> 0.2) - logger (~> 1.4) - rexml (~> 3.2, >= 3.2.5) - rubyzip (>= 1.2.2, < 3.0) - websocket (~> 1.0) - sprockets (4.2.1) - concurrent-ruby (~> 1.0) - rack (>= 2.2.4, < 4) - sprockets-rails (3.5.1) - actionpack (>= 6.1) - activesupport (>= 6.1) - sprockets (>= 3.0.0) - stimulus-rails (1.3.3) - railties (>= 6.0.0) - stringio (3.1.1) - strscan (3.1.0) - thor (1.3.1) - tilt (2.4.0) - timeout (0.4.1) - turbo-rails (2.0.6) - actionpack (>= 6.0.0) - activejob (>= 6.0.0) - railties (>= 6.0.0) - tzinfo (2.0.6) - concurrent-ruby (~> 1.0) - tzinfo-data (1.2024.1) - tzinfo (>= 1.0.0) - unicode-display_width (2.5.0) - uri (0.13.0) - warden (1.2.9) - rack (>= 2.0.9) - web-console (4.2.1) - actionview (>= 6.0.0) - activemodel (>= 6.0.0) - bindex (>= 0.4.0) - railties (>= 6.0.0) - webrick (1.8.1) - websocket (1.2.11) - websocket-driver (0.7.6) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) - xpath (3.2.0) - nokogiri (~> 1.8) - zeitwerk (2.6.16) - -PLATFORMS - x64-mingw-ucrt - x86_64-linux - -DEPENDENCIES - administrate - bootsnap - capybara - cssbundling-rails - debug - devise - digest - faraday - ffi (~> 1.16.3) - httparty - importmap-rails - jbuilder - pg (~> 1.1) - pkce_challenge - puma (>= 5.0) - rails (~> 7.1.3, >= 7.1.3.4) - rails_performance - rubocop (~> 1.65) - selenium-webdriver - sprockets-rails - stimulus-rails - turbo-rails - tzinfo-data - web-console - -RUBY VERSION - ruby 3.2.4p170 - -BUNDLED WITH - 2.4.19 +GEM + remote: https://rubygems.org/ + specs: + actioncable (7.1.3.4) + actionpack (= 7.1.3.4) + activesupport (= 7.1.3.4) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + zeitwerk (~> 2.6) + actionmailbox (7.1.3.4) + actionpack (= 7.1.3.4) + activejob (= 7.1.3.4) + activerecord (= 7.1.3.4) + activestorage (= 7.1.3.4) + activesupport (= 7.1.3.4) + mail (>= 2.7.1) + net-imap + net-pop + net-smtp + actionmailer (7.1.3.4) + actionpack (= 7.1.3.4) + actionview (= 7.1.3.4) + activejob (= 7.1.3.4) + activesupport (= 7.1.3.4) + mail (~> 2.5, >= 2.5.4) + net-imap + net-pop + net-smtp + rails-dom-testing (~> 2.2) + actionpack (7.1.3.4) + actionview (= 7.1.3.4) + activesupport (= 7.1.3.4) + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + actiontext (7.1.3.4) + actionpack (= 7.1.3.4) + activerecord (= 7.1.3.4) + activestorage (= 7.1.3.4) + activesupport (= 7.1.3.4) + globalid (>= 0.6.0) + nokogiri (>= 1.8.5) + actionview (7.1.3.4) + activesupport (= 7.1.3.4) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activejob (7.1.3.4) + activesupport (= 7.1.3.4) + globalid (>= 0.3.6) + activemodel (7.1.3.4) + activesupport (= 7.1.3.4) + activerecord (7.1.3.4) + activemodel (= 7.1.3.4) + activesupport (= 7.1.3.4) + timeout (>= 0.4.0) + activestorage (7.1.3.4) + actionpack (= 7.1.3.4) + activejob (= 7.1.3.4) + activerecord (= 7.1.3.4) + activesupport (= 7.1.3.4) + marcel (~> 1.0) + activesupport (7.1.3.4) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) + administrate (0.20.1) + actionpack (>= 6.0, < 8.0) + actionview (>= 6.0, < 8.0) + activerecord (>= 6.0, < 8.0) + jquery-rails (~> 4.6.0) + kaminari (~> 1.2.2) + sassc-rails (~> 2.1) + selectize-rails (~> 0.6) + ast (2.4.2) + base64 (0.2.0) + bcrypt (3.1.20) + bigdecimal (3.1.8) + bindex (0.8.1) + bootsnap (1.18.3) + msgpack (~> 1.2) + browser (6.0.0) + builder (3.3.0) + capybara (3.40.0) + addressable + matrix + mini_mime (>= 0.1.3) + nokogiri (~> 1.11) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) + concurrent-ruby (1.3.3) + connection_pool (2.4.1) + crass (1.0.6) + cssbundling-rails (1.4.0) + railties (>= 6.0.0) + csv (3.3.0) + date (3.3.4) + debug (1.9.2) + irb (~> 1.10) + reline (>= 0.3.8) + devise (4.9.4) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) + digest (3.1.1) + drb (2.2.1) + erubi (1.13.0) + faraday (2.10.0) + faraday-net_http (>= 2.0, < 3.2) + logger + faraday-net_http (3.1.0) + net-http + ffi (1.16.3) + globalid (1.2.1) + activesupport (>= 6.1) + httparty (0.22.0) + csv + mini_mime (>= 1.0.0) + multi_xml (>= 0.5.2) + i18n (1.14.5) + concurrent-ruby (~> 1.0) + importmap-rails (2.0.1) + actionpack (>= 6.0.0) + activesupport (>= 6.0.0) + railties (>= 6.0.0) + io-console (0.7.2) + irb (1.14.0) + rdoc (>= 4.0.0) + reline (>= 0.4.2) + jbuilder (2.12.0) + actionview (>= 5.0.0) + activesupport (>= 5.0.0) + jquery-rails (4.6.0) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + json (2.7.2) + kaminari (1.2.2) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.2.2) + kaminari-activerecord (= 1.2.2) + kaminari-core (= 1.2.2) + kaminari-actionview (1.2.2) + actionview + kaminari-core (= 1.2.2) + kaminari-activerecord (1.2.2) + activerecord + kaminari-core (= 1.2.2) + kaminari-core (1.2.2) + language_server-protocol (3.17.0.3) + logger (1.6.0) + loofah (2.22.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.4) + matrix (0.4.2) + mini_mime (1.1.5) + minitest (5.24.1) + msgpack (1.7.2) + multi_xml (0.7.1) + bigdecimal (~> 3.1) + mutex_m (0.2.0) + net-http (0.4.1) + uri + net-imap (0.4.14) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.5.0) + net-protocol + nio4r (2.7.3) + nokogiri (1.16.6-aarch64-linux) + racc (~> 1.4) + nokogiri (1.16.6-arm-linux) + racc (~> 1.4) + nokogiri (1.16.6-arm64-darwin) + racc (~> 1.4) + nokogiri (1.16.6-x86-linux) + racc (~> 1.4) + nokogiri (1.16.6-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.16.6-x86_64-linux) + racc (~> 1.4) + orm_adapter (0.5.0) + parallel (1.25.1) + parser (3.3.4.0) + ast (~> 2.4.1) + racc + pg (1.5.6) + pkce_challenge (1.0.0) + psych (5.1.2) + stringio + public_suffix (6.0.1) + puma (6.4.2) + nio4r (~> 2.0) + racc (1.8.0) + rack (3.1.7) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) + rails (7.1.3.4) + actioncable (= 7.1.3.4) + actionmailbox (= 7.1.3.4) + actionmailer (= 7.1.3.4) + actionpack (= 7.1.3.4) + actiontext (= 7.1.3.4) + actionview (= 7.1.3.4) + activejob (= 7.1.3.4) + activemodel (= 7.1.3.4) + activerecord (= 7.1.3.4) + activestorage (= 7.1.3.4) + activesupport (= 7.1.3.4) + bundler (>= 1.15.0) + railties (= 7.1.3.4) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + rails_performance (1.2.2) + browser + railties + redis + redis-namespace + railties (7.1.3.4) + actionpack (= 7.1.3.4) + activesupport (= 7.1.3.4) + irb + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) + rainbow (3.1.1) + rake (13.2.1) + rdoc (6.7.0) + psych (>= 4.0.0) + redis (5.2.0) + redis-client (>= 0.22.0) + redis-client (0.22.2) + connection_pool + redis-namespace (1.11.0) + redis (>= 4) + regexp_parser (2.9.2) + reline (0.5.9) + io-console (~> 0.5) + responders (3.1.1) + actionpack (>= 5.2) + railties (>= 5.2) + rexml (3.3.2) + strscan + rubocop (1.65.0) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.4, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + ruby-progressbar (1.13.0) + rubyzip (2.3.2) + sassc (2.4.0) + ffi (~> 1.9) + sassc-rails (2.1.2) + railties (>= 4.0.0) + sassc (>= 2.0) + sprockets (> 3.0) + sprockets-rails + tilt + selectize-rails (0.12.6) + selenium-webdriver (4.23.0) + base64 (~> 0.2) + logger (~> 1.4) + rexml (~> 3.2, >= 3.2.5) + rubyzip (>= 1.2.2, < 3.0) + websocket (~> 1.0) + sprockets (4.2.1) + concurrent-ruby (~> 1.0) + rack (>= 2.2.4, < 4) + sprockets-rails (3.5.1) + actionpack (>= 6.1) + activesupport (>= 6.1) + sprockets (>= 3.0.0) + stimulus-rails (1.3.3) + railties (>= 6.0.0) + stringio (3.1.1) + strscan (3.1.0) + thor (1.3.1) + tilt (2.4.0) + timeout (0.4.1) + turbo-rails (2.0.6) + actionpack (>= 6.0.0) + activejob (>= 6.0.0) + railties (>= 6.0.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.5.0) + uri (0.13.0) + warden (1.2.9) + rack (>= 2.0.9) + web-console (4.2.1) + actionview (>= 6.0.0) + activemodel (>= 6.0.0) + bindex (>= 0.4.0) + railties (>= 6.0.0) + webrick (1.8.1) + websocket (1.2.11) + websocket-driver (0.7.6) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + xpath (3.2.0) + nokogiri (~> 1.8) + zeitwerk (2.6.16) + +PLATFORMS + aarch64-linux + arm-linux + arm64-darwin + x86-linux + x86_64-darwin + x86_64-linux + +DEPENDENCIES + administrate + bootsnap + capybara + cssbundling-rails + debug + devise + digest + faraday + ffi (~> 1.16.3) + httparty + importmap-rails + jbuilder + pg (~> 1.1) + pkce_challenge + puma (>= 5.0) + rails (~> 7.1.3, >= 7.1.3.4) + rails_performance + rubocop (~> 1.65) + selenium-webdriver + sprockets-rails + stimulus-rails + turbo-rails + tzinfo-data + web-console + +RUBY VERSION + ruby 3.2.4p170 + +BUNDLED WITH + 2.5.16 diff --git a/app/services/metrics_calculator.rb b/app/services/metrics_calculator.rb index 0af8964..d782c39 100644 --- a/app/services/metrics_calculator.rb +++ b/app/services/metrics_calculator.rb @@ -19,7 +19,7 @@ def call(user) metric.comments_likes_ratio = comments_likes_ratio(user) metric.target_comments_likes_ratio = target_comments_likes_ratio(user) - metric.average_engagement_score = average_engagement_score(user) + metric.average_engagement_rate = average_engagement_rate(user) metric.audience_score = audience_score(user) metric.save @@ -71,16 +71,14 @@ def target_comments(user, target_comments_ratio: 0.1) end # Computes ratio of total comments to total likes - # in the form "comments:likes" def comments_likes_ratio(user) - common_divisor = user.total_comments.gcd(user.total_likes) - return '0:0' if common_divisor.zero? + total_activity = user.total_comments + user.total_likes + return 0 if total_activity.zero? - "#{user.total_comments / common_divisor}:#{user.total_likes / common_divisor}" + (user.total_comments.to_f / total_activity * 100).round end # Computes target ratio of total comments to total likes - # in the form "comments:likes" # Target number - theoretically possible with the current # number of followers def target_comments_likes_ratio( @@ -88,34 +86,34 @@ def target_comments_likes_ratio( target_likes_ratio: 0.3, target_comments_ratio: 0.1 ) - target_comments = (user.total_comments * target_comments_ratio).round - target_likes = (user.total_likes * target_likes_ratio).round + target_total_comments = (user.total_comments * target_comments_ratio).round + target_total_likes = (user.total_likes * target_likes_ratio).round + target_total_activity = target_total_comments + target_total_likes + return 0 if target_total_activity.zero? - common_divisor = target_comments.gcd(target_likes) - return '0:0' if common_divisor.zero? - - "#{target_comments / common_divisor}:#{target_likes / common_divisor}" + (target_total_comments.to_f / target_total_activity * 100).round end - # Calculates engagement score for a single post - # Engagement score - average activity shown by each user - def engagement_score(post, engagement_score_factor: 1000) + # Calculates engagement rate for a single post + # Engagement rate - average activity shown by each user + def engagement_rate(post) return 0 if post.user.friends.empty? - ((post.likes.size + post.comments.size).to_f / post.user.friends.size * engagement_score_factor).round + engagement_rate = ((post.likes.size + post.comments.size).to_f / post.user.friends.size * 100).round + [engagement_rate, 100].min end - # Computes average engagement score of posts - def average_engagement_score(user) + # Computes average engagement rate of posts + def average_engagement_rate(user) return 0 if user.posts.empty? user.posts.each do |post| - post.engagement_score = engagement_score(post) + post.engagement_rate = engagement_rate(post) post.save end - total_engagement_score = user.posts.sum(&:engagement_score) - (total_engagement_score / user.posts.size).round + total_engagement_rate = user.posts.sum(&:engagement_rate) + (total_engagement_rate / user.posts.size).round end # Computes audience score based on the ratio of active friends diff --git a/db/migrate/20240724075401_rename_engagement_score_to_engagement_rate.rb b/db/migrate/20240724075401_rename_engagement_score_to_engagement_rate.rb new file mode 100644 index 0000000..7dba948 --- /dev/null +++ b/db/migrate/20240724075401_rename_engagement_score_to_engagement_rate.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class RenameEngagementScoreToEngagementRate < ActiveRecord::Migration[7.1] + def change + rename_column :metrics, :average_engagement_score, :average_engagement_rate + rename_column :posts, :engagement_score, :engagement_rate + end +end diff --git a/db/migrate/20240724080223_change_ratio_type_in_metric.rb b/db/migrate/20240724080223_change_ratio_type_in_metric.rb new file mode 100644 index 0000000..a2146f3 --- /dev/null +++ b/db/migrate/20240724080223_change_ratio_type_in_metric.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class ChangeRatioTypeInMetric < ActiveRecord::Migration[7.1] + def change + change_column :metrics, :comments_likes_ratio, :integer, using: 'comments_likes_ratio::integer' + change_column :metrics, :target_comments_likes_ratio, :integer, using: 'target_comments_likes_ratio::integer' + end +end diff --git a/db/schema.rb b/db/schema.rb index 8555daa..e521a21 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,7 +12,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 20_240_719_150_646) do +ActiveRecord::Schema[7.1].define(version: 20_240_724_080_223) do # These are extensions that must be enabled in order to support this database enable_extension 'plpgsql' @@ -29,12 +29,12 @@ create_table 'metrics', force: :cascade do |t| t.integer 'average_likes' t.integer 'target_likes' - t.integer 'average_cosmments' + t.integer 'average_comments' t.integer 'target_comments' - t.string 'comments_likes_ratio' - t.string 'target_comments_likes_ratio' + t.integer 'comments_likes_ratio' + t.integer 'target_comments_likes_ratio' t.decimal 'audience_score' - t.integer 'average_engagement_score' + t.integer 'average_engagement_rate' t.bigint 'user_id', null: false t.datetime 'created_at', null: false t.datetime 'updated_at', null: false @@ -47,7 +47,7 @@ t.text 'image_url' t.integer 'likes', array: true t.integer 'comments', array: true - t.integer 'engagement_score' + t.integer 'engagement_rate' t.bigint 'user_id', null: false t.datetime 'created_at', null: false t.datetime 'updated_at', null: false