| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dtc\QueueBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ODM\MongoDB\DocumentManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dtc\QueueBundle\Doctrine\BaseJobTimingManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Dtc\QueueBundle\Model\JobTiming; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Dtc\QueueBundle\ODM\JobManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Dtc\QueueBundle\ODM\JobTimingManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class TrendsController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use ControllerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Show a graph of job trends. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @Route("/trends", name="dtc_queue_trends") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @Template("@DtcQueue/Queue/trends.html.twig") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function trendsAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $recordTimings = $this->container->getParameter('dtc_queue.record_timings'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $params = ['record_timings' => $recordTimings, 'states' => JobTiming::getStates()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->addCssJs($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         return $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @Route("/timings", name="dtc_queue_timings") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |     public function getTimingsAction(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |         $begin = $request->query->get('begin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $end = $request->query->get('end'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         $type = $request->query->get('type', 'HOUR'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         $beginDate = \DateTime::createFromFormat('Y-m-d\TH:i:s.uO', $begin) ?: null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $endDate = \DateTime::createFromFormat('Y-m-d\TH:i:s.uO', $end) ?: new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         $recordTimings = $this->container->getParameter('dtc_queue.record_timings'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         if ($recordTimings) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             $params = $this->calculateTimings($type, $beginDate, $endDate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         return new JsonResponse($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param \DateTime|null $beginDate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param \DateTime      $endDate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     protected function calculateTimings($type, $beginDate, $endDate) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         $params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $this->validateJobTimingManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         /** @var BaseJobTimingManager $jobTimingManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         $jobTimingManager = $this->get('dtc_queue.job_timing_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         if ($jobTimingManager instanceof JobTimingManager) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             $timings = $this->getJobTimingsOdm($type, $endDate, $beginDate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |             $timings = $this->getJobTimingsOrm($type, $endDate, $beginDate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         $timingStates = JobTiming::getStates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         $timingsDates = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         foreach (array_keys($timingStates) as $state) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             if (!isset($timings[$state])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |             $timingsData = $timings[$state]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             $timingsDates = array_unique(array_merge(array_keys($timingsData), $timingsDates)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         $format = $this->getDateFormat($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |         usort($timingsDates, function ($date1str, $date2str) use ($format) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $date1 = \DateTime::createFromFormat($format, $date1str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $date2 = \DateTime::createFromFormat($format, $date2str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             if (!$date2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             if (!$date1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             return $date1 > $date2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         $timingsDatesAdjusted = $this->getTimingsDatesAdjusted($timingsDates, $format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         $this->setTimingsData($timingStates, $timings, $timingsDates, $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |         $params['timings_dates'] = $timingsDates; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 1 |  |         $params['timings_dates_rfc3339'] = $timingsDatesAdjusted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         return $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Timings offset by timezone if necessary. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param array  $timingsDates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param string $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |     protected function getTimingsDatesAdjusted(array $timingsDates, $format) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         $timezoneOffset = $this->container->getParameter('dtc_queue.record_timings_timezone_offset'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |         $timingsDatesAdjusted = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |         foreach ($timingsDates as $dateStr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |             $date = \DateTime::createFromFormat($format, $dateStr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             if (0 !== $timezoneOffset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $date->setTimestamp($date->getTimestamp() + ($timezoneOffset * 3600)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             if (false === $date) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 throw new \InvalidArgumentException("'$date' is not in the right format: ".DATE_RFC3339); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |             $timingsDatesAdjusted[] = $date->format(DATE_RFC3339); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |         return $timingsDatesAdjusted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |     protected function setTimingsData(array $timingStates, array $timings, array $timingsDates, array &$params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |         foreach (array_keys($timingStates) as $state) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |             if (!isset($timings[$state])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 1 |  |             $timingsData = $timings[$state]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |             foreach ($timingsDates as $date) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |                 $params['timings_data_'.$state][] = isset($timingsData[$date]) ? $timingsData[$date] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |     protected function getJobTimingsOdm($type, \DateTime $end, \DateTime $begin = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         /** @var JobTimingManager $runManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 1 |  |         $jobTimingManager = $this->get('dtc_queue.job_timing_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 1 |  |         $jobTimingClass = $jobTimingManager->getJobTimingClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         /** @var DocumentManager $documentManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |         $documentManager = $jobTimingManager->getObjectManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |         $regexInfo = $this->getRegexDate($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |         if (!$begin) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |             $begin = clone $end; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |             $begin->sub($regexInfo['interval']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         // Run a map reduce function get worker and status break down | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $mapFunc = 'function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             var dateStr = this.finishedAt.toISOString(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |             dateStr = dateStr.replace(/'.$regexInfo['replace']['regex']."/,'".$regexInfo['replace']['replacement']."'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 1 |  |             var dateBegin = new Date('".$begin->format('c')."'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |             var dateEnd = new Date('".$end->format('c')."'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             if (this.finishedAt >= dateBegin && this.finishedAt <= dateEnd) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 var result = {}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 result[dateStr] = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 emit(this.status, result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         }"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |         $reduceFunc = JobManager::REDUCE_FUNCTION; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |         $builder = $documentManager->createQueryBuilder($jobTimingClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 1 |  |         $builder->map($mapFunc) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 1 |  |             ->reduce($reduceFunc); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 1 |  |         $query = $builder->getQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 1 |  |         $results = $query->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 1 |  |         $resultHash = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 1 |  |         foreach ($results as $info) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 1 |  |             $resultHash[$info['_id']] = $info['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 1 |  |         return $resultHash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 1 |  |     protected function getDateFormat($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 1 |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             case 'YEAR': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 1 |  |                 return 'Y'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             case 'MONTH': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 1 |  |                 return 'Y-m'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             case 'DAY': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 1 |  |                 return 'Y-m-d'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             case 'HOUR': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 1 |  |                 return 'Y-m-d H'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             case 'MINUTE': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 1 |  |                 return 'Y-m-d H:i'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |                 throw new \InvalidArgumentException("Invalid date format type '$type''"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 1 |  |     protected function getRegexDate($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 1 |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 207 |  | View Code Duplication |             case 'YEAR': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 1 |  |                 return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 | 1 |  |                     'interval' => new \DateInterval('P10Y'), ]; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 210 |  | View Code Duplication |             case 'MONTH': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 | 1 |  |                 return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 | 1 |  |                     'interval' => new \DateInterval('P12M'), ]; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 213 |  | View Code Duplication |             case 'DAY': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 1 |  |                 return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2-$3'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 1 |  |                     'interval' => new \DateInterval('P31D'), ]; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 216 |  | View Code Duplication |             case 'HOUR': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 1 |  |                 return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2-$3 $4'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 1 |  |                     'interval' => new \DateInterval('PT24H'), ]; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 219 |  | View Code Duplication |             case 'MINUTE': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 1 |  |                 return ['replace' => ['regex' => '(\d+)\-(\d+)\-(\d+)T(\d+):(\d+):(\d+).+$', 'replacement' => '$1-$2-$3 $4:$5'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 | 1 |  |                     'interval' => new \DateInterval('PT3600S'), ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         throw new \InvalidArgumentException("Invalid type $type"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 | 1 |  |     protected function getOrmGroupBy($type) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 1 |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             case 'YEAR': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 1 |  |                 return ['groupby' => 'YEAR(j.finishedAt)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 | 1 |  |                     'interval' => new \DateInterval('P10Y'), ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |             case 'MONTH': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 | 1 |  |                 return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt))', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 | 1 |  |                     'interval' => new \DateInterval('P12M'), ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |             case 'DAY': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 | 1 |  |                 return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt),\'-\',DAY(j.finishedAt))', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 1 |  |                     'interval' => new \DateInterval('P31D'), ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |             case 'HOUR': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 1 |  |                 return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt),\'-\',DAY(j.finishedAt),\' \',HOUR(j.finishedAt))', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 | 1 |  |                     'interval' => new \DateInterval('PT24H'), ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             case 'MINUTE': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 1 |  |                 return ['groupby' => 'CONCAT(YEAR(j.finishedAt),\'-\',MONTH(j.finishedAt),\'-\',DAY(j.finishedAt),\' \',HOUR(j.finishedAt),\':\',MINUTE(j.finishedAt))', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 | 1 |  |                     'interval' => new \DateInterval('PT3600S'), ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         throw new \InvalidArgumentException("Invalid type $type"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 248 | 1 |  |     protected function getJobTimingsOrm($type, \DateTime $end, \DateTime $begin = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 249 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 250 |  |  |         /** @var JobTimingManager $jobTimingManager */ | 
            
                                                                        
                            
            
                                    
            
            
                | 251 | 1 |  |         $jobTimingManager = $this->get('dtc_queue.job_timing_manager'); | 
            
                                                                        
                            
            
                                    
            
            
                | 252 | 1 |  |         $jobTimingClass = $jobTimingManager->getJobTimingClass(); | 
            
                                                                        
                            
            
                                    
            
            
                | 253 |  |  |         /** @var EntityManager $entityManager */ | 
            
                                                                        
                            
            
                                    
            
            
                | 254 | 1 |  |         $entityManager = $jobTimingManager->getObjectManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 256 | 1 |  |         $groupByInfo = $this->getOrmGroupBy($type); | 
            
                                                                        
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 258 | 1 |  |         if (!$begin) { | 
            
                                                                        
                            
            
                                    
            
            
                | 259 | 1 |  |             $begin = clone $end; | 
            
                                                                        
                            
            
                                    
            
            
                | 260 | 1 |  |             $begin->sub($groupByInfo['interval']); | 
            
                                                                        
                            
            
                                    
            
            
                | 261 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 262 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 263 | 1 |  |         $queryBuilder = $entityManager->createQueryBuilder()->select("j.status as status, count(j.finishedAt) as thecount, {$groupByInfo['groupby']} as thedate") | 
            
                                                                        
                            
            
                                    
            
            
                | 264 | 1 |  |             ->from($jobTimingClass, 'j') | 
            
                                                                        
                            
            
                                    
            
            
                | 265 | 1 |  |             ->where('j.finishedAt <= :end') | 
            
                                                                        
                            
            
                                    
            
            
                | 266 | 1 |  |             ->andWhere('j.finishedAt >= :begin') | 
            
                                                                        
                            
            
                                    
            
            
                | 267 | 1 |  |             ->setParameter(':end', $end) | 
            
                                                                        
                            
            
                                    
            
            
                | 268 | 1 |  |             ->setParameter(':begin', $begin) | 
            
                                                                        
                            
            
                                    
            
            
                | 269 | 1 |  |             ->groupBy('status') | 
            
                                                                        
                            
            
                                    
            
            
                | 270 | 1 |  |             ->addGroupBy('thedate'); | 
            
                                                                        
                            
            
                                    
            
            
                | 271 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 272 |  |  |         $result = $queryBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 273 | 1 |  |             ->getQuery()->getArrayResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 275 | 1 |  |         $resultHash = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 276 | 1 |  |         foreach ($result as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 277 | 1 |  |             $date = $this->formatOrmDateTime($type, $row['thedate']); | 
            
                                                                        
                            
            
                                    
            
            
                | 278 | 1 |  |             $resultHash[$row['status']][$date] = intval($row['thecount']); | 
            
                                                                        
                            
            
                                    
            
            
                | 279 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 280 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 281 | 1 |  |         return $resultHash; | 
            
                                                                        
                            
            
                                    
            
            
                | 282 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 | 1 |  |     protected function strPadLeft($str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 | 1 |  |         return str_pad($str, 2, '0', STR_PAD_LEFT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 | 1 |  |     protected function formatOrmDateTime($type, $str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 | 1 |  |         switch ($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |             case 'MONTH': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 1 |  |                 $parts = explode('-', $str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 | 1 |  |                 return $parts[0].'-'.$this->strPadLeft($parts[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |             case 'DAY': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 1 |  |                 $parts = explode('-', $str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 | 1 |  |                 return $parts[0].'-'.$this->strPadLeft($parts[1]).'-'.$this->strPadLeft($parts[2]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |             case 'HOUR': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 1 |  |                 $parts = explode(' ', $str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 | 1 |  |                 $dateParts = explode('-', $parts[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 1 |  |                 return $dateParts[0].'-'.$this->strPadLeft($dateParts[1]).'-'.$this->strPadLeft($dateParts[2]).' '.$this->strPadLeft($parts[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |             case 'MINUTE': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 | 1 |  |                 $parts = explode(' ', $str); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 | 1 |  |                 $dateParts = explode('-', $parts[0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 | 1 |  |                 $timeParts = explode(':', $parts[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 | 1 |  |                 return $dateParts[0].'-'.$this->strPadLeft($dateParts[1]).'-'.$this->strPadLeft($dateParts[2]).' '.$this->strPadLeft($timeParts[0]).':'.$this->strPadLeft($timeParts[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 | 1 |  |         return $str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 316 | 1 | View Code Duplication |     protected function validateJobTimingManager() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 | 1 |  |         if ($this->container->hasParameter('dtc_queue.job_timing_manager')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |             $this->validateManagerType('dtc_queue.job_timing_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 | 1 |  |         } elseif ($this->container->hasParameter('dtc_queue.job_timing_manager')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |             $this->validateManagerType('dtc_queue.run_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 | 1 |  |             $this->validateManagerType('dtc_queue.default_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 326 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 327 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.