Skip to content

Commit

Permalink
peerplays-network#237 Move generateIndexName(...) from util to ES plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Dobromyslov committed Jan 20, 2022
1 parent d04e0c1 commit 694fcf0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
14 changes: 12 additions & 2 deletions libraries/plugins/elasticsearch/elasticsearch_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* THE SOFTWARE.
*/

#include <boost/algorithm/string.hpp>
#include <graphene/elasticsearch/elasticsearch_plugin.hpp>
#include <graphene/chain/impacted.hpp>
#include <graphene/chain/account_evaluator.hpp>
Expand All @@ -33,6 +34,15 @@ namespace graphene { namespace elasticsearch {
namespace detail
{

const std::string generateIndexName(const fc::time_point_sec& block_date, const std::string& _elasticsearch_index_prefix)
{
auto block_date_string = block_date.to_iso_string();
std::vector<std::string> parts;
boost::split(parts, block_date_string, boost::is_any_of("-"));
std::string index_name = _elasticsearch_index_prefix + parts[0] + "-" + parts[1];
return index_name;
}

class elasticsearch_plugin_impl
{
public:
Expand Down Expand Up @@ -107,7 +117,7 @@ elasticsearch_plugin_impl::~elasticsearch_plugin_impl()
bool elasticsearch_plugin_impl::update_account_histories( const signed_block& b )
{
checkState(b.timestamp);
index_name = graphene::utilities::generateIndexName(b.timestamp, _elasticsearch_index_prefix);
index_name = generateIndexName(b.timestamp, _elasticsearch_index_prefix);

graphene::chain::database& db = database();
const vector<optional< operation_history_object > >& hist = db.get_applied_operations();
Expand Down Expand Up @@ -710,7 +720,7 @@ void elasticsearch_plugin::plugin_initialize(const boost::program_options::varia
void elasticsearch_plugin::plugin_startup()
{
// Nothing to do

ilog("elasticsearch ACCOUNT HISTORY: plugin_startup() begin");
}

Expand Down
10 changes: 0 additions & 10 deletions libraries/utilities/elasticsearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <graphene/utilities/elasticsearch.hpp>

#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string.hpp>
#include <fc/log/logger.hpp>
#include <fc/io/json.hpp>

Expand Down Expand Up @@ -182,15 +181,6 @@ const std::string getEndPoint(ES& es)
return doCurl(curl_request);
}

const std::string generateIndexName(const fc::time_point_sec& block_date, const std::string& _elasticsearch_index_prefix)
{
auto block_date_string = block_date.to_iso_string();
std::vector<std::string> parts;
boost::split(parts, block_date_string, boost::is_any_of("-"));
std::string index_name = _elasticsearch_index_prefix + parts[0] + "-" + parts[1];
return index_name;
}

const std::string doCurl(CurlRequest& curl)
{
std::string CurlReadBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ namespace graphene { namespace utilities {
bool deleteAll(ES& es);
bool handleBulkResponse(long http_code, const std::string& CurlReadBuffer);
const std::string getEndPoint(ES& es);
const std::string generateIndexName(const fc::time_point_sec& block_date, const std::string& _elasticsearch_index_prefix);
const std::string doCurl(CurlRequest& curl);
const std::string joinBulkLines(const std::vector<std::string>& bulk);
long getResponseCode(CURL *handler);
Expand Down
24 changes: 14 additions & 10 deletions tests/elasticsearch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ using namespace graphene::chain;
using namespace graphene::chain::test;
using namespace graphene::app;

const std::string g_es_url = "http://localhost:9200/";
const std::string g_es_index_prefix = "peerplays-";
const std::string g_es_ppobjects_prefix = "ppobjects-";

BOOST_FIXTURE_TEST_SUITE( elasticsearch_tests, database_fixture )

BOOST_AUTO_TEST_CASE(elasticsearch_account_history) {
Expand All @@ -48,8 +52,8 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) {

graphene::utilities::ES es;
es.curl = curl;
es.elasticsearch_url = "http://localhost:9200/";
es.index_prefix = "peerplays-";
es.elasticsearch_url = g_es_url;
es.index_prefix = g_es_index_prefix;
//es.auth = "elastic:changeme";

// delete all first
Expand Down Expand Up @@ -114,7 +118,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) {

// check the visitor data
auto block_date = db.head_block_time();
std::string index_name = graphene::utilities::generateIndexName(block_date, "peerplays-");
std::string index_name = g_es_index_prefix + block_date.to_iso_string().substr( 0, 7 ); // yyyy-mm

es.endpoint = index_name + "/_doc/2.9.12"; // we know last op is a transfer of amount 300
res = graphene::utilities::getEndPoint(es);
Expand All @@ -137,8 +141,8 @@ BOOST_AUTO_TEST_CASE(elasticsearch_objects) {

graphene::utilities::ES es;
es.curl = curl;
es.elasticsearch_url = "http://localhost:9200/";
es.index_prefix = "ppobjects-";
es.elasticsearch_url = g_es_url;
es.index_prefix = g_es_ppobjects_prefix;
//es.auth = "elastic:changeme";

// delete all first
Expand Down Expand Up @@ -192,11 +196,11 @@ BOOST_AUTO_TEST_CASE(elasticsearch_suite) {

graphene::utilities::ES es;
es.curl = curl;
es.elasticsearch_url = "http://localhost:9200/";
es.index_prefix = "peerplays-";
es.elasticsearch_url = g_es_url;
es.index_prefix = g_es_index_prefix;
auto delete_account_history = graphene::utilities::deleteAll(es);
fc::usleep(fc::milliseconds(1000));
es.index_prefix = "ppobjects-";
es.index_prefix = g_es_ppobjects_prefix;
auto delete_objects = graphene::utilities::deleteAll(es);
fc::usleep(fc::milliseconds(1000));

Expand All @@ -218,8 +222,8 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) {

graphene::utilities::ES es;
es.curl = curl;
es.elasticsearch_url = "http://localhost:9200/";
es.index_prefix = "peerplays-";
es.elasticsearch_url = g_es_url;
es.index_prefix = g_es_index_prefix;

auto delete_account_history = graphene::utilities::deleteAll(es);

Expand Down

0 comments on commit 694fcf0

Please sign in to comment.