Skip to content

Commit

Permalink
Merge pull request #682 from shaojunda/shaojunda-add-flush-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda authored Jul 8, 2020
2 parents f97746e + 600cc31 commit 1149c90
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/daily_statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class DailyStatisticsController < ApplicationController
before_action :validate_query_params, only: :show

def show
daily_statistics = DailyStatistic.order(:created_at_unixtimestamp).limit(365)
daily_statistics = DailyStatistic.order(:created_at_unixtimestamp)
render json: DailyStatisticSerializer.new(daily_statistics, { params: { indicator: params[:id] } })
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/statistic_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def maintenance_info
Rails.cache.fetch("maintenance_info")
end

def flush_cache_info
Rails.cache.realize("flush_cache_info") || []
end

private

attr_reader :hash_rate_statistical_interval, :average_block_time_interval
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class StatisticSerializer
params && params[:info_name] == "blockchain_info"
}

attribute :flush_cache_info, if: Proc.new { |_record, params|
params && params[:info_name] == "flush_cache_info"
}

attribute :address_balance_ranking, if: Proc.new { |_record, params|
params && params[:info_name] == "address_balance_ranking"
}
Expand Down
16 changes: 16 additions & 0 deletions doc/api.raml
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@ types:
"hash_rate": {
type: "string",
},
"flush_cache_info": {
type: "string[]"
}
"blockchain_info": {
type: "object"
},
Expand Down Expand Up @@ -2429,6 +2432,19 @@ types:
}
}
}
flush_cache_info: |
{
"data": {
"id": "1594109792",
"type": "statistic",
"attributes": {
"flush_cache_info": [
"liquidity",
"deposit_compensation"
]
}
}
}
address_balance_ranking: |
{
"data": {
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/set_flush_cache_info.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
desc "Usage: RAILS_ENV=production bundle exec rake 'set_flush_cache_info[liquidity deposit_compensation, true]'"
task :set_flush_cache_info, [:indicators, :dry_run] => :environment do |_, args|
raise "please input indicators" if args[:indicators].blank?
binding.pry

indicators = args[:indicators].split(" ")
dry_run = args[:dry_run] || "true"
if dry_run == "true"
puts "indicators: #{indicators.join(", ")}"
else
Rails.cache.write("flush_cache_info", indicators, expires_in: 1.day)
puts "done"
end
end
14 changes: 13 additions & 1 deletion public/api_doc.html
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@
}
]
}
</code></pre></div></div></div></div></div></div></div></div><div class="panel panel-white resource-modal"><div class="panel-heading"><h4 class="panel-title"><a class="collapsed" data-toggle="collapse" href="#panel_statistics__info_name_"><span class="parent">/statistics</span>/{info_name}</a> <span class="methods"><a href="#statistics__info_name__get"><span class="badge badge_get">get</span></a></span></h4></div><div id="panel_statistics__info_name_" class="panel-collapse collapse"><div class="panel-body"><div class="list-group"><div onclick="window.location.href = '#statistics__info_name__get'" class="list-group-item"><span class="badge badge_get">get</span><div class="method_description"><p>Returns the specific statistic info</p></div><div class="clearfix"></div></div></div></div></div><div class="modal fade" tabindex="0" id="statistics__info_name__get"><div class="modal-dialog modal-lg"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="myModalLabel"><span class="badge badge_get">get</span> <span class="parent">/statistics</span>/{info_name}</h4></div><div class="modal-body"><div class="alert alert-info"><p>Returns the specific statistic info</p></div><ul class="nav nav-tabs"><li class="active"><a href="#statistics__info_name__get_request" data-toggle="tab">Request</a></li><li><a href="#statistics__info_name__get_response" data-toggle="tab">Response</a></li></ul><div class="tab-content"><div class="tab-pane active" id="statistics__info_name__get_request"><h3>URI Parameters</h3><ul><li><strong>info_name</strong>: <em><span class="required">required</span>(string)</em><p>The statistic info name</p></li></ul></div><div class="tab-pane" id="statistics__info_name__get_response"><h2>HTTP status code <a href="http://httpstatus.es/200" target="_blank">200</a></h2><h3>Body</h3><p><strong>Media type</strong>: application/vnd.api+json</p><p><strong>Type</strong>: object</p><strong>Properties</strong><ul><li><strong>data</strong>: <em><span class="required">required</span>(object)</em><ul><li><strong>tip_block_number</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>average_block_time</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>current_epoch_difficulty</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>hash_rate</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>blockchain_info</strong>: <em><span class="required">required</span>(object)</em><ul></ul></li><li><strong>address_balance_ranking</strong>: <em><span class="required">required</span>(object)</em><ul><li><strong>ranking</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>address</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>balance</strong>: <em><span class="required">required</span>(string)</em></li></ul></li><li><strong>maintenance_info"</strong>: <em><span class="required">required</span>(object)</em><ul><li><strong>stat_at</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>end_at</strong>: <em><span class="required">required</span>(string)</em></li></ul></li></ul></li></ul><p><strong>Examples</strong>:</p><div class="examples"><p><strong>tip_block_number</strong>:<br></p><pre><code>{
</code></pre></div></div></div></div></div></div></div></div><div class="panel panel-white resource-modal"><div class="panel-heading"><h4 class="panel-title"><a class="collapsed" data-toggle="collapse" href="#panel_statistics__info_name_"><span class="parent">/statistics</span>/{info_name}</a> <span class="methods"><a href="#statistics__info_name__get"><span class="badge badge_get">get</span></a></span></h4></div><div id="panel_statistics__info_name_" class="panel-collapse collapse"><div class="panel-body"><div class="list-group"><div onclick="window.location.href = '#statistics__info_name__get'" class="list-group-item"><span class="badge badge_get">get</span><div class="method_description"><p>Returns the specific statistic info</p></div><div class="clearfix"></div></div></div></div></div><div class="modal fade" tabindex="0" id="statistics__info_name__get"><div class="modal-dialog modal-lg"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="myModalLabel"><span class="badge badge_get">get</span> <span class="parent">/statistics</span>/{info_name}</h4></div><div class="modal-body"><div class="alert alert-info"><p>Returns the specific statistic info</p></div><ul class="nav nav-tabs"><li class="active"><a href="#statistics__info_name__get_request" data-toggle="tab">Request</a></li><li><a href="#statistics__info_name__get_response" data-toggle="tab">Response</a></li></ul><div class="tab-content"><div class="tab-pane active" id="statistics__info_name__get_request"><h3>URI Parameters</h3><ul><li><strong>info_name</strong>: <em><span class="required">required</span>(string)</em><p>The statistic info name</p></li></ul></div><div class="tab-pane" id="statistics__info_name__get_response"><h2>HTTP status code <a href="http://httpstatus.es/200" target="_blank">200</a></h2><h3>Body</h3><p><strong>Media type</strong>: application/vnd.api+json</p><p><strong>Type</strong>: object</p><strong>Properties</strong><ul><li><strong>data</strong>: <em><span class="required">required</span>(object)</em><ul><li><strong>tip_block_number</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>average_block_time</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>current_epoch_difficulty</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>hash_rate</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>flush_cache_info</strong>: <em><span class="required">required</span>(array of string)</em></li><li><strong>blockchain_info"</strong>: <em><span class="required">required</span>(object)</em><ul></ul></li><li><strong>address_balance_ranking</strong>: <em><span class="required">required</span>(object)</em><ul><li><strong>ranking</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>address</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>balance</strong>: <em><span class="required">required</span>(string)</em></li></ul></li><li><strong>maintenance_info"</strong>: <em><span class="required">required</span>(object)</em><ul><li><strong>stat_at</strong>: <em><span class="required">required</span>(string)</em></li><li><strong>end_at</strong>: <em><span class="required">required</span>(string)</em></li></ul></li></ul></li></ul><p><strong>Examples</strong>:</p><div class="examples"><p><strong>tip_block_number</strong>:<br></p><pre><code>{
"data": {
"id": "1",
"type": "statistic_info",
Expand Down Expand Up @@ -1148,6 +1148,18 @@
}
}
}
</code></pre><p><strong>flush_cache_info</strong>:<br></p><pre><code>{
"data": {
"id": "1594109792",
"type": "statistic",
"attributes": {
"flush_cache_info": [
"liquidity",
"deposit_compensation"
]
}
}
}
</code></pre><p><strong>address_balance_ranking</strong>:<br></p><pre><code>{
"data": {
"id": "1562726172",
Expand Down
3 changes: 2 additions & 1 deletion test/controllers/api/v1/daily_statistics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class DailyStatisticsControllerTest < ActionDispatch::IntegrationTest
end

test "should return addresses count and timestamp" do
daily_statistic_data = create_list(:daily_statistic, 15)
create_list(:daily_statistic, 15)
daily_statistic_data = DailyStatistic.order(:created_at_unixtimestamp)
valid_get api_v1_daily_statistic_url("addresses_count")

assert_equal [%w(addresses_count created_at_unixtimestamp).sort], json.dig("data").map { |item| item.dig("attributes").keys.sort }.uniq
Expand Down

0 comments on commit 1149c90

Please sign in to comment.