Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
farthest witness
Browse files Browse the repository at this point in the history
  • Loading branch information
allenan committed Sep 17, 2019
1 parent 012edf9 commit 9fc85d5
Showing 1 changed file with 107 additions and 35 deletions.
142 changes: 107 additions & 35 deletions lib/blockchain_api/query/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ defmodule BlockchainAPI.Query.Stats do
Block,
ConsensusMember,
ElectionTransaction,
POCPathElement,
POCReceiptsTransaction,
POCWitness,
RewardTxn,
Transaction
}
Expand All @@ -24,7 +27,6 @@ defmodule BlockchainAPI.Query.Stats do
%{avg_time_interval: month_election_time, avg_block_interval: month_election_block} =
get_election_time(days: -30)


%{
"token_supply" => %{
"total" => get_supply()
Expand Down Expand Up @@ -55,6 +57,11 @@ defmodule BlockchainAPI.Query.Stats do
"7d" => get_query_by_shift(&query_top_grossing_hotspots/2, days: -7),
"30d" => get_query_by_shift(&query_top_grossing_hotspots/2, days: -30)
},
"farthest_witness" => %{
"24h" => get_query_by_shift(&query_farthest_witness/2, hours: -24),
"7d" => get_query_by_shift(&query_farthest_witness/2, days: -7),
"30d" => get_query_by_shift(&query_farthest_witness/2, days: -30)
}
}
}
end
Expand Down Expand Up @@ -177,27 +184,29 @@ defmodule BlockchainAPI.Query.Stats do
group_by: cm.address,
select: %{
count: fragment("count(*)"),
gateway: cm.address,
gateway: cm.address
}
)

rank_query = from(
cq in subquery(count_query),
select: %{
count: cq.count,
gateway: cq.gateway,
rank: rank() |> over(order_by: [desc: cq.count])
}
)
rank_query =
from(
cq in subquery(count_query),
select: %{
count: cq.count,
gateway: cq.gateway,
rank: rank() |> over(order_by: [desc: cq.count])
}
)

query = from(
rq in subquery(rank_query),
where: rq.rank == 1,
select: %{
count: rq.count,
gateway: rq.gateway
}
)
query =
from(
rq in subquery(rank_query),
where: rq.rank == 1,
select: %{
count: rq.count,
gateway: rq.gateway
}
)

query
|> Repo.all()
Expand All @@ -219,28 +228,30 @@ defmodule BlockchainAPI.Query.Stats do
where: not is_nil(rt.gateway),
group_by: rt.gateway,
select: %{
sum: sum(rt.amount),
amount: sum(rt.amount),
gateway: rt.gateway
}
)

rank_query = from(
sq in subquery(sum_query),
select: %{
sum: sq.sum,
gateway: sq.gateway,
rank: rank() |> over(order_by: [desc: sq.sum])
}
)
rank_query =
from(
sq in subquery(sum_query),
select: %{
amount: sq.amount,
gateway: sq.gateway,
rank: rank() |> over(order_by: [desc: sq.amount])
}
)

query = from(
rq in subquery(rank_query),
where: rq.rank == 1,
select: %{
sum: rq.sum,
gateway: rq.gateway
}
)
query =
from(
rq in subquery(rank_query),
where: rq.rank == 1,
select: %{
amount: rq.amount,
gateway: rq.gateway
}
)

query
|> Repo.all()
Expand All @@ -249,6 +260,67 @@ defmodule BlockchainAPI.Query.Stats do
end)
end

# select * from poc_witnesses pw
# inner join poc_path_elements pe
# on pe.id = pw.poc_path_elements_id
# inner join poc_receipts_transactions rt
# on rt.hash = pe.poc_receipts_transactions_hash
# inner join transactions tx
# on tx.hash = rt.hash
# inner join blocks b
# on b.height = tx.block_height
# WHERE (b.time >= 1568156776) AND (b.time <= 1568243176)
# order by pw.distance desc, b.time asc

defp query_farthest_witness(start, finish) do
distance_query =
from(
pw in POCWitness,
inner_join: pe in POCPathElement,
on: pe.id == pw.poc_path_elements_id,
inner_join: rt in POCReceiptsTransaction,
on: rt.hash == pe.poc_receipts_transactions_hash,
inner_join: tx in Transaction,
on: tx.hash == rt.hash,
inner_join: b in Block,
on: b.height == tx.block_height,
where: b.time >= ^start,
where: b.time <= ^finish,
select: %{
gateway: pw.gateway,
distance: pw.distance,
timestamp: pw.timestamp
}
)

rank_query =
from(
dq in subquery(distance_query),
select: %{
gateway: dq.gateway,
distance: dq.distance,
rank: rank() |> over(order_by: [desc: dq.distance, asc: dq.timestamp])
}
)

query =
from(
rq in subquery(rank_query),
where: rq.rank == 1,
select: %{
gateway: rq.gateway,
distance: rq.distance
}
)

query
|> Repo.all()
|> Enum.map(fn %{gateway: gateway, owner: owner} = m ->
m
|> Map.put(:gateway, Util.bin_to_string(gateway))
end)
end

defp normalize_interval(interval) do
case interval do
nil -> 0.0
Expand Down

0 comments on commit 9fc85d5

Please sign in to comment.