Completed
Push — master ( e02161...971421 )
by Matthew
12:50
created

QueueController::getJobTimingsOrm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 16
cp 0
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 0
crap 6
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\BaseRunManager;
8
use Dtc\QueueBundle\Exception\UnsupportedException;
9
use Dtc\QueueBundle\Model\Worker;
10
use Dtc\QueueBundle\ODM\RunManager;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
15
class QueueController extends Controller
16
{
17
    /**
18
     * Summary stats.
19
     *
20
     * @Route("/")
21
     * @Route("/status/")
22
     * @Template()
23
     */
24
    public function statusAction()
25
    {
26
        $params = array();
27
        $jobManager = $this->get('dtc_queue.job_manager');
28
29
        $params['status'] = $jobManager->getStatus();
30
        $this->addCssJs($params);
31
32
        return $params;
33
    }
34
35
    /**
36
     * List jobs in system by default.
37
     *
38
     * @Route("/jobs_all", name="dtc_queue_jobs_all")
39
     */
40 View Code Duplication
    public function jobsAllAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
41
    {
42
        $this->validateManagerType('dtc_queue.default_manager');
43
        $class1 = $this->container->getParameter('dtc_queue.class_job');
44
        $class2 = $this->container->getParameter('dtc_queue.class_job_archive');
45
        $label1 = 'Non-Archived Jobs';
46
        $label2 = 'Archived Jobs';
47
48
        $params = $this->getDualGridParams($class1, $class2, $label1, $label2);
49
50
        return $this->render('@DtcQueue/Queue/grid.html.twig', $params);
51
    }
52
53
    /**
54
     * List jobs in system by default.
55
     *
56
     * @Template()
57
     * @Route("/jobs", name="dtc_queue_jobs")
58
     */
59
    public function jobsAction()
60
    {
61
        $this->validateManagerType('dtc_queue.default_manager');
62
        $managerType = $this->container->getParameter('dtc_queue.default_manager');
63
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
64
        $renderer = $rendererFactory->create('datatables');
65
        $gridSource = $this->get('dtc_queue.grid_source.live_jobs.'.($managerType = 'mongodb' ? 'odm' : $managerType));
66
        $renderer->bind($gridSource);
67
        $params = $renderer->getParams();
68
        $this->addCssJs($params);
69
70
        return $params;
71
    }
72
73
    protected function validateManagerType($type)
74
    {
75
        $managerType = $this->container->getParameter($type);
76
        if ('mongodb' !== $managerType && 'orm' != $managerType && 'odm' != $managerType) {
77
            throw new UnsupportedException("Unsupported manager type: $managerType");
78
        }
79
    }
80
81
    /**
82
     * @param string $class1
83
     * @param string $class2
84
     * @param string $label1
85
     * @param string $label2
86
     *
87
     * @return \Symfony\Component\HttpFoundation\Response
88
     *
89
     * @throws \Exception
90
     */
91
    protected function getDualGridParams($class1, $class2, $label1, $label2)
92
    {
93
        $rendererFactory = $this->get('dtc_grid.renderer.factory');
94
        $renderer = $rendererFactory->create('datatables');
95
        $gridSource = $this->get('dtc_grid.manager.source')->get($class1);
96
        $renderer->bind($gridSource);
97
        $params = $renderer->getParams();
98
99
        $renderer2 = $rendererFactory->create('datatables');
100
        $gridSource = $this->get('dtc_grid.manager.source')->get($class2);
101
        $renderer2->bind($gridSource);
102
        $params2 = $renderer2->getParams();
103
104
        $params['archive_grid'] = $params2['dtc_grid'];
105
106
        $params['dtc_queue_grid_label1'] = $label1;
107
        $params['dtc_queue_grid_label2'] = $label2;
108
        $this->addCssJs($params);
109
110
        return $params;
111
    }
112
113
    protected function addCssJs(array &$params)
114
    {
115
        $params['css'] = $this->container->getParameter('dtc_grid.theme.css');
116
        $params['js'] = $this->container->getParameter('dtc_grid.theme.js');
117
        $params['chartjs'] = $this->container->getParameter('dtc_queue.admin.chartjs');
118
    }
119
120
    protected function validateRunManager()
121
    {
122
        if ($this->container->hasParameter('dtc_queue.run_manager')) {
123
            $this->validateManagerType('dtc_queue.run_manager');
124
        } else {
125
            $this->validateManagerType('dtc_queue.default_manager');
126
        }
127
    }
128
129
    /**
130
     * List jobs in system by default.
131
     *
132
     * @Route("/runs", name="dtc_queue_runs")
133
     */
134 View Code Duplication
    public function runsAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
135
    {
136
        $this->validateRunManager();
137
        $class1 = $this->container->getParameter('dtc_queue.class_run');
138
        $class2 = $this->container->getParameter('dtc_queue.class_run_archive');
139
        $label1 = 'Live Runs';
140
        $label2 = 'Archived Runs';
141
142
        $params = $this->getDualGridParams($class1, $class2, $label1, $label2);
143
144
        return $this->render('@DtcQueue/Queue/grid.html.twig', $params);
145
    }
146
147
    /**
148
     * List registered workers in the system.
149
     *
150
     * @Route("/workers")
151
     * @Template()
152
     */
153
    public function workersAction()
154
    {
155
        $workerManager = $this->get('dtc_queue.worker_manager');
156
        $workers = $workerManager->getWorkers();
157
158
        $workerList = [];
159
        foreach ($workers as $workerName => $worker) {
160
            /* @var Worker $worker */
161
            $workerList[$workerName] = get_class($worker);
162
        }
163
        $params = ['workers' => $workerList];
164
        $this->addCssJs($params);
165
166
        return $params;
167
    }
168
169
    /**
170
     * Show a graph of job trends.
171
     *
172
     * @Route("/trends")
173
     * @Template()
174
     */
175
    public function trendsAction()
176
    {
177
        $recordTimings = $this->container->getParameter('dtc_queue.record_timings');
178
        $params = ['record_timings' => $recordTimings];
179
        if ($recordTimings) {
180
            $this->validateRunManager();
181
182
            /** @var BaseRunManager $runManager */
183
            $runManager = $this->get('dtc_queue.run_manager');
184
            if ($runManager instanceof RunManager) {
185
                $timings = $this->getJobTimingsOdm();
186
            } else {
187
                $timings = $this->getJobTimingsOrm();
188
            }
189
            uksort($timings, function ($date1str, $date2str) {
190
                $date1 = \DateTime::createFromFormat('Y-m-d H', $date1str);
191
                $date2 = \DateTime::createFromFormat('Y-m-d H', $date2str);
192
                if (!$date2) {
193
                    return false;
194
                }
195
                if (!$date1) {
196
                    return false;
197
                }
198
199
                return $date1 > $date2;
200
            });
201
            $params['timings_dates'] = json_encode(array_keys($timings));
202
            $params['timings_data'] = json_encode(array_values($timings));
203
        }
204
        $this->addCssJs($params);
205
206
        return $params;
207
    }
208
209
    protected function getJobTimingsOdm()
210
    {
211
        /** @var RunManager $runManager */
212
        $runManager = $this->get('dtc_queue.run_manager');
213
        $jobTimingClass = $runManager->getJobTimingClass();
214
        /** @var DocumentManager $documentManager */
215
        $documentManager = $runManager->getObjectManager();
216
217
        // Run a map reduce function get worker and status break down
218
        $mapFunc = "function() {
219
            var dateStr = this.finishedAt.toISOString();
220
            dateStr = dateStr.replace(/:.+\$/,'');
221
            dateStr = dateStr.replace(/T0*/,' ');
222
            emit(dateStr, 1);
223
        }";
224
        $reduceFunc = 'function(k, vals) {
225
            return Array.sum(vals);
226
        }';
227
228
        $builder = $documentManager->createQueryBuilder($jobTimingClass);
229
        $builder->map($mapFunc)
230
            ->reduce($reduceFunc);
231
        $query = $builder->getQuery();
232
        $results = $query->execute();
233
        $resultHash = [];
234
        foreach ($results as $info) {
235
            $resultHash[$info['_id']] = $info['value'];
236
        }
237
238
        return $resultHash;
239
    }
240
241
    protected function getJobTimingsOrm()
242
    {
243
        /** @var RunManager $runManager */
244
        $runManager = $this->get('dtc_queue.run_manager');
245
        $jobTimingClass = $runManager->getJobTimingClass();
246
        /** @var EntityManager $entityManager */
247
        $entityManager = $runManager->getObjectManager();
248
        $queryBuilder = $entityManager->createQueryBuilder()->select("count(j.finishedAt) as thecount, CONCAT(YEAR(j.finishedAt),'-',MONTH(j.finishedAt),'-',DAY(j.finishedAt),' ',HOUR(j.finishedAt)) as thedate")
249
            ->from($jobTimingClass, 'j')
250
            ->groupBy('thedate');
251
252
        $result = $queryBuilder
253
            ->getQuery()->getArrayResult();
254
255
        $resultHash = [];
256
        foreach ($result as $row) {
257
            $resultHash[$row['thedate']] = intval($row['thecount']);
258
        }
259
260
        return $resultHash;
261
    }
262
}
263