Skip to content

Commit

Permalink
Command Efficiency (#357)
Browse files Browse the repository at this point in the history
* Initializing Command_Efficiency for empty Pull Request

* Add indexes to calendars table to greatly speed up queries

* Ensure we upgrade filament when we upgrade deps

* Greatly increase the efficiency of demand-payment command

* Switch to stechstudio/filament-impersonate
  • Loading branch information
V13Axel authored Jan 26, 2024
1 parent e149577 commit 3e3ae10
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 77 deletions.
71 changes: 48 additions & 23 deletions app/Console/Commands/DisableUnpaidCalendars.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Calendar;
use App\Models\User;
use DB;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

Expand Down Expand Up @@ -42,38 +43,62 @@ public function handle()
{
$updated = Calendar::with(['user', 'user.subscriptions'])
->where('disabled', '=', true)
->whereHas('user.subscriptions', function(Builder $query) {
->whereHas('user.subscriptions', function (Builder $query) {
$query->where('stripe_status', '=', 'active');
})
->update([
'disabled' => false
]);

if($updated > 0) {
if ($updated > 0) {
$this->info("Re-enabled " . $updated . " calendars.");
}

User::with('subscriptions')
->whereHas('subscriptions', function(Builder $query) {
$query->where('stripe_status', '!=', 'active');
$this->disableCalendarsWhen('<', '2020-11-08', 15);
$this->disableCalendarsWhen('>=', '2020-11-08', 2);
}

private function disableCalendarsWhen(string $operator, string $date, int $max_calendars)
{
$query = User::with('subscriptions', 'calendars')
->where('beta_authorised', '=', false)
->where('created_at', $operator, $date)
->whereHas('subscriptions')
->whereDoesntHave('subscriptions', function (Builder $query) {
$query->active();
})
->whereHas('calendars', function(Builder $query){
$query->where('disabled', '=', true);
}, '>', '2')
->chunk(100, function($users){
foreach ($users as $user) {
$this->debug('Processing ' . $user->username);

$max_calendars = $user->isEarlySupporter() ? 15 : 2;

$user->calendars()->orderBy('date_created', 'ASC')->each(function($calendar, $index) use ($max_calendars){
$calendar->disabled = ($index < $max_calendars) ? 0 : 1;
$calendar->parent_id = Null;
$calendar->parent_offset = Null;
$calendar->parent_link_date = Null;
$calendar->save();
});
}
});
->whereHas('calendars', function (Builder $query) {
$query->where('disabled', '!=', true);
}, '>', $max_calendars);

$count = $query->count();

if ($count === 0) {
logger()->info("No users found exceeding maximum of " . $max_calendars . " calendars.");

return;
}

logger()->info("Disabling calendars for $count users who have more than " . $max_calendars . " calendars.");

$query->chunk(100, function ($users) use ($max_calendars) {
foreach ($users as $user) {
$this->info('Processing ' . $user->username);

$disableIds = $user->calendars
->sortBy('created_at')
->take($user->calendars->count() - $max_calendars)
->pluck('id');

$user->calendars()
->whereIn('id', $disableIds)
->update([
'disabled' => true,
'parent_id' => null,
'parent_offset' => null,
'parent_link_date' => null,
]);
}
});
}
}
6 changes: 2 additions & 4 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Password;
use STS\FilamentImpersonate\Tables\Actions\Impersonate;

class UserResource extends Resource
{
Expand Down Expand Up @@ -78,10 +79,7 @@ public static function table(Table $table): Table
Tables\Filters\Filter::make('beta_authorised')
->query(fn(Builder $query): Builder => $query->where('beta_authorised', true))
])->actions([
Tables\Actions\Action::make('impersonate')
->label('Impersonate')
->icon('heroicon-o-user-circle')
->url(fn($record) => route('admin.impersonate', ['userid' => $record->id, 'returnPath' => request()->url()])),
Impersonate::make(),
])->bulkActions([
Tables\Actions\BulkAction::make('password_reset')
->action(function($records){
Expand Down
8 changes: 8 additions & 0 deletions app/Filament/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

use App\Filament\Resources\UserResource;
use Filament\Resources\Pages\EditRecord;
use STS\FilamentImpersonate\Pages\Actions\Impersonate;

class EditUser extends EditRecord
{
protected static string $resource = UserResource::class;

protected function getActions(): array
{
return [
Impersonate::make()->record($this->getRecord()),
];
}
}
36 changes: 0 additions & 36 deletions app/Http/Controllers/AdminController.php

This file was deleted.

6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"spatie/laravel-export": "^0.3.11",
"spatie/laravel-honeypot": "^4.3",
"spatie/laravel-ignition": "^2.2",
"spatie/laravel-log-dumper": "^1.4"
"spatie/laravel-log-dumper": "^1.4",
"stechstudio/filament-impersonate": "^3.5"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.7",
Expand Down Expand Up @@ -94,7 +95,8 @@
],
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
"@php artisan package:discover --ansi",
"@php artisan filament:upgrade"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
Expand Down
112 changes: 111 additions & 1 deletion composer.lock

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('calendars', function (Blueprint $table) {
$table->index('user_id');
$table->index('parent_id');
$table->index('disabled');
$table->index('advancement_enabled');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('calendars', function (Blueprint $table) {
//
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('discord_auths', function (Blueprint $table) {
$table->index('user_id');
$table->index('discord_user_id');
$table->index('discord_email');
});

Schema::table('discord_guilds', function (Blueprint $table) {
$table->index('user_id');
$table->index('discord_auth_id');
$table->index('guild_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('discord_auths', function (Blueprint $table) {
//
});
}
};
Loading

0 comments on commit 3e3ae10

Please sign in to comment.