updated stats, add logs and other data in user resource
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Models\Meta;
|
||||
use App\Models\PremiumEmail;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
@@ -16,55 +17,137 @@ class StatsOverview extends BaseWidget
|
||||
protected function getStats(): array
|
||||
{
|
||||
return [
|
||||
Stat::make('Total Users', $this->getUser()),
|
||||
Stat::make('Customers', $this->getCustomerCount()),
|
||||
Stat::make('Paid Users', $this->getUserPaid()),
|
||||
Stat::make('Logs Count', $this->getLogsCount()),
|
||||
Stat::make('Total Mailbox', $this->getTotalMailbox()),
|
||||
Stat::make('Emails Received', $this->getTotalEmailsReceived()),
|
||||
Stat::make('Emails Stored', $this->getStoreEmailsCount()),
|
||||
Stat::make('Open Tickets', $this->getOpenTicketsCount()),
|
||||
Stat::make('Closed Tickets', $this->getClosedTicketsCount()),
|
||||
Stat::make('Total Users', $this->getUser())
|
||||
->description($this->getComparisonDescription($this->getUser(), $this->getUser('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getUser(), $this->getUser('yesterday')))
|
||||
->color($this->getComparisonColor($this->getUser(), $this->getUser('yesterday'))),
|
||||
Stat::make('Customers', $this->getCustomerCount())
|
||||
->description($this->getComparisonDescription($this->getCustomerCount(), $this->getCustomerCount('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getCustomerCount(), $this->getCustomerCount('yesterday')))
|
||||
->color($this->getComparisonColor($this->getCustomerCount(), $this->getCustomerCount('yesterday'))),
|
||||
Stat::make('Paid Users', $this->getUserPaid())
|
||||
->description($this->getComparisonDescription($this->getUserPaid(), $this->getUserPaid('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getUserPaid(), $this->getUserPaid('yesterday')))
|
||||
->color($this->getComparisonColor($this->getUserPaid(), $this->getUserPaid('yesterday'))),
|
||||
Stat::make('Logs Count', $this->getLogsCount())
|
||||
->description($this->getComparisonDescription($this->getLogsCount(), $this->getLogsCount('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getLogsCount(), $this->getLogsCount('yesterday')))
|
||||
->color($this->getComparisonColor($this->getLogsCount(), $this->getLogsCount('yesterday'))),
|
||||
Stat::make('Total Mailbox', $this->getTotalMailbox())
|
||||
->description($this->getComparisonDescription($this->getTotalMailbox(), $this->getTotalMailbox('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getTotalMailbox(), $this->getTotalMailbox('yesterday')))
|
||||
->color($this->getComparisonColor($this->getTotalMailbox(), $this->getTotalMailbox('yesterday'))),
|
||||
Stat::make('Emails Received', $this->getTotalEmailsReceived())
|
||||
->description($this->getComparisonDescription($this->getTotalEmailsReceived(), $this->getTotalEmailsReceived('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getTotalEmailsReceived(), $this->getTotalEmailsReceived('yesterday')))
|
||||
->color($this->getComparisonColor($this->getTotalEmailsReceived(), $this->getTotalEmailsReceived('yesterday'))),
|
||||
Stat::make('Emails Stored', $this->getStoreEmailsCount())
|
||||
->description($this->getComparisonDescription($this->getStoreEmailsCount(), $this->getStoreEmailsCount('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getStoreEmailsCount(), $this->getStoreEmailsCount('yesterday')))
|
||||
->color($this->getComparisonColor($this->getStoreEmailsCount(), $this->getStoreEmailsCount('yesterday'))),
|
||||
Stat::make('Open Tickets', $this->getOpenTicketsCount())
|
||||
->description($this->getComparisonDescription($this->getOpenTicketsCount(), $this->getOpenTicketsCount('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getOpenTicketsCount(), $this->getOpenTicketsCount('yesterday')))
|
||||
->color($this->getComparisonColor($this->getOpenTicketsCount(), $this->getOpenTicketsCount('yesterday'))),
|
||||
Stat::make('Closed Tickets', $this->getClosedTicketsCount())
|
||||
->description($this->getComparisonDescription($this->getClosedTicketsCount(), $this->getClosedTicketsCount('yesterday')))
|
||||
->descriptionIcon($this->getComparisonIcon($this->getClosedTicketsCount(), $this->getClosedTicketsCount('yesterday')))
|
||||
->color($this->getComparisonColor($this->getClosedTicketsCount(), $this->getClosedTicketsCount('yesterday'))),
|
||||
];
|
||||
}
|
||||
private function getUser(): int
|
||||
|
||||
private function getComparisonDescription(int $today, int $yesterday): string
|
||||
{
|
||||
return User::all()->count();
|
||||
if ($today == $yesterday) {
|
||||
return 'No change';
|
||||
}
|
||||
|
||||
$difference = $today - $yesterday;
|
||||
$percentage = $yesterday > 0 ? ($difference / $yesterday) * 100 : ($today > 0 ? 100 : 0);
|
||||
|
||||
return sprintf(
|
||||
'Today: %d, Yesterday: %d (%s%.1f%%)',
|
||||
$today,
|
||||
$yesterday,
|
||||
$difference >= 0 ? '+' : '-',
|
||||
abs($percentage)
|
||||
);
|
||||
}
|
||||
private function getUserPaid(): int
|
||||
|
||||
private function getComparisonIcon(int $today, int $yesterday): ?string
|
||||
{
|
||||
if ($today == $yesterday) {
|
||||
return null;
|
||||
}
|
||||
return $today > $yesterday ? 'heroicon-o-arrow-up' : 'heroicon-o-arrow-down';
|
||||
}
|
||||
|
||||
private function getComparisonColor(int $today, int $yesterday): string
|
||||
{
|
||||
if ($today == $yesterday) {
|
||||
return 'gray';
|
||||
}
|
||||
return $today > $yesterday ? 'success' : 'danger';
|
||||
}
|
||||
|
||||
private function getUser(string $period = 'today'): int
|
||||
{
|
||||
if ($period === 'yesterday') {
|
||||
return User::where('created_at', '<', Carbon::today('UTC')->startOfDay())->count();
|
||||
}
|
||||
return User::count();
|
||||
}
|
||||
|
||||
private function getUserPaid(string $period = 'today'): int
|
||||
{
|
||||
return DB::table('subscriptions')
|
||||
->where(['stripe_status' => 'active'])
|
||||
->where('stripe_status', 'active')
|
||||
->distinct('user_id')
|
||||
->count('user_id');
|
||||
}
|
||||
private function getLogsCount(): int
|
||||
|
||||
private function getLogsCount(string $period = 'today'): int
|
||||
{
|
||||
return Log::all()->count();
|
||||
}
|
||||
private function getTotalMailbox(): int
|
||||
{
|
||||
return Meta::select('value')->where(['key' => 'email_ids_created'])->first()->value;
|
||||
}
|
||||
private function getTotalEmailsReceived(): int
|
||||
{
|
||||
return Meta::select('value')->where(['key' => 'messages_received'])->first()->value;
|
||||
if ($period === 'yesterday') {
|
||||
return Log::where('created_at', '<', Carbon::today('UTC')->startOfDay())->count();
|
||||
}
|
||||
return Log::count();
|
||||
}
|
||||
|
||||
private function getCustomerCount(): int
|
||||
private function getTotalMailbox(string $period = 'today'): int
|
||||
{
|
||||
return Meta::select('value')->where('key', 'email_ids_created')->first()->value ?? 0;
|
||||
}
|
||||
|
||||
private function getTotalEmailsReceived(string $period = 'today'): int
|
||||
{
|
||||
return Meta::select('value')->where('key', 'messages_received')->first()->value ?? 0;
|
||||
}
|
||||
|
||||
private function getCustomerCount(string $period = 'today'): int
|
||||
{
|
||||
if ($period === 'yesterday') {
|
||||
return User::whereNotNull('stripe_id')
|
||||
->where('created_at', '<', Carbon::today('UTC')->startOfDay())
|
||||
->count();
|
||||
}
|
||||
return User::whereNotNull('stripe_id')->count();
|
||||
}
|
||||
|
||||
private function getStoreEmailsCount(): int
|
||||
private function getStoreEmailsCount(string $period = 'today'): int
|
||||
{
|
||||
return PremiumEmail::all()->count();
|
||||
if ($period === 'yesterday') {
|
||||
return PremiumEmail::where('created_at', '<', Carbon::today('UTC')->startOfDay())->count();
|
||||
}
|
||||
return PremiumEmail::count();
|
||||
}
|
||||
private function getOpenTicketsCount(): int
|
||||
|
||||
private function getOpenTicketsCount(string $period = 'today'): int
|
||||
{
|
||||
return Ticket::whereIn('status', ['open', 'pending'])->count();
|
||||
}
|
||||
private function getClosedTicketsCount(): int
|
||||
|
||||
private function getClosedTicketsCount(string $period = 'today'): int
|
||||
{
|
||||
return Ticket::whereIn('status', ['closed'])->count();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user