Writing Basic Scheduling

Muhammad Ayaz Dzulfikar
MeetU Engineering
Published in
3 min readApr 3, 2018

Last week was quite hectic for us in MeetU Engineering, as we were going to have our first sprint review. There are several tasks that we have to do, and we decided to pull an all-nighter to do them. However, it looks like our tools can smell our stress, and decided to break every time we push.

Bloody pipeline, can’t even make it past build stage

OK, Enough chit-chat :”) For last week, I wrote logic to connect our home activity with the server, and wrote algorithm + backend for basic time scheduling. As logic for connecting is quite boring (really, once you’ve done it at least once somehow it’s easy to do it again), I want to share my experience in writing stuff for basic time scheduling.

Basic Time Scheduling

In MeetU, we want to have a feature to give the user several time options to hold a meetup. User may give us time range where the meetup will be held, how the meetup will repeat (once, daily, weekly, or even monthly), and how many times it will repeat. Then, MeetU will give several time intervals, where he/she can create a meetup from that time onwards, according to the repetition.

As for the current sprint, the requirements for the scheduling were quite simple:

  • There is only 1 user and 1 group, so fetching data is quite trivial
  • Meetup can only be held if everyone is available, it makes part of the logic quite easy

And so, I wrote the algorithm in python, as we use Django for our backend. As for the algorithm, I break it into several parts:

  • Fetching the data
  • Finding all available time (in minutes) in each day, using “+1 -1” trick I learned in Competitive Programming
  • Checking whether we can use a day as the start of the repetition for the meetup, using data from previous step and simple loop
  • Finally, combining data from each day, creating as large time interval as possible

Let’s ignore the 1st step as it is database related :) While each step has to be done sequentially, I wrote the algorithm spesifically so we can do potential speed up, like splitting the task in parallel. However, I don’t do it right now, as it is quite risky as my experience in that stuff is still limited.

REST API

After that, I have to write end-point for the REST API. Thankfully Ricky already wrote some stuff for the API, so I can take a glance on them :P All was well, until I can’t get the JSON data from request! Turns out, it was my fault. Ricky wrote POST method, and to get the data we have to use .data. Me, stupid as always, wrote GET method, tried to use .data, and gloriously failed to get the data. After a bit googling, I found out that I should’ve used .query_params. Well, today I learned. Though, after discussing with Ricky, in the end, we decided to go with POST.

Now then, suppose the user’s current schedule is as shown below

Taken from our debug Django view

And then suppose we have our request like this:

{
"start_time": "2018-02-01T00:00:00Z",
"end_time": "2018-02-01T00:00:00Z",
"repeat_type": "DAILY",
"duration": 1
}

Then, the algorithm will return:

Again, taken from our debug Django view

Conclusion

I rarely wrote something very algorithmic in python (usually I used C++), so it was quite fun, with a mix of hilarious stuff (semicolon vs tab). However, coding it was quite simple. Also, by doing this task I learned a bit about Django, mostly about the Model and API.

I think that’s it for now. There are several things that we planned to add to this scheduling algorithm, so stay tuned!

--

--