This episode is for members only

Sign up to access "Build An Uptime Monitor with Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
09. Setting up endpoints

Episodes

0%
Your progress
  • Total: 4h 59m
  • Played: 0m
  • Remaining: 4h 59m
Join or sign in to track your progress

Transcript

00:00
So next up, we are focusing on our endpoints. And in this episode, we're going to go ahead and get the database set up and the model set up for our endpoints and talk a little bit about the relationship
00:12
between sites and endpoints. So first things first, we're just going to go ahead and create out an endpoint model here. And of course, let's generate a migration alongside of that
00:21
as well. So let's open up the Create Endpoints table migration. And let's start filling this in with the information that we would expect to see here.
00:29
Now, every single endpoint belongs to a site. So we're going to set up a foreign ID here with the site ID. We're going to constrain this.
00:38
And then really importantly, if a site is deleted, we're going to go ahead and cascade down and delete all of the endpoints for that particular site. Next up is the location, of course, really important.
00:52
That's going to be something like forward slash or forward slash pro, whatever URL for the site that you are monitoring. And we're just going to call that location.
01:01
The next thing and something we're going to have to return back to is the frequency. So this is going to be stored in seconds. It's shown as minutes on the UI, but we're
01:10
going to store it in the database as seconds. And this is going to be an unsigned integer. So let's go down and choose this. And of course, we're going to call that frequency.
01:20
Now, with this, we do need a default value. And this is where we're going to have to come back to this when we set up an enum to store the allowed frequencies. The reason that we're going to set up an enum
01:31
is that's going to contain a preset list of values that we're allowed to use. So for example, for one minute, we're going to have 60. For two minutes, it will be 120, et cetera, et cetera.
01:42
And this will allow us to only allow specific frequencies and also validate these properly as well. So just for now, we're going to set a default of 60, but we'll return to this when we set up the enum
01:54
so we can get this working nicely. OK, so we can go ahead and migrate the changes we have here. And we're going to head straight over to the site model first of all.
02:05
And we're going to set up the very simple relationship, which will be the endpoints for this site. So let's go ahead and say that this has many. And of course, passing the endpoint, and we're done.
02:18
So that's the very simple relationship to grab all endpoints for a site. Now, if we go over to the endpoint model itself, let's do a little bit of setup here.
02:27
The first thing that we'll do is set up the fillable fields. So this will be the location and the frequency. And that should just be about it. We did actually forget to include the next check.
02:41
So let's go ahead and first of all, roll back the changes that we just migrated. And let's head back over to the endpoints table. The next check is pretty important.
02:50
So this is going to be a timestamp. And we're going to call this next check. So what will happen is when we initially create the endpoint, the next check will be the current date, obviously,
03:01
plus, say, 60 seconds. And then when this is checked, what we will then do is go ahead and add 60 seconds onto that. Obviously, if it's a minute frequency, if it's more,
03:11
it will increment this by more. So next check is really important. It's never going to be null, because we always want the next check to be stored.
03:19
So let's go ahead and just re-migrate what we've done here. And we can go back over to our endpoint table and fill this in. So let's go ahead and add in the next check here.
03:28
We also want to make sure that we add some dates in here, or just one date in this case. And the reason that we're going to do that is so that when we access the next check,
03:38
that gives us back a carbon instance that we can easily work with just by adding this to this dates property. OK, so really the only last thing that we need to do here
03:48
is go ahead and set the relationship up back to the site in case we need to access this. And that's just going to be a belongs to relationship. And of course, that's going to belong to a site.
04:00
So there we go. We have our location, frequency, and next check, everything we need to go ahead and actually hit this at a particular frequency, and then increment
04:09
the next check so our queue can start to deal with that. In the next episode, let's return back to the frequency that we have here and talk about the enum, set that up, and create the enum default value inside of this migration.

Episode summary

In this episode, we're tackling the setup for our "endpoints." You'll see how we create the database migration and model for endpoints, and we spend a bit of time discussing how endpoints relate to sites. To kick things off, we generate a migration for the endpoints table and start filling out the expected fields, like the site foreign key, location, and frequency (which we store in seconds, even though it's shown as minutes in the UI).

We also talk about handling the situation where if a site is deleted, its endpoints should be deleted too—so we'll set up cascading deletes. Once the basic fields are in, we add a really important one: next_check, which tells us when to check the endpoint next. This gets kept up to date by adding the frequency to it each time the endpoint is checked.

After those fields are set up, we migrate the changes, and then it's time to wire up model relationships—so sites can easily access their endpoints, and each endpoint knows which site it belongs to.

We finish by noting we'll need an enum for allowed frequencies soon, but leave that for the next episode. You'll come out of this video with a solid endpoint table and model ready to roll.

Episode discussion

No comments, yet. Be the first!