Add AggregatedRiderRating entity. Add command to process all rider ratings into AggregatedRiderRating. #764

This commit is contained in:
Korina Cordero 2023-10-05 18:05:15 +08:00
parent 78956de357
commit 53a45ac4f4
2 changed files with 198 additions and 0 deletions

View file

@ -0,0 +1,112 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\RiderRating;
use App\Entity\AggregatedRiderRating;
use PDO;
class LoadAggregateRiderRatingsComand extends Command
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('aggregated_rider_rating:load')
->setDescription('Add rider ratings to aggregated rider rating.')
->setHelp('Add rider ratings to aggregated rider rating.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// get the ids of all riders
$rider_id_list = $this->getRiderIds();
$this->processAggregateRiderRating($rider_id_list);
return 0;
}
protected function processAggregateRiderRating($rider_id_list)
{
$db = $this->em->getConnection();
$agg_rider_ratings = [];
// get all rider ratings per rider
$rr_query_sql = 'SELECT rating FROM rider_rating WHERE rider_id = :id';
$rr_query_stmt = $db->prepare($rr_query_sql);
foreach ($rider_id_list as $key => $rider_id)
{
$rr_query_stmt->bindValue('id', $rider_id);
$results = $rr_query_stmt->executeQuery();
$total_jos = 0;
$total_rating = 0;
while ($row = $results->fetchAssociative())
{
$rating = $row['rating'];
$total_rating = bcadd($total_rating, $rating, 2);
// increment number of JOs per rider
$total_jos++;
}
// compute average
$agg_rating = 0;
if ($total_jos > 0)
$agg_rating = bcdiv($total_rating, $total_jos, 2);
$agg_rider_ratings[$rider_id] = [
'agg_rating' => $agg_rating,
'agg_count' => $total_jos,
];
}
// create aggregated rider rating
$this->createAggregatedRiderRating($agg_rider_ratings);
}
protected function createAggregatedRiderRating($agg_rider_ratings)
{
error_log(print_r($agg_rider_ratings, true));
// TODO: create new AggregatedRiderRating object
// set fields
// save to database
}
protected function getRiderIds()
{
$rider_ids = [];
$db = $this->em->getConnection();
$query_sql = 'SELECT id FROM rider';
$query_stmt = $db->prepare($query_sql);
$results = $query_stmt->executeQuery();
while ($row = $results->fetchAssociative())
{
$rider_ids[] = $row['id'];
}
return $rider_ids;
}
}

View file

@ -0,0 +1,86 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="aggregated_rider_rating", indexes={
* @ORM\Index(name="rider_id_idx", columns={"rider_id"}),
* })
*/
class AggregatedRiderRating
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// rider id, loose association
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $rider_id;
// average rating of rider
/**
* @ORM\Column(type="integer")
*/
protected $aggregate_rating;
// number of job orders
/**
* @ORM\Column(type="integer")
*/
protected $aggregate_count;
public function __construct()
{
$this->aggregate_rating = 0;
$this->aggregate_count = 0;
}
public function getID()
{
return $this->id;
}
public function setRiderId($rider_id)
{
$this->rider_id = $rider_id;
return $this;
}
public function getRiderId()
{
return $this->rider_id;
}
public function setAggregateRating($aggregate_rating)
{
$this->aggregate_rating = $aggregate_rating;
return $this;
}
public function getAggregateRating()
{
return $this->aggregate_rating;
}
public function setAggregateCount($aggregate_count)
{
$this->aggregate_count = $aggregate_count;
return $this;
}
public function getAggregateCount()
{
return $this->aggregate_count;
}
}