-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add filters to three days and weekly ranking brtr/coin_elite#728
- Loading branch information
Showing
4 changed files
with
108 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ | |
get :get_24hr_tickers | ||
get :list | ||
get :ranking_graph | ||
get :refresh_tickers | ||
end | ||
end | ||
|
||
|