Add saving to aggregated rider rating when adding a rider rating from app. #764

This commit is contained in:
Korina Cordero 2023-10-06 15:25:09 +08:00 committed by Korina Cordero
parent 25d7c30f73
commit a248731851
2 changed files with 114 additions and 1 deletions

View file

@ -71,6 +71,7 @@ use App\Entity\Hub;
use App\Entity\SAPBattery;
use App\Entity\WarrantySerial;
use App\Entity\CustomerMetadata;
use App\Entity\AggregatedRiderRating;
use DateTime;
use DateInterval;
@ -1715,7 +1716,13 @@ class APIController extends Controller implements LoggedController
$em->persist($rating);
$em->flush();
// TODO: set average rating in rider entity
// need to update or add aggregated rider rating
$this->updateAggregatedRiderRating($em, $rider, $rating_num);
// TODO: preliminary rating computation on the entity for now
$rider->updateRatingAverage();
$em->persist($rider);
$em->flush();
$res->setData([]);
@ -4849,6 +4856,57 @@ class APIController extends Controller implements LoggedController
return $jo_data;
}
protected function updateAggregatedRiderRating($em, $rider, $rating_num)
{
$rider_id = $rider->getID();
// check if rider is in the the aggregated rider rating table
$agg_rider_rating = $em->getRepository(AggregatedRiderRating::class)->findOneBy(['rider_id' => $rider_id]);
if ($agg_rider_rating == null)
{
// new rider, new entry
$old_rating = 0;
$old_count = 0;
$new_count = 1;
$agg_rating = $this->computeAggregatedRiderRating($old_rating, $rating_num, $old_count, $new_count);
$obj = new AggregatedRiderRating();
$obj->setRiderId($rider_id)
->setAggregateRating($agg_rating)
->setAggregateCount($new_count);
$em->persist($obj);
}
else
{
// existing rider, update entry
$r_rating = $agg_rider_rating->getAggregateRating();
$r_count = $agg_rider_rating->getAggregateCount();
$new_count = ++$r_count;
$agg_rating = $this->computeAggregatedRiderRating($r_rating, $rating_num, $r_count, $new_count);
// set updated values for entry
$agg_rider_rating->setAggregateRating($agg_rating)
->setAggregateCount($new_count);
}
$em->flush();
}
protected function computeAggregatedRiderRating($old_rating, $new_rating, $r_count, $new_count)
{
// ((existing aggregate rating * existing aggregate count) + new rating) / new count
$agg_comp = bcmul($old_rating, $r_count, 2);
$rating = bcadd($agg_comp, $new_rating, 2);
$agg_rating = bcdiv($rating, $new_count, 2);
return $agg_rating;
}
protected function normalizeString($string)
{
return trim(strtolower($string));

View file

@ -9,6 +9,7 @@ use App\Ramcar\JOStatus;
use App\Ramcar\APIRiderStatus;
use App\Entity\RiderRating;
use App\Entity\JobOrder;
use App\Entity\AggregatedRiderRating;
use App\Service\RiderTracker;
use Exception;
@ -230,6 +231,9 @@ class RiderController extends ApiController
$this->em->persist($rating);
$this->em->flush();
// need to update or add aggregated rider rating
$this->updateAggregatedRiderRating($rider, $rating_num);
// TODO: preliminary rating computation on the entity for now
$rider->updateRatingAverage();
$this->em->persist($rider);
@ -238,4 +242,55 @@ class RiderController extends ApiController
// response
return new ApiResponse();
}
protected function updateAggregatedRiderRating($rider, $rating_num)
{
$rider_id = $rider->getID();
// check if rider is in the the aggregated rider rating table
$agg_rider_rating = $this->em->getRepository(AggregatedRiderRating::class)->findOneBy(['rider_id' => $rider_id]);
if ($agg_rider_rating == null)
{
// new rider, new entry
$old_rating = 0;
$old_count = 0;
$new_count = 1;
$agg_rating = $this->computeAggregatedRiderRating($old_rating, $rating_num, $old_count, $new_count);
$obj = new AggregatedRiderRating();
$obj->setRiderId($rider_id)
->setAggregateRating($agg_rating)
->setAggregateCount($new_count);
$this->em->persist($obj);
}
else
{
// existing rider, update entry
$r_rating = $agg_rider_rating->getAggregateRating();
$r_count = $agg_rider_rating->getAggregateCount();
$new_count = ++$r_count;
$agg_rating = $this->computeAggregatedRiderRating($r_rating, $rating_num, $r_count, $new_count);
// set updated values for entry
$agg_rider_rating->setAggregateRating($agg_rating)
->setAggregateCount($new_count);
}
$this->em->flush();
}
protected function computeAggregatedRiderRating($old_rating, $new_rating, $r_count, $new_count)
{
// ((existing aggregate rating * existing aggregate count) + new rating) / new count
$agg_comp = bcmul($old_rating, $r_count, 2);
$rating = bcadd($agg_comp, $new_rating, 2);
$agg_rating = bcdiv($rating, $new_count, 2);
return $agg_rating;
}
}