A platform for community discussion. Free, open, simple.
FIX: Only actually fetch initial report count in Rewind (#36940)
Martin Brennan9ae904b3ee7c
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rbindex 725f5406951..d018c0adad3 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb@@ -33,17 +33,26 @@ module DiscourseRewindmodel :for_user # see FetchReportsHelper#fetch_for_usermodel :year # see FetchReportsHelper#fetch_year- model :all_reports+ model :datemodel :reportprivate- def fetch_all_reports(for_user:, year:)- load_reports_from_cache(for_user.username, year)+ def fetch_date(params:, year:)+ Date.new(year).all_yearend- def fetch_report(all_reports:, params:)- all_reports[params.index]+ def fetch_report(params:, for_user:, year:, date:)+ report_class = FetchReports::REPORTS[params.index]+ return if !report_class++ report_name = report_class.name.demodulize+ report = load_single_report_from_cache(for_user.username, year, report_name)+ if !report+ report = report_class.call(date:, user: for_user)+ cache_single_report(for_user.username, year, report_name, report.as_json)+ end+ reportendendenddiff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rbindex fbaa647703f..31d986549b8 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb@@ -51,7 +51,6 @@ module DiscourseRewindmodel :for_user # see FetchReportsHelper#fetch_for_usermodel :year # see FetchReportsHelper#fetch_yearmodel :date- model :all_reportsmodel :reportsmodel :total_available@@ -61,28 +60,26 @@ module DiscourseRewindDate.new(year).all_yearend- def fetch_all_reports(date:, for_user:, year:)+ def fetch_reports(date:, for_user:, year:)reports = load_reports_from_cache(for_user.username, year)if !reportsreports =- REPORTS.filter_map do |report|- report.call(date:, user: for_user)- rescue StandardError- nil- end+ REPORTS+ .first(INITIAL_REPORT_COUNT)+ .filter_map do |report|+ report.call(date:, user: for_user)+ rescue StandardError+ nil+ endcache_reports(for_user.username, year, reports)endreportsend- def fetch_reports(all_reports:)- all_reports.first(INITIAL_REPORT_COUNT)- end-- def fetch_total_available(all_reports:)- all_reports.size+ def fetch_total_available+ REPORTS.sizeendendenddiff --git a/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb b/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rbindex b78080db794..85893abbb27 100644--- a/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb+++ b/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb@@ -4,23 +4,42 @@ module DiscourseRewindmodule FetchReportsHelperextend self- REWIND_REPORT_VERSION = "1"+ REWIND_REPORT_VERSION = "2"CACHE_DURATION = Rails.env.development? ? 10.seconds : 3.daysdef cache_key(username, year)"rewind:#{username}:#{year}:v#{REWIND_REPORT_VERSION}"end+ def single_report_cache_key(username, year, name)+ "rewind:#{username}:#{year}:#{name}:v#{REWIND_REPORT_VERSION}"+ end+def load_reports_from_cache(username, year)reports = Discourse.redis.get(cache_key(username, year))return nil if !reportsMultiJson.load(reports, symbolize_keys: true)end+ def load_single_report_from_cache(username, year, name)+ report = Discourse.redis.get(single_report_cache_key(username, year, name))+ return nil if !report+ MultiJson.load(report, symbolize_keys: true)+ end++ # NOTE: This only caches the first INITIAL_REPORT_COUNT reports.def cache_reports(username, year, reports)Discourse.redis.setex(cache_key(username, year), CACHE_DURATION, MultiJson.dump(reports))end+ def cache_single_report(username, year, name, report)+ Discourse.redis.setex(+ single_report_cache_key(username, year, name),+ CACHE_DURATION,+ MultiJson.dump(report),+ )+ end+def fetch_for_user(guardian:, params:)return guardian.user if params.for_user_username.blank?diff --git a/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb b/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rbindex 89ec10e8af9..9f735442432 100644--- a/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb+++ b/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb@@ -101,17 +101,6 @@ RSpec.describe DiscourseRewind::RewindsController docontext "when in valid month" dobefore { freeze_time DateTime.parse("2022-12-24") }- context "when reports are not cached" do- it "returns 404 with message" do- get "/rewinds/0.json"-- expect(response.status).to eq(404)- expect(response.parsed_body["errors"].first).to eq(- I18n.t("discourse_rewind.reports_not_cached"),- )- end- end-context "when reports are cached" dobefore { get "/rewinds.json" }diff --git a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb b/plugins/discourse-rewind/spec/services/fetch_report_spec.rbindex ce3cb1d7fde..f5a30e9e226 100644--- a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb+++ b/plugins/discourse-rewind/spec/services/fetch_report_spec.rb@@ -10,173 +10,266 @@ RSpec.describe(DiscourseRewind::FetchReport) dolet(:params) { { index: 0 } }let(:dependencies) { { guardian:, params: } }- context "when reports are cached" do- before do- SiteSetting.discourse_rewind_enabled = true- freeze_time DateTime.parse("2021-12-22")- reports = [- { type: "top_words", data: "report 1" },- { type: "reading_time", data: "report 2" },- { type: "writing_analysis", data: "report 3" },- ]- DiscourseRewind::FetchReportsHelper.cache_reports(current_user.username, 2021, reports)+ before { SiteSetting.discourse_rewind_enabled = true }++ context "when index is not provided" do+ let(:params) { {} }++ it { is_expected.to fail_a_contract }+ end++ context "when index is negative" do+ let(:params) { { index: -1 } }++ it { is_expected.to fail_a_contract }+ end++ context "when index is out of bounds" do+ before { freeze_time DateTime.parse("2021-12-22") }++ let(:params) { { index: 999 } }++ it { is_expected.to fail_to_find_a_model(:report) }+ end++ context "when in january" do+ before { freeze_time DateTime.parse("2021-01-22") }++ let(:params) { { index: 0 } }++ it "fetches reports from the previous year" do+ expect(result.year).to eq(2020)+ expect(result).to be_successend+ end++ context "when out of valid months" do+ before { freeze_time DateTime.parse("2021-02-22") }- context "when index is not provided" do- let(:params) { {} }+ it { is_expected.to fail_to_find_a_model(:year) }+ end- it { is_expected.to fail_a_contract }+ context "in development mode" do+ before do+ Rails.env.stubs(:development?).returns(true)+ freeze_time DateTime.parse("2021-06-22")end- context "when index is negative" do- let(:params) { { index: -1 } }+ let(:params) { { index: 0 } }- it { is_expected.to fail_a_contract }+ it "finds the year no matter what month" do+ expect(result.year).to eq(2021)+ expect(result).to be_successend+ end- context "when fetching a report by index" do- context "when index is 0" do- let(:params) { { index: 0 } }+ context "when a report is cached" do+ before { freeze_time DateTime.parse("2021-12-22") }- it "returns the first report" do- expect(result).to be_success- expect(result.report[:type]).to eq("top_words")- end- end+ context "when index is 0" do+ let(:params) { { index: 0 } }- context "when index is 1" do- let(:params) { { index: 1 } }+ before do+ cached_report = { data: "cached top words", identifier: "top-words" }+ DiscourseRewind::FetchReportsHelper.cache_single_report(+ current_user.username,+ 2021,+ "TopWords",+ cached_report,+ )+ end- it "returns the second report" do- expect(result).to be_success- expect(result.report[:type]).to eq("reading_time")- end+ it "returns the cached report" do+ allow(DiscourseRewind::Action::TopWords).to receive(:call)+ expect(result).to be_success+ expect(result.report[:identifier]).to eq("top-words")+ expect(result.report[:data]).to eq("cached top words")+ expect(DiscourseRewind::Action::TopWords).to_not have_received(:call)end+ end++ context "when index is 1" do+ let(:params) { { index: 1 } }- context "when index is out of bounds" do- let(:params) { { index: 10 } }+ before do+ cached_report = { data: "cached reading time", identifier: "reading-time" }+ DiscourseRewind::FetchReportsHelper.cache_single_report(+ current_user.username,+ 2021,+ "ReadingTime",+ cached_report,+ )+ end- it { is_expected.to fail_to_find_a_model(:report) }+ it "returns the cached report" do+ allow(DiscourseRewind::Action::ReadingTime).to receive(:call)+ expect(result).to be_success+ expect(result.report[:identifier]).to eq("reading-time")+ expect(DiscourseRewind::Action::ReadingTime).to_not have_received(:call)endend- context "when in january" do+ context "when index is beyond INITIAL_REPORT_COUNT" do+ let(:params) { { index: 3 } }+before do- freeze_time DateTime.parse("2021-01-22")- reports = [{ type: "top_words", data: "report 1" }]- DiscourseRewind::FetchReportsHelper.cache_reports(current_user.username, 2020, reports)+ cached_report = { data: "cached reactions", identifier: "reactions" }+ DiscourseRewind::FetchReportsHelper.cache_single_report(+ current_user.username,+ 2021,+ "Reactions",+ cached_report,+ )end- it "fetches reports from the previous year" do- expect(result.year).to eq(2020)+ it "returns the cached report even though it's beyond INITIAL_REPORT_COUNT" do+ allow(DiscourseRewind::Action::Reactions).to receive(:call)expect(result).to be_success+ expect(result.report[:identifier]).to eq("reactions")+ expect(DiscourseRewind::Action::Reactions).to_not have_received(:call)endend+ end- context "when out of valid months" do- before { freeze_time DateTime.parse("2021-02-22") }+ context "when a report is not cached" do+ before { freeze_time DateTime.parse("2021-12-22") }- it { is_expected.to fail_to_find_a_model(:year) }- end+ context "when index is 0" do+ let(:params) { { index: 0 } }- context "in development mode" do- before do- Rails.env.stubs(:development?).returns(true)- freeze_time DateTime.parse("2021-06-22")- reports = [{ type: "top_words", data: "report 1" }]- DiscourseRewind::FetchReportsHelper.cache_reports(current_user.username, 2021, reports)+ it "generates and caches the report" do+ allow(DiscourseRewind::Action::TopWords).to receive(:call).and_return(+ { data: "generated top words", identifier: "top-words" },+ )+ expect(result).to be_success+ expect(result.report[:identifier]).to eq("top-words")+ expect(result.report[:data]).to eq("generated top words")+ expect(DiscourseRewind::Action::TopWords).to have_received(:call)++ cached =+ DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(+ current_user.username,+ 2021,+ "TopWords",+ )+ expect(cached[:identifier]).to eq("top-words")end+ end- it "finds the year no matter what month" do- expect(result.year).to eq(2021)+ context "when index is beyond INITIAL_REPORT_COUNT" do+ let(:params) { { index: 3 } }++ it "generates and caches the report even though it's beyond INITIAL_REPORT_COUNT" do+ allow(DiscourseRewind::Action::Reactions).to receive(:call).and_return(+ { data: "generated reactions", identifier: "reactions" },+ )expect(result).to be_success+ expect(result.report[:identifier]).to eq("reactions")+ expect(DiscourseRewind::Action::Reactions).to have_received(:call)++ cached =+ DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(+ current_user.username,+ 2021,+ "Reactions",+ )+ expect(cached[:identifier]).to eq("reactions")endend+ end- context "with for_user_username parameter" do- fab!(:other_user, :user)- fab!(:admin, :admin)+ context "with for_user_username parameter" do+ fab!(:other_user, :user)+ fab!(:admin, :admin)- before do- freeze_time DateTime.parse("2021-12-22")- reports = [- { type: "top_words", data: "other user report 1" },- { type: "reading_time", data: "other user report 2" },- ]- DiscourseRewind::FetchReportsHelper.cache_reports(other_user.username, 2021, reports)- end+ before { freeze_time DateTime.parse("2021-12-22") }- context "when for_user_username is blank" do- let(:params) { { index: 0, for_user_username: "" } }+ context "when for_user_username is blank" do+ let(:params) { { index: 0, for_user_username: "" } }- it "uses the guardian user" do- expect(result).to be_success- expect(result.for_user).to eq(current_user)- end+ it "uses the guardian user" do+ expect(result).to be_success+ expect(result.for_user).to eq(current_user)end+ end++ context "when for_user_username is provided but user does not exist" do+ let(:params) { { index: 0, for_user_username: "nonexistent" } }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end- context "when for_user_username is provided but user does not exist" do- let(:params) { { index: 0, for_user_username: "nonexistent" } }+ context "when viewing own rewind" do+ let(:params) { { index: 0, for_user_username: current_user.username } }- it { is_expected.to fail_to_find_a_model(:for_user) }+ it "returns the user's report" do+ expect(result).to be_success+ expect(result.for_user).to eq(current_user)end+ end- context "when viewing own rewind" do- let(:params) { { index: 0, for_user_username: current_user.username } }+ context "when viewing another user's rewind" do+ context "when the other user has sharing enabled" do+ before { other_user.user_option.update!(discourse_rewind_share_publicly: true) }- it "returns the user's report" do+ let(:params) { { index: 0, for_user_username: other_user.username } }++ before do+ cached_report = { data: "other user report", identifier: "top-words" }+ DiscourseRewind::FetchReportsHelper.cache_single_report(+ other_user.username,+ 2021,+ "TopWords",+ cached_report,+ )+ end++ it "allows access to the report" doexpect(result).to be_success- expect(result.for_user).to eq(current_user)+ expect(result.for_user).to eq(other_user)+ expect(result.report[:identifier]).to eq("top-words")+ expect(result.report[:data]).to eq("other user report")+ end++ context "when the other user has hide_profile enabled" do+ before { other_user.user_option.update!(hide_profile: true) }++ it { is_expected.to fail_to_find_a_model(:for_user) }endend- context "when viewing another user's rewind" do- context "when the other user has sharing enabled" do- before { other_user.user_option.update!(discourse_rewind_share_publicly: true) }+ context "when the other user has sharing disabled" do+ before { other_user.user_option.update!(discourse_rewind_share_publicly: false) }+ context "when guardian is an admin" do+ let(:guardian) { Guardian.new(admin) }let(:params) { { index: 0, for_user_username: other_user.username } }+ before do+ cached_report = { data: "other user report", identifier: "top-words" }+ DiscourseRewind::FetchReportsHelper.cache_single_report(+ other_user.username,+ 2021,+ "TopWords",+ cached_report,+ )+ end+it "allows access to the report" doexpect(result).to be_successexpect(result.for_user).to eq(other_user)- expect(result.report[:type]).to eq("top_words")- expect(result.report[:data]).to eq("other user report 1")- end-- context "when the other user has hide_profile enabled" do- before { other_user.user_option.update!(hide_profile: true) }-- it { is_expected.to fail_to_find_a_model(:for_user) }+ expect(result.report[:identifier]).to eq("top-words")+ expect(result.report[:data]).to eq("other user report")endend- context "when the other user has sharing disabled" do- before { other_user.user_option.update!(discourse_rewind_share_publicly: false) }-- context "when guardian is an admin" do- let(:guardian) { Guardian.new(admin) }- let(:params) { { index: 0, for_user_username: other_user.username } }-- it "allows access to the report" do- expect(result).to be_success- expect(result.for_user).to eq(other_user)- expect(result.report[:type]).to eq("top_words")- expect(result.report[:data]).to eq("other user report 1")- end- end-- context "when guardian is not an admin" do- let(:params) { { index: 0, for_user_username: other_user.username } }+ context "when guardian is not an admin" do+ let(:params) { { index: 0, for_user_username: other_user.username } }- it { is_expected.to fail_to_find_a_model(:for_user) }- end+ it { is_expected.to fail_to_find_a_model(:for_user) }endendendend-- context "when reports are not cached" do- it { is_expected.to fail_to_find_a_model(:all_reports) }- endendenddiff --git a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rbindex ef14d67c80c..67647ffc8df 100644--- a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb+++ b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb@@ -49,7 +49,7 @@ RSpec.describe(DiscourseRewind::FetchReports) doit "returns the cached reports" doinitial_count = result.reports.length- expect(initial_count).to be > 0+ expect(initial_count).to eq(DiscourseRewind::FetchReports::INITIAL_REPORT_COUNT)allow(DiscourseRewind::Action::TopWords).to receive(:call)expect(result.reports.length).to eq(initial_count)@@ -61,9 +61,24 @@ RSpec.describe(DiscourseRewind::FetchReports) dobefore { freeze_time DateTime.parse("2021-01-22") }it "returns the reports" do- allow(DiscourseRewind::Action::TopWords).to receive(:call)- expect(result.reports.length).to be > 0- expect(DiscourseRewind::Action::TopWords).to have_received(:call)+ expect(result.reports.length).to eq(DiscourseRewind::FetchReports::INITIAL_REPORT_COUNT)+ end++ it "only actually calls the first INITIAL_REPORT_COUNT reports" do+ DiscourseRewind::Action::TopWords.expects(:call).once+ DiscourseRewind::Action::ReadingTime.expects(:call).once+ DiscourseRewind::Action::WritingAnalysis.expects(:call).once+ DiscourseRewind::Action::Reactions.expects(:call).never+ result+ end+ end++ context "when checking total_available" do+ before { freeze_time DateTime.parse("2021-12-22") }++ it "returns the total number of available reports" do+ expect(result.total_available).to eq(DiscourseRewind::FetchReports::REPORTS.size)+ expect(result.total_available).to be > result.reports.lengthendend