Skip to content

Commit

Permalink
Implement redis key
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Sep 28, 2023
1 parent 37370d3 commit 56decd6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
8 changes: 5 additions & 3 deletions app/controllers/ranking_snapshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ def get_24hr_tickers
@symbols = @daily_ranking.map{|r| [r["symbol"], r["source"]] }
@three_days_duration = $redis.get('three_days_duration') || 12
@three_days_select = $redis.get('three_days_select').to_i
@three_days_ranking = JSON.parse($redis.get("get_three_days_tickers")) rescue []
@three_days_ranking = JSON.parse($redis.get("get_three_days_tickers_#{@three_days_duration}_#{@three_days_select}")) rescue []
@weekly_duration = $redis.get('weekly_duration') || 12
@weekly_select = $redis.get('weekly_select').to_i
@weekly_ranking = JSON.parse($redis.get("get_one_week_tickers")) rescue []
@weekly_ranking = JSON.parse($redis.get("get_one_week_tickers_#{@weekly_duration}_#{@weekly_select}")) rescue []
end

def refresh_tickers
data_type = params[:data_type]
RefreshRankingSnapshotsJob.perform_later(params[:three_days_duration], params[:three_days_select], data_type)
duration = params[:three_days_duration] || params[:weekly_duration]
rank = params[:three_days_select] || params[:weekly_select]
RefreshRankingSnapshotsJob.perform_later(duration, rank, data_type)

redirect_to get_24hr_tickers_ranking_snapshots_path(anchor: get_anchor(data_type)), notice: "正在更新,请稍等刷新查看最新排名..."
end
Expand Down
14 changes: 13 additions & 1 deletion app/models/ranking_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ def self.get_daily_rankings
end

def self.get_rankings(duration: 12, rank: nil, data_type: nil)
redis_key = data_type == 'weekly' ? 'get_one_week_tickers' : 'get_three_days_tickers'
if data_type == 'weekly'
duration_key = 'weekly_duration'
rank_key = 'weekly_select'
redis_key = "get_one_week_tickers_#{duration}_#{rank}"
else
duration_key = 'three_days_duration'
rank_key = 'three_days_select'
redis_key = "get_three_days_tickers_#{duration}_#{rank}"
end

$redis.set(duration_key, duration)
$redis.set(rank_key, rank)

result = JSON.parse($redis.get(redis_key)) rescue nil
if result.nil?
result = RankingSnapshot.order(event_date: :asc).group_by{|snapshot| [snapshot.symbol, snapshot.source]}.map do |key, data|
Expand Down
2 changes: 1 addition & 1 deletion app/services/sync_futures_ticker_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_24hr_tickers(bottom_select, top_select, duration, data_type)
last_price = ticker["last"].to_f
margin = ticker["high24h"].to_f - ticker["low24h"].to_f
price_change = (ticker["last"].to_f - open_price) / open_price
price_ratio = get_price_ratio(from_symbol, last_price, rank, price_type, duration)
price_ratio = get_price_ratio(from_symbol, last_price, rank, duration)
{
"symbol" => from_symbol + to_symbol,
"instId" => ticker["instId"],
Expand Down

0 comments on commit 56decd6

Please sign in to comment.