Skip to content

Commit

Permalink
Feat/add more demo seeder (#468)
Browse files Browse the repository at this point in the history
* Add demo user seeder and adjust event seeder

* Lint stuff
  • Loading branch information
TitusKirch authored Sep 13, 2023
1 parent 27cf98d commit e47b6a1
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 37 deletions.
19 changes: 10 additions & 9 deletions app/Helpers/DivisionLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class DivisionLogger
{
public string $logFilePath;

public int $writeFlags;

public function __construct($logFilePath, $writeFlags = FILE_APPEND)
Expand All @@ -20,7 +21,7 @@ public function __construct($logFilePath, $writeFlags = FILE_APPEND)
/**
* Logs current state of specified event
*
* @param Event $event The event that will be logged
* @param Event $event The event that will be logged
* @return void
*/
public function logEvent(Event $event)
Expand All @@ -29,7 +30,7 @@ public function logEvent(Event $event)
$regsTotal = $event->registrations()->count();
$nonDrinkerRegsTotal = $event->registrations()->where('drinks_alcohol', '=', false)->count();

$line = sprintf('Event %d :: %d groups, %d total regs, %d non-drinker regs (%d %%)', $event->id, $groupsTotal, $regsTotal, $nonDrinkerRegsTotal, $nonDrinkerRegsTotal/$regsTotal * 100);
$line = sprintf('Event %d :: %d groups, %d total regs, %d non-drinker regs (%d %%)', $event->id, $groupsTotal, $regsTotal, $nonDrinkerRegsTotal, $nonDrinkerRegsTotal / $regsTotal * 100);

$line .= "\n";
foreach (Course::all() as $course) {
Expand All @@ -41,10 +42,10 @@ public function logEvent(Event $event)
$regsCount = $regsOfCourse->count();
$nonDrinkerRegsCount = $regsOfCourse->where('drinks_alcohol', '=', false)->count();

$line .= sprintf('%s : [ %d t (%d %%), %d nd ] ;', $course->abbreviation, $regsCount, $regsCount/$regsTotal * 100, $nonDrinkerRegsCount);
$line .= sprintf('%s : [ %d t (%d %%), %d nd ] ;', $course->abbreviation, $regsCount, $regsCount / $regsTotal * 100, $nonDrinkerRegsCount);
}

$this->logMsg($line . "\n-----\n");
$this->logMsg($line."\n-----\n");

foreach ($event->groups as $group) {
$this->logGroup($group);
Expand All @@ -56,7 +57,7 @@ public function logEvent(Event $event)
/**
* Logs current state of specified group
*
* @param Group $group The group that will be logged
* @param Group $group The group that will be logged
* @return void
*/
public function logGroup(Group $group)
Expand All @@ -69,12 +70,13 @@ public function logGroup(Group $group)
if ($regsTotal == 0) {
$line .= "\n";
$this->logMsg($line);

return;
}

$this->logMsg($line);

$line = "";
$line = '';
foreach (Course::all() as $course) {
$regs = $group->registrations()->get();
$regsOfCourse = $regs->toQuery()
Expand All @@ -84,15 +86,14 @@ public function logGroup(Group $group)
$regsCount = $regsOfCourse->count();
$nonDrinkerRegsCount = $regsOfCourse->where('drinks_alcohol', '=', false)->count();

$line .= sprintf('%s : [ %d t (%d %%), %d nd ] ;', $course->abbreviation, $regsCount, $regsCount/$regsTotal * 100, $nonDrinkerRegsCount);
$line .= sprintf('%s : [ %d t (%d %%), %d nd ] ;', $course->abbreviation, $regsCount, $regsCount / $regsTotal * 100, $nonDrinkerRegsCount);
}
$this->logMsg($line . "\n");
$this->logMsg($line."\n");
}

/**
* Logs a single message
*
* @param string $msg
* @return void
*/
public function logMsg(string $msg)
Expand Down
8 changes: 4 additions & 4 deletions app/Helpers/GroupBalancedDivision.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ public function assign()
});

if ($groupsNotAssigned) {
$this->loggingEnabled ? $this->logCurrState("Pre-assignInitial") : 0;
$this->loggingEnabled ? $this->logCurrState('Pre-assignInitial') : 0;
$this->assignInitial();
$this->loggingEnabled ? $this->logCurrState("Post-assignInitial") : 0;
$this->loggingEnabled ? $this->logCurrState('Post-assignInitial') : 0;
}
$this->loggingEnabled ? $this->logCurrState("Pre-assignLeftover") : 0;
$this->loggingEnabled ? $this->logCurrState('Pre-assignLeftover') : 0;
$this->assignLeftover();
if ($this->maxGroupSize > 0) {
$this->updateQueuePos($this->getUnassignedRegs()->sortBy('queue_position'));
}
$this->loggingEnabled ? $this->logCurrState("Post-assignLeftover") : 0;
$this->loggingEnabled ? $this->logCurrState('Post-assignLeftover') : 0;
}
}
8 changes: 4 additions & 4 deletions app/Helpers/GroupCourseDivision.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public function assign()
});

if ($courseNotAssigned) {
$this->loggingEnabled ? $this->logCurrState("Pre-assignInitial") : 0;
$this->loggingEnabled ? $this->logCurrState('Pre-assignInitial') : 0;
$this->assignInitial();
$this->loggingEnabled ? $this->logCurrState("Post-assignInitial") : 0;
$this->loggingEnabled ? $this->logCurrState('Post-assignInitial') : 0;
}
$this->loggingEnabled ? $this->logCurrState("Pre-assignLeftover") : 0;
$this->loggingEnabled ? $this->logCurrState('Pre-assignLeftover') : 0;
$this->assignLeftover();
if ($this->maxGroupSize > 0) {
$this->updateQueuePos($this->getUnassignedRegs()->sortBy('queue_position'));
}
$this->loggingEnabled ? $this->logCurrState("Pre-assignLeftover") : 0;
$this->loggingEnabled ? $this->logCurrState('Pre-assignLeftover') : 0;
}
}
26 changes: 16 additions & 10 deletions app/Helpers/GroupDivision.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ abstract class GroupDivision
protected int $minNonDrinkers;

protected bool $loggingEnabled;

protected string $loggingFilePath;

public function __construct(Event $event, bool $assignByAlc, int $maxGroups = 0, int $maxGroupSize = 0, int $minNonDrinkers = 3)
Expand All @@ -36,34 +37,38 @@ public function __construct(Event $event, bool $assignByAlc, int $maxGroups = 0,
$this->minNonDrinkers = $minNonDrinkers;

$this->loggingEnabled = false;
$this->loggingFilePath = storage_path('logs/' . env('DIVISION_DEBUG_LOG', 'division.log'));
$this->loggingFilePath = storage_path('logs/'.env('DIVISION_DEBUG_LOG', 'division.log'));
}

/**
* Enables detailed logging for this division to a specified file path. Use before calling "assign()"
*
* @param string $logFilePath Path where the logs will be written
* @param string $logFilePath Path where the logs will be written
* @return void
*/
public function enableLogging(string $logFilePath = "")
public function enableLogging(string $logFilePath = '')
{
$this->loggingEnabled = true;
if ($logFilePath) $this->loggingFilePath = $logFilePath;
if ($logFilePath) {
$this->loggingFilePath = $logFilePath;
}
}

/**
* Logs current state of this division (meaning groups with info on their assigned registrations) if logging is enabled
*
* @param string $statename Describes what state the division is in at the time of logging
* @param string $statename Describes what state the division is in at the time of logging
* @return void
*/
public function logCurrState(string $statename)
{
if ($this->loggingEnabled == false) return;
if ($this->loggingEnabled == false) {
return;
}

$logger = new DivisionLogger($this->loggingFilePath);

$logger->logMsg("-----\n DIVISION STATE: " . $statename . "\n-----\n");
$logger->logMsg("-----\n DIVISION STATE: ".$statename."\n-----\n");
$logger->logEvent($this->event);
}

Expand Down Expand Up @@ -134,10 +139,10 @@ protected function assignInitial()
{
if ($this->assignByAlc) {
$this->assignNonDrinkers();
$this->loggingEnabled ? $this->logCurrState("Post-assignNonDrinkers") : 0;
$this->loggingEnabled ? $this->logCurrState('Post-assignNonDrinkers') : 0;
}
$this->assignUntilSatisfies();
$this->loggingEnabled ? $this->logCurrState("Post-assignUntilSatisfies") : 0;
$this->loggingEnabled ? $this->logCurrState('Post-assignUntilSatisfies') : 0;
if ($this->maxGroupSize > 0) {
$this->updateQueuePos($this->getUnassignedRegs());
}
Expand Down Expand Up @@ -184,8 +189,9 @@ public function assignLeftover()
$group = $groupsWithOpenSpots->first();

// If there are no non-drinkers left, the following assign cycles can safely skip alcohol considerations
if ($cycleAssignByAlc && $unassignedRegs->where('drinks_alcohol', '=', false)->count() == 0)
if ($cycleAssignByAlc && $unassignedRegs->where('drinks_alcohol', '=', false)->count() == 0) {
$cycleAssignByAlc = false;
}

$registration = $unassignedRegs->pop();

Expand Down
13 changes: 8 additions & 5 deletions app/Http/Controllers/DivisionLoggingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Http\Controllers;

use App\Helpers\GroupBalancedDivision;
use App\Helpers\DivisionLogger;
use App\Models\Event;
use App\Models\Group;
Expand All @@ -11,12 +10,14 @@ class DivisionLoggingController extends Controller
{
public function logGrp(int $groupId)
{
$logPath = 'logs/' . env('DIVISION_DEBUG_LOG', 'division.log');
$logPath = 'logs/'.env('DIVISION_DEBUG_LOG', 'division.log');
$logger = new DivisionLogger(storage_path($logPath));

$group = Group::find($groupId);

if (! $group) return sprintf('DIVISION_LOGGER_ERROR: Could not find group with ID %&d', $groupId);
if (! $group) {
return sprintf('DIVISION_LOGGER_ERROR: Could not find group with ID %&d', $groupId);
}

$logger->logGroup($group);

Expand All @@ -25,12 +26,14 @@ public function logGrp(int $groupId)

public function logEvt(int $eventId)
{
$logPath = 'logs/' . env('DIVISION_DEBUG_LOG', 'division.log');
$logPath = 'logs/'.env('DIVISION_DEBUG_LOG', 'division.log');
$logger = new DivisionLogger(storage_path($logPath));

$event = Event::find($eventId);

if (! $event) return sprintf('DIVISION_LOGGER_ERROR: Could not find event with ID %&d', $eventId);
if (! $event) {
return sprintf('DIVISION_LOGGER_ERROR: Could not find event with ID %&d', $eventId);
}

$logger->logEvent($event);

Expand Down
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RouteServiceProvider extends ServiceProvider
public function boot(): void
{
// check if app debug mode is not enabled and then apply rate limiter
if (!config('app.debug')) {
if (! config('app.debug')) {
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(1200)->by($request->user()?->id ?: $request->ip());
});
Expand Down
2 changes: 1 addition & 1 deletion config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
'course_id' => env('SEEDER_COURSE_ID', 1),
'slot_id' => env('SEEDER_SLOT_ID', null),
'regs_total' => env('SEEDER_TOTAL', 1),
'regs_nd' => env('SEEDER_NONDRINKERS', 1)
'regs_nd' => env('SEEDER_NONDRINKERS', 1),
],

/*
Expand Down
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function run(): void
if (config('app.event_type') == 'demo') {
$seeders = [
...$seeders,
UserDemoSeeder::class,
ModuleDemoSeeder::class,
EventsDemoSeeder::class,
PageDemoSeeder::class,
Expand Down
60 changes: 60 additions & 0 deletions database/seeders/EventsDemoSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use App\Models\Event;
use App\Models\Group;
use App\Models\Registration;
use App\Models\Slot;
use App\Models\User;
use DateTime;
use Illuminate\Database\Seeder;

Expand Down Expand Up @@ -65,6 +67,18 @@ public function runGroupPhase(): void
$group->event_id = $event->id;
$group->save();
}

// get all users (except admin and tutor)
$users = User::where('is_admin', false)->where('is_tutor', false)->get();

// register users to event
foreach ($users as $user) {
// create registration
$event->registrations()->create([
'user_id' => $user->id,
'drinks_alcohol' => rand(0, 1) == 1,
]);
}
}

/**
Expand Down Expand Up @@ -110,6 +124,20 @@ public function runEventRegistration(): void

// save the event
$event->save();

// get all users (except admin and tutor)
$users = User::where('is_admin', false)->where('is_tutor', false)->get();

// register users to event
foreach ($users as $user) {
// create registration
$event->registrations()->create([
'user_id' => $user->id,
'form_responses' => json_encode([
'lorem' => ['ipsum', 'dolor', 'sit'][rand(0, 2)],
]),
]);
}
}

/**
Expand Down Expand Up @@ -181,6 +209,7 @@ public function runSlotBooking(): void
],
];

// save slots
foreach ($slots as $slotData) {
$slot = new Slot();
$slot->name = $slotData['name'];
Expand All @@ -190,5 +219,36 @@ public function runSlotBooking(): void

$slot->save();
}

// get all users (except admin and tutor)
$users = User::where('is_admin', false)->where('is_tutor', false)->get();

// get all slots of the event
$slots = $event->slots()->get();

// register users to event
foreach ($users as $user) {
// get random slot
$slot = $slots[rand(0, count($slots) - 1)];

// check if slot has maximum_participants
$queuePosition = null;
if ($slot->maximum_participants) {
$queuePosition = Registration::where('event_id', $event->id)->where('slot_id', $slot->id)->max('queue_position');

if (! $queuePosition || $queuePosition == -1) {
$queuePosition = -1;
} else {
$queuePosition++;
}
}

// create registration
$event->registrations()->create([
'user_id' => $user->id,
'slot_id' => $slot->id,
'queue_position' => $queuePosition ?? null,
]);
}
}
}
32 changes: 32 additions & 0 deletions database/seeders/UserDemoSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Database\Seeders;

use App\Models\User;
use Illuminate\Database\Seeder;

class UserDemoSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// create one admin user
User::factory()->elevatedPrivileges(true, true)->create([
'firstname' => 'Admin',
'lastname' => 'User',
'email' => '[email protected]',
]);

// create one tutor user
User::factory()->elevatedPrivileges(true, false)->create([
'firstname' => 'Tutor',
'lastname' => 'User',
'email' => '[email protected]',
]);

// create 800 student users
User::factory()->count(800)->create();
}
}
Loading

0 comments on commit e47b6a1

Please sign in to comment.