Skip to content

Commit

Permalink
add filters to three days and weekly ranking brtr/coin_elite#728
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-xiong committed Sep 28, 2023
1 parent a40126c commit a13383a
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 18 deletions.
32 changes: 30 additions & 2 deletions app/controllers/ranking_snapshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,25 @@ def get_24hr_tickers
@duration = $redis.get('top_select_duration').presence || 12
@daily_ranking = @daily_ranking.select{|d| d['source'] == @source} if @source.present?
@symbols = @daily_ranking.map{|r| [r["symbol"], r["source"]] }
@three_days_ranking = RankingSnapshot.where("event_date >= ?", Date.yesterday - 3.days).get_rankings
@weekly_ranking = RankingSnapshot.where("event_date >= ?", Date.yesterday - 1.week).get_rankings
@three_days_duration = params[:three_days_duration] || 12
@three_days_select = params[:three_days_select]
@three_days_ranking = RankingSnapshot.where("event_date >= ?", Date.yesterday - 3.days).get_rankings(duration: @three_days_duration, rank: @three_days_select)
@weekly_duration = params[:weekly_duration] || 12
@weekly_select = params[:weekly_select]
@weekly_ranking = RankingSnapshot.where("event_date >= ?", Date.yesterday - 1.week).get_rankings(duration: @weekly_duration, rank: @weekly_select)
end

def refresh_tickers
data_type = params[:data_type]
res = { anchor: get_anchor(data_type) }

if data_type == 'three_days'
res.merge!({ three_days_duration: params[:three_days_duration], three_days_select: params[:three_days_select] })
else
res.merge!({ weekly_duration: params[:weekly_duration], weekly_select: params[:weekly_select] })
end

redirect_to get_24hr_tickers_ranking_snapshots_path(res), notice: "正在更新,请稍等刷新查看最新排名..."
end

def ranking_graph
Expand Down Expand Up @@ -64,4 +81,15 @@ def split_event_dates(infos)

date_records
end

def get_anchor(data_type)
case data_type
when 'three_days'
'three_days_select_table'
when 'weekly'
'weekly_select_table'
else
''
end
end
end
49 changes: 44 additions & 5 deletions app/models/ranking_snapshot.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
class RankingSnapshot < ApplicationRecord
scope :with_top10, ->(source, symbol) { where(is_top10: true, source: source, symbol: symbol) }
def self.get_rankings

def self.get_rankings(duration: 12, rank: nil)
RankingSnapshot.order(event_date: :asc).group_by{|snapshot| [snapshot.symbol, snapshot.source]}.map do |key, data|
open_price = data.first.open_price
last_record = data.last
last_price = last_record.last_price
last_price = data.last.last_price
price_ratio = get_price_ratio(fetch_symbol(key[0]), last_price, rank, duration)
price_change = (last_price - open_price) / open_price rescue 0
{
"symbol" => key[0],
"lastPrice" => last_price,
"priceChangePercent" => (price_change * 100).round(4).to_s,
"bottomPriceRatio" => last_record.bottom_price_ratio.to_s,
"source" => key[1]
"bottomPriceRatio" => (price_ratio['bottom_ratio'].to_s rescue ''),
"topPriceRatio" => (price_ratio['top_ratio'].to_s rescue ''),
"source" => key[1],
"rank" => get_coin_ranking(key[0])
}
end
end

private

def self.get_coin_ranking(symbol)
redis_key = "get_coin_ranking_#{symbol}"
rank = $redis.get(redis_key).to_f

if rank.zero?
symbol = fetch_symbol(symbol)
rank = CoinRanking.find_by(symbol: symbol.downcase)&.rank

$redis.set(redis_key, rank, ex: 5.hours)
end

rank
end

def self.fetch_symbol(symbol)
SyncFuturesTickerService.fetch_symbol(symbol)
end

def self.get_price_ratio(symbol, price, rank, duration)
redis_key = "get_price_ratio_#{symbol}_#{price}_#{rank}_#{duration}"
price_ratio = JSON.parse($redis.get(redis_key)) rescue nil

if price_ratio.nil?
url = ENV['COIN_ELITE_URL'] + "/api/user_positions/get_price_ratio?symbol=#{symbol}&price=#{price}&duration=#{duration}"
url += "&rank=#{rank}" if rank.to_i > 0
response = RestClient.get(url)
data = JSON.parse(response)

$redis.set(redis_key, data.to_json, ex: 5.hour)
end

price_ratio
end
end
44 changes: 33 additions & 11 deletions app/views/ranking_snapshots/get_24hr_tickers.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,31 @@
</table>
</div>
</div>
<h3 class="mt-5">最近3天币种涨跌排名列表</h3>
<h3 class="mt-5" id="three_days_select_table">最近3天币种涨跌排名列表</h3>
<div class="mt-3 col-4">
<%= form_tag refresh_tickers_ranking_snapshots_path, class: "position-relative", method: "GET" do %>
<div class="input-group mb-3 position-filter">
<%= select_tag(:three_days_duration, options_for_select([["最近一年内", 12], ["最近两年内", 24]], @three_days_duration ),{ class: 'select2-dropdown' }) %>
<span class='ms-3'></span>
<%= select_tag(:three_days_select, options_for_select([["所有币种", 0], ["前100名以内",3], ["前100名以外",4], ["前200名以内",5], ["前200名以外",6]], @three_days_select),{ prompt: '请选择币种排名', class: 'select2-dropdown' }) %>
<%= hidden_field_tag :data_type, :three_days %>
<button type="submit" class="btn btn-primary mx-3">确定</button>
</div>
<% end %>
</div>
<div class="row mt-3">
<div class="col-6">
<table class="table">
<thead>
<tr class="table-container-tr">
<th>币种</th>
<th>3天内涨幅</th>
<th>一年内底部比例</th>
<th>距离顶部比例</th>
<th>来源</th>
</tr>
</thead>
<tbody>
<% @three_days_ranking.sort_by{|d| d["priceChangePercent"].to_f}.reverse.first(10).each do |d| %>
<% @three_days_ranking.sort_by{|d| [d["priceChangePercent"].to_f, d["topPriceRatio"].to_f]}.reverse.first(10).each do |d| %>
<tr>
<td><%= get_symbol_url d["symbol"], d["source"] %></td>
<td><%= d["priceChangePercent"] + " %" %></td>
Expand All @@ -185,12 +196,12 @@
<tr class="table-container-tr">
<th>币种</th>
<th>3天内跌幅</th>
<th>一年内底部比例</th>
<th>距离底部比例</th>
<th>来源</th>
</tr>
</thead>
<tbody>
<% @three_days_ranking.sort_by{|d| d["priceChangePercent"].to_f}.first(10).each do |d| %>
<% @three_days_ranking.sort_by{|d| [d["priceChangePercent"].to_f, d["bottomPriceRatio"].to_f]}.first(10).each do |d| %>
<tr>
<td><%= get_symbol_url d["symbol"], d["source"] %></td>
<td><%= d["priceChangePercent"] + " %" %></td>
Expand All @@ -202,24 +213,35 @@
</table>
</div>
</div>
<h3 class="mt-5">最近一周币种涨跌排名列表</h3>
<h3 class="mt-5" id="weekly_select_table">最近一周币种涨跌排名列表</h3>
<div class="mt-3 col-4">
<%= form_tag refresh_tickers_ranking_snapshots_path, class: "position-relative", method: "GET" do %>
<div class="input-group mb-3 position-filter">
<%= select_tag(:weekly_duration, options_for_select([["最近一年内", 12], ["最近两年内", 24]], @weekly_duration ),{ class: 'select2-dropdown' }) %>
<span class='ms-3'></span>
<%= select_tag(:weekly_select, options_for_select([["所有币种", 0], ["前100名以内",3], ["前100名以外",4], ["前200名以内",5], ["前200名以外",6]], @weekly_select),{ prompt: '请选择币种排名', class: 'select2-dropdown' }) %>
<%= hidden_field_tag :data_type, :weekly %>
<button type="submit" class="btn btn-primary mx-3">确定</button>
</div>
<% end %>
</div>
<div class="row mt-3">
<div class="col-6">
<table class="table">
<thead>
<tr class="table-container-tr">
<th>币种</th>
<th>一周内涨幅</th>
<th>一年内底部比例</th>
<th>距离顶部比例</th>
<th>来源</th>
</tr>
</thead>
<tbody>
<% @weekly_ranking.sort_by{|d| d["priceChangePercent"].to_f}.reverse.first(10).each do |d| %>
<% @weekly_ranking.sort_by{|d| [d["priceChangePercent"].to_f, d["topPriceRatio"].to_f]}.reverse.first(10).each do |d| %>
<tr>
<td><%= get_symbol_url d["symbol"], d["source"] %></td>
<td><%= d["priceChangePercent"] + " %" %></td>
<td><%= d["bottomPriceRatio"] + " %" %></td>
<td><%= d["topPriceRatio"] + " %" %></td>
<td><%= d["source"] %></td>
</tr>
<% end %>
Expand All @@ -232,12 +254,12 @@
<tr class="table-container-tr">
<th>币种</th>
<th>一周内跌幅</th>
<th>一年内底部比例</th>
<th>距离底部比例</th>
<th>来源</th>
</tr>
</thead>
<tbody>
<% @weekly_ranking.sort_by{|d| d["priceChangePercent"].to_f}.first(10).each do |d| %>
<% @weekly_ranking.sort_by{|d| [d["priceChangePercent"].to_f, d["bottomPriceRatio"].to_f]}.first(10).each do |d| %>
<tr>
<td><%= get_symbol_url d["symbol"], d["source"] %></td>
<td><%= d["priceChangePercent"] + " %" %></td>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
get :get_24hr_tickers
get :list
get :ranking_graph
get :refresh_tickers
end
end

Expand Down

0 comments on commit a13383a

Please sign in to comment.