Skip to content

Commit

Permalink
Merge pull request #579 from steemit/578-trending-tags
Browse files Browse the repository at this point in the history
Report trending tags #578
  • Loading branch information
Michael Vandeberg committed Nov 18, 2016
2 parents c831bcf + 8c3bfc7 commit 0b8c68f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 25 deletions.
42 changes: 28 additions & 14 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,17 +1060,31 @@ map< uint32_t, applied_operation > database_api::get_account_history( string acc
return result;
}

vector<tags::tag_stats_object> database_api::get_trending_tags( string after, uint32_t limit )const {
vector<tags::tag_stats_object> result;
const auto& tags_stats_idx = my->_db.get_index<tags::tag_stats_index>().indices().get<tags::by_tag>();
auto itr = tags_stats_idx.lower_bound(after);
vector<tag_api_obj> database_api::get_trending_tags( string after, uint32_t limit )const
{
limit = std::min( limit, uint32_t(100) );
vector<tag_api_obj> result;
result.reserve( limit );

while( itr != tags_stats_idx.end() && limit > 0 ) {
result.push_back(*itr);
--limit; ++itr;
}
const auto& nidx = my->_db.get_index<tags::tag_stats_index>().indices().get<tags::by_tag>();

return result;
const auto& ridx = my->_db.get_index<tags::tag_stats_index>().indices().get<tags::by_trending>();
auto itr = ridx.begin();
if( after != "" && nidx.size() )
{
auto nitr = nidx.lower_bound( after );
if( nitr == nidx.end() )
itr = ridx.end();
else
itr = ridx.iterator_to( *nitr );
}

while( itr != ridx.end() && result.size() < limit )
{
result.push_back( *itr );
++itr;
}
return result;
}

discussion database_api::get_discussion( comment_id_type id )const {
Expand Down Expand Up @@ -1569,12 +1583,12 @@ state database_api::get_state( string path )const
path = "trending";

/// FETCH CATEGORY STATE
auto trending_cat = get_trending_categories( "", 100 );
for( const auto& c : trending_cat )
auto trending_tags = get_trending_tags( "", 100 );
for( const auto& t : trending_tags )
{
string name = c.name;
_state.category_idx.trending.push_back( name );
_state.categories[ name ] = category_api_obj( c );
string name = t.name;
_state.tag_idx.trending.push_back( name );
_state.tags[ name ] = t;
}
auto best_cat = get_best_categories( "", 50 );
for( const auto& c : best_cat )
Expand Down
2 changes: 1 addition & 1 deletion libraries/app/include/steemit/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class database_api
*/
void cancel_all_subscriptions();

vector<tags::tag_stats_object> get_trending_tags( string after_tag, uint32_t limit )const;
vector<tag_api_obj> get_trending_tags( string after_tag, uint32_t limit )const;

/**
* This API is a short-cut for returning all of the state required for a particular URL
Expand Down
15 changes: 12 additions & 3 deletions libraries/app/include/steemit/app/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ namespace steemit { namespace app {

struct category_index
{
vector< string > trending; /// pending payouts
vector< string > active; /// recent activity
vector< string > recent; /// recently created
vector< string > best; /// total lifetime payout
};

struct tag_index
{
vector< string > trending; /// pending payouts
};

struct vote_state
{
string voter;
Expand Down Expand Up @@ -151,12 +155,16 @@ namespace steemit { namespace app {
*/
app::category_index category_idx;

app::tag_index tag_idx;

/**
* "" is the global discussion index, otherwise the indicies are ranked by category
*/
map<string, discussion_index> discussion_idx;

map< string, category_api_obj > categories;
map< string, tag_api_obj > tags;

/**
* map from account/slug to full nested discussion
*/
Expand Down Expand Up @@ -186,10 +194,11 @@ FC_REFLECT( steemit::app::vote_state, (voter)(weight)(rshares)(percent)(reputati
FC_REFLECT( steemit::app::account_vote, (authorperm)(weight)(rshares)(percent)(time) );

FC_REFLECT( steemit::app::discussion_index, (category)(trending)(trending30)(updated)(created)(responses)(active)(votes)(maturing)(best)(hot)(promoted)(cashout) )
FC_REFLECT( steemit::app::category_index, (trending)(active)(recent)(best) )
FC_REFLECT( steemit::app::category_index, (active)(recent)(best) )
FC_REFLECT( steemit::app::tag_index, (trending) )
FC_REFLECT_DERIVED( steemit::app::discussion, (steemit::app::comment_api_obj), (url)(root_title)(pending_payout_value)(total_pending_payout_value)(active_votes)(replies)(author_reputation)(promoted)(first_reblogged_by)(first_reblogged_on) )

FC_REFLECT( steemit::app::state, (current_route)(props)(category_idx)(categories)(content)(accounts)(pow_queue)(witnesses)(discussion_idx)(witness_schedule)(feed_price)(error)(market_data) )
FC_REFLECT( steemit::app::state, (current_route)(props)(category_idx)(tag_idx)(categories)(tags)(content)(accounts)(pow_queue)(witnesses)(discussion_idx)(witness_schedule)(feed_price)(error)(market_data) )

FC_REFLECT_DERIVED( steemit::app::extended_limit_order, (steemit::app::limit_order_api_obj), (real_price)(rewarded) )
FC_REFLECT( steemit::app::order_history_item, (time)(type)(sbd_quantity)(steem_quantity)(real_price) );
Expand Down
31 changes: 31 additions & 0 deletions libraries/app/include/steemit/app/steem_api_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <steemit/chain/transaction_object.hpp>
#include <steemit/chain/witness_objects.hpp>

#include <steemit/tags/tags_plugin.hpp>

namespace steemit { namespace app {

using namespace steemit::chain;
Expand Down Expand Up @@ -160,6 +162,26 @@ struct category_api_obj
time_point_sec last_update;
};

struct tag_api_obj
{
tag_api_obj( const tags::tag_stats_object& o ) :
name( o.tag ),
total_children_rshares2(o.total_children_rshares2),
total_payouts(o.total_payout),
net_votes(o.net_votes),
top_posts(o.top_posts),
comments(o.comments) {}

tag_api_obj() {}

string name;
fc::uint128_t total_children_rshares2;
asset total_payouts;
int32_t net_votes = 0;
uint32_t top_posts = 0;
uint32_t comments = 0;
};

struct account_api_obj
{
account_api_obj( const chain::account_object& a, const chain::account_authority_object& auth ) :
Expand Down Expand Up @@ -483,6 +505,15 @@ FC_REFLECT( steemit::app::feed_history_api_obj,
(price_history)
)

FC_REFLECT( steemit::app::tag_api_obj,
(name)
(total_children_rshares2)
(total_payouts)
(net_votes)
(top_posts)
(comments)
)

FC_REFLECT( steemit::app::witness_api_obj,
(id)
(owner)
Expand Down
16 changes: 9 additions & 7 deletions libraries/plugins/tags/include/steemit/tags/tags_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,26 +279,28 @@ typedef multi_index_container<
indexed_by<
ordered_unique< tag< by_id >, member< tag_stats_object, tag_stats_id_type, &tag_stats_object::id > >,
ordered_unique< tag< by_tag >, member< tag_stats_object, tag_name_type, &tag_stats_object::tag > >,
/*
ordered_non_unique< tag< by_comments >,
composite_key< tag_stats_object,
member< tag_stats_object, tag_name_type, &tag_stats_object::tag >,
member< tag_stats_object, uint32_t, &tag_stats_object::comments >
member< tag_stats_object, uint32_t, &tag_stats_object::comments >,
member< tag_stats_object, tag_name_type, &tag_stats_object::tag >
>,
composite_key_compare< std::less< tag_name_type >, std::greater< uint32_t > >
>,
ordered_non_unique< tag< by_top_posts >,
composite_key< tag_stats_object,
member< tag_stats_object, tag_name_type, &tag_stats_object::tag >,
member< tag_stats_object, uint32_t, &tag_stats_object::top_posts >
member< tag_stats_object, uint32_t, &tag_stats_object::top_posts >,
member< tag_stats_object, tag_name_type, &tag_stats_object::tag >
>,
composite_key_compare< std::less< tag_name_type >, std::greater< uint32_t > >
>,
*/
ordered_non_unique< tag< by_trending >,
composite_key< tag_stats_object,
member< tag_stats_object, tag_name_type, &tag_stats_object::tag >,
member< tag_stats_object, fc::uint128_t, &tag_stats_object::total_children_rshares2 >
member< tag_stats_object, fc::uint128_t, &tag_stats_object::total_children_rshares2 >,
member< tag_stats_object, tag_name_type, &tag_stats_object::tag >
>,
composite_key_compare< std::less< tag_name_type >, std::greater< uint128_t > >
composite_key_compare< std::greater< uint128_t >, std::less< tag_name_type > >
>
>,
allocator< tag_stats_object >
Expand Down

0 comments on commit 0b8c68f

Please sign in to comment.