Skip to content

Commit

Permalink
Add Ticke seeding (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmohns authored Dec 12, 2024
1 parent 5f95e76 commit f29e5ba
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 135 deletions.
35 changes: 16 additions & 19 deletions docs/development/development-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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 [email protected]
Expand Down Expand Up @@ -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)

Expand Down
116 changes: 0 additions & 116 deletions src/backend/app/Console/Commands/DemoDataCreator.php

This file was deleted.

91 changes: 91 additions & 0 deletions src/backend/database/seeders/TicketSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
}

0 comments on commit f29e5ba

Please sign in to comment.