Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
55 / 55 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
PerformanceForm | |
100.00% |
55 / 55 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
getFormId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getQuestion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCancelUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
submitForm | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
batch | |
100.00% |
41 / 41 |
|
100.00% |
1 / 1 |
4 | |||
dropTable | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Drupal\visitors\Form; |
6 | |
7 | use Drupal\Core\Url; |
8 | use Drupal\Core\Form\FormStateInterface; |
9 | use Drupal\Core\Form\ConfirmFormBase; |
10 | |
11 | /** |
12 | * Form to migrate the performance data. |
13 | */ |
14 | final class PerformanceForm extends ConfirmFormBase { |
15 | |
16 | /** |
17 | * {@inheritdoc} |
18 | */ |
19 | public function getFormId() { |
20 | return 'visitors_performance_form'; |
21 | } |
22 | |
23 | /** |
24 | * {@inheritdoc} |
25 | */ |
26 | public function getQuestion() { |
27 | return $this->t('This will migrate the performance data from the old table. Are you sure?'); |
28 | } |
29 | |
30 | /** |
31 | * {@inheritdoc} |
32 | */ |
33 | public function getCancelUrl() { |
34 | return new Url('visitors.settings'); |
35 | } |
36 | |
37 | /** |
38 | * {@inheritdoc} |
39 | */ |
40 | public function submitForm(array &$form, FormStateInterface $form_state) { |
41 | |
42 | $batch = [ |
43 | 'operations' => [ |
44 | |
45 | ['Drupal\visitors\Form\PerformanceForm::batch', []], |
46 | ['Drupal\visitors\Form\PerformanceForm::dropTable', []], |
47 | ], |
48 | 'title' => $this->t('Migrating performance data.'), |
49 | ]; |
50 | |
51 | \batch_set($batch); |
52 | } |
53 | |
54 | /** |
55 | * Batch update performance data. |
56 | * |
57 | * @param array $context |
58 | * The batch context. |
59 | */ |
60 | public static function batch(array &$context) { |
61 | $time = \Drupal::service('datetime.time'); |
62 | $database = \Drupal::database(); |
63 | |
64 | $start_time = $time->getRequestTime(); |
65 | |
66 | if (!isset($context['sandbox']['total'])) { |
67 | $select = $database->select('visitors_performance', 'vp'); |
68 | $select->fields('vp', ['visitors_id']); |
69 | $select->join('visitors', 'v', 'vp.visitors_id = v.visitors_id'); |
70 | $select->condition('v.pf_total', NULL, 'IS NOT NULL'); |
71 | $total = $select->countQuery()->execute()->fetchField(); |
72 | $context['sandbox']['total'] = $total; |
73 | $context['sandbox']['current'] = 0; |
74 | |
75 | if ($total == 0) { |
76 | $context['finished'] = 1; |
77 | return; |
78 | } |
79 | } |
80 | |
81 | $select = $database->select('visitors_performance', 'vp'); |
82 | $select->fields('vp'); |
83 | $select->join('visitors', 'v', 'vp.visitors_id = v.visitors_id'); |
84 | $select->condition('v.pf_total', NULL, 'IS NOT NULL'); |
85 | $select->range(0, 1000); |
86 | |
87 | $rows = $select->execute()->fetchAll(); |
88 | |
89 | do { |
90 | $row = array_shift($rows); |
91 | |
92 | $update = $database->update('visitors') |
93 | ->fields([ |
94 | 'pf_total' => $row->total, |
95 | 'pf_network' => $row->network, |
96 | 'pf_server' => $row->server, |
97 | 'pf_transfer' => $row->transfer, |
98 | 'pf_dom_processing' => $row->dom_processing, |
99 | 'pf_dom_complete' => $row->dom_complete, |
100 | 'pf_on_load' => $row->on_load, |
101 | ]) |
102 | ->condition('visitors_id', $row->visitors_id) |
103 | ->execute(); |
104 | |
105 | $delete = $database->delete('visitors_performance') |
106 | ->condition('visitors_id', $row->id) |
107 | ->execute(); |
108 | |
109 | $context['sandbox']['current'] += 1; |
110 | $context['finished'] = $context['sandbox']['current'] / $context['sandbox']['total']; |
111 | $now = $time->getCurrentTime(); |
112 | $diff = $now - $start_time; |
113 | } while ($diff < 30 && !empty($rows)); |
114 | |
115 | } |
116 | |
117 | /** |
118 | * Drop the performance table. |
119 | */ |
120 | public static function dropTable() { |
121 | $database = \Drupal::database(); |
122 | $schema = $database->schema(); |
123 | $schema->dropTable('visitors_performance'); |
124 | } |
125 | |
126 | } |