| Conditions | 9 |
| Paths | 192 |
| Total Lines | 123 |
| Code Lines | 88 |
| 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 |
||
| 177 | public function actionPagesAreaChart( |
||
| 178 | string $start, |
||
| 179 | string $end, |
||
| 180 | $pageUrl = '', |
||
| 181 | int $siteId = 0 |
||
| 182 | ): Response { |
||
| 183 | PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
||
| 184 | $data = []; |
||
| 185 | // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 |
||
| 186 | $end = date('Y-m-d', strtotime($end.'+1 day')); |
||
| 187 | $pageUrl = urldecode($pageUrl); |
||
| 188 | $dateStart = new \DateTime($start); |
||
| 189 | $dateEnd = new \DateTime($end); |
||
| 190 | $interval = date_diff($dateStart, $dateEnd); |
||
| 191 | $dateFormat = "'%Y-%m-%d %H'"; |
||
| 192 | if ($interval->days > 30) { |
||
| 193 | $dateFormat = "'%Y-%m-%d'"; |
||
| 194 | } |
||
| 195 | // Different dbs do it different ways |
||
| 196 | $stats = null; |
||
| 197 | $db = Craft::$app->getDb(); |
||
| 198 | if ($db->getIsPgsql()) { |
||
| 199 | $dateFormat = "'yyyy-mm-dd HH:MM'"; |
||
| 200 | if ($interval->days > 30) { |
||
| 201 | $dateFormat = "'yyyy-mm-dd'"; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | // Query the db |
||
| 205 | $query = (new Query()) |
||
| 206 | ->from('{{%webperf_data_samples}}') |
||
| 207 | ->select([ |
||
| 208 | 'AVG([[pageLoad]]) AS [[pageLoad]]', |
||
| 209 | 'AVG([[domInteractive]]) AS [[domInteractive]]', |
||
| 210 | 'AVG([[firstContentfulPaint]]) AS [[firstContentfulPaint]]', |
||
| 211 | 'AVG([[firstPaint]]) AS [[firstPaint]]', |
||
| 212 | 'AVG([[firstByte]]) AS [[firstByte]]', |
||
| 213 | 'AVG([[connect]]) AS [[connect]]', |
||
| 214 | 'AVG([[dns]]) AS [[dns]]', |
||
| 215 | 'AVG([[craftTotalMs]]) AS [[craftTotalMs]]', |
||
| 216 | 'AVG([[craftTwigMs]]) AS [[craftTwigMs]]', |
||
| 217 | 'AVG([[craftDbMs]]) AS [[craftDbMs]]', |
||
| 218 | ]) |
||
| 219 | ->where(['between', 'dateCreated', $start, $end]) |
||
| 220 | ; |
||
| 221 | if ((int)$siteId !== 0) { |
||
| 222 | $query->andWhere(['siteId' => $siteId]); |
||
| 223 | } |
||
| 224 | if ($pageUrl !== '') { |
||
| 225 | $query->andWhere(['url' => $pageUrl]); |
||
| 226 | } |
||
| 227 | if ($db->getIsMysql()) { |
||
| 228 | $query |
||
| 229 | ->addSelect([ |
||
| 230 | 'DATE_FORMAT([[dateCreated]], '.$dateFormat.') AS [[sampleDate]]', |
||
| 231 | ]) |
||
| 232 | ->groupBy('sampleDate') |
||
| 233 | ; |
||
| 234 | } |
||
| 235 | if ($db->getIsPgsql()) { |
||
| 236 | $query |
||
| 237 | ->addSelect([ |
||
| 238 | 'to_char([[dateCreated]], '.$dateFormat.') AS [[sampleDate]]', |
||
| 239 | ]) |
||
| 240 | ->groupBy(['to_char([[dateCreated]], '.$dateFormat.')']) |
||
| 241 | ; |
||
| 242 | } |
||
| 243 | $stats = $query |
||
| 244 | ->all(); |
||
| 245 | // Massage the data |
||
| 246 | if ($stats) { |
||
| 247 | $data[] = [ |
||
| 248 | 'name' => 'Database Queries', |
||
| 249 | 'data' => ArrayHelper::getColumn($stats, 'craftDbMs'), |
||
| 250 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 251 | ]; |
||
| 252 | $data[] = [ |
||
| 253 | 'name' => 'Twig Rendering', |
||
| 254 | 'data' => ArrayHelper::getColumn($stats, 'craftTwigMs'), |
||
| 255 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 256 | ]; |
||
| 257 | $data[] = [ |
||
| 258 | 'name' => 'Craft Rendering', |
||
| 259 | 'data' => ArrayHelper::getColumn($stats, 'craftTotalMs'), |
||
| 260 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 261 | ]; |
||
| 262 | $data[] = [ |
||
| 263 | 'name' => 'DNS Lookup', |
||
| 264 | 'data' => ArrayHelper::getColumn($stats, 'dns'), |
||
| 265 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 266 | ]; |
||
| 267 | $data[] = [ |
||
| 268 | 'name' => 'Connect', |
||
| 269 | 'data' => ArrayHelper::getColumn($stats, 'connect'), |
||
| 270 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 271 | ]; |
||
| 272 | $data[] = [ |
||
| 273 | 'name' => 'First Byte', |
||
| 274 | 'data' => ArrayHelper::getColumn($stats, 'firstByte'), |
||
| 275 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 276 | ]; |
||
| 277 | $data[] = [ |
||
| 278 | 'name' => 'First Paint', |
||
| 279 | 'data' => ArrayHelper::getColumn($stats, 'firstPaint'), |
||
| 280 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 281 | ]; |
||
| 282 | $data[] = [ |
||
| 283 | 'name' => 'First Contentful Paint', |
||
| 284 | 'data' => ArrayHelper::getColumn($stats, 'firstContentfulPaint'), |
||
| 285 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 286 | ]; |
||
| 287 | $data[] = [ |
||
| 288 | 'name' => 'DOM Interactive', |
||
| 289 | 'data' => ArrayHelper::getColumn($stats, 'domInteractive'), |
||
| 290 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 291 | ]; |
||
| 292 | $data[] = [ |
||
| 293 | 'name' => 'Page Load', |
||
| 294 | 'data' => ArrayHelper::getColumn($stats, 'pageLoad'), |
||
| 295 | 'labels' => ArrayHelper::getColumn($stats, 'sampleDate'), |
||
| 296 | ]; |
||
| 297 | } |
||
| 298 | |||
| 299 | return $this->asJson($data); |
||
| 300 | } |
||
| 318 |