Skip to content

Commit 61b851e

Browse files
authored
Merge pull request #66 from laravelcm/admin-stats
Admin stats
2 parents 2fe4085 + 6d816eb commit 61b851e

38 files changed

+627
-29
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Cpanel;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
8+
class AnalyticsController extends Controller
9+
{
10+
public function __invoke()
11+
{
12+
return view('cpanel.analytics');
13+
}
14+
}

app/Http/Controllers/HomeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function index()
3333
->get();
3434
});
3535

36-
$latestDiscussions = Cache::remember('latestDiscussions', now()->addDay(), function () {
36+
$latestDiscussions = Cache::remember('latestDiscussions', now()->addHour(), function () {
3737
return Discussion::query()
38-
->scopes('popular')
38+
->recent()
3939
->orderByViews()
4040
->limit(3)
4141
->get();

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Kernel extends HttpKernel
2222
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
2323
\App\Http\Middleware\TrimStrings::class,
2424
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
25+
\AndreasElia\Analytics\Http\Middleware\Analytics::class,
2526
];
2627

2728
/**

app/Models/Article.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public function scopeNotDeclined(Builder $query): Builder
396396
public function scopeRecent(Builder $query): Builder
397397
{
398398
return $query->orderByDesc('published_at')
399-
->orderByDesc('published_at');
399+
->orderByDesc('created_at');
400400
}
401401

402402
/**

app/Models/Discussion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Discussion extends Model implements ReactableInterface, ReplyInterface, Su
5151
/**
5252
* The attributes that should be cast to native types.
5353
*
54-
* @var array<string, string>
54+
* @var string[]
5555
*/
5656
protected $casts = [
5757
'locked' => 'boolean',

app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function register(): void
3737
public function boot(): void
3838
{
3939
date_default_timezone_set('Africa/Douala');
40-
setlocale(LC_TIME, 'French');
41-
setlocale(LC_ALL, 'fr_FR.UTF-8');
40+
setlocale(LC_TIME, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
41+
setlocale(LC_ALL, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
4242
Carbon::setLocale('fr');
4343

4444
$this->bootMacros();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Widgets;
4+
5+
use App\Models\User;
6+
use Arrilot\Widgets\AbstractWidget;
7+
use Illuminate\Contracts\View\View;
8+
use Illuminate\Database\Eloquent\Builder;
9+
10+
class MostActiveUsersPerWeek extends AbstractWidget
11+
{
12+
/**
13+
* The configuration array.
14+
*
15+
* @var array
16+
*/
17+
protected $config = [];
18+
19+
/**
20+
* The number of seconds before each reload.
21+
*
22+
* @var int|float
23+
*/
24+
public $reloadTimeout = 60 * 60 * 24 * 2; // 2 days
25+
26+
/**
27+
* The number of minutes before cache expires.
28+
* False means no caching at all.
29+
*
30+
* @var int|float|bool
31+
*/
32+
public $cacheTime = 0;
33+
34+
/**
35+
* Treat this method as a controller action.
36+
* Return view() or other content to display.
37+
*/
38+
public function run(): View
39+
{
40+
$users = User::with('activities')
41+
->withCount('activities')
42+
->verifiedUsers()
43+
->whereHas('activities', function (Builder $query) {
44+
return $query->whereBetween('created_at', [
45+
now()->startOfWeek(),
46+
now()->endOfWeek()
47+
]);
48+
})
49+
->orderByDesc('activities_count')
50+
->limit(5)
51+
->get();
52+
53+
return view('widgets.most_active_users_per_week', [
54+
'config' => $this->config,
55+
'users' => $users,
56+
]);
57+
}
58+
}

app/Widgets/MostLikedPostsPerWeek.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Widgets;
4+
5+
use App\Models\Article;
6+
use Arrilot\Widgets\AbstractWidget;
7+
use Illuminate\Contracts\View\View;
8+
9+
class MostLikedPostsPerWeek extends AbstractWidget
10+
{
11+
/**
12+
* The configuration array.
13+
*
14+
* @var array
15+
*/
16+
protected $config = [];
17+
18+
/**
19+
* The number of seconds before each reload.
20+
*
21+
* @var int|float
22+
*/
23+
public $reloadTimeout = 60 * 60 * 24 * 2; // 2 days
24+
25+
/**
26+
* The number of minutes before cache expires.
27+
* False means no caching at all.
28+
*
29+
* @var int|float|bool
30+
*/
31+
public $cacheTime = 0;
32+
33+
/**
34+
* Treat this method as a controller action.
35+
* Return view() or other content to display.
36+
*/
37+
public function run(): View
38+
{
39+
$articles = Article::published()
40+
->popular()
41+
->limit(5)
42+
->get();
43+
44+
return view('widgets.most_liked_posts_per_week', [
45+
'config' => $this->config,
46+
'articles' => $articles,
47+
]);
48+
}
49+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace App\Widgets;
4+
5+
use App\Models\Article;
6+
use Arrilot\Widgets\AbstractWidget;
7+
use CyrildeWit\EloquentViewable\Support\Period;
8+
use Illuminate\Contracts\View\View;
9+
10+
class MostViewedPostsPerWeek extends AbstractWidget
11+
{
12+
/**
13+
* The configuration array.
14+
*
15+
* @var array
16+
*/
17+
protected $config = [];
18+
19+
/**
20+
* The number of seconds before each reload.
21+
*
22+
* @var int|float
23+
*/
24+
public $reloadTimeout = 60 * 60 * 24 * 2; // 2 days
25+
26+
/**
27+
* The number of minutes before cache expires.
28+
* False means no caching at all.
29+
*
30+
* @var int|float|bool
31+
*/
32+
public $cacheTime = 90;
33+
34+
/**
35+
* Treat this method as a controller action.
36+
* Return view() or other content to display.
37+
*/
38+
public function run(): View
39+
{
40+
$articles = Article::withViewsCount(Period::create(now()->startOfWeek(), now()->endOfWeek()))
41+
->published()
42+
->orderByDesc('views_count')
43+
->orderByDesc('published_at')
44+
->limit(5)
45+
->get();
46+
47+
return view('widgets.most_viewed_posts_per_week', [
48+
'config' => $this->config,
49+
'articles' => $articles,
50+
]);
51+
}
52+
}

app/Widgets/RecentNumbers.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ public function run(): View
5656
$differenceArticle = $currentMonthArticles - $lastMonthArticles;
5757

5858
$totalViews = views(Article::class)->count();
59-
$lastMonthViews = views(Article::class)->period(Period::pastMonths(1))->count();
60-
$currentViews = views(Article::class)->period(Period::create(now()->startOfMonth()))->count();
59+
$lastMonthViews = views(Article::class)
60+
->period(Period::create(now()->subMonth()->startOfMonth(), now()->subMonth()->endOfMonth()))
61+
->remember(now()->addMonth())
62+
->count();
63+
$currentViews = views(Article::class)->period(Period::upto(now()->startOfMonth()))->count();
6164
$differenceViews = $currentViews - $lastMonthViews;
6265

6366
return view('widgets.recent_numbers', [

app/Widgets/RecentPostsPerWeek.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Widgets;
4+
5+
use App\Models\Article;
6+
use Arrilot\Widgets\AbstractWidget;
7+
use Illuminate\Contracts\View\View;
8+
9+
class RecentPostsPerWeek extends AbstractWidget
10+
{
11+
/**
12+
* The configuration array.
13+
*
14+
* @var array
15+
*/
16+
protected $config = [];
17+
18+
/**
19+
* Treat this method as a controller action.
20+
* Return view() or other content to display.
21+
*/
22+
public function run(): View
23+
{
24+
$articles = Article::recent()
25+
->whereBetween('created_at', [now()->startOfWeek(), now()->endOfWeek()])
26+
->limit(5)
27+
->get();
28+
29+
return view('widgets.recent_posts_per_week', [
30+
'config' => $this->config,
31+
'articles' => $articles,
32+
]);
33+
}
34+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"php": "^8.0",
99
"ext-fileinfo": "*",
1010
"ext-json": "*",
11+
"andreaselia/analytics": "^1.5",
1112
"archtechx/laravel-seo": "^0.4.0",
1213
"arrilot/laravel-widgets": "^3.13",
1314
"blade-ui-kit/blade-heroicons": "^1.3",

composer.lock

Lines changed: 75 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)