Overview of Nova metrics 0:00So, now we have all our resources defined, and if we take a step back and go back to our dashboard, we still see the help page that ships with Nova, and I want to change this now. So, what I want to do is I want to add some metrics on our dashboard. And with Nova, metrics provide you with an easy way on how you can get a quick insight into some of the key values and metrics that you want to track in your application and want to provide your users. So, let's get started with some custom metrics. When you create a metric, you can basically choose from three available metric types, which is value metrics, trend metrics, and partition metrics, and we'll cover each Creating a value metric 0:44When you create a metric, you can basically choose from three available metric types, which is value metrics, trend metrics, and partition metrics, and we'll cover each one after another. So, let's start with a value metric. To do this, we can use the CLI commands that ship with Nova to create a new value metric. So, we can do phpArtisan, nova, value, and then give it the name of the value metric that we want to define. So, in my case, I want to simply have a metric that tells me how many posts do we have in our system. So, let's just call it post count.our system. So, let's just call it post count. Okay, metric created successfully. So, let's take a look at the generated class or file in Sublime. So, if we go to our app directory in Nova, we now have metrics, and in here we have this post count metric class, and if we take a look at it, we can see that it extends value. So, this is a value metric, and it has a calculate method. This method will be used to define and calculate the value that will be visible to the user. We have some time ranges, so we can apply these time ranges to our metric. So, for example, we could choose to show the number of posts that were created in the lastWe have some time ranges, so we can apply these time ranges to our metric. So, for example, we could choose to show the number of posts that were created in the last 30, 60 days, or in the last year, quarter, or month. And then we have a cache for method where we can define if we want to cache the result of this metric, and for how long. And we have a URI key method that defines the key that will be used when we visit this metric in the browser. So, let's start with the calculate method. Out of the box, we have a this count, then it gets the request, which will be used for validation, and we have the model.Out of the box, we have a this count, then it gets the request, which will be used for validation, and we have the model. So, since we want to count the posts, we can just change this to post, and import it. So, make sure you do not import the Nova resource, but your model. And that's pretty much it. So, let's take a look at how this very simple value metric works, and how it looks like. To define this value metric, you have a few options available. Let's take the most easy one. So, we go to our app providers directory, and into our Nova service provider. And in here, we see that we have cards. Adding metrics to dashboard 3:33So, we go to our app providers directory, and into our Nova service provider. And in here, we see that we have cards. And these cards define which cards, or metrics, will be visible on your dashboard. As you can see, out of the box, we have this help card. And if we remove this, or comment it out, and then visit the dashboard again, it's empty. So, to add our post count metric to this dashboard, all we have to do is spin up a new instance of it, and return it in this cards array. So, new post count, and import the class, save this. And if we now refresh, we now have our post count metric. So, we see we have six posts created in the last 30 days.And if we now refresh, we now have our post count metric. So, we see we have six posts created in the last 30 days. And now we can basically just switch the dates that we defined in this metric. And at the bottom, you see there's no prior data. And what Nova does in the background is, when you select a date range, or a time range, it will perform the calculate method for this specific range. So, for the quarter to date in this example. But at the same time, it will perform the same query for the previous range that you selected. So, if I would choose 30 days, it will count the number of posts created within the last 30 days. And for this, it would count the number of posts created before 30 days,So, if I would choose 30 days, it will count the number of posts created within the last 30 days. And for this, it would count the number of posts created before 30 days, for the last 30 days from that time on. So, to show you what I mean, I can just go to SQL Pro, and in my database, I can just change the created at value for one of the posts. So, let's change this maybe to May. Alright, and if I now switch back to quarter to date, we now have five posts that were created in the last quarter. And this is now an increase of 400% compared to the previous quarter, which only had one post, which is the one that we just changed in the database.And this is now an increase of 400% compared to the previous quarter, which only had one post, which is the one that we just changed in the database. So, if we go back to Sublime, you can basically just change these ranges. So, if you want to use some other predefined date values, and dates that you want to filter in your metric, you can just modify this array. Or, if you don't want any ranges, you can just remove it entirely. And you can also modify this calculate method. So, right now, we only count the number of models. So, maybe we want to do something else. Value metric calculations 6:34So, right now, we only count the number of models. So, maybe we want to do something else. And Nova metrics provide you with a number of helper methods that are very handy. So, what you can do besides count, is you can also do something like average. So, we could do this, average. Then give it the request, the class, and then a column, on what we want to call this average method on. So, maybe we would have something like a word count, stored in our posts. Then we can use this average method to get the average number of wordsstored in our posts. Then we can use this average method to get the average number of words that we have in our post models. Now, I don't have this column in my database, but this would just be an example. In addition to count and average, we also have sum, which you can use to sum up the values of a specific column. We have min, which returns the minimum value of one specific column. And we have max, which does the opposite, which returns the maximum of a specific column. And this is especially helpful if you maybe have orders in your databasewhich returns the maximum of a specific column. And this is especially helpful if you maybe have orders in your database and want to show maybe the sum of all the orders in total, or the maximum order that was purchased in the last 30 days. Things like that. And also, if none of these suits your needs, and you want to provide a custom value, you can do that as well. So, you can also do something like this result, and then just provide a static result that you want to show here. So, you can say 100,and then just provide a static result that you want to show here. So, you can say 100, and the previous value was 50. And if we now go back, refresh here, now we have 100 posts and a 100% increase, because we said that the previous value was 50, and now we have 100. So, if none of the integrated methods like count, average, sum, min, and max help you with what you want to display in your specific value metric, you can just do the calculation in here yourself, and then return the values manually using the result and previous method. Building trend metrics 9:04you can just do the calculation in here yourself, and then return the values manually using the result and previous method. Okay, let's move on to the next metric available. Besides value metrics, there are also trend metrics. So, let's create a trend metric now. Similar to the value metrics, we can just do phpArtisan nova trend, and then give it the name of the trend metric that we want to create. So, maybe we want to show the number of posts that were created during a specific time range, so that we can see on each day how many posts were created.during a specific time range, so that we can see on each day how many posts were created. So, let's just call it posts per day. Okay, let's close this post count metric, and we now should have a new metric called posts per day. There it is. And as you can see, this now extends trend, because this is a trend metric and not a value metric. And the calculate method looks a little bit different. So, let's take a look at the other methods first.And the calculate method looks a little bit different. So, let's take a look at the other methods first. So, we have the ranges, which is basically the same as for the value metric. We have the cache for, where we can cache the value of this specific metric, and we have the URI key. So, this is all the same. Now, if we take a look at the calculate method, we now have a count by days method. And what this does is, let me show you this on the dashboard. So, if we now display the new posts per day metric,And what this does is, let me show you this on the dashboard. So, if we now display the new posts per day metric, and import the class, and go back here and refresh. And of course, we need to provide the model that we want to count. So, this would be post and import it. And we can see that we now got a count per each day for this specific model. So, then we can see that within the last 30 days, this was the date where we created three posts, and this was the date where we created two posts,this was the date where we created three posts, and this was the date where we created two posts, and the others are just empty. And then, just like with the value metric, we can change the date range and increase it or decrease it. Now, similar to the value metrics, we can also change this count by days method, and there are a number of different methods available. So, instead of just counting by days, we can also do something like count by month.So, instead of just counting by days, we can also do something like count by month. So, if we want to count by month, go back here. So, now we have September, August, July, June, May. So, now, of course, this is not 30 days, but this is 30 months. So, then we should do something like 12 months, have a 12, and maybe six months, like this. So, now we can see that in September we created five posts, and this is the one that we modified in our database,So, now we can see that in September we created five posts, and this is the one that we modified in our database, which was in May, we created one post. And you can even change this not only by month, but you can also do count by weeks, we had count by days, you can also count by hours, and you can even count by minutes. So, depending on what you want to display in your specific trend metric, Nova has you covered in a lot of these aspects.in your specific trend metric, Nova has you covered in a lot of these aspects. Okay, so let's get back to count by month, and now we have a nice view of all the posts that were created within the last six months. And similar to what you can do with the value metrics, you can also not only count, but do things like average by month, and then provide the column that you want to perform this average method on, or sum by month, and min by month, and max by month.this average method on, or sum by month, and min by month, and max by month. So, all the individual filters and different methods are also available for the trend metric. There is also a nice helper method that allows you to show the latest value inside of this metric card. So, if we add a showLatestValue call to this calculate method and refresh, we now see that we have five posts created in this specific month,and refresh, we now see that we have five posts created in this specific month, and, well, in this case, it's also five in this specific month. So, the latest value will then be visible in here. Now, as you can see, this card and this metric card still says posts per day. That's because Nova is actually pretty smart and turns the class name of our metric into a human readable name. So, it turns into posts per day.into a human readable name. So, it turns into posts per day. But now we want to change this, because we changed the calculate method to say that we want to count by month. So, you have two ways of doing this. You can either change the name attribute into something like posts per month. Save this and go back. Oh, and it needs to be public.Save this and go back. Oh, and it needs to be public. So, there we go. Now it says posts per month. And if you want to do some PHP instead of just providing a static string, you can also define a method called name. And in there, you can return whatever you want. So, this can also be posts per month now. And we got the same result. Okay. So, similar to the value metrics,And we got the same result. Okay. So, similar to the value metrics, if you want to define these trend results yourself, you can just do this manually. So, instead of returning count by month and this helper method, what you can do is you can return a new trend result. Let's import the class. And then specify the trend that you want to return. So, you can do something like day one,And then specify the trend that you want to return. So, you can do something like day one, day two, day three, et cetera. And if we now check this out in Nova, we now have three values. We have day one, day two, and day three. So, if none of the helper methods help you with the trend metric, you can just provide this trend result yourself in here. Creating partition metrics 17:01help you with the trend metric, you can just provide this trend result yourself in here. All right. So, let's check out the last metric that is still available, which is a partition metric. So, similar to the previous ones, we can use phpArtisanNovaPartition and then give it the name. So, we want to maybe show the posts per category. I'll show you what it looks like. So, if we go to our posts per category metric,I'll show you what it looks like. So, if we go to our posts per category metric, we can see it's a partition metric because it extends the partition class. And the calculate method now has a third argument, which is a group by column. So, what we can do is we can count our posts and we want to group the results by the category field. Oh, and let's add it to the Nova service provider so that we can see it.Oh, and let's add it to the Nova service provider so that we can see it. So, Nova posts per category import. And now we see that we have 50% of our posts are in the news category. So, three of them. And three of them are in the tutorials category. So, the partition metric allows you to display a grouped result of your models grouped by a column of your choice.a grouped result of your models grouped by a column of your choice. And similar to the value and trend metric, you can also change this so that you're not only counting, but you can also do something like average, sum, min, or max. So, these are the three available metric types that Nova gives you. And you can display them not only on the dashboard, as we've done so far, but you can also include them into your resource pages. Showing metrics on resources 19:07as we've done so far, but you can also include them into your resource pages. So, let's say we don't want this visible on the dashboard, but since this is post related information, we only want this to be visible in our post resource page. So, what we can do is go into our post resource and in there, there is a method called cards. And similar to the cards that we can define in the Nova service provider, we can just define the cards for this specific resource.in the Nova service provider, we can just define the cards for this specific resource. So, let's just pull over these three cards and add them here, and then add the classes and import them. Okay. Now, if I go back to Nova and refresh, we now have our post resource. And on the top of this resource list, we have our three metrics that we defined.And on the top of this resource list, we have our three metrics that we defined. You can also change the width of your metrics. So, by default, a Nova metric takes up a third of the width that is available in the listing, but you can change this. So, maybe we want to have a full width posts per month metric on top and underneath, we want to split the post count and the posts per category evenly. So, to do this, let's head back to our post resource.and the posts per category evenly. So, to do this, let's head back to our post resource. And in here, we can just say, let's move this one up and say new posts per day. And the width of this specific card is going to be full. And we have a new post count and the width of this card will be half and the same goes for the posts per category. All right. So, if we refresh, we now have our post count,All right. So, if we refresh, we now have our post count, posts per category, and our posts per month now takes up the full width of our metrics. So, this is how you can create metrics that show the specific key identifiers and key numbers that you want to provide for your application. And as you can see, Nova is super flexible with that. You do not need to use the built-in methods. They help you a lot, but if you need some more flexibility,You do not need to use the built-in methods. They help you a lot, but if you need some more flexibility, you can just provide your own values, trends, or partitions.