Skip to content

Instantly share code, notes, and snippets.

@RhysLees
Created February 15, 2023 15:03
Show Gist options
  • Save RhysLees/52046f0abeab609e1193a5550b7289be to your computer and use it in GitHub Desktop.
Save RhysLees/52046f0abeab609e1193a5550b7289be to your computer and use it in GitHub Desktop.
<?php
namespace App\Nova\Metrics;
use App\Models\Team;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Metrics\Partition;
class TeamsWithSubscriptions extends Partition
{
public $name = 'Teams with Subscriptions';
/**
* Calculate the value of the metric.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return mixed
*/
public function calculate(NovaRequest $request)
{
$teams = Team::with('tenancy')->get();
// Teams on trial
$teamsOnTrial = $teams->filter(function ($team) {
return $team->onTrial();
});
// Teams with active subscription
$teamsWithActiveSubscription = $teams->filter(function ($team) {
return $team->subscribed();
});
// Teams with no subscription
$teamsWithNoSubscription = $teams->filter(function ($team) {
return ! $team->subscribed() && ! $team->onTrial() && ! $team->tenancy?->free_access;
});
// Teams with free access
$teamsWithFreeAccess = $teams->filter(function ($team) {
return $team->tenancy?->free_access;
});
return $this->result([
'Active' => $teamsWithActiveSubscription->count(),
'On Trial' => $teamsOnTrial->count(),
'Free Access' => $teamsWithFreeAccess->count(),
'No Subscription' => $teamsWithNoSubscription->count(),
])->colors(
[
'Active' => '#22c55e',
'On Trial' => '#f59e0b',
'Free Access' => '#6366f1',
'No Subscription' => '#ef4444',
]
);
}
/**
* Determine the amount of time the results of the metric should be cached.
*
* @return \DateTimeInterface|\DateInterval|float|int|null
*/
public function cacheFor()
{
return now()->addMinutes(5);
}
/**
* Get the URI key for the metric.
*
* @return string
*/
public function uriKey()
{
return 'teams-with-active-subscriptions';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment