#Blurtopian Development: Blocktivity Statistics API Update

in blurtopian •  7 months ago  (edited)


image.png

Have you ever experienced waking up and something's on fire? Well, that's what I experienced today. I woke up with an emergency. I was scheduled to work on another task this morning, unfortunately, because of a bug in the previous version of Blurt's blocktivity api, I ended up spending the last hour working and debugging it.

Yup, that's the life of a software developer. Sometimes your software works, sometimes it kicks you in the butt. Sometimes, you're a firefighter. This time, the fire's mental and in the software you created and you're the one responsible for putting out the fire.

I won't be writing much about it, but I'll be sharing the screenshots I shared with @megadrive.

Basically, the fault's mine. I assumed that the program's working as intended without checking or testing if it was indeed behaving the way I expected or thought it would behave.

The problem was in the setting of the timestamp field in the blockchainstats document. This field determined a document's time-to-live[1] in mongodb:

Stands for “time to live” and represents an expiration time or period for a given piece of information to remain in a cache or other temporary storage before the system deletes it or ages it out. MongoDB has a TTL collection feature. See Expire Data from Collections by Setting TTL.
[1] https://docs.mongodb.com/manual/reference/glossary/#term-ttl

It seems that the timestamp which I declared to have a default value of Date.now is not being set when I use mongoose's findOneAndUpdate. I'm not sure but that seems to be the case.

const BlockchainStatsSchema = new Schema({
  ...
  timestamp: {
    type: Date,
    expires: '125m',
    default: Date.now
  },
});

I chose to use this method as an elegant way of writing code. Unfortunately again, writing for elegance is not always the best approach when using a syntax you're not yet familiar with.

When starting, writing for safety is better than writing for elegance

As such, I had to re-write the code so that it ensures that the timestamp field is set. The fix is in this commit.

Instead of just using findOneAndUpdate, I checked first if a document for a certain hour already exists. If it's not, then I create a new BlockchainStats document and use the model's save method.

      let hour = moment().utc().format("HH00");
      let doc = await BlockchainStats.find({ key: hour });
      if (doc.length === 0) {
        let doc = new BlockchainStats(
          {
            key: hour,
            operations: ops_count,
            transactions : trx_count,
            timestamp: new Date()
          }
        );
        await doc.save();
      } else {
        await BlockchainStats.findOneAndUpdate(
          { key: hour },
          {
            $inc : { operations: ops_count, transactions : trx_count }
          }
        );
       }

To check if the timestamp field is set and that the document gets automatically deleted when it expires, here are the screenshots I shared with @megadrive.

TTL index definition

image.png

Timestamp set

image.png

Document automatically deleted (after 125 minutes)

image.png

Again, I'd like to apologize to @megadrive for this miss and would like to thank @therealwolf for reporting it.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  

I wish I could understand much more of this Techy stuff, but I'm just very glad that you have the technical ability to fix code for Blurt 🤓

Thank you. Besides fixing, there's also the importance of testing. :)

The content of this post goes above my head :)

However, it appears to me that you are doing great job for this platform. Claps :)

No worries. This is highly technical. I'll try to add a tldr; to explain it in a non-technical way.

Thanks for your quick response and professional way of handling it!

Thank you. Yup, didn't want to put gas on fire.

Congratulations! This post has been upvoted by the @blurtcurator communal account,
You can request a vote every 12 hours from the #getupvote channel in the official Blurt Discord.Don't wait to join ,lots of good stuff happening there.