-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from Zhyltix/feature/ID-204-post-trip-process
Feature/id 204 post trip process
- Loading branch information
Showing
42 changed files
with
1,183 additions
and
653 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ip/Helper/AbstractTripStatisticParser.php → ...r/Parsers/AbstractTripStatisticParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
app/Domain/Trip/Helper/Parsers/Profile/AbstractProfileParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace App\Domain\Trip\Helper\Parsers\Profile; | ||
|
||
use App\Domain\Trip\Enum\TripEventEnum; | ||
use App\Domain\Trip\Enum\TripStatisticParserEnum; | ||
use App\Domain\Trip\Model\Trip; | ||
use App\Domain\Trip\Model\TripEvent; | ||
use App\Domain\Trip\Model\TripStatistic; | ||
use App\Domain\Trip\ValueObject\Statistic\Profile\TimelineValueObject; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileStatisticValueObject; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileValueObject; | ||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
abstract class AbstractProfileParser | ||
{ | ||
protected TripStatistic $profile; | ||
|
||
public function __construct( | ||
protected Trip $trip, | ||
) { | ||
$this->profile = $this->getTripStatistic(); | ||
|
||
$data = $this->profile->data ?? []; | ||
foreach ($data as $key => $value) { | ||
$this->{$key} = $value; | ||
} | ||
} | ||
|
||
abstract public function parse(Trip $trip): void; | ||
|
||
abstract protected function getParserEnum(): TripStatisticParserEnum; | ||
|
||
/** | ||
* @return ProfileStatisticValueObject[] | ||
*/ | ||
abstract protected function getProfileTitle(): string; | ||
|
||
abstract protected function getProfileStatistics(): array; | ||
|
||
public function getProfile(): ProfileValueObject | ||
{ | ||
return new ProfileValueObject( | ||
title: $this->getProfileTitle(), | ||
statistics: $this->getProfileStatistics(), | ||
events: $this->getTimeLineEvents()->getProfileEvents() | ||
); | ||
} | ||
|
||
protected function getTripStatistic(bool $override = false): TripStatistic | ||
{ | ||
$profile = $this->trip->statistics()->where('parser', $this->getParserEnum())->first(); | ||
if ($profile instanceof TripStatistic) { | ||
if ($override) { | ||
$profile->delete(); | ||
} else { | ||
return $profile; | ||
} | ||
} | ||
|
||
return new TripStatistic([ | ||
'parser' => $this->getParserEnum(), | ||
'trip_id' => $this->trip->id, | ||
]); | ||
} | ||
|
||
protected function getEvents(): Collection | ||
{ | ||
return $this->trip | ||
->events() | ||
->where('is_processed', true) | ||
->whereIn('type', $this->getParserEnum()->getEventEnums()) | ||
->get(); | ||
} | ||
|
||
private function fakeData(Collection &$events): void | ||
{ | ||
foreach (TripEventEnum::values() as $event) { | ||
$event = TripEventEnum::from($event); | ||
if (!in_array($event, $this->getParserEnum()->getEventEnums())) { | ||
continue; | ||
} | ||
|
||
for($i = 0; $i < rand(2, 5); $i++) { | ||
$data = []; | ||
if ($event === TripEventEnum::SPEEDING) { | ||
$data = [ | ||
'speed' => rand(0, 100), | ||
'speed_limit' => rand(0, 100), | ||
]; | ||
} | ||
|
||
$events->push(new TripEvent([ | ||
'type' => $event, | ||
'trip_id' => $this->trip->id, | ||
'distance' => rand(0, 100), | ||
'data' => $data, | ||
])); | ||
} | ||
} | ||
} | ||
|
||
protected function getTimeLineEvents(): TimelineValueObject | ||
{ | ||
$events = $this->getEvents(); | ||
|
||
// $this->fakeData($events); | ||
|
||
return new TimelineValueObject($events); | ||
} | ||
|
||
protected function countEvents(TripEventEnum $event): int | ||
{ | ||
return $this->getEvents()->where('type', $event)->count(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/Domain/Trip/Helper/Parsers/Profile/BreakingProfileParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Domain\Trip\Helper\Parsers\Profile; | ||
|
||
use App\Domain\Trip\Enum\TripEventEnum; | ||
use App\Domain\Trip\Enum\TripStatisticParserEnum; | ||
use App\Domain\Trip\Model\Trip; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileEventValueObject; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileStatisticValueObject; | ||
|
||
class BreakingProfileParser extends AbstractProfileParser | ||
{ | ||
protected function getParserEnum(): TripStatisticParserEnum | ||
{ | ||
return TripStatisticParserEnum::BREAK_PROFILE; | ||
} | ||
|
||
protected function getProfileTitle(): string | ||
{ | ||
return 'Breaking'; | ||
} | ||
|
||
protected function getProfileStatistics(): array | ||
{ | ||
return [ | ||
new ProfileStatisticValueObject( | ||
title: 'Abrupt Braking', | ||
value: $this->countEvents(TripEventEnum::HARSH_BRAKING) | ||
), | ||
new ProfileStatisticValueObject( | ||
title: 'Incorrect paddle usage', | ||
value: $this->countEvents(TripEventEnum::BREAKING_AND_GAS) | ||
), | ||
]; | ||
} | ||
|
||
public function parse(Trip $trip): void | ||
{ | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/Domain/Trip/Helper/Parsers/Profile/GearProfileParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Domain\Trip\Helper\Parsers\Profile; | ||
|
||
use App\Domain\Trip\Enum\TripEventEnum; | ||
use App\Domain\Trip\Enum\TripStatisticParserEnum; | ||
use App\Domain\Trip\Model\Trip; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileEventValueObject; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileStatisticValueObject; | ||
|
||
class GearProfileParser extends AbstractProfileParser | ||
{ | ||
protected function getParserEnum(): TripStatisticParserEnum | ||
{ | ||
return TripStatisticParserEnum::GEAR_PROFILE; | ||
} | ||
|
||
protected function getProfileTitle(): string | ||
{ | ||
return 'Gear'; | ||
} | ||
|
||
protected function getProfileStatistics(): array | ||
{ | ||
return [ | ||
new ProfileStatisticValueObject( | ||
title: 'Engagement Omitted', | ||
value: $this->countEvents(TripEventEnum::MISSED_GEAR) | ||
), | ||
new ProfileStatisticValueObject( | ||
title: 'Late Shift', | ||
value: $this->countEvents(TripEventEnum::LATE_SHIFT_GEAR) | ||
) | ||
]; | ||
} | ||
|
||
public function parse(Trip $trip): void | ||
{ | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/Domain/Trip/Helper/Parsers/Profile/SpeedProfileParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\Domain\Trip\Helper\Parsers\Profile; | ||
|
||
use App\Domain\Trip\Enum\TripStatisticParserEnum; | ||
use App\Domain\Trip\Model\Trip; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileEventValueObject; | ||
use App\Domain\Trip\ValueObject\Statistic\ProfileStatisticValueObject; | ||
|
||
class SpeedProfileParser extends AbstractProfileParser | ||
{ | ||
protected int $averageSpeed = 0; | ||
|
||
protected function getParserEnum(): TripStatisticParserEnum | ||
{ | ||
return TripStatisticParserEnum::SPEED_PROFILE; | ||
} | ||
|
||
protected function getProfileTitle(): string | ||
{ | ||
return 'Speed'; | ||
} | ||
|
||
protected function getProfileStatistics(): array | ||
{ | ||
return [ | ||
new ProfileStatisticValueObject( | ||
title: 'Average speed', | ||
value: $this->averageSpeed, | ||
unit: 'km/h' | ||
) | ||
]; | ||
} | ||
|
||
public function parse(Trip $trip): void | ||
{ | ||
$statistic = $this->getTripStatistic(true); | ||
|
||
$statistic->data = [ | ||
'averageSpeed' => $trip->data()->avg('speed'), | ||
]; | ||
|
||
$statistic->save(); | ||
} | ||
|
||
public function getAverageSpeed(): int | ||
{ | ||
return $this->averageSpeed; | ||
} | ||
|
||
} |
Oops, something went wrong.