Skip to content

Commit

Permalink
minor fixes for optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
carduz committed Jan 7, 2016
1 parent fe7ae62 commit ea593a6
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app/Console/Commands/Optimise/Optimise.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ public function optimise()
$solver = $solver->solve();
$this->solver = $solver;
}catch(OptimiseException $e) {
if(!$e->isEmpty())
\Event::fire(new ErrorEvent($this->company, $e->getMessage()));
throw $e;
}catch (\Exception $e) {
//TODO use the correct exceptions to avoid to share private data
\Event::fire(new ErrorEvent($this->company, $e->getMessage()));
throw new OptimiseException('Optimising error', 0, $e);
//TODO catch specif exception
Expand Down Expand Up @@ -425,6 +428,7 @@ public function save()
try {
$this->saveMeetings($this->solver);
$this->saveEmployeesMeetings($this->solver);
//TODO use the correct exceptions to avoid to share private data
} catch (\Exception $e) {
\Event::fire(new ErrorEvent($this->company, $e->getMessage()));
throw new OptimiseException('Optimising error', 0, $e);
Expand Down
18 changes: 11 additions & 7 deletions app/Console/Commands/Optimise/OptimiseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,17 @@ private function makeForeground(Company $company)
try {
(new Optimise($company, $this->schedule, $this->laravel))->optimise()->save();
$this->info('Optimisation ' . $company->id . ' completed');
}catch(OptimiseException $e){
if($e->isEmpty())
$this->warn('Company ' . $company->id . ' has no sufficient data');
else
$this->error('Error during optimisation of company ' . $company->id .': '. $e->getMessage());
}catch(\Exception $e) {
$this->error('Error during optimisation of company ' . $company->id .': '. $e->getMessage());
}catch(OptimiseException $e) {
if ($e->isEmpty()) {
$mex = 'Company ' . $company->id . ' doesn\'t have sufficient data';
$this->warn($mex);
\Log::info($mex);
} else {
$mex = 'Error during optimisation of company ' . $company->id . ': ' . $e->getMessage();
$this->error($mex);
//TODO log cause, but don't send it to the user
//\Log::error($mex); //already logged in listener
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/SyncCaldav/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function getEvents()
$calendars = $caldavClient->findCalendars();
if(!isset($calendars[$this->calendar->calendar_name]))
throw new CaldavException("calendar inserted doesn't exist");
$caldavClient->setCalendar($calendars[$this->calendar->calendar_name]);//TODO error if the calendar name is wrong
$caldavClient->setCalendar($calendars[$this->calendar->calendar_name]);
/**
* 26 hours before to avoid tiemezone problems and dst problems
* 30 days after
Expand Down
1 change: 1 addition & 0 deletions app/Console/Commands/SyncCaldav/SyncCaldav.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function makeForeground(Caldav $calendar)
{
$this->info('Sync calendar ' . $calendar->calendar_id . ' started');
(new Sync($calendar))->sync();
//TODO show errors as warning
$this->info('Sync calendar ' . $calendar->calendar_id . ' completed');
}

Expand Down
1 change: 1 addition & 0 deletions app/Listeners/Caldav/OkListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function handle(OkEvent $event)
{
//
$calendar = $event->getCalendar();
\Log::info('caldav (calendar id = ' . $calendar->calendar_id . ') correctly synchronized');
//$calendar = $event->getCalendar()->fresh();
$calendar->sync_errors = '';
$calendar->save();
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/Optimise/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
public function handle(ErrorEvent $event)
{
//
\Log::info('problems during optimise (company id = ' . $event->getCompany()->id . '): ' . $event->getError());
\Log::error('problems during optimise (company id = ' . $event->getCompany()->id . '): ' . $event->getError());
$company = $event->getCompany();
//$company = $event->getCompany()->fresh();
self::sendEmail($company->email, $event->getError());
Expand Down
4 changes: 3 additions & 1 deletion app/Listeners/Optimise/OkListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function handle(OkEvent $event)
//send email to company
self::sendCompanyEmail($company->email);
//send emails to employees
$employees = $company->employees()->with('meetings')->get();
$employees = $company->employees()->with(['meetings'=>function($query){
$query->where('start_time', '>=', new \DateTime());
}])->get();
foreach ($employees as $employee)
self::sendEmployeeEmail($employee->email, $employee->meetings);
}
Expand Down
3 changes: 2 additions & 1 deletion resources/views/emails/optimise/error.blade.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
The following error occurred during the meeting optimisation:<br>
{{$error}}
{{$error}}
if you need help don't hesitate to contact us
10 changes: 10 additions & 0 deletions tests/ConsoleTests/Optimise/OptimiseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ public function testSimpleModel()
$this->assertEquals(1, $meetingShort->employees->count());
$this->assertEquals(2, $meetingLong->employees->count());
}

public function testAll()
{
if (!$this->doConsole())
return;

//create data
$status = \Artisan::call('optimise:meetings');
$this->assertEquals(0, $status);
}
}
11 changes: 9 additions & 2 deletions tests/ConsoleTests/Optimise/OptimiseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ public function testNoData()
return;
$company = factory(\plunner\Company::class)->create();
$optimise = new Optimise($company, new Schedule(), \App::getInstance());
$this->setExpectedException('\plunner\Console\Commands\Optimise\OptimiseException');
$optimise->optimise();
$exception = null;
try {
$optimise->optimise();
}catch(\plunner\Console\Commands\Optimise\OptimiseException $e)
{
$exception = $e;
}
$this->assertNotNull($exception);
$this->assertTrue($exception->isEmpty());
}
}

0 comments on commit ea593a6

Please sign in to comment.