Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add asset names and amount/price calculations to additional #1351

Merged
merged 8 commits into from
Oct 3, 2018
35 changes: 35 additions & 0 deletions libraries/plugins/elasticsearch/elasticsearch_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class elasticsearch_plugin_impl
virtual ~elasticsearch_plugin_impl();

bool update_account_histories( const signed_block& b );
void populatePowTable();

graphene::chain::database& database()
{
Expand Down Expand Up @@ -72,6 +73,7 @@ class elasticsearch_plugin_impl
std::string bulk_line;
std::string index_name;
bool is_sync = false;
map <uint8_t , uint64_t > lookup_pow_table;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a simple static const array with static initialization instead of a map? Would be much simpler IMO.
Also, use double values instead of uint64_t - they're always converted to double anyway, and it even saves you some casting when using the values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, i agree and changed. cant make it static as a member of elasticsearch_plugin_impl. maybe somewhere else, however not sure where.

private:
bool add_elasticsearch( const account_id_type account_id, const optional<operation_history_object>& oho );
const account_transaction_history_object& addNewEntry(const account_statistics_object& stats_obj,
Expand Down Expand Up @@ -227,23 +229,37 @@ void elasticsearch_plugin_impl::doBlock(uint32_t trx_in_block, const signed_bloc

void elasticsearch_plugin_impl::doVisitor(const optional <operation_history_object>& oho)
{
graphene::chain::database& db = database();

operation_visitor o_v;
oho->op.visit(o_v);

vs.fee_data.asset = o_v.fee_asset;
vs.fee_data.asset_name = o_v.fee_asset(db).symbol;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're doing two lookups on fee_asset - better to look up only once.

vs.fee_data.amount = o_v.fee_amount;
vs.fee_data.amount_units = (double)(o_v.fee_amount.value)/lookup_pow_table[o_v.fee_asset(db).precision];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove all (double) casts here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to keep (double) cast in the fill_price_units when using the lookup table integrated in the codebase or the program will segfault with SIGFPE, Arithmetic exception.


vs.transfer_data.asset = o_v.transfer_asset_id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for transfer_asset.

vs.transfer_data.asset_name = o_v.transfer_asset_id(db).symbol;
vs.transfer_data.amount = o_v.transfer_amount;
vs.transfer_data.amount_units = (double)(o_v.transfer_amount.value)/lookup_pow_table[o_v.transfer_asset_id(db).precision];
vs.transfer_data.from = o_v.transfer_from;
vs.transfer_data.to = o_v.transfer_to;

vs.fill_data.order_id = o_v.fill_order_id;
vs.fill_data.account_id = o_v.fill_account_id;
vs.fill_data.pays_asset_id = o_v.fill_pays_asset_id;
vs.fill_data.pays_asset_name = o_v.fill_pays_asset_id(db).symbol;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three lookups for fill_pays_asset and fill_receives_asset each.

vs.fill_data.pays_amount = o_v.fill_pays_amount;
vs.fill_data.pays_amount_units = (double)(o_v.fill_pays_amount.value)/lookup_pow_table[o_v.fill_pays_asset_id(db).precision];
vs.fill_data.receives_asset_id = o_v.fill_receives_asset_id;
vs.fill_data.receives_asset_name = o_v.fill_receives_asset_id(db).symbol;
vs.fill_data.receives_amount = o_v.fill_receives_amount;
vs.fill_data.receives_amount_units = (double)(o_v.fill_receives_amount.value)/lookup_pow_table[o_v.fill_receives_asset_id(db).precision];

auto fill_price = (double)(o_v.fill_receives_amount.value/lookup_pow_table[o_v.fill_receives_asset_id(db).precision]) /
(double)(o_v.fill_pays_amount.value / lookup_pow_table[o_v.fill_pays_asset_id(db).precision]);
vs.fill_data.fill_price_units = fill_price;
vs.fill_data.fill_price = o_v.fill_fill_price;
vs.fill_data.is_maker = o_v.fill_is_maker;
}
Expand Down Expand Up @@ -368,6 +384,23 @@ void elasticsearch_plugin_impl::populateESstruct()
es.query = "";
}

void elasticsearch_plugin_impl::populatePowTable()
{
lookup_pow_table[0] = 1;
lookup_pow_table[1] = 10;
lookup_pow_table[2] = 100;
lookup_pow_table[3] = 1000;
lookup_pow_table[4] = 10000;
lookup_pow_table[5] = 100000;
lookup_pow_table[6] = 1000000;
lookup_pow_table[7] = 10000000;
lookup_pow_table[8] = 100000000;
lookup_pow_table[9] = 1000000000;
lookup_pow_table[10] = 10000000000;
lookup_pow_table[11] = 100000000000;
lookup_pow_table[12] = 1000000000000;
}

} // end namespace detail

elasticsearch_plugin::elasticsearch_plugin() :
Expand Down Expand Up @@ -445,6 +478,8 @@ void elasticsearch_plugin::plugin_startup()
if(!graphene::utilities::checkES(es))
FC_THROW_EXCEPTION(fc::exception, "ES database is not up in url ${url}", ("url", my->_elasticsearch_node_url));
ilog("elasticsearch ACCOUNT HISTORY: plugin_startup() begin");

my->populatePowTable();
}

} }
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,16 @@ struct block_struct {

struct fee_struct {
asset_id_type asset;
std::string asset_name;
share_type amount;
double amount_units;
};

struct transfer_struct {
asset_id_type asset;
std::string asset_name;
share_type amount;
double amount_units;
account_id_type from;
account_id_type to;
};
Expand All @@ -152,10 +156,15 @@ struct fill_struct {
object_id_type order_id;
account_id_type account_id;
asset_id_type pays_asset_id;
std::string pays_asset_name;
share_type pays_amount;
double pays_amount_units;
asset_id_type receives_asset_id;
std::string receives_asset_name;
share_type receives_amount;
double receives_amount_units;
double fill_price;
double fill_price_units;
bool is_maker;
};

Expand All @@ -178,8 +187,10 @@ struct bulk_struct {

FC_REFLECT( graphene::elasticsearch::operation_history_struct, (trx_in_block)(op_in_trx)(operation_result)(virtual_op)(op) )
FC_REFLECT( graphene::elasticsearch::block_struct, (block_num)(block_time)(trx_id) )
FC_REFLECT( graphene::elasticsearch::fee_struct, (asset)(amount) )
FC_REFLECT( graphene::elasticsearch::transfer_struct, (asset)(amount)(from)(to) )
FC_REFLECT( graphene::elasticsearch::fill_struct, (order_id)(account_id)(pays_asset_id)(pays_amount)(receives_asset_id)(receives_amount)(fill_price)(is_maker))
FC_REFLECT( graphene::elasticsearch::fee_struct, (asset)(asset_name)(amount)(amount_units) )
FC_REFLECT( graphene::elasticsearch::transfer_struct, (asset)(asset_name)(amount)(amount_units)(from)(to) )
FC_REFLECT( graphene::elasticsearch::fill_struct, (order_id)(account_id)(pays_asset_id)(pays_asset_name)(pays_amount)(pays_amount_units)
(receives_asset_id)(receives_asset_name)(receives_amount)(receives_amount_units)(fill_price)
(fill_price_units)(is_maker))
FC_REFLECT( graphene::elasticsearch::visitor_struct, (fee_data)(transfer_data)(fill_data) )
FC_REFLECT( graphene::elasticsearch::bulk_struct, (account_history)(operation_history)(operation_type)(operation_id_num)(block_data)(additional_data) )