1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: claudio |
5
|
|
|
* Date: 12/12/15 |
6
|
|
|
* Time: 15.41 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace plunner\Console\Commands\Optimise; |
10
|
|
|
|
11
|
|
|
use Illuminate\Console\Scheduling\Schedule; |
12
|
|
|
use plunner\company; |
13
|
|
|
use plunner\Events\Optimise\ErrorEvent; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Optimise |
17
|
|
|
* @author Claudio Cardinale <[email protected]> |
18
|
|
|
* @copyright 2015 Claudio Cardinale |
19
|
|
|
* @version 1.0.0 |
20
|
|
|
* @package plunner\Console\Commands\Optimise |
21
|
|
|
*/ |
22
|
|
|
class Optimise |
23
|
|
|
{ |
24
|
|
|
//TODo max timeslots can be an environment var |
25
|
|
|
const TIME_SLOT_DURATION = 900; //seconds -> 15 minutes |
26
|
|
|
const DEFAULT_MAX_TIME_SLOTS = 20; //max duration of a meeting in term of timeslots //20 |
27
|
|
|
const DEFAULT_TIME_SLOTS = 672; //total amount of timeslots that must be optimised -> one week 4*24*7 = 672 |
28
|
|
|
|
29
|
|
|
private $max_time_slots = self::DEFAULT_MAX_TIME_SLOTS; |
30
|
|
|
private $time_slots = self::DEFAULT_TIME_SLOTS; |
31
|
|
|
|
32
|
|
|
//TODO timezone |
33
|
|
|
/** |
34
|
|
|
* @var \DateTime |
35
|
|
|
*/ |
36
|
|
|
private $startTime; |
37
|
|
|
/** |
38
|
|
|
* @var \DateTime |
39
|
|
|
*/ |
40
|
|
|
private $endTime; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Company |
44
|
|
|
*/ |
45
|
|
|
private $company; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Schedule laravel schedule object needed to perform command in background |
49
|
|
|
*/ |
50
|
|
|
private $schedule; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var \Illuminate\Contracts\Foundation\Application; |
54
|
|
|
*/ |
55
|
|
|
private $laravel; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Solver |
59
|
|
|
*/ |
60
|
|
|
private $solver = null; |
61
|
|
|
|
62
|
|
|
//TODO clone |
63
|
|
|
//TODO to_string |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Optimise constructor. |
67
|
|
|
* @param company $company |
68
|
|
|
* @param Schedule $schedule |
69
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $laravel |
70
|
|
|
*/ |
71
|
2 |
|
public function __construct(company $company, Schedule $schedule, \Illuminate\Contracts\Foundation\Application $laravel) |
72
|
|
|
{ |
73
|
2 |
|
$this->company = $company; |
|
|
|
|
74
|
2 |
|
$this->schedule = $schedule; |
75
|
2 |
|
$this->laravel = $laravel; |
76
|
|
|
|
77
|
2 |
|
$this->setStartTime((new \DateTime())->modify('next monday')); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \DateTime $startTime |
83
|
|
|
*/ |
84
|
2 |
|
public function setStartTime(\DateTime $startTime) |
85
|
|
|
{ |
86
|
2 |
|
$this->startTime = clone $startTime; |
87
|
2 |
|
$this->endTime = clone $this->startTime; |
88
|
2 |
|
$this->endTime->add(new \DateInterval('PT'.(($this->max_time_slots+$this->time_slots)*self::TIME_SLOT_DURATION).'S')); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function getMaxTimeSlots() |
95
|
|
|
{ |
96
|
|
|
return $this->max_time_slots; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $max_time_slots |
101
|
|
|
*/ |
102
|
2 |
|
public function setMaxTimeSlots($max_time_slots) |
103
|
|
|
{ |
104
|
2 |
|
$this->max_time_slots = $max_time_slots; |
105
|
2 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
2 |
|
public function getTimeSlots() |
111
|
|
|
{ |
112
|
2 |
|
return $this->time_slots; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $time_slots |
117
|
|
|
*/ |
118
|
2 |
|
public function setTimeSlots($time_slots) |
119
|
|
|
{ |
120
|
2 |
|
$this->time_slots = $time_slots; |
121
|
2 |
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Company |
126
|
|
|
*/ |
127
|
|
|
public function getCompany() |
128
|
|
|
{ |
129
|
|
|
return $this->company; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param Company $company |
134
|
|
|
*/ |
135
|
|
|
public function setCompany($company) |
136
|
|
|
{ |
137
|
|
|
$this->company = $company; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return Solver |
142
|
|
|
*/ |
143
|
2 |
|
public function getSolver() |
144
|
|
|
{ |
145
|
2 |
|
return $this->solver; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Optimise |
151
|
|
|
*/ |
152
|
2 |
|
public function optimise() |
153
|
|
|
{ |
154
|
2 |
|
try { |
155
|
2 |
|
$solver = new Solver($this->schedule, $this->laravel); |
|
|
|
|
156
|
2 |
|
$solver = $this->setData($solver); |
157
|
2 |
|
$solver = $solver->solve(); |
158
|
2 |
|
$this->solver = $solver; |
159
|
2 |
|
}catch(\Exception $e) |
160
|
|
|
{ |
161
|
|
|
\Event::fire(new ErrorEvent($this->company, $e->getMessage())); |
162
|
|
|
throw new OptimiseException('Optimising error', 0, $e); |
163
|
|
|
//TODO catch specif exception |
164
|
|
|
} |
165
|
2 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return Optimise |
170
|
|
|
*/ |
171
|
2 |
|
public function save() |
172
|
|
|
{ |
173
|
2 |
|
if(!($this->solver instanceof Solver)) { |
174
|
|
|
\Event::fire(new ErrorEvent($this->company, 'solver is not an instace of Solver')); |
175
|
|
|
throw new OptimiseException('solver is not an instance of Solver'); |
176
|
|
|
return; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
//TODO check results before save them |
179
|
|
|
|
180
|
|
|
try { |
181
|
2 |
|
$this->saveMeetings($this->solver); |
182
|
2 |
|
$this->saveEmployeesMeetings($this->solver); |
183
|
2 |
|
}catch(\Exception $e) |
184
|
|
|
{ |
185
|
|
|
\Event::fire(new ErrorEvent($this->company, $e->getMessage())); |
186
|
|
|
throw new OptimiseException('Optimising error', 0, $e); |
187
|
|
|
//TODO catch specif exception |
188
|
|
|
} |
189
|
2 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
//TODO fix php doc with exceptions |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param Solver $solver |
196
|
|
|
*/ |
197
|
2 |
|
private function saveMeetings(Solver $solver) |
198
|
|
|
{ |
199
|
2 |
|
$meetings = $solver->getYResults(); |
200
|
2 |
|
foreach($meetings as $id=>$meeting){ |
201
|
2 |
|
$meetingO = \plunner\Meeting::findOrFail($id);//TODO catch error |
202
|
2 |
|
$meetingO->start_time = $this->toDateTime(array_search('1', $meeting)); |
203
|
2 |
|
$meetingO->save(); |
204
|
2 |
|
} |
205
|
2 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param Solver $solver |
209
|
|
|
*/ |
210
|
2 |
|
private function saveEmployeesMeetings(Solver $solver) |
211
|
|
|
{ |
212
|
2 |
|
$employeesMeetings = $solver->getXResults(); |
213
|
2 |
|
foreach($employeesMeetings as $eId =>$employeeMeetings) |
214
|
|
|
{ |
215
|
2 |
|
$employee = \plunner\Employee::findOrFail($eId); |
216
|
2 |
|
$employeeMeetings = collect($employeeMeetings); |
217
|
|
|
$employeeMeetings = $employeeMeetings->filter(function ($item) { |
218
|
2 |
|
return $item == 1; |
219
|
2 |
|
}); |
220
|
2 |
|
$employee->meetings()->attach($employeeMeetings->keys()->toArray()); |
221
|
2 |
|
} |
222
|
2 |
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param Solver $solver |
227
|
|
|
* @return Solver |
228
|
|
|
* @throws OptimiseException |
229
|
|
|
*/ |
230
|
2 |
|
private function setData(Solver $solver) |
231
|
|
|
{ |
232
|
2 |
|
$solver = $this->setTimeSlotsSolver($solver); |
233
|
2 |
|
$solver = $this->setUsers($solver); |
234
|
2 |
|
$solver = $this->setAllMeetingsInfo($solver); |
235
|
2 |
|
$solver = $this->setUserAvailability($solver); |
236
|
2 |
|
$solver = $this->setUsersMeetings($solver); |
237
|
2 |
|
return $solver; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param Solver $solver |
242
|
|
|
* @return Solver |
243
|
|
|
* @throws OptimiseException |
244
|
|
|
*/ |
245
|
2 |
|
private function setTimeSlotsSolver(Solver $solver) |
246
|
|
|
{ |
247
|
2 |
|
return $solver->setTimeSlots($this->time_slots)->setMaxTimeSlots($this->max_time_slots); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param Solver $solver |
252
|
|
|
* @return Solver |
253
|
|
|
*/ |
254
|
2 |
|
private function setUsers(Solver $solver) |
255
|
|
|
{ |
256
|
|
|
//since we consider busy timeslots, we need to get all users |
257
|
2 |
|
$users = $this->company->employees->pluck('id')->toArray(); |
258
|
2 |
|
return $solver->setUsers($users); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param Solver $solver |
263
|
|
|
* @return Solver |
264
|
|
|
*/ |
265
|
2 |
|
private function setAllMeetingsInfo(Solver $solver) |
266
|
|
|
{ |
267
|
|
|
/** |
268
|
|
|
* @var $meetings \Illuminate\Support\Collection |
269
|
|
|
*/ |
270
|
2 |
|
$meetings = collect($this->company->getMeetingsTimeSlots($this->startTime, $this->endTime)); |
271
|
|
|
$timeslots = $meetings->groupBy('id')->map(function($item) { //convert timeslots |
272
|
2 |
|
return $this->timeSlotsConverter($item); |
273
|
2 |
|
}); |
274
|
2 |
|
return $solver->setMeetings($timeslots->keys()->toArray()) |
275
|
2 |
|
->setMeetingsDuration($meetings->pluck('duration','id')->toArray()) |
276
|
2 |
|
->setMeetingsAvailability(self::getAvailabilityArray($timeslots, $this->time_slots)); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param Solver $solver |
281
|
|
|
* @return Solver |
282
|
|
|
* @throws OptimiseException |
283
|
|
|
*/ |
284
|
2 |
|
private function setUserAvailability(Solver $solver) |
285
|
|
|
{ |
286
|
|
|
/** |
287
|
|
|
* @var $users \Illuminate\Support\Collection |
288
|
|
|
*/ |
289
|
2 |
|
$users = collect($this->company->getEmployeesTimeSlots($this->startTime, $this->endTime)); |
290
|
|
|
$timeslots = $users->groupBy('id')->map(function($item) { //convert timeslots |
291
|
2 |
|
return $this->timeSlotsConverter($item); |
292
|
2 |
|
}); |
293
|
2 |
|
return $solver->setUsersAvailability(self::getAvailabilityArray($timeslots, $this->time_slots, false)); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @param Solver $solver |
298
|
|
|
* @return Solver |
299
|
|
|
* @throws OptimiseException |
300
|
|
|
*/ |
301
|
2 |
|
private function setUsersMeetings(Solver $solver) |
302
|
|
|
{ |
303
|
2 |
|
$users = $solver->getUsers(); |
304
|
2 |
|
$meetings = $solver->getMeetings(); |
305
|
|
|
/** |
306
|
|
|
* @var $usersMeetings \Illuminate\Support\Collection |
307
|
|
|
*/ |
308
|
2 |
|
$usersMeetings = collect($this->company->getUsersMeetings($users, $meetings))->groupBy('employee_id'); |
309
|
|
|
|
310
|
2 |
|
return $solver->setUsersMeetings(self::getUsersMeetingsArray($users, $meetings, $usersMeetings)); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param array $users |
315
|
|
|
* @param array $meetings |
316
|
|
|
* @param \Illuminate\Support\Collection $usersMeetings |
317
|
|
|
* @return array |
318
|
|
|
*/ |
319
|
2 |
|
static private function getUsersMeetingsArray($users, $meetings, \Illuminate\Support\Collection $usersMeetings) |
320
|
|
|
{ |
321
|
2 |
|
$ret = []; |
322
|
2 |
|
foreach($users as $user) |
323
|
|
|
{ |
324
|
2 |
|
$usersMeetingsTmp = $usersMeetings->get($user); |
325
|
2 |
|
foreach($meetings as $meeting){ |
326
|
2 |
|
if($usersMeetingsTmp->contains('meeting_id', $meeting)){ |
327
|
2 |
|
$ret[$user][$meeting] = 1; |
328
|
2 |
|
}else{ |
329
|
2 |
|
$ret[$user][$meeting] = 0; |
330
|
|
|
} |
331
|
2 |
|
} |
332
|
2 |
|
} |
333
|
|
|
|
334
|
2 |
|
return $ret; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
private function timeSlotsConverter($item) |
338
|
|
|
{ |
339
|
2 |
|
return $item->each(function($item2){ |
340
|
2 |
|
$item2->time_start = $this->toTimeSlot($item2->time_start); |
341
|
2 |
|
$item2->time_end = $this->toTimeSlot($item2->time_end); |
342
|
2 |
|
return $item2; |
343
|
|
|
//TODO try catch |
344
|
2 |
|
}); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @param \Illuminate\Support\Collection $timeSlots |
349
|
|
|
* @param bool|true $free if true the array is filled with 1 for timeslots values else with 0 for timeslots values |
350
|
|
|
* @return array |
351
|
|
|
*/ |
352
|
2 |
|
static private function getAvailabilityArray(\Illuminate\Support\Collection $timeSlots, $timeslotsN, $free=true) |
353
|
|
|
{ |
354
|
2 |
|
$ret = []; |
355
|
2 |
|
foreach($timeSlots as $id=>$timeSlots2) |
356
|
|
|
{ |
357
|
2 |
|
$ret = self::fillTimeSlots($ret, $id, $timeSlots2, $free?'1':'0'); |
358
|
2 |
|
$ret = self::fillRow($ret, $id, $timeslotsN, $free?'0':'1'); |
359
|
2 |
|
} |
360
|
|
|
|
361
|
2 |
|
return $ret; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param array $array |
366
|
|
|
* @param int $id |
367
|
|
|
* @param \Illuminate\Support\Collection $timeSlots |
368
|
|
|
* @param string $fill |
369
|
|
|
* @return array |
370
|
|
|
*/ |
371
|
2 |
|
static private function fillTimeSlots(array $array, $id, \Illuminate\Support\Collection $timeSlots, $fill = '0') |
372
|
|
|
{ |
373
|
2 |
|
foreach($timeSlots as $timeSlot) { |
374
|
2 |
|
if(!isset($array[$id])) |
375
|
2 |
|
$array[$id] = []; |
376
|
2 |
|
$array[$id] = self::arrayPadInterval($array[$id], $timeSlot->time_start, $timeSlot->time_end, $fill); |
377
|
2 |
|
} |
378
|
2 |
|
return $array; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param array $array |
383
|
|
|
* @param int $id |
384
|
|
|
* @param string $fill |
385
|
|
|
* @return array |
386
|
|
|
*/ |
387
|
2 |
|
static private function fillRow(array $array, $id, $until, $fill = '0') |
388
|
|
|
{ |
389
|
2 |
|
for($i = 1; $i <= $until; $i++){ |
390
|
2 |
|
if(!isset($array[$id][$i])) |
391
|
2 |
|
$array[$id][$i] = $fill; |
392
|
2 |
|
} |
393
|
|
|
|
394
|
2 |
|
return $array; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* @param array $array |
399
|
|
|
* @param int $from |
400
|
|
|
* @param int $to |
401
|
|
|
* @param string $pad |
402
|
|
|
* @return array |
403
|
|
|
*/ |
404
|
2 |
|
static private function arrayPadInterval(array $array, $from, $to, $pad = '0') |
405
|
|
|
{ |
406
|
2 |
|
for($i = $from; $i<$to; $i++) |
407
|
2 |
|
$array[$i] = $pad; |
408
|
2 |
|
return $array; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @param mixed $time |
414
|
|
|
* @return int |
415
|
|
|
* @throws OptimiseException |
416
|
|
|
*/ |
417
|
2 |
|
private function toTimeSlot($time) |
418
|
|
|
{ |
419
|
2 |
|
$dateTime = new \DateTime($time); |
420
|
2 |
|
$diff = $dateTime->diff($this->startTime); |
421
|
2 |
|
$diff = explode(':',$diff->format('%R:%d:%h:%i:%s')); |
422
|
2 |
|
$diff = $diff[1]*86400 + $diff[2]*3600 + $diff[3]*60 + $diff[4]; |
423
|
|
|
//if($diff[0] != '-' && $diff != 0) |
424
|
|
|
// throw new OptimiseException('timeslot time <= startTime'); |
425
|
|
|
//TODO fix check |
426
|
|
|
//TODO check if diff makes sense |
427
|
|
|
//TODO check upper limit |
428
|
2 |
|
return (int)(round($diff/self::TIME_SLOT_DURATION)+1); //TODO can round cause overlaps? |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param int $timeslot |
433
|
|
|
* @return \DateTime |
434
|
|
|
*/ |
435
|
2 |
|
private function toDateTime($timeslot) |
436
|
|
|
{ |
437
|
2 |
|
$ret = clone $this->startTime; |
438
|
2 |
|
return $ret->add(new \DateInterval('PT'.(($timeslot-1)*self::TIME_SLOT_DURATION).'S')); |
439
|
|
|
} |
440
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..