Merge pull request #1278 from poanetwork/gsf-market-history-chart-load-async
market history chart load asyncpull/1282/head
commit
e9e6d3444d
@ -0,0 +1,51 @@ |
||||
defmodule BlockScoutWeb.Chain.MarketHistoryChartController do |
||||
use BlockScoutWeb, :controller |
||||
|
||||
alias Explorer.{Chain, Market} |
||||
alias Explorer.ExchangeRates.Token |
||||
|
||||
def show(conn, _params) do |
||||
with true <- ajax?(conn) do |
||||
exchange_rate = Market.get_exchange_rate(Explorer.coin()) || Token.null() |
||||
|
||||
market_history_data = |
||||
30 |
||||
|> Market.fetch_recent_history() |
||||
|> case do |
||||
[today | the_rest] -> [%{today | closing_price: exchange_rate.usd_value} | the_rest] |
||||
data -> data |
||||
end |
||||
|> encode_market_history_data() |
||||
|
||||
json(conn, %{ |
||||
history_data: market_history_data, |
||||
supply_data: available_supply(Chain.supply_for_days(30), exchange_rate) |
||||
}) |
||||
else |
||||
_ -> unprocessable_entity(conn) |
||||
end |
||||
end |
||||
|
||||
defp available_supply(:ok, exchange_rate) do |
||||
to_string(exchange_rate.available_supply || 0) |
||||
end |
||||
|
||||
defp available_supply({:ok, supply_for_days}, _exchange_rate) do |
||||
supply_for_days |
||||
|> Jason.encode() |
||||
|> case do |
||||
{:ok, data} -> data |
||||
_ -> [] |
||||
end |
||||
end |
||||
|
||||
defp encode_market_history_data(market_history_data) do |
||||
market_history_data |
||||
|> Enum.map(fn day -> Map.take(day, [:closing_price, :date]) end) |
||||
|> Jason.encode() |
||||
|> case do |
||||
{:ok, data} -> data |
||||
_ -> [] |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,24 @@ |
||||
defmodule BlockScoutWeb.Chain.MarketHistoryChartControllerTest do |
||||
use BlockScoutWeb.ConnCase |
||||
|
||||
describe "GET show/2" do |
||||
test "returns error when not an ajax request" do |
||||
path = market_history_chart_path(BlockScoutWeb.Endpoint, :show) |
||||
|
||||
conn = get(build_conn(), path) |
||||
|
||||
assert conn.status == 422 |
||||
end |
||||
|
||||
test "returns ok when request is ajax" do |
||||
path = market_history_chart_path(BlockScoutWeb.Endpoint, :show) |
||||
|
||||
conn = |
||||
build_conn() |
||||
|> put_req_header("x-requested-with", "xmlhttprequest") |
||||
|> get(path) |
||||
|
||||
assert json_response(conn, 200) |
||||
end |
||||
end |
||||
end |
@ -1,19 +0,0 @@ |
||||
defmodule BlockScoutWeb.ChainViewTest do |
||||
use BlockScoutWeb.ConnCase, async: true |
||||
|
||||
alias BlockScoutWeb.ChainView |
||||
|
||||
describe "encode_market_history_data/1" do |
||||
test "returns a JSON encoded market history data" do |
||||
market_history_data = [ |
||||
%Explorer.Market.MarketHistory{ |
||||
closing_price: Decimal.new("0.078"), |
||||
date: ~D[2018-08-20] |
||||
} |
||||
] |
||||
|
||||
assert "[{\"closing_price\":\"0.078\",\"date\":\"2018-08-20\"}]" == |
||||
ChainView.encode_market_history_data(market_history_data) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue