Greenshift 012: Timestamps

in #greenshift6 years ago

So, last time we finished off our first relationship. We are now able to grab the permlink of the most recent piece of content voted upon by a single account. But as mentioned last time, the implementation wasn't that great and there was definitely room for improvement. Today, we'll add a couple improvements.

The first improvement we will perform is to remove the while loop from last time and simply grab the most recent 200 results rather than loop backwards. This means we only have to send a single query to the blockchain versus sending up to 10 queries. The computational overhead of filtering an extra 180 results is probably small compared to multiple requests to the Steem blockchain.

The second improvement will be to filter any content older than seven days old. As mentioned in the earlier brainstorming posts, we want to provide content consumers with content that they can vote upon. Luckily for us, each operation stored on the blockchain has an associated timestamp of the block that it was placed in. Thus we can calculate how old a piece of content is using that.


Greenshift F.png

So, above you can see how the code simplifies significantly by removing the extra logic. Sometimes even if you have a less efficient option, if an optimization requires so much additional complexity and thought, it may not be worth it. In our case, we improve the worst case scenario in both queries to the blockchains and content that we would have to filter through. That's because in the old code we cycled and filtered through some of the same content multiple times. Not so great if you ask me.

First in the loop, we calculate the age of the content by getting the timestamp. We then parse the timestamp string into a DateTime object before converting that object into time. This conversion is necessary if we are going to calculate the age of the content by subtracting the current time (another time object) by the time listed in the blockchain.

The last chance we have made to the code is that we have added another condition to our filter in that we make sure that the age is less the number of seconds in the week which is a defined constant which we list in all caps at the top of the program. It is normally convention to define constants in all caps and avoid using random numbers all over your code.

So now we have an improved and working relationship and are now ready to move onto the next one. Like the first one, we'll start from scratch in a separate folder and begin developing the next relationship. This one will be similar in structure to the current one. Where we find the latest piece of content by an author and grab the permlink from that relationship.

Sort:  

I recommend doing DateTime.parse(op[1].timestamp + 'Z') so you're parsing it as Zulu time (UTC).

That's a good point. Especially since Ruby Time uses UTC.

This post has been just added as new item to timeline of Q-Filter on Steem Projects.

If you want to be notified about new updates from this project, register on Steem Projects and add Q-Filter to your favorite projects.