This episode is for members only

Get started

Already a member? Sign in to continue

Playing

30. Calculating uptime for endpoints

01. Introduction

02. Sites

03. Endpoints

09. Setting up endpoints

4m 23s

0%

10. Enum endpoint frequencies

3m 50s

0%

11. Creating the endpoint form

12m 58s

0%

12. Storing an endpoint

7m 21s

0%

13. Validating and authorizing endpoint stores

7m 6s

0%

14. Normalizing the endpoint location

6m 4s

0%

15. Displaying endpoints

6m 22s

0%

16. Deleting endpoints

6m 21s

0%

17. Toggling inline editing for endpoints

5m 12s

0%

18. Applying a watcher to the endpoint form

5m 31s

0%

19. Storing endpoint edits

8m 56s

0%

04. Checking endpoints

20. Determining endpoints to check

2m 57s

0%

21. Creating a job to perform a check

3m 21s

0%

22. Making an HTTP request

3m 39s

0%

23. Scheduling checks every second

4m 27s

0%

24. Setting up the check relationship

3m 14s

0%

25. Updating the job

2m 10s

0%

26. Showing the last status

12m 50s

0%

27. Expanded list of checks

14m 10s

0%

28. The response body modal

2m 49s

0%

29. Authorizing the endpoint details

2m 27s

0%

30. Calculating uptime for endpoints

8m 29s

0%

06. Email notifications

07. Downtime notifications

00:00

We're now going to implement the uptime percentage that we have for each of our endpoints. And this is a little trickier than it might seem at first. So if we go over to the pricing location here,

00:12

you can see that we've got three successful responses and three unsuccessful responses. So we know that overall for this endpoint over its lifetime has a 50% success rate. So we want the uptime for this to display 50%. Now let's look at how we might start to calculate this out.

00:31

We're going to implement this as a method over on the endpoint model. So let's just go up here before we get to our relationships. And let's create out a method in here to handle this. So let's just call this uptime percentage.

00:45

And this is just going to be over time. We're not going to look into granularity detail. So what we really want to do in here, and let's just kind of mock this out. We want to return the number of successful checks overall.

00:59

So that could be any number. We want to divide that by the overall check count, which we already know how to do. We can access the checks relationship here and grab the count. Or we could use an aggregate to pull this in.

01:13

And then we want to multiply that by 100. So that is the equation to do that. Very simple. So how do we go about finding out, first of all, we'll leave this for now,

01:25

the number of successful checks? Well, we could do that all in here and calculate that out all in here. But what it would be really good to do is for every single endpoint, always include a count as an aggregate of the total amount of successful checks.

01:41

Now to do this, what we can do is over on the site model itself, where we have the checks or the endpoint relationship here, we could also then go ahead and include with count. So we can do this count at the database level,

01:56

just so we don't have to iterate through all of the endpoint checks and build this up in code. So what we can do within Laravel is we can count a particular column. So checks, for example, we could count. Now we want to name this to something different.

02:09

We want to call this successful checks count. And then what we can do here is assign this a function or a closure and then start to build up a query based on this. Because remember, successful checks in the database, if we just go over,

02:26

are checks which have a particular status code. So what we're going to do in here, and by the way, this is much like if we go over to each of our checks, the method that we created here. But what we don't want to do is iterate over each of our checks,

02:42

use is successful to build up this count. It's better to do this at the database level for speed. So we're going to get in a count here called successful checks count. Let's just take a look at this before we do anything with a query,

02:54

actually, to see what we get here. Now, the best place to showcase this would be in each of the endpoint resources. So let's go ahead and just dump out here successful check count just to demonstrate this.

03:09

So once we do this count on the database level, that's going to give us back a property with the name that we've chosen here. So let's go ahead and say this successful checks count. So now if we come over to this endpoint component,

03:22

what we can do is just dump this out somewhere. So let's just dump it out at the top here. So we're going to say endpoint successful check count. And let's head over.

03:34

And there we go. We've got 0 for this and 6. Now, 6 isn't the successful check count. It's the overall check count for each of these.

03:42

So what we now need to do is back over in site when we define this relationship, add in the conditions which give us a successful check. So it's just going to be exactly the same that we wrote in code in that method. So we're going to say where the response code is greater than or equal to 200

04:01

and where the response code is less than 300. And that's it. So at the database level now, that's going to give us back a single number of the successful checks based on this condition.

04:14

So if we just come over now and give this a refresh, sure enough, that's changed to 3. So we've now got a really convenient property on each of our endpoints, which tells us the amount of successful checks that we have. So now that we've done that, we can go ahead and get rid of this.

04:29

And we can get rid of that from our endpoint resource as well. Of course, we could have just died and dumped that. It's fine. Now what we can do is pull together on each of the endpoints the uptime percentage.

04:40

So now we can say this successful check count, if we just spell successful correctly, divided by the checks count multiplied by 100. Give that a refresh. And we should have that when we add this to our endpoint resource.

04:56

So now let's actually add this to our endpoint resource. So uptime percentage, that's the actual thing we want to show in our UI. And we just call the uptime percentage method. So if we give that a refresh now, division by 0.

05:10

So let's just have a look at what we've done here. And yeah, of course. So at the moment, we get a division by 0. But that is for the first endpoint, which has no checks at all.

05:21

This is going to be 0. This is going to be 0. And we can't divide by 0. So obviously, we're going to have to add a little failsafe in here if that isn't the case.

05:29

So let's just go and say, if this checks and count, and we can just either compare that to 0 or say no, then we're going to return a null value. Or we could return 0. It doesn't really matter.

05:41

So let's go over and give that a refresh now. And there we go. So now we can dump this value out on the page over in our endpoint. And let's just dump it down here, where we're actually outputting the percent.

05:54

So this is now going to be endpoint and uptime percentage. And let's head over and check this out. OK, so we get 0 here for some reason. Let's just have a look at why that might be.

06:07

I think we call this successful checks count. Yeah, we did. OK, so there we go. We get a 50% uptime.

06:14

Now, let's just look at this. Because if, for example, this one was 200 as well, we're going to get a slightly unformatted version of this. That's not what we want to see there.

06:25

So really, what we want to do here is just wrap this in the native number format function, which takes in the value, of course, and the amount of decimal places. So 2 is enough for this. So we've got 66.67%.

06:39

And of course, you can format that in any way that you like. OK, so for the uptime percent here, we don't really want to show 0% on the UI, because it's a little bit misleading. So what we can do here is add in another if statement.

06:52

So we can say if endpoint and uptime percentage. Now, we've got to be careful here, because this could be 0. So really, what we want to do is strictly compare this to null. And then otherwise, we want to show a hyphen.

07:09

Otherwise, if we'd added an endpoint and it had 0 uptime, this would still show 0%. So we want to show a dash otherwise. OK, let's head over and give this a refresh. And yeah, again, we forgot the else on there.

07:23

There we go. Great. So this endpoint, of course, with no checks at all, has no uptime. So we just show a hyphen.

07:29

And then otherwise, we show the actual uptime percent. So there we go. Let's just recap this, because what we've done here might not be familiar to you if you've not done this kind of thing before.

07:39

Now, the reason that we did this is because otherwise, a code level inside of the method over on our endpoint here to get the uptime percentage, we would have had to iterate through all of the checks and then go ahead and build this value up that we could just get at the database ourselves.

07:57

Instead, what we've done is created a count at the database level calling this successful check count, which will always be available on the endpoint. And then what we can do with that is just go ahead and use that value that we always get returned in that model to go ahead and grab the percentage by just checking against

08:16

the checks count. Once again, this is going to introduce some problems when we don't egoload. So we're going to come back to that a little bit later to check the speed of everything, check how many queries we're performing, and we will get all of that fixed up.

44 episodes•4 hrs 59 mins•1 year ago

Ready to dive into Inertia? Let's build a real-world app with Laravel, Inertia and Vue! If you're already working with Inertia, you'll pick up some tips and techniques for future projects.

This uptime monitor allows you to create and switch between sites, then add endpoints to monitor with frequency intervals. Using the power of scheduling and queues, it'll automatically alert the email addresses you've added via the UI when an endpoint goes down.

In this course, you'll learn to:

- Build a real app from scratch with Inertia
- Work with Laravel queues
- Perform actions on models at user-defined intervals
- Work with sub-minute schedules in Laravel
- Send out channel notifications with Laravel
- Use API resources to simplify Inertia data
- Organise apps with events and observers
- Create modals in Vue
- Perform inline editing with Inertia forms

Alex Garrett-Smith

Hey, I'm the founder of Codecourse!

No comments, yet. Be the first to leave a comment.