| Conditions | 18 |
| Paths | 60 |
| Total Lines | 136 |
| Code Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 59 | public function actionPagesIndex( |
||
| 60 | string $start = '', |
||
| 61 | string $end = '', |
||
| 62 | string $sort = 'pageLoad|DESC', |
||
| 63 | int $page = 1, |
||
| 64 | int $per_page = 20, |
||
| 65 | $filter = '', |
||
| 66 | $siteId = 0 |
||
| 67 | ): Response { |
||
| 68 | PermissionHelper::controllerPermissionCheck('webperf:performance'); |
||
| 69 | $data = []; |
||
| 70 | $sortField = 'pageLoad'; |
||
| 71 | $sortType = 'DESC'; |
||
| 72 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
||
| 73 | $end = date('Y-m-d', strtotime($end.'+1 day')); |
||
| 74 | // Figure out the sorting type |
||
| 75 | if ($sort !== '') { |
||
| 76 | if (strpos($sort, '|') === false) { |
||
| 77 | $sortField = $sort; |
||
| 78 | } else { |
||
| 79 | list($sortField, $sortType) = explode('|', $sort); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | // Query the db table |
||
| 83 | $offset = ($page - 1) * $per_page; |
||
| 84 | $query = (new Query()) |
||
| 85 | ->select([ |
||
| 86 | '[[url]]', |
||
| 87 | 'MIN([[title]]) AS [[title]]', |
||
| 88 | 'COUNT([[url]]) AS [[cnt]]', |
||
| 89 | 'AVG([[pageLoad]]) AS [[pageLoad]]', |
||
| 90 | 'AVG([[domInteractive]]) AS [[domInteractive]]', |
||
| 91 | 'AVG([[firstContentfulPaint]]) AS [[firstContentfulPaint]]', |
||
| 92 | 'AVG([[firstPaint]]) AS [[firstPaint]]', |
||
| 93 | 'AVG([[firstByte]]) AS [[firstByte]]', |
||
| 94 | 'AVG([[connect]]) AS [[connect]]', |
||
| 95 | 'AVG([[dns]]) AS [[dns]]', |
||
| 96 | 'AVG([[craftTotalMs]]) AS [[craftTotalMs]]', |
||
| 97 | 'AVG([[craftDbCnt]]) AS [[craftDbCnt]]', |
||
| 98 | 'AVG([[craftDbMs]]) AS [[craftDbMs]]', |
||
| 99 | 'AVG([[craftTwigCnt]]) AS [[craftTwigCnt]]', |
||
| 100 | 'AVG([[craftTwigMs]]) AS [[craftTwigMs]]', |
||
| 101 | 'AVG([[craftTotalMemory]]) AS [[craftTotalMemory]]', |
||
| 102 | ]) |
||
| 103 | ->from(['{{%webperf_data_samples}}']) |
||
| 104 | ->offset($offset) |
||
| 105 | ->where(['between', 'dateCreated', $start, $end]) |
||
| 106 | ; |
||
| 107 | if ((int)$siteId !== 0) { |
||
| 108 | $query->andWhere(['siteId' => $siteId]); |
||
| 109 | } |
||
| 110 | if ($filter !== '') { |
||
| 111 | $query |
||
| 112 | ->andWhere(['like', 'url', $filter]) |
||
| 113 | ->orWhere(['like', 'title', $filter]) |
||
| 114 | ; |
||
| 115 | } |
||
| 116 | $query |
||
| 117 | ->orderBy("[[{$sortField}]] {$sortType}") |
||
| 118 | ->groupBy('url') |
||
| 119 | ->limit($per_page) |
||
| 120 | ; |
||
| 121 | |||
| 122 | $stats = $query->all(); |
||
| 123 | if ($stats) { |
||
| 124 | $user = Craft::$app->getUser()->getIdentity(); |
||
| 125 | // Compute the largest page load time |
||
| 126 | $maxTotalPageLoad = 0; |
||
| 127 | foreach ($stats as &$stat) { |
||
| 128 | // Determine the stat type |
||
| 129 | if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { |
||
| 130 | $stat['type'] = 'both'; |
||
| 131 | } |
||
| 132 | if (empty($stat['firstByte'])) { |
||
| 133 | $stat['type'] = 'craft'; |
||
| 134 | } |
||
| 135 | if (empty($stat['craftTotalMs'])) { |
||
| 136 | $stat['type'] = 'frontend'; |
||
| 137 | } |
||
| 138 | if ($stat['pageLoad'] > $maxTotalPageLoad) { |
||
| 139 | $maxTotalPageLoad = $stat['pageLoad']; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | // Massage the stats |
||
| 143 | $index = 1; |
||
| 144 | foreach ($stats as &$stat) { |
||
| 145 | $stat['id'] = $index++; |
||
| 146 | $stat['cnt'] = (int)$stat['cnt']; |
||
| 147 | $stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; |
||
| 148 | // Decode any emojis in the title |
||
| 149 | if (!empty($stat['title'])) { |
||
| 150 | $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); |
||
| 151 | } |
||
| 152 | // Set up the appropriate helper links |
||
| 153 | $stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-samples-by-url', [ |
||
| 154 | 'pageUrl' => $stat['url'], |
||
| 155 | 'siteId' => $siteId |
||
| 156 | ]); |
||
| 157 | $stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/performance/page-detail', [ |
||
| 158 | 'pageUrl' => $stat['url'], |
||
| 159 | 'siteId' => $siteId, |
||
| 160 | ]); |
||
| 161 | // Override based on permissions |
||
| 162 | if (!$user->can('webperf:delete-data-samples')) { |
||
| 163 | $stat['deleteLink'] = ''; |
||
| 164 | } |
||
| 165 | if (!$user->can('webperf:performance-detail')) { |
||
| 166 | $stat['detailPageUrl'] = ''; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | // Format the data for the API |
||
| 170 | $data['data'] = $stats; |
||
| 171 | $query = (new Query()) |
||
| 172 | ->select(['[[url]]']) |
||
| 173 | ->from(['{{%webperf_data_samples}}']) |
||
| 174 | ->groupBy('[[url]]') |
||
| 175 | ->where(['between', 'dateCreated', $start, $end]) |
||
| 176 | ; |
||
| 177 | if ($filter !== '') { |
||
| 178 | $query->andWhere(['like', 'url', $filter]); |
||
| 179 | $query->orWhere(['like', 'title', $filter]); |
||
| 180 | } |
||
| 181 | $count = $query->count(); |
||
| 182 | $data['links']['pagination'] = [ |
||
| 183 | 'total' => $count, |
||
| 184 | 'per_page' => $per_page, |
||
| 185 | 'current_page' => $page, |
||
| 186 | 'last_page' => ceil($count / $per_page), |
||
| 187 | 'next_page_url' => null, |
||
| 188 | 'prev_page_url' => null, |
||
| 189 | 'from' => $offset + 1, |
||
| 190 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this->asJson($data); |
||
| 195 | } |
||
| 464 |