Skip to content

Commit

Permalink
Add account/<account>/hotspots
Browse files Browse the repository at this point in the history
Also adds animal name to hotspots output
  • Loading branch information
madninja committed Feb 11, 2020
1 parent 1db4822 commit 9aebef1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{epgsql, "4.3.0"},
{observer_cli, "1.5.0"},
{telemetry, "0.4.1"},
{erl_angry_purple_tiger, ".*", {git, "https://github.com/helium/erl_angry_purple_tiger.git", {branch, "master"}}},
{psql_migration, {git, "https://github.com/helium/psql-migration.git", {branch, "master"}}},
{helium_proto, {git, "https://github.com/helium/proto.git", {branch, "master"}}},
{envloader, {git, "https://github.com/nuex/envloader.git", {branch, "master"}}}
Expand Down
4 changes: 4 additions & 0 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
{git,"git://github.com/artemeff/eql.git",
{ref,"a6727a3f878bfdd06648b7a886cbd2c0630db172"}},
1},
{<<"erl_angry_purple_tiger">>,
{git,"https://github.com/helium/erl_angry_purple_tiger.git",
{ref,"c5476b6639314a75a99400c9dfa7603b24a6d18a"}},
0},
{<<"erl_base58">>,{pkg,<<"erl_base58">>,<<"0.0.1">>},1},
{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},1},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1},
Expand Down
15 changes: 9 additions & 6 deletions src/bh_route_accounts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
-define(SELECT_ACCOUNT_BASE(A), "select l.address, l.dc_balance, l.dc_nonce, l.security_balance, l.security_nonce, l.balance, l.nonce" A " from account_ledger l ").
-define(SELECT_ACCOUNT_BASE, ?SELECT_ACCOUNT_BASE("")).


prepare_conn(Conn) ->
{ok, _} = epgsql:parse(Conn, ?S_ACCOUNT_LIST_BEFORE,
?SELECT_ACCOUNT_BASE "where l.address < $1 order by block desc, address limit $2", []),
Expand All @@ -39,6 +38,10 @@ handle('GET', [], Req) ->
?MK_RESPONSE(get_account_list(Before, Limit));
handle('GET', [Account], _Req) ->
?MK_RESPONSE(get_account(Account));
handle('GET', [Account, <<"hotspots">>], Req) ->
Before = ?GET_ARG_BEFORE(Req, undefined),
Limit = ?GET_ARG_LIMIT(Req),
?MK_RESPONSE(bh_route_hotspots:get_owner_hotspot_list(Account, Before, Limit));

handle(_, _, _Req) ->
?RESPONSE_404.
Expand All @@ -55,7 +58,7 @@ get_account(Account) ->
{ok, _, [Result]} ->
{ok, account_to_json(Result)};
_ ->
{error, not_found}
{ok, account_to_json({Account, 0, 0, 0, 0, 0, 0, 0})}
end.


Expand All @@ -71,10 +74,10 @@ account_to_json({Address, DCBalance, DCNonce, SecBalance, SecNonce, Balance, Non
<<"address">> => Address,
<<"balance">> => Balance,
<<"nonce">> => Nonce,
<<"data_credit_balance">> => DCBalance,
<<"data_credit_nonce">> => DCNonce,
<<"security_balance">> => SecBalance,
<<"security_nonce">> => SecNonce
<<"dc_balance">> => DCBalance,
<<"dc_nonce">> => DCNonce,
<<"sec_balance">> => SecBalance,
<<"sec_nonce">> => SecNonce
};
account_to_json({Address, DCBalance, DCNonce, SecBalance, SecNonce, Balance, Nonce, SpecNonce}) ->
Base = account_to_json({Address, DCBalance, DCNonce, SecBalance, SecNonce, Balance, Nonce}),
Expand Down
29 changes: 28 additions & 1 deletion src/bh_route_hotspots.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

-export([prepare_conn/1, handle/3]).
%% Utilities
-export([get_hotspot_list/2, get_hotspot/1]).
-export([get_hotspot_list/2,
get_owner_hotspot_list/3,
get_hotspot/1]).


-define(S_HOTSPOT_LIST_BEFORE, "hotspot_list_before").
-define(S_HOTSPOT_LIST, "hotspot_list").
-define(S_OWNER_HOTSPOT_LIST_BEFORE, "owner_hotspot_list_before").
-define(S_OWNER_HOTSPOT_LIST, "owner_hotspot_list").
-define(S_HOTSPOT, "hotspot").

-define(SELECT_HOTSPOT_BASE, "select l.block, l.address, l.owner, l.location, l.score from gateway_ledger l ").
Expand All @@ -23,6 +27,12 @@ prepare_conn(Conn) ->
{ok, _} = epgsql:parse(Conn, ?S_HOTSPOT_LIST,
?SELECT_HOTSPOT_BASE "order by block desc, address limit $1", []),

{ok, _} = epgsql:parse(Conn, ?S_OWNER_HOTSPOT_LIST_BEFORE,
?SELECT_HOTSPOT_BASE "where l.owner = $1 and l.address < $2 order by block desc, address limit $3", []),

{ok, _} = epgsql:parse(Conn, ?S_OWNER_HOTSPOT_LIST,
?SELECT_HOTSPOT_BASE "where l.owner = $1 order by block desc, address limit $2", []),

{ok, _} = epgsql:parse(Conn, ?S_HOTSPOT,
?SELECT_HOTSPOT_BASE "where l.address = $1", []),

Expand Down Expand Up @@ -53,6 +63,21 @@ get_hotspot(Address) ->
{error, not_found}
end.

get_owner_hotspot_list(Account, undefined, Limit) ->
lager:info("ACCOUNT ~p ~p ~p", [Account, undefined, Limit]),
case ?PREPARED_QUERY(?S_OWNER_HOTSPOT_LIST, [Account, Limit]) of
{ok, _, Results} ->
{ok, hotspot_list_to_json(Results)};
Other ->
lager:error("OTHER ~p", [Other])
end;
get_owner_hotspot_list(Account, Before, Limit) ->
case ?PREPARED_QUERY(?S_OWNER_HOTSPOT_LIST_BEFORE, [Account, Before, Limit]) of
{ok, _, Results} ->
{ok, hotspot_list_to_json(Results)};
Other ->
lager:error("OTHER ~p", [Other])
end.

%%
%% to_jaon
Expand All @@ -62,9 +87,11 @@ hotspot_list_to_json(Results) ->
lists:map(fun hotspot_to_json/1, Results).

hotspot_to_json({Block, Address, Owner, Location, Score}) ->
{ok, Name} = erl_angry_purple_tiger:animal_name(Address),
?INSERT_LAT_LON(Location,
#{
<<"address">> => Address,
<<"name">> => list_to_binary(Name),
<<"owner">> => Owner,
<<"location">> => Location,
<<"score_update_height">> => Block,
Expand Down
1 change: 1 addition & 0 deletions src/blockchain_http.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
psql_migration,
helium_proto,
libp2p_crypto,
erl_angry_purple_tiger,
elli,
recon
]
Expand Down

0 comments on commit 9aebef1

Please sign in to comment.