Skip to content

Commit

Permalink
Refactor a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Apr 3, 2024
1 parent e1ec516 commit e3aa2f6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
10 changes: 4 additions & 6 deletions src/Console/Commands/CountUniqueUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ class CountUniqueUser extends Command
*/
public function handle()
{
$nbrUser = $this->countUniqueUser();
$fromDate = Carbon::now()->subMonth()->startOfMonth()->toDateString();
$tillDate = Carbon::now()->subMonth()->endOfMonth()->toDateString();

$date = Carbon::now()->subMonth()->endOfMonth()->toDateString();
$nbrUser = User::whereBetween('login_at', [$fromDate, $tillDate])->count();

DB::table('kpis_unique_users')->insert(['date' => $date, 'value' => $nbrUser]);
DB::table('kpis_unique_users')->insert(['date' => $tillDate, 'value' => $nbrUser]);
}

private function countUniqueUser()
{
$fromDate = Carbon::now()->subMonth()->startOfMonth()->toDateString();
$tillDate = Carbon::now()->subMonth()->endOfMonth()->toDateString();
return User::whereBetween('login_at', [$fromDate, $tillDate])->count();
}
}
10 changes: 2 additions & 8 deletions src/Console/Commands/CountUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@ class CountUser extends Command
* @return void
*/
public function handle()
{
$nbrUser = $this->countUser();

DB::table('kpis_users')->insert(['date' => Carbon::yesterday()->toDateString(), 'value' => $nbrUser]);
}

private function countUser()
{
$yesterday = Carbon::yesterday()->toDateString();
$nbrUser = User::where('login_at', '=', $yesterday)->count();

return User::where('login_at', '=', $yesterday)->count();
DB::table('kpis_users')->insert(['date' => $yesterday, 'value' => $nbrUser]);
}
}
11 changes: 8 additions & 3 deletions src/Console/Commands/DetermineStorageUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

class DetermineStorageUsage extends Command
{
/**
* GB in byte.
*/
const GB = 1000000000;

/**
* The console command name.
*
Expand All @@ -32,17 +37,17 @@ class DetermineStorageUsage extends Command
public function handle()
{
$size = $this->getSizeInGB();

$date = Carbon::now()->subMonth()->endOfMonth()->toDateString();

DB::table('kpis_storage_usage')->insert(['date' => $date, 'value' => $size]);
}

private function getSizeInGB()
{
$byte = 1000000000;
$imageStorageUsage = Image::sum(DB::raw("(attrs->>'size')::bigint"));
$videoStorageUsage = Video::sum(DB::raw("(attrs->>'size')::bigint"));
return ($imageStorageUsage + $videoStorageUsage)/$byte;

return ($imageStorageUsage + $videoStorageUsage) / self::GB;
}
}
4 changes: 0 additions & 4 deletions src/KpisServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public function boot(Modules $modules, Router $router)
],
]);

$this->publishes([
__DIR__.'/public/assets' => public_path('vendor/kpis'),
], 'public');

if ($this->app->runningInConsole()) {
$this->commands([
CountUniqueUser::class,
Expand Down
4 changes: 1 addition & 3 deletions src/config/kpis.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

return [
/**
*
* Token to verify cron job request to save visits/actions
*
**/
*/
'token' => env('KPIS_TOKEN'),
];

0 comments on commit e3aa2f6

Please sign in to comment.