Skip to content

Commit

Permalink
Merge pull request #66 from laravelcm/admin-stats
Browse files Browse the repository at this point in the history
Admin stats
  • Loading branch information
mckenziearts authored Aug 22, 2022
2 parents 2fe4085 + 6d816eb commit 61b851e
Show file tree
Hide file tree
Showing 38 changed files with 627 additions and 29 deletions.
14 changes: 14 additions & 0 deletions app/Http/Controllers/Cpanel/AnalyticsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Controllers\Cpanel;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AnalyticsController extends Controller
{
public function __invoke()
{
return view('cpanel.analytics');
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function index()
->get();
});

$latestDiscussions = Cache::remember('latestDiscussions', now()->addDay(), function () {
$latestDiscussions = Cache::remember('latestDiscussions', now()->addHour(), function () {
return Discussion::query()
->scopes('popular')
->recent()
->orderByViews()
->limit(3)
->get();
Expand Down
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\AndreasElia\Analytics\Http\Middleware\Analytics::class,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function scopeNotDeclined(Builder $query): Builder
public function scopeRecent(Builder $query): Builder
{
return $query->orderByDesc('published_at')
->orderByDesc('published_at');
->orderByDesc('created_at');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Discussion extends Model implements ReactableInterface, ReplyInterface, Su
/**
* The attributes that should be cast to native types.
*
* @var array<string, string>
* @var string[]
*/
protected $casts = [
'locked' => 'boolean',
Expand Down
4 changes: 2 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function register(): void
public function boot(): void
{
date_default_timezone_set('Africa/Douala');
setlocale(LC_TIME, 'French');
setlocale(LC_ALL, 'fr_FR.UTF-8');
setlocale(LC_TIME, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
setlocale(LC_ALL, 'fr_FR', 'fr', 'FR', 'French', 'fr_FR.UTF-8');
Carbon::setLocale('fr');

$this->bootMacros();
Expand Down
58 changes: 58 additions & 0 deletions app/Widgets/MostActiveUsersPerWeek.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Widgets;

use App\Models\User;
use Arrilot\Widgets\AbstractWidget;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;

class MostActiveUsersPerWeek extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [];

/**
* The number of seconds before each reload.
*
* @var int|float
*/
public $reloadTimeout = 60 * 60 * 24 * 2; // 2 days

/**
* The number of minutes before cache expires.
* False means no caching at all.
*
* @var int|float|bool
*/
public $cacheTime = 0;

/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run(): View
{
$users = User::with('activities')
->withCount('activities')
->verifiedUsers()
->whereHas('activities', function (Builder $query) {
return $query->whereBetween('created_at', [
now()->startOfWeek(),
now()->endOfWeek()
]);
})
->orderByDesc('activities_count')
->limit(5)
->get();

return view('widgets.most_active_users_per_week', [
'config' => $this->config,
'users' => $users,
]);
}
}
49 changes: 49 additions & 0 deletions app/Widgets/MostLikedPostsPerWeek.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Widgets;

use App\Models\Article;
use Arrilot\Widgets\AbstractWidget;
use Illuminate\Contracts\View\View;

class MostLikedPostsPerWeek extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [];

/**
* The number of seconds before each reload.
*
* @var int|float
*/
public $reloadTimeout = 60 * 60 * 24 * 2; // 2 days

/**
* The number of minutes before cache expires.
* False means no caching at all.
*
* @var int|float|bool
*/
public $cacheTime = 0;

/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run(): View
{
$articles = Article::published()
->popular()
->limit(5)
->get();

return view('widgets.most_liked_posts_per_week', [
'config' => $this->config,
'articles' => $articles,
]);
}
}
52 changes: 52 additions & 0 deletions app/Widgets/MostViewedPostsPerWeek.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Widgets;

use App\Models\Article;
use Arrilot\Widgets\AbstractWidget;
use CyrildeWit\EloquentViewable\Support\Period;
use Illuminate\Contracts\View\View;

class MostViewedPostsPerWeek extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [];

/**
* The number of seconds before each reload.
*
* @var int|float
*/
public $reloadTimeout = 60 * 60 * 24 * 2; // 2 days

/**
* The number of minutes before cache expires.
* False means no caching at all.
*
* @var int|float|bool
*/
public $cacheTime = 90;

/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run(): View
{
$articles = Article::withViewsCount(Period::create(now()->startOfWeek(), now()->endOfWeek()))
->published()
->orderByDesc('views_count')
->orderByDesc('published_at')
->limit(5)
->get();

return view('widgets.most_viewed_posts_per_week', [
'config' => $this->config,
'articles' => $articles,
]);
}
}
7 changes: 5 additions & 2 deletions app/Widgets/RecentNumbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ public function run(): View
$differenceArticle = $currentMonthArticles - $lastMonthArticles;

$totalViews = views(Article::class)->count();
$lastMonthViews = views(Article::class)->period(Period::pastMonths(1))->count();
$currentViews = views(Article::class)->period(Period::create(now()->startOfMonth()))->count();
$lastMonthViews = views(Article::class)
->period(Period::create(now()->subMonth()->startOfMonth(), now()->subMonth()->endOfMonth()))
->remember(now()->addMonth())
->count();
$currentViews = views(Article::class)->period(Period::upto(now()->startOfMonth()))->count();
$differenceViews = $currentViews - $lastMonthViews;

return view('widgets.recent_numbers', [
Expand Down
34 changes: 34 additions & 0 deletions app/Widgets/RecentPostsPerWeek.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Widgets;

use App\Models\Article;
use Arrilot\Widgets\AbstractWidget;
use Illuminate\Contracts\View\View;

class RecentPostsPerWeek extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [];

/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run(): View
{
$articles = Article::recent()
->whereBetween('created_at', [now()->startOfWeek(), now()->endOfWeek()])
->limit(5)
->get();

return view('widgets.recent_posts_per_week', [
'config' => $this->config,
'articles' => $articles,
]);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^8.0",
"ext-fileinfo": "*",
"ext-json": "*",
"andreaselia/analytics": "^1.5",
"archtechx/laravel-seo": "^0.4.0",
"arrilot/laravel-widgets": "^3.13",
"blade-ui-kit/blade-heroicons": "^1.3",
Expand Down
76 changes: 75 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 61b851e

Please sign in to comment.