diff --git a/app/controllers/historical_trends/base_controller.rb b/app/controllers/historical_trends/base_controller.rb index a88844a906..d460ba686c 100644 --- a/app/controllers/historical_trends/base_controller.rb +++ b/app/controllers/historical_trends/base_controller.rb @@ -1,32 +1,5 @@ class HistoricalTrends::BaseController < ApplicationController - def series(type) - items = [] - - current_organization.items.sort.each do |item| - next if item.line_items.where(itemizable_type: type, item: item).blank? - - dates = Hash.new - - (1..Time.zone.today.month).each do |month| - dates[month] = 0 - end - - total_items(item.line_items, type).each do |line_item| - month = line_item.dig(0).to_date.month - dates[month] = line_item.dig(1) - end - - items << { name: item.name, data: dates.values } - end - - items.sort_by { |hsh| hsh[:name] } - end - - private - - def total_items(line_items, type) - line_items.where(itemizable_type: type) - .group_by_month(:created_at) - .sum(:quantity) + def cached_series(type) + Rails.cache.fetch("#{current_organization.short_name}-historical-#{type}-data") { HistoricalTrendService.new(current_organization.id, type).series } end end diff --git a/app/controllers/historical_trends/distributions_controller.rb b/app/controllers/historical_trends/distributions_controller.rb index d626092328..e13a6ede62 100644 --- a/app/controllers/historical_trends/distributions_controller.rb +++ b/app/controllers/historical_trends/distributions_controller.rb @@ -1,6 +1,6 @@ class HistoricalTrends::DistributionsController < HistoricalTrends::BaseController def index - @series = series('Distribution') + @series = cached_series('Distribution') @title = 'Monthly Distributions' end end diff --git a/app/controllers/historical_trends/donations_controller.rb b/app/controllers/historical_trends/donations_controller.rb index 2d3608b95a..92a3f5422e 100644 --- a/app/controllers/historical_trends/donations_controller.rb +++ b/app/controllers/historical_trends/donations_controller.rb @@ -1,6 +1,6 @@ class HistoricalTrends::DonationsController < HistoricalTrends::BaseController def index - @series = series('Donation') + @series = cached_series('Donation') @title = 'Monthly Donations' end end diff --git a/app/controllers/historical_trends/purchases_controller.rb b/app/controllers/historical_trends/purchases_controller.rb index ee5031fe56..1f5859b2af 100644 --- a/app/controllers/historical_trends/purchases_controller.rb +++ b/app/controllers/historical_trends/purchases_controller.rb @@ -1,6 +1,6 @@ class HistoricalTrends::PurchasesController < HistoricalTrends::BaseController def index - @series = series('Purchase') + @series = cached_series('Purchase') @title = "Monthly Purchases" end end diff --git a/app/helpers/historical_trends_helper.rb b/app/helpers/historical_trends_helper.rb new file mode 100644 index 0000000000..29091194c5 --- /dev/null +++ b/app/helpers/historical_trends_helper.rb @@ -0,0 +1,21 @@ +module HistoricalTrendsHelper + MONTHS = [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" + ] + + def last_12_months + current_month = Time.zone.now.month + MONTHS.rotate(current_month) + end +end diff --git a/app/jobs/historical_data_cache_job.rb b/app/jobs/historical_data_cache_job.rb new file mode 100644 index 0000000000..5c1f71f4f0 --- /dev/null +++ b/app/jobs/historical_data_cache_job.rb @@ -0,0 +1,7 @@ +class HistoricalDataCacheJob < ApplicationJob + def perform(org_id:, type:) + organization = Organization.find_by(id: org_id) + + Rails.cache.write("#{organization.short_name}-historical-#{type}-data", HistoricalTrendService.new(organization.id, type).series) + end +end diff --git a/app/services/historical_trend_service.rb b/app/services/historical_trend_service.rb new file mode 100644 index 0000000000..c68c2590a5 --- /dev/null +++ b/app/services/historical_trend_service.rb @@ -0,0 +1,36 @@ +class HistoricalTrendService + def initialize(organization_id, type) + @organization = Organization.find(organization_id) + @type = type + end + + def series + items = [] + + @organization.items.active.sort.each do |item| + next if item.line_items.where(itemizable_type: @type, item: item).blank? + + month_offset = [*1..12].rotate(12 - Time.zone.today.month) + + dates = (1..12).index_with { |i| 0 } + + total_items(item.line_items, @type).each do |line_item| + month = line_item.dig(0).to_date.month + dates[(month_offset.index(month) + 1)] = line_item.dig(1) + end + + items << {name: item.name, data: dates.values, visible: false} + end + + items.sort_by { |hsh| hsh[:name] } + end + + private + + def total_items(line_items, type) + line_items.where(created_at: 1.year.ago.beginning_of_month..Time.current) + .where(itemizable_type: type) + .group_by_month(:created_at) + .sum(:quantity) + end +end diff --git a/app/views/historical_trends/distributions/index.html.erb b/app/views/historical_trends/distributions/index.html.erb index da97f42480..50cb5ce5fc 100644 --- a/app/views/historical_trends/distributions/index.html.erb +++ b/app/views/historical_trends/distributions/index.html.erb @@ -8,23 +8,10 @@ text: @title }, subtitle: { - text: "Source: humanessentials.app" + text: "Cached Data, may be up to 24 hours old" }, xAxis: { - categories: [ - 'Jan', - 'Feb', - 'Mar', - 'Apr', - 'May', - 'Jun', - 'Jul', - 'Aug', - 'Sep', - 'Oct', - 'Nov', - 'Dec' - ], + categories: last_12_months, crosshair: true }, yAxis: { diff --git a/app/views/historical_trends/donations/index.html.erb b/app/views/historical_trends/donations/index.html.erb index 285ef96852..0fda67877a 100644 --- a/app/views/historical_trends/donations/index.html.erb +++ b/app/views/historical_trends/donations/index.html.erb @@ -8,23 +8,10 @@ text: @title }, subtitle: { - text: "Source: humanessentials.app" + text: "Cached Data, may be up to 24 hours old" }, xAxis: { - categories: [ - 'Jan', - 'Feb', - 'Mar', - 'Apr', - 'May', - 'Jun', - 'Jul', - 'Aug', - 'Sep', - 'Oct', - 'Nov', - 'Dec' - ], + categories: last_12_months, crosshair: true }, yAxis: { diff --git a/app/views/historical_trends/purchases/index.html.erb b/app/views/historical_trends/purchases/index.html.erb index 1cafad158c..879334e09b 100644 --- a/app/views/historical_trends/purchases/index.html.erb +++ b/app/views/historical_trends/purchases/index.html.erb @@ -8,23 +8,10 @@ text: @title }, subtitle: { - text: "Source: humanessentials.app" + text: "Cached Data, may be up to 24 hours old" }, xAxis: { - categories: [ - 'Jan', - 'Feb', - 'Mar', - 'Apr', - 'May', - 'Jun', - 'Jul', - 'Aug', - 'Sep', - 'Oct', - 'Nov', - 'Dec' - ], + categories: last_12_months, crosshair: true }, yAxis: { diff --git a/app/views/shared/_highcharts.html.erb b/app/views/shared/_highcharts.html.erb index 92989c0f79..fa93289bb6 100644 --- a/app/views/shared/_highcharts.html.erb +++ b/app/views/shared/_highcharts.html.erb @@ -1,7 +1,7 @@