Skip to content

feat:(LAR-171) BO add Stat in dashboard #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ArticleResource\Widgets;

use App\Models\Article;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;

final class ArticleStatsOverview extends BaseWidget
{
protected static ?string $pollingInterval = null;

protected ?string $heading = 'Posts';

protected function getColumns(): int
{
return 2;
}

protected function getStats(): array
{
$currentWeekStart = now()->startOfWeek();
$currentWeekEnd = now()->endOfWeek();

return [
Stat::make('Total Post', Article::query()->published()->count())
->icon('heroicon-o-newspaper')
->chart(
Trend::query(Article::query()->published())
->between(
start: $currentWeekStart,
end: $currentWeekEnd,
)->perDay()
->count()
->map(fn (TrendValue $value) => $value->aggregate)->toArray()
)->description(__('Total Post')),

Stat::make(
'Recent post',
Article::query()
->recent()
->whereBetween('created_at', [
$currentWeekStart,
$currentWeekEnd,
])->count()
)
->chart(
Trend::query(Article::query())
->between(
start: $currentWeekStart,
end: $currentWeekEnd,
)
->perDay()
->count()
->map(fn (TrendValue $value) => $value->aggregate)
->toArray()
)->icon('heroicon-o-newspaper')
->color('primary')
->description('Total Posts of the week'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ArticleResource\Widgets;

use App\Models\Article;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Str;

final class MostLikedPostsChart extends ChartWidget
{
protected static ?string $heading = 'Most Liked Posts';

protected static ?string $maxHeight = '200px';

protected int|string|array $columnSpan = 'full';

protected int $titleLength = 10;

protected function getData(): array
{
$articles = Article::published()
->popular()
->limit(10)
->get();

return [
'datasets' => [
[
'label' => 'Total Liked',
'data' => $articles->pluck('reactions_count')->toArray(),
],
],
'labels' => $articles->pluck('title')
->map(fn ($title) => Str::limit($title, $this->titleLength, '...'))
->toArray(),
];
}

protected function getType(): string
{
return 'bar';
}

protected function getOptions(): array
{
return [
'scales' => [
'y' => [
'beginAtZero' => true,
],
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\ArticleResource\Widgets;

use App\Models\Article;
use CyrildeWit\EloquentViewable\Support\Period;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Str;

final class MostViewedPostsChart extends ChartWidget
{
protected static ?string $heading = 'Most Viewed Posts';

protected static ?string $maxHeight = '200px';

protected int|string|array $columnSpan = 'full';

protected int $titleLength = 10;

protected function getData(): array
{
$articles = Article::withViewsCount(Period::create(now()->startOfWeek(), now()->endOfWeek())) // @phpstan-ignore-line
->published()
->orderByDesc('views_count')
->orderByDesc('published_at')
->limit(10)
->get();

return [
'datasets' => [
[
'label' => 'Most viewed post',
'data' => $articles->pluck('views_count')->toArray(),
],
],
'labels' => $articles->pluck('title')
->map(fn ($title) => Str::limit($title, $this->titleLength, '...'))
->toArray(),
];
}

protected function getType(): string
{
return 'bar';
}
}
58 changes: 58 additions & 0 deletions app/Filament/Resources/UserResource/Widgets/UserActivityWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\UserResource\Widgets;

use App\Models\User;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Builder;

final class UserActivityWidget extends ChartWidget
{
protected static ?string $heading = 'Most active users this week';

protected int|string|array $columnSpan = 'full';

protected static ?string $maxHeight = '200px';

protected function getData(): array
{
$users = User::with('activities')
->withCount('activities')
->verifiedUsers()
->whereHas('activities', fn (Builder $query) => $query->whereBetween('created_at', [
now()->startOfWeek(),
now()->endOfWeek(),
]))
->orderByDesc('activities_count')
->limit(10)
->get();

return [
'datasets' => [
[
'label' => 'Total Activities',
'data' => $users->pluck('activities_count')->toArray(),
],
],
'labels' => $users->pluck('name')->toArray(),
];
}

protected function getType(): string
{
return 'bar';
}

protected function getOptions(): array
{
return [
'scales' => [
'y' => [
'beginAtZero' => true,
],
],
];
}
}
79 changes: 79 additions & 0 deletions app/Filament/Resources/UserResource/Widgets/UserChartWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace App\Filament\Resources\UserResource\Widgets;

use App\Models\User;
use Filament\Widgets\ChartWidget;
use Flowframe\Trend\Trend;
use Flowframe\Trend\TrendValue;

final class UserChartWidget extends ChartWidget
{
protected static ?string $heading = 'Account Creation';

protected int|string|array $columnSpan = 'full';

protected static ?string $maxHeight = '200px';

public ?string $filter = 'week';

protected function getColumns(): int
{
return 2;
}

protected function getFilters(): array
{
return [
'week' => 'Last Week',
'month' => 'Last Month',
'3months' => 'Last 3 Months',
];
}

protected function getData(): array
{
match ($this->filter) { // @phpstan-ignore-line
'week' => $data = Trend::model(User::class)
->between(
start: now()->subWeeks(),
end: now()
)
->perDay()
->count(),

'month' => $data = Trend::model(User::class)
->between(
start: now()->subMonth(),
end: now()
)
->perDay()
->count(),

'3months' => $data = Trend::model(User::class)
->between(
start: now()->subMonths(3),
end: now()
)
->perMonth()
->count(),
};

return [
'datasets' => [
[
'label' => 'Account created',
'data' => $data->map(fn (TrendValue $value) => $value->aggregate), // @phpstan-ignore-line
],
],
'labels' => $data->map(fn (TrendValue $value) => $value->date), // @phpstan-ignore-line
];
}

protected function getType(): string
{
return 'line';
}
}
Loading
Loading