Skip to content

Commit

Permalink
improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienmo committed Sep 13, 2018
1 parent 2b4059e commit d146188
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 53 deletions.
1 change: 1 addition & 0 deletions apps/neoscan/lib/neoscan/assets/asset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Neoscan.Asset do
field(:owner, :binary)
field(:precision, :integer)
field(:type, :string)
field(:symbol, :string)
field(:issued, :decimal)
field(:block_time, :utc_datetime)
field(:contract, :binary)
Expand Down
52 changes: 50 additions & 2 deletions apps/neoscan/lib/neoscan/assets/assets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Neoscan.Assets do
import Ecto.Query, warn: false
alias Neoscan.Repo
alias Neoscan.Asset
alias Neoscan.Counter

@doc """
Gets a single asset by its hash value
Expand All @@ -18,7 +19,30 @@ defmodule Neoscan.Assets do
nill
"""
def get(hash) do
Repo.one(from(e in Asset, where: e.transaction_hash == ^hash))
query =
from(
a in Asset,
left_join: ca in Counter,
on: a.transaction_hash == ca.ref and ca.name == "addresses_by_asset",
left_join: ct in Counter,
on: a.transaction_hash == ct.ref and ct.name == "transactions_by_asset",
where: a.transaction_hash == ^hash,
select: %{
transaction_hash: a.transaction_hash,
name: a.name,
block_time: a.block_time,
type: a.type,
owner: a.owner,
admin: a.admin,
issued: a.issued,
precision: a.precision,
amount: a.amount,
addr_count: ca.value,
tx_count: ct.value
}
)

Repo.one(query)
end

@doc """
Expand All @@ -28,7 +52,31 @@ defmodule Neoscan.Assets do
[%Asset{}, ...]
"""
def paginate(page) do
assets_query = from(e in Asset, order_by: [desc: e.block_time], limit: @page_size)
assets_query =
from(
a in Asset,
left_join: ca in Counter,
on: a.transaction_hash == ca.ref and ca.name == "addresses_by_asset",
left_join: ct in Counter,
on: a.transaction_hash == ct.ref and ct.name == "transactions_by_asset",
order_by: [desc: fragment("coalesce(?, 0)", ct.value)],
limit: @page_size,
select: %{
transaction_hash: a.transaction_hash,
name: a.name,
block_time: a.block_time,
type: a.type,
owner: a.owner,
admin: a.admin,
issued: a.issued,
precision: a.precision,
amount: a.amount,
symbol: a.symbol,
addr_count: ca.value,
tx_count: ct.value
}
)

Repo.paginate(assets_query, page: page, page_size: @page_size)
end
end
4 changes: 3 additions & 1 deletion apps/neoscan/lib/neoscan/counters/counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ defmodule Neoscan.Counter do
"""
use Ecto.Schema

@primary_key {:name, :string, []}
@primary_key false
schema "counters" do
field(:name, :string, primary_key: true)
field(:ref, :binary, primary_key: true)
field(:value, :integer)
end
end
1 change: 1 addition & 0 deletions apps/neoscan/lib/neoscan/counters/counter_queue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Neoscan.CounterQueue do
@primary_key false
schema "counters_queue" do
field(:name, :string)
field(:ref, :binary)
field(:value, :integer)
end
end
10 changes: 0 additions & 10 deletions apps/neoscan/lib/neoscan/counters/counters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ defmodule Neoscan.Counters do
Repo.one(from(c in Counter, where: c.name == "addresses", select: c.value))
end

def count_addresses(asset_hash) do
name = "addresses_by_asset_" <> Base.encode16(asset_hash, case: :lower)
Repo.one(from(c in Counter, where: c.name == ^name, select: c.value)) || 0
end

def count_blocks do
Repo.one(from(c in Counter, where: c.name == "blocks", select: c.value))
end
Expand All @@ -40,11 +35,6 @@ defmodule Neoscan.Counters do
]
end

def count_transactions(asset_hash) do
name = "transactions_by_asset_" <> Base.encode16(asset_hash, case: :lower)
Repo.one(from(c in Counter, where: c.name == ^name, select: c.value)) || 0
end

def count_assets do
Repo.one(from(c in Counter, where: c.name == "assets", select: c.value))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Neoscan.Repo.Migrations.Assets do
add(:name, {:array, :map}, null: false)
add(:owner, :binary, null: false)
add(:precision, :integer, null: false)
add(:symbol, :string, null: true)
add(:type, :string, null: false)
add(:issued, :decimal)
add(:block_time, :naive_datetime, null: false)
Expand Down
16 changes: 9 additions & 7 deletions apps/neoscan/priv/repo/migrations/20180607000011_counters.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ defmodule Neoscan.Repo.Migrations.Counters do
def change do
create table(:counters_cached, primary_key: false) do
add(:name, :string, primary_key: true)
add(:ref, :binary, primary_key: true)
add(:value, :integer, null: false)
end

create table(:counters_queue, primary_key: false) do
add(:name, :string, null: false)
add(:ref, :binary, null: false)
add(:value, :integer, null: false)
end

execute """
CREATE OR REPLACE VIEW counters AS
SELECT name, SUM(value) AS value
SELECT name, ref, SUM(value) AS value
FROM (
SELECT name, value FROM counters_queue
SELECT name, ref, value FROM counters_queue
UNION ALL
SELECT name, value FROM counters_cached
SELECT name, ref, value FROM counters_cached
) combine
GROUP BY name;
GROUP BY name, ref;
"""

Expand All @@ -41,13 +43,13 @@ defmodule Neoscan.Repo.Migrations.Counters do
WITH
aggregated_queue AS (
SELECT name, SUM(value) AS value
SELECT name, ref, SUM(value) AS value
FROM counters_queue
GROUP BY name
GROUP BY name, ref
),
perform_updates AS (
INSERT INTO counters_cached
SELECT name, value
SELECT name, ref, value
FROM aggregated_queue
ON CONFLICT ON CONSTRAINT counters_cached_pkey DO
UPDATE SET value = counters_cached.value + EXCLUDED.value
Expand Down
14 changes: 7 additions & 7 deletions apps/neoscan/priv/repo/migrations/20180607000015_triggers.exs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ defmodule Neoscan.Repo.Migrations.Triggers do
execute """
CREATE OR REPLACE FUNCTION block_counter() RETURNS TRIGGER LANGUAGE plpgsql AS $body$
BEGIN
INSERT INTO counters_queue (name, value) VALUES ('blocks', 1), ('transactions', NEW.tx_count - 1);
INSERT INTO counters_queue (name, ref, value) VALUES ('blocks', E'\\\\x00', 1), ('transactions', E'\\\\x00', NEW.tx_count - 1);
RETURN NULL;
END;
$body$;
Expand All @@ -173,7 +173,7 @@ defmodule Neoscan.Repo.Migrations.Triggers do
execute """
CREATE OR REPLACE FUNCTION address_counter() RETURNS TRIGGER LANGUAGE plpgsql AS $body$
BEGIN
INSERT INTO counters_queue (name, value) VALUES ('addresses', 1);
INSERT INTO counters_queue (name, ref, value) VALUES ('addresses', E'\\\\x00', 1);
RETURN NULL;
END;
$body$;
Expand All @@ -188,7 +188,7 @@ defmodule Neoscan.Repo.Migrations.Triggers do
execute """
CREATE OR REPLACE FUNCTION asset_counter() RETURNS TRIGGER LANGUAGE plpgsql AS $body$
BEGIN
INSERT INTO counters_queue (name, value) VALUES ('assets', 1);
INSERT INTO counters_queue (name, ref, value) VALUES ('assets', E'\\\\x00', 1);
RETURN NULL;
END;
$body$;
Expand Down Expand Up @@ -220,8 +220,8 @@ defmodule Neoscan.Repo.Migrations.Triggers do
execute """
CREATE OR REPLACE FUNCTION transaction_by_asset_counter() RETURNS TRIGGER LANGUAGE plpgsql AS $body$
BEGIN
INSERT INTO counters_queue (name, value)
VALUES ('transactions_by_asset_' || encode(NEW.asset_hash, 'hex'), 1);
INSERT INTO counters_queue (name, ref, value)
VALUES ('transactions_by_asset', NEW.asset_hash, 1);
RETURN NULL;
END;
$body$;
Expand All @@ -236,8 +236,8 @@ defmodule Neoscan.Repo.Migrations.Triggers do
execute """
CREATE OR REPLACE FUNCTION address_by_asset_counter() RETURNS TRIGGER LANGUAGE plpgsql AS $body$
BEGIN
INSERT INTO counters_queue (name, value)
VALUES ('addresses_by_asset_' || encode(NEW.asset_hash, 'hex'), 1);
INSERT INTO counters_queue (name, ref, value)
VALUES ('addresses_by_asset', NEW.asset_hash, 1);
RETURN NULL;
END;
$body$;
Expand Down
1 change: 1 addition & 0 deletions apps/neoscan/test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ defmodule Neoscan.Factory do
def counter_factory do
%CounterQueue{
name: sequence("name"),
ref: <<0>>,
value: sequence(1, & &1)
}
end
Expand Down
1 change: 1 addition & 0 deletions apps/neoscan_cache/test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ defmodule NeoscanCache.Factory do
def counter_factory do
%CounterQueue{
name: sequence("name"),
ref: <<0>>,
value: sequence(1, & &1)
}
end
Expand Down
1 change: 1 addition & 0 deletions apps/neoscan_sync/lib/neoscan_sync/token_syncer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ defmodule NeoscanSync.TokenSyncer do
admin: <<0>>,
amount: 0.0,
name: [%{"lang" => "en", "name" => token.token.name}],
symbol: token.token.symbol,
owner: <<0>>,
precision: token.token.decimals,
type: "NEP5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule NeoscanWeb.AssetController do
use NeoscanWeb, :controller

alias Neoscan.Assets
alias Neoscan.Counters

@asset_hash_spec [
asset_hash: %{
Expand All @@ -12,10 +11,7 @@ defmodule NeoscanWeb.AssetController do

def index(conn, params) do
if_valid_query conn, params, @asset_hash_spec do
asset =
Assets.get(parsed.asset_hash)
|> Map.put(:tx_count, Counters.count_transactions(parsed.asset_hash))
|> Map.put(:addr_count, Counters.count_addresses(parsed.asset_hash))
asset = Assets.get(parsed.asset_hash)

if is_nil(asset) do
redirect(conn, to: home_path(conn, :index))
Expand Down
16 changes: 2 additions & 14 deletions apps/neoscan_web/lib/neoscan_web/templates/asset/asset.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,17 @@
<a href="<%= address_path(@conn, :index, render_address_hash(@asset.admin))%>" class="large-blue-link"><%= render_address_hash(@asset.admin) %></a>
<% end %>
</li>
<li class="border-right border-bottom tooltip <%= if get_tooltips(@conn) == "on" do %>add-hover<% end %>">
<p class="small-detail-text"><%= gettext "Issued" %></p>
<p class="large-detail-text"><%= render_balance(@asset.issued, @asset.precision) %></p>
</li>
<li class="border-right border-bottom border-right-smaller remove-border-right-660px tooltip <%= if get_tooltips(@conn) == "on" do %>add-hover<% end %>">
<p class="small-detail-text"><%= gettext "Supply" %></p>
<p class="large-detail-text"><%= render_balance(@asset.amount, @asset.precision) %></p>
</li>
<li class="border-bottom tooltip <%= if get_tooltips(@conn) == "on" do %>add-hover<% end %>">
<p class="small-detail-text"><%= gettext "Precision" %></p>
<p class="large-detail-text"><%= @asset.precision %></p>
</li>
<li class="border-right border-right-smaller remove-border-right-660px tooltip <%= if get_tooltips(@conn) == "on" do %>add-hover<% end %>">
<p class="small-detail-text"><%= gettext "Transactions"%></p>
<p class="large-detail-text"><%= @asset.tx_count %></p>
<p class="large-detail-text"><%= @asset.tx_count || 0 %></p>
</li>
<li class="border-right remove-border-right-660px remove-border-bottom-660px tooltip <%= if get_tooltips(@conn) == "on" do %>add-hover<% end %>">
<p class="small-detail-text"><%= gettext "Addresses"%></p>
<p class="large-detail-text"><%= @asset.addr_count %></p>
</li>
<li class="remove-border-bottom-660px tooltip <%= if get_tooltips(@conn) == "on" do %>add-hover<% end %>">
<p class="small-detail-text"><%= gettext "Created"%></p>
<p class=" wallet-address"><span class="utc_time"><%= get_minutes(@asset.block_time)%></span></a>
<p class="large-detail-text"><%= @asset.addr_count || 0 %></p>
</li>
</ul>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

<div class="detail-bar">
<p class="detail-type detail-type-first-3-col"><%= gettext "Name" %></p>
<p class="detail-type col-6-width"><%= gettext "Asset Hash" %></p>
<p class="detail-type col-3-width"><%= gettext "Created on" %></p>
<p class="detail-type col-2-width"><%= gettext "Symbol" %></p>
<p class="detail-type col-3-width"><%= gettext "Type" %></p>
<p class="detail-type col-2-width"><%= gettext "Addresses" %></p>
<p class="detail-type col-2-width"><%= gettext "Transactions" %></p>
<p class="detail-type tablet-full-width"><%= gettext "All asset information" %></p>
</div>

Expand All @@ -17,12 +19,12 @@
<%= for asset <- @assets do %>
<div class="full-width-bar asset">
<div class="information-wrapper">
<p class="medium-detail-text col-3-width"><span class="fa <%= render_asset_style(asset.type) %>"></span><%= render_asset_name(asset) %></p>
<p class="medium-detail-text col-3-width"><span class="fa <%= render_asset_style(asset.type) %>"></span><a href="<%= asset_path(@conn, :index, render_hash(asset.transaction_hash))%>" alt="<%= gettext "View asset"%>" title="<%= gettext "View asset"%>"><%= render_asset_name(asset) %></a></p>
<div class="secondary-info-wrapper">
<a href="<%= asset_path(@conn, :index, render_hash(asset.transaction_hash))%>" alt="<%= gettext "View asset"%>" title="<%= gettext "View asset"%>" class="large-blue-link col-6-width"><%= render_hash(asset.transaction_hash) %></a>
<p class="medium-detail-text col-3-width remove-550px">
<span class="utc_time"><%= render_date_time(asset.block_time) %></span>
</p>
<p class="medium-detail-text col-2-width"><%= asset.symbol || "N/A" %></p>
<p class="medium-detail-text col-3-width"><%= Macro.camelize(asset.type) %></p>
<p class="medium-detail-text col-2-width"><%= asset.addr_count || 0 %></p>
<p class="medium-detail-text col-2-width remove-550px"><%= asset.tx_count || 0 %></p>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions apps/neoscan_web/test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ defmodule NeoscanWeb.Factory do
def counter_factory do
%CounterQueue{
name: sequence("name"),
ref: <<0>>,
value: sequence(1, & &1)
}
end
Expand Down

0 comments on commit d146188

Please sign in to comment.