From f29e5ba5fae9f2c5b14fbe12fa630762ef12d4e4 Mon Sep 17 00:00:00 2001 From: Daniel Mohns Date: Thu, 12 Dec 2024 18:09:29 +0100 Subject: [PATCH] Add Ticke seeding (#423) --- docs/development/development-environment.md | 35 +++--- .../app/Console/Commands/DemoDataCreator.php | 116 ------------------ src/backend/database/seeders/TicketSeeder.php | 91 ++++++++++++++ 3 files changed, 107 insertions(+), 135 deletions(-) delete mode 100644 src/backend/app/Console/Commands/DemoDataCreator.php diff --git a/docs/development/development-environment.md b/docs/development/development-environment.md index 97d6222a..af48ada9 100644 --- a/docs/development/development-environment.md +++ b/docs/development/development-environment.md @@ -46,19 +46,18 @@ Before proceeding, ensure you can open a terminal (Terminal app on macOS, termin ## Seeding the database +When you run the application in development mode for the first time, it will automatically +seed the database with demo data. +This demo data tries to mimic a real-world use-case and is generally helpful when exploring MicroPowerManager's functionality or feature development. + > [!INFO] > -> When you run the application for the first time, you can explore a vanilla MicroPowerManager instance where you can register new users and tenants. +> This behaviour is controlled by the environment variable `MPM_LOAD_DEMO_DATA`, see [here](/installation/environment-variables.html#micropowermanager). +> +> If you wish to explore a vanilla MicroPowerManager instance where you can register new users and tenants from scratch, set `MPM_LOAD_DEMO_DATA` to `false`. > > However, for a better development flow it is generally recommended to seed the local development environment with demo data. -To seed the database with Demo data, run: - -```bash -docker exec -it backend-dev bash -php artisan db:seed -``` - Log in to the application using the following credentials: ```sh @@ -68,17 +67,17 @@ password: 123123 The Demo Company protected page password of this company is `123123`. -## Generating ticket data +## Running `artisan` commands + +When working with Laravel it can be helpful to have access to Laravel's CLI tool [Artisan](https://laravel.com/docs/9.x/artisan). -To generate ticket for the Demo Company data, run: +To access `artisan` in MicroPowerManager development environment, run ```sh -php artisan demo:create-data --type=ticket 25 +docker exec -it backend-dev bash +php artisan --help ``` -This commands will generate 25 tickets within the past 365 days respectively. -It can be run multiple times to generate more data as required. - ## Reseting the Demo data > [!WARNING] @@ -114,9 +113,7 @@ Configurations for a different editor will work a like. For local development and editor integration it can be helpful to have a local instance of PHP. This will allow you to run composer scripts like `larastan` without the need to use Docker. -These steps are highly dependant on your system. - -For example using `brew` on MacOS +These steps are highly dependant on your system setup. For example using `brew` on MacOS ```sh brew install php@8.2 @@ -175,8 +172,8 @@ And configure the following database connection - **Host:** `localhost`/`127.0.0.1` - **Port:** `3306` - **User:** `root` -- **Password:** Take it from [`.env`](https://github.com/EnAccess/micropowermanager/blob/main/docker/.env) -- **Database:** Leave empty if possible, as we will be interacting with multiple databases. Alternatively configure a seperate connection for each database. For example `micro_power_manager` and `DummyCompany_1`. +- **Password:** Take it from [`.env.mysql`](https://github.com/EnAccess/micropowermanager/blob/main/dev/.env.mysql) +- **Database:** Leave empty if possible, as we will be interacting with multiple databases. Alternatively configure a seperate connection for each database. For example `micro_power_manager` and `DemoCompany_1`. ![SQL Editor database connection](/screenshots/sql-editor-database-connection.png) diff --git a/src/backend/app/Console/Commands/DemoDataCreator.php b/src/backend/app/Console/Commands/DemoDataCreator.php deleted file mode 100644 index 1054dfef..00000000 --- a/src/backend/app/Console/Commands/DemoDataCreator.php +++ /dev/null @@ -1,116 +0,0 @@ -option('company-id'); - $type = $this->option('type') ?? 'transaction'; - $amount = $this->argument('amount'); - - if (config('app.env') == 'production') { - echo 'production mode is not allowed to create fake transactions'; - - return; - } - - for ($i = 1; $i <= $amount; ++$i) { - echo "$type is generating number: $i \n"; - try { - DB::connection('shard')->beginTransaction(); - if ($type == 'transaction') { - (new Warn($this->getOutput()))->render( - 'Generating Transaction data using script is no longer supported. Use `artisan db:seed --TransactionSeeder` instead.`' - ); - } else { - $this->generateTicket(); - } - DB::connection('shard')->commit(); - } catch (\Exception $e) { - DB::connection('shard')->rollBack(); - echo $e->getMessage(); - throw $e; - } - } - } - - private function generateTicket() { - $randomCategory = TicketCategory::query()->inRandomOrder()->first(); - $fakeSentence = $this->generateFakeSentence(); - $randomCreator = User::inRandomOrder()->first(); - $demoDate = date('Y-m-d', strtotime('-'.mt_rand(0, 365).' days')); - $ticketUser = TicketUser::inRandomOrder()->first(); - $randomMaintenanceUser = MaintenanceUsers::inRandomOrder()->first(); - $randomPerson = Person::inRandomOrder()->where('is_customer', 1)->first(); - $dueDate = date('Y-m-d', strtotime('+3 days', strtotime($demoDate))); - $status = rand(0, 1); - - $ticket = Ticket::query()->make([ - 'ticket_id' => Str::random(10), - 'creator_type' => 'admin', - 'creator_id' => $randomCreator->id, - 'status' => $status, - 'due_date' => $dueDate, - 'title' => 'Dummy Ticket', - 'content' => $fakeSentence, - 'category_id' => $randomCategory->id, - 'created_at' => $demoDate, - 'updated_at' => $demoDate, - ]); - - if ($randomCategory->out_source) { - $ticket->assigned_id = null; - $ticket->owner_id = $randomMaintenanceUser->id; - $ticket->owner_type = 'maintenance_user'; - $ticket->save(); - try { - $amount = random_int(10, 200); - } catch (\Exception $e) { - $amount = 50; - } - TicketOutsource::query()->create([ - 'ticket_id' => $ticket->id, - 'amount' => $amount, - 'created_at' => $demoDate, - 'updated_at' => $demoDate, - ]); - } else { - $ticket->assigned_id = $ticketUser->id; - $ticket->owner_id = $randomPerson->id; - $ticket->owner_type = 'person'; - $ticket->save(); - } - } - - private function generateFakeSentence($minWords = 5, $maxWords = 15) { - $loremIpsum = - 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; - $words = explode(' ', $loremIpsum); - $numWords = rand($minWords, $maxWords); - - shuffle($words); - $fakeSentence = implode(' ', array_slice($words, 0, $numWords)); - - // Capitalize the first letter of the sentence. - $fakeSentence = ucfirst($fakeSentence); - - // Add a period at the end. - $fakeSentence .= '.'; - - return $fakeSentence; - } -} diff --git a/src/backend/database/seeders/TicketSeeder.php b/src/backend/database/seeders/TicketSeeder.php index 4b920c19..a03c7c9b 100644 --- a/src/backend/database/seeders/TicketSeeder.php +++ b/src/backend/database/seeders/TicketSeeder.php @@ -7,8 +7,15 @@ use App\Models\MaintenanceUsers; use App\Models\MiniGrid; use App\Models\Person\Person; +use App\Models\User; +use Illuminate\Console\View\Components\Info; use Illuminate\Database\Seeder; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Str; +use Inensus\Ticket\Models\Ticket; use Inensus\Ticket\Models\TicketCategory; +use Inensus\Ticket\Models\TicketOutsource; +use Inensus\Ticket\Models\TicketUser; use MPM\DatabaseProxy\DatabaseProxyManagerService; class TicketSeeder extends Seeder { @@ -18,12 +25,18 @@ public function __construct( $this->databaseProxyManagerService->buildDatabaseConnectionDummyCompany(); } + private $amount = 100; + /** * Run the database seeds. * * @return void */ public function run() { + (new Info($this->command->getOutput()))->render( + "Running TransactionSeeder to generate $this->amount tickets. This may take some time." + ); + // Create Ticket categories TicketCategory::factory() ->count(12) @@ -130,5 +143,83 @@ public function run() { ->for($agentPerson) ->create(); } + + // Seed tickets + for ($i = 1; $i <= $this->amount; ++$i) { + try { + DB::connection('shard')->beginTransaction(); + $this->generateTicket(); + DB::connection('shard')->commit(); + } catch (\Exception $e) { + DB::connection('shard')->rollBack(); + echo $e->getMessage(); + } + } + } + + private function generateTicket() { + $randomCategory = TicketCategory::query()->inRandomOrder()->first(); + $fakeSentence = $this->generateFakeSentence(); + $randomCreator = User::inRandomOrder()->first(); + $demoDate = date('Y-m-d', strtotime('-'.mt_rand(0, 365).' days')); + $ticketUser = TicketUser::inRandomOrder()->first(); + $randomMaintenanceUser = MaintenanceUsers::inRandomOrder()->first(); + $randomPerson = Person::inRandomOrder()->where('is_customer', 1)->first(); + $dueDate = date('Y-m-d', strtotime('+3 days', strtotime($demoDate))); + $status = rand(0, 1); + + $ticket = Ticket::query()->make([ + 'ticket_id' => Str::random(10), + 'creator_type' => 'admin', + 'creator_id' => $randomCreator->id, + 'status' => $status, + 'due_date' => $dueDate, + 'title' => 'Dummy Ticket', + 'content' => $fakeSentence, + 'category_id' => $randomCategory->id, + 'created_at' => $demoDate, + 'updated_at' => $demoDate, + ]); + + if ($randomCategory->out_source) { + $ticket->assigned_id = null; + $ticket->owner_id = $randomMaintenanceUser->id; + $ticket->owner_type = 'maintenance_user'; + $ticket->save(); + try { + $amount = random_int(10, 200); + } catch (\Exception $e) { + $amount = 50; + } + TicketOutsource::query()->create([ + 'ticket_id' => $ticket->id, + 'amount' => $amount, + 'created_at' => $demoDate, + 'updated_at' => $demoDate, + ]); + } else { + $ticket->assigned_id = $ticketUser->id; + $ticket->owner_id = $randomPerson->id; + $ticket->owner_type = 'person'; + $ticket->save(); + } + } + + private function generateFakeSentence($minWords = 5, $maxWords = 15) { + $loremIpsum = + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'; + $words = explode(' ', $loremIpsum); + $numWords = rand($minWords, $maxWords); + + shuffle($words); + $fakeSentence = implode(' ', array_slice($words, 0, $numWords)); + + // Capitalize the first letter of the sentence. + $fakeSentence = ucfirst($fakeSentence); + + // Add a period at the end. + $fakeSentence .= '.'; + + return $fakeSentence; } }