[Utopian-IO] Retrieving Top Projects with the Utopian API

in #utopian-io7 years ago (edited)

The backend has new API endpoints for retrieving the top projects on the Utopian. This can be used for statistical or analytical purposes or for a front end to display the top projects. Other bug fixes were made as well :)

Here's what can be be done out of the box

Endpoint to retrieve the top 3 projects based on number of contributions in the last week
/api/posts/top?limit=3&start_date=2017-12-1&end_date=2017-12-8
Endpoint to retrieve the top new 4 projects of the last week based on the number of contributions/rewards
/api/posts/top?limit=4&start_date=2017-12-1&end_date=2017-12-8&include_rewards=true&only_new=true
Endpoint to retrieve the top 10 contributions of the last week, based on number of rewards
/api/posts/top?limit=10&start_date=2017-12-1&end_date=2017-12-8&include_rewards=true&sort_by=rewards

Query parameters

  • limit How many top projects to receive from the API
  • start_date Analyze top projects starting from this date.
  • end_date Analyze top projects ending to this day, used in combination with start_date to form a query for retrieving top projects in a given week.
  • include_rewards When set to true the Steem API will be queried to aggregate the rewards across the top projects.
  • sort_by Can be set to contributions or rewards. When set to rewards then include_rewards must be set to true for this to work. The Steem API will be queried for many more posts to get an accurate projection of top projects based only on rewards.

Technical details

Querying the local database is easy (as seen in the first endpoint sample). The server can aggregate the data quickly in less than a second. When rewards are involved, things are much trickier. We must now query the DB on many posts. The DB backend is MongoDB and data is queried through the aggregation pipeline.

First I tried response streaming but this wasn't playing well at all, it also had the downside of needing to query the blockchain again. This was then replaced with an async model. The API server will return a status and any data it completes.

There is a caching system in place for storing these expensive results. As a result data may become stale after a certain amount of time. So the caching system is capable of expiring the stale data after a certain amount of time, allowing for fresh data to be retrieved. The caching system is smart in regards to the parameters passed in and knows whether or not it is cached based on the query parameters parameter. A weak hash (md5) creates a unique identifier of the cache data in the backend. Caching happens entirely transparently to the user. Checks are added if the data has already been paid out or not, to avoid querying the Steem API if necessary.

A rate limiting system is in place as well to prevent abuse of this endpoint, but only applies if data needs to be queried from the blockchain. The limit is tied to a Steem account to prevent circumventing the system. The date range is limited to 8 days maximum and a maximum limit of 100 projects.

Refactoring

A universal post updater is now in use, allowing data queried from the Steem API to update the stale data in the DB, greatly simplifying the process and reducing coupling of the previous review function.

Bug fixes

  • No longer does the front end lock you out when you revoke GitHub access to Utopian
  • The auth system now uses refresh tokens, allowing the backend to nifty new things
  • Querying the DB for flagged posts no longer returns an empty set

Commits

All commits can be found at https://github.com/utopian-io/api.utopian.io/commits/master



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

1up-logo.png


You've got a 1UP from the @utopian-1up curation trail. 31 Utopians have upvoted your quality contribution to the open source community.

Join 1UP for better posts and high curation rewards.

1UP is neither organized nor endorsed by Utopian.io!

Nice work samrg ;)
Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Awesome job!

I don't understand all this mumbo jumbo o.o

Excellent work, bro.

Keep going Brother you are making amazing contributions

😁 😁 😁 with what i keep reading here expecially from sammy and other dev guys, I'm a rapper and a writer but you guys are already tuning me into a geek, ☺ ☺ ☺ keep up the good work for the greater good of utopian.

Perfect! That's what I need to automate the Utopian Weekly! Thanks!

brillant job

Great job Sam!

Hey @samrg472 I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Excellent code work. @Utopian-io is really benefitting from @samrg472!