In an industrial plant, I would like to make statistics to determine how much machines are occupied for a given date range (daily
, monthly
, quarterly
, yearly
or else )
In my database I have a list of tasks with the following details:
{
"task": "task_1"
"machine_id": 1,
"start_date": "2018-01-29 12:30:00",
"end_date": "2018-01-31 08:56:00",
}
I've written code to determine which tasks where active for a given week and what where there occupation time :
$week_number = 5;
$year = 2018;
//Monday
$start_range = date("Y-m-d", strtotime($year . "W" . sprintf("%02d", $week_number)));
//Sunday at 23:59:00
$end_range = Carbon::parse($start_range)->addDays(7)->subSecond()->toDateTimeString();
$task_machine_occupation = GanttTask::where('task_type', 'task')
->select(DB::raw('machine_id, start_date, end_date,
CASE
WHEN start_date> "' . $start_range . '"
AND end_date> "' . $end_range . '" then TIMEDIFF("' . $end_range . '" , start_date)/60
WHEN start_date> "' . $start_range . '"
AND end_date< "' . $end_range . '" then TIMEDIFF(end_date , start_date)/60
WHEN start_date< "' . $start_range . '"
AND end_date> "' . $end_range . '" then TIMEDIFF("' . $start_range . '" , "' . $end_range . '")/60
WHEN start_date> "' . $start_range . '"
AND end_date> "' . $end_range . '" then TIMEDIFF(start_date , "' . $end_range . '")/60
END AS minutes_active
'))
//Overlapping conditions
->where('end_date', '>', $start_range)
->where('start_date', '<', $end_range)
->get()
;
return $task_machine_occupation;
It works, but I'm sure I'm doing it the wrong way:
- it's not very Eloquent
- it returns time occupation by task and not by machine
Could I set custom parametrized attribute in the GanttTask
model to get the occupation time by task and the sum it / group it by machine ?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire