I'm trying to build a metric card that displays the average length of time between a resource's 'created_at' and 'updated_at' field for a given time range. This is for our ticket system, so I am building a metric that shows the average length of time it takes to "complete" a ticket.
This is the calculate method so far:
/**
* Calculate the value of the metric.
*
* @param NovaRequest $request
* @return ValueResult
*/
public function calculate(NovaRequest $request)
{
// update rounding precision to handle low values < 0.5
$this->roundingPrecision = 2;
// calculate the average length of time between created_at and updated_at
// for all tickets where the ticket status is marked as TicketStatus::COMPLETED()
return $this->average(
$request,
Ticket::query()->where('status', TicketStatus::COMPLETED()),
DB::raw('TIMESTAMPDIFF(MINUTE, created_at, updated_at) / 60')
)->format('0.00')
->suffix('hrs');
}
And it works well to get the average as a decimal point, but I am stuck trying to figure out how to convert that decimal point to a string, say from 0.38 to "0 hours 22 minutes".
I noticed in the documentation that Nova uses numbro.js to handle the formatting, but I don't see anything that allows for the kind of formatting that I'm looking for.
Is something like this possible, or do I need to look at building a custom package that extends the metrics?