Skip to content

Commit

Permalink
feat: add cache for daily statistic data (#1489)
Browse files Browse the repository at this point in the history
* feat: add cache for daily statistic data

Signed-off-by: Miles Zhang <[email protected]>

* test: fix test

Signed-off-by: Miles Zhang <[email protected]>

---------

Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Nov 1, 2023
1 parent 70f3d61 commit 896fa7d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion app/controllers/api/v1/daily_statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class DailyStatisticsController < ApplicationController
def show
daily_statistics = DailyStatistic.order(created_at_unixtimestamp: :asc).valid_indicators

render json: rendered_json(daily_statistics)
if stale?(daily_statistics, public: true)
render json: rendered_json(daily_statistics)
end
end

private
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/api/v1/monetary_data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class MonetaryDataController < ApplicationController
before_action :validate_query_params, only: :show

def show
expires_in 1.hour, public: true, stale_while_revalidate: 10.minutes, stale_if_error: 1.hour

monetary_data = MonetaryData.new

render json: MonetaryDataSerializer.new(monetary_data, params: { indicator: params[:id] })
Expand Down
2 changes: 1 addition & 1 deletion app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class DailyStatistic < ApplicationRecord

attr_accessor :from_scratch

scope :valid_indicators, -> { select(VALID_INDICATORS - %w(burnt liquidity created_at updated_at) + %w(id)) }
scope :valid_indicators, -> { select(VALID_INDICATORS - %w(burnt liquidity created_at) + %w(id updated_at)) }
scope :recent, -> { order("created_at_unixtimestamp desc nulls last") }
scope :recent_year, -> {
where("created_at_unixtimestamp >= ? and created_at_unixtimestamp < ?", Time.current.beginning_of_year.to_i, Time.current.to_i)
Expand Down
2 changes: 1 addition & 1 deletion test/models/daily_statistic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class DailyStatisticTest < ActiveSupport::TestCase
test "valid_indicators should only return valid indicators" do
create(:daily_statistic)
attrs = DailyStatistic.valid_indicators.first.attribute_names + %w(burnt liquidity)
assert_equal (DailyStatistic::VALID_INDICATORS + %w(id)).sort, attrs.sort
assert_equal (DailyStatistic::VALID_INDICATORS + %w(id updated_at)).sort, attrs.sort
end
end

0 comments on commit 896fa7d

Please sign in to comment.