Add time restriction in analytics #409

This commit is contained in:
Kendrick Chan 2020-06-27 07:42:04 +08:00
parent 2803c264ac
commit 94116a2958
2 changed files with 42 additions and 7 deletions

View file

@ -107,6 +107,9 @@ class AnalyticsController extends Controller
$hub_list = $req->request->get('hub_ids', []);
$distances = $req->request->get('distances', []);
$time_start = $req->request->get('time_from');
$time_end = $req->request->get('time_to');
$date_from = DateTime::createFromFormat('d M Y', $req->request->get('date_from'));
$date_to = DateTime::createFromFormat('d M Y', $req->request->get('date_to'));
@ -123,7 +126,7 @@ class AnalyticsController extends Controller
$hub = $em->getRepository(Hub::class)->find($hub_id);
$coords = $hub->getCoordinates();
$hub_data[$hub_id] = $this->generateHubData($em, $hub, $dist, $date_from, $date_to, $overlaps);
$hub_data[$hub_id] = $this->generateHubData($em, $hub, $dist, $date_from, $date_to, $time_start, $time_end, $overlaps);
$hub_coverage[] = [
'longitude' => $coords->getLongitude(),
@ -328,14 +331,14 @@ class AnalyticsController extends Controller
return $data;
}
protected function generateHubData($em, $hub, $distance_limit, DateTime $date_start, DateTime $date_end, &$overlaps)
protected function generateHubData($em, $hub, $distance_limit, DateTime $date_start, DateTime $date_end, $time_start, $time_end, &$overlaps)
{
// get hub to analyze
// $hub = $em->getRepository(Hub::class)->find($hub_id);
$conn = $em->getConnection();
// get job order data (job orders within coverage area)
$jos = $this->generateJobOrderData($conn, $hub, $distance_limit, $date_start, $date_end);
$jos = $this->generateJobOrderData($conn, $hub, $distance_limit, $date_start, $date_end, $time_start, $time_end);
// initialize counters
$c_weekday = [];
@ -409,19 +412,35 @@ class AnalyticsController extends Controller
return $params;
}
protected function generateJobOrderData($conn, $hub, $distance_limit, DateTime $date_start, DateTime $date_end)
protected function generateJobOrderData($conn, $hub, $distance_limit, DateTime $date_start, DateTime $date_end, $time_start, $time_end)
{
$hub_coord = $hub->getCoordinates();
// create query
// formula to convert to km is 111195 * st_distance
$sql = "select id, round(st_distance(coordinates, Point(:lng, :lat)) * 111195) as dist, date_schedule from job_order where st_distance(coordinates, Point(:lng, :lat)) * 111195 <= :distance_limit and status <> 'cancelled' and date_schedule >= :date_start and date_schedule <= :date_end order by date_schedule asc";
$sql = "select id, round(st_distance(coordinates, Point(:lng, :lat)) * 111195) as dist, date_schedule from job_order where st_distance(coordinates, Point(:lng, :lat)) * 111195 <= :distance_limit and status <> 'cancelled' and date_schedule >= :date_start and date_schedule <= :date_end";
// check if time is specified
if (!empty($time_start))
$sql .= ' and time(date_schedule) >= :time_start';
if (!empty($time_end))
$sql .= ' and time(date_schedule) <= :time_end';
$sql .= " order by date_schedule asc";
$stmt = $conn->prepare($sql);
$stmt->bindValue('lng', $hub_coord->getLongitude());
$stmt->bindValue('lat', $hub_coord->getLatitude());
$stmt->bindValue('distance_limit', $distance_limit);
$stmt->bindValue('date_start', $date_start->format('Y-m-d H:i:s'));
$stmt->bindValue('date_end', $date_end->format('Y-m-d H:i:s'));
if (!empty($time_start))
$stmt->bindValue('time_start', $time_start);
if (!empty($time_end))
$stmt->bindValue('time_end', $time_end);
$stmt->execute();
$jos = $stmt->fetchAll();

View file

@ -30,11 +30,27 @@
<form id="row-form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ url('analytics_forecast_submit') }}">
<div class="m-portlet__body">
<div class="form-group m-form__group row no-border">
<div class="col-lg-6">
<label data-field="time_from">Restrict Time From</label>
<div class="input-group">
<input type="time" name="time_from" id="time_from" class="form-control m-input" value="">
</div>
<div class="form-control-feedback hide" data-field="date_from"></div>
</div>
<div class="col-lg-6">
<label data-field="time_to">Restrict Time To</label>
<div class="input-group">
<input type="time" name="time_to" id="time_to" class="form-control m-input" value="">
</div>
<div class="form-control-feedback hide" data-field="date_from"></div>
</div>
</div>
<div class="form-group m-form__group row no-border">
<div class="col-lg-6">
<label data-field="date_from">Date From</label>
<div class="input-group date dp">
<input type="text" name="date_from" id="date" class="form-control m-input" value="{{ "now"|date('d M Y') }}" readonly placeholder="Select a date" >
<input type="text" name="date_from" id="date_from" class="form-control m-input" value="{{ "now"|date('d M Y') }}" readonly placeholder="Select a date" >
<span class="input-group-addon">
<i class="la la-calendar glyphicon-th"></i>
</span>
@ -44,7 +60,7 @@
<div class="col-lg-6">
<label data-field="date_to">Date To</label>
<div class="input-group date dp">
<input type="text" name="date_to" id="date" class="form-control m-input" value="{{ "now"|date('d M Y') }}" readonly placeholder="Select a date" >
<input type="text" name="date_to" id="date_to" class="form-control m-input" value="{{ "now"|date('d M Y') }}" readonly placeholder="Select a date" >
<span class="input-group-addon">
<i class="la la-calendar glyphicon-th"></i>
</span>