The world’s leading publication for data science, AI, and ML professionals.

Build an SMS Alert System for Canada Covid-19 Cases over the weekend with Rust and Zero Cost

Weekend build and learn

.

SMS Alert for Ontario, Canada Covid-19 new cases

Before we start this weekend build and learn, I would like to take a moment to thank you, all my readers, who always support me and provide me with some suggestions and guidance.

Photo by Pro Church Media on Unsplash
Photo by Pro Church Media on Unsplash

You are the most important motivation for me to keep this journey going. If you like my weekend build and learn series, please follow me on Medium.

Wei Huang – Medium


Use cases

Covid-19 has changed many of our daily life; with the new normal situation, we think and do things differently.

Every day, my wife and son are trying to get the latest update from the internet about the Covid-19 net new case growth in Ontario, Canada, which has become their daily routine.

This week, my son is busy with his school work. So he misses his routine. When my wife asks him about the number, he suddenly realizes that he forgets this essential daily activity. When he rushed out to the computer and started searching for the number, one of his words catches my attention.

Will be awesome if can get some push notification about those daily number!

"Right! What you give me a new idea for my next weekend, build and learn series. Thank you, son."

The idea always comes from our daily life.

Photo by AbsolutVision on Unsplash
Photo by AbsolutVision on Unsplash

Non-functional Requirements

Let’s see what we want to build before putting our hands on the keyboard, which is an important step.

  1. Rust lang.
  2. Cost-efficient.
  3. For push notification, let’s use the SMS for initial use cases, which Twilio provides some out of box feature. And most importantly, there is a free subscription too.
  4. Capability to scale to different use cases, such as email notification or even further chatbot implementation – design Pattern. (Dependency Inversion Principle and Open/Closed Principle)
  5. Timely scheduler – Cron job.
  6. Easy to build and deploy – My favorite Heroku.
  7. Accurate data source – Canada government website provided some open and trustable data sources.

Epidemiological summary of COVID-19 cases in Canada – Canada.ca

Architecture & Dataflow

After some first-hand analysis:

  1. The data available on the website is the CSV file.
  2. The data will update daily at 7 PM EDT. Some time expect a 1-hour delay.
  3. That schema covered all we need, such as date, total new case number per Province, etc. For simplicity, we only focus on the entire new case number and total unique death number.
  4. When we finish processed, we need to clean up the tmp file. Mitigate un-necessary space and operational overhead.
simply dataflow - Screenshot by Author
simply dataflow – Screenshot by Author

Let’s start the coding part.

Hands on the keyboard.

Code Structure - Screenshot by Author
Code Structure – Screenshot by Author

Code structure

The key business logical in this application have 3 majors parts:

  1. Download, based HTTP request to get the csv file and return the file path for future processing.
  2. Process – MapReduce, only for the Ontario number, seems that’s what my son wants it :), which is easy to scale to different use cases, such as save to the DB, or producer to Near-Realtime, streaming, etc.
  3. Alert component, because we want this can scale based on the open/closed principle, opens extending the new use cases, however, closed to modification of the current implementation.

Additionally, the Procfile, it’s the Heroku schedule implementation.


Download in action

Processing with MapReduce in Rust way

Alert component

For the alert component, we will use the Rust Trait, which simulates Golang interface implementation. The benefit of it: we could choose a specific use case implementation, such as Sms, Email, or chatbot, while the general abstraction workflow remains the same. (Dependency Inversion Principle and Open/Closed Principle)

Next time we need to add Email use cases, we only create the email.rs in the alert folder without touch underline mod.rs within the alert component.

Final Result

job execution logs - Screenshot by Author
job execution logs – Screenshot by Author

Deployment and Cron job setup

For my personal project, I usually use the Heroku for the deployment; one important aspect is the pricing model, scale, and use as a pay as we go.

We can also build containers to deploy to most cloud container providers, such as AKS, AWS, and GKE. I have written some articles on this topic.

Processing 14 GB files with Rust – Part 3— Azure Stacks

However, at the end of the day is always the "build vs. buy" decision. For a builder, I really love how easy it is to start the development, deploy, and set up the DevOps capabilities on top of Heroku.

Heroku, all the necessary plugin is always available, such as build pack, cron jobs set up are mostly straightforward and easy.

Heroku Dev Center

The CRON job in Heroku is really easy to setup. In the Procfile simple add

CRON job setup - Screenshot by Author
CRON job setup – Screenshot by Author

And go to the Heroku UI config your scheduler as needed.

setup the in UI - Screenshot by Author
setup the in UI – Screenshot by Author

Final result

Finally, for my son, every day at 8:30 PM, the SMS arrived. I’m also using the free tiers for both Heroku and Twilio services, so the total cost is 0 $.

SMS message - Screenshot by Author
SMS message – Screenshot by Author

Closing thoughts

  1. The building idea usually comes from real life. Enjoy it, listen to it, and feel it.
  2. When you build, you need to think about the scale. Think about getting most of the benefits out of it for your use cases; this comes back to our daily works. Reduce the I.T. Infrastruce cost is not nice to have; it’s a must to have. The fundamental back to how you build it. A great example is why I always trace the container build size, which is the most important indicator for your run time footprint, which will impact your cloud runtime cost. Etc.
  3. Code it right, code it for scale, code it with the lower cost.

That’s it for this week. And by the way, my son is really excited about what I built. Now we will start coding python together.

Thank you again for your time to read my weekend build and learn. See you next time.

See my other articles below for more weekend build and learn, which you may be interested in.

Wei Huang – Medium


Related Articles