Skip to content

Commit

Permalink
Merge pull request #99 from P3D-Legacy/stats
Browse files Browse the repository at this point in the history
Stats
  • Loading branch information
dsbilling authored Mar 30, 2022
2 parents c38601f + 18b7bcb commit 91a57e5
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Actions\Fortify;

use App\Models\User;
use App\Stats\UserRegistrationStats;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
Expand Down Expand Up @@ -39,6 +40,8 @@ public function create(array $input)
'text' => config('app.consents')[config('app.required_consent')],
]);

UserRegistrationStats::increase();

return $user;
}
}
2 changes: 2 additions & 0 deletions app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions\Jetstream;

use App\Stats\UserRegistrationStats;
use Laravel\Jetstream\Contracts\DeletesUsers;

class DeleteUser implements DeletesUsers
Expand All @@ -17,5 +18,6 @@ public function delete($user)
$user->deleteProfilePhoto();
$user->tokens->each->delete();
$user->delete();
UserRegistrationStats::decrease();
}
}
18 changes: 18 additions & 0 deletions app/Http/Controllers/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StatsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('stats.index');
}
}
29 changes: 29 additions & 0 deletions app/Http/Livewire/Admin/ResourceCreationStatsGraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Livewire\Admin;

use App\Stats\ResourceCreationStats;
use Livewire\Component;

class ResourceCreationStatsGraph extends Component
{
public $stats;
public $labels;
public $values;

public function mount()
{
$this->stats = ResourceCreationStats::query()
->start(now()->subDays(10))
->end(now()->subSecond())
->groupByDay()
->get();
$this->labels = $this->stats->pluck('start')->toArray();
$this->values = $this->stats->pluck('value')->toArray();
}

public function render()
{
return view('livewire.admin.resource-creation-stats-graph');
}
}
31 changes: 31 additions & 0 deletions app/Http/Livewire/Admin/UserRegistrationStatsGraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Livewire\Admin;

use Livewire\Component;
use App\Stats\UserRegistrationStats;

class UserRegistrationStatsGraph extends Component
{
public $stats;
public $labels;
public $values;

public function mount()
{
//UserRegistrationStats::set(random_int(1, 1000), now()->subDays(10));
$this->stats = UserRegistrationStats::query()
->start(now()->subDays(10))
->end(now()->subSecond())
->groupByDay()
->get();
$this->labels = $this->stats->pluck('start')->toArray();
$this->values = $this->stats->pluck('value')->toArray();
//dd($this->stats->pluck('value')->toArray());
}

public function render()
{
return view('livewire.admin.user-registration-stats-graph');
}
}
8 changes: 8 additions & 0 deletions app/Models/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Spatie\Tags\HasTags;
use App\Models\BaseModel;
use Illuminate\Support\Str;

use App\Stats\ResourceCreationStats;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\LogOptions;
use Overtrue\LaravelLike\Traits\Likeable;
use Spatie\Activitylog\Traits\LogsActivity;
Expand Down Expand Up @@ -35,13 +38,18 @@ public static function boot()
self::creating(function ($model) {
$model->uuid = Str::uuid()->toString();
$model->user_id = auth()->id();
ResourceCreationStats::increase();
});

self::updating(function ($model) {
if (!$model->uuid) {
$model->uuid = Str::uuid()->toString();
}
});

self::deleting(function ($model) {
ResourceCreationStats::decrease();
});
}

/**
Expand Down
14 changes: 14 additions & 0 deletions app/Stats/ResourceCreationStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Stats;

use Spatie\Stats\BaseStats;

class ResourceCreationStats extends BaseStats
{
/*
public function getName() : string{
return 'my-custom-name'; // stats will be stored with using name `my-custom-name`
}
*/
}
14 changes: 14 additions & 0 deletions app/Stats/UserRegistrationStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Stats;

use Spatie\Stats\BaseStats;

class UserRegistrationStats extends BaseStats
{
/*
public function getName() : string{
return 'my-custom-name'; // stats will be stored with using name `my-custom-name`
}
*/
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"spatie/laravel-health": "^1.8",
"spatie/laravel-medialibrary": "^10.0",
"spatie/laravel-permission": "^5.5",
"spatie/laravel-stats": "^2.0",
"spatie/laravel-tags": "^4.3",
"symfony/http-client": "^6.0",
"symfony/postmark-mailer": "^6.0",
Expand All @@ -77,6 +78,7 @@
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"pestphp/pest-plugin": true
}
},
Expand Down
90 changes: 84 additions & 6 deletions composer.lock

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

2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql')
? array_filter([
Expand Down
21 changes: 21 additions & 0 deletions database/migrations/2022_01_06_195808_create_stats_tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStatsTables extends Migration
{
public function up()
{
Schema::create('stats_events', function (Blueprint $table) {
$table->id();

$table->string('name');
$table->string('type');
$table->bigInteger('value');

$table->timestamps();
});
}
}
3 changes: 3 additions & 0 deletions database/seeders/PermissionSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function run()
$p15 = Permission::firstOrCreate(['name' => 'categories.update']);
$p16 = Permission::firstOrCreate(['name' => 'categories.destroy']);

$p17 = Permission::firstOrCreate(['name' => 'stats']);

// Super Admin permissions
$r1->givePermissionTo(Permission::all());

Expand All @@ -55,6 +57,7 @@ public function run()
$r2->givePermissionTo($p11->name);
$r2->givePermissionTo($p12->name);
$r2->givePermissionTo($p13->name);
$r2->givePermissionTo($p14->name);

// Moderator permissions
$r3->givePermissionTo($p8->name);
Expand Down
Loading

0 comments on commit 91a57e5

Please sign in to comment.