| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Webperf plugin for Craft CMS 3.x | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Monitor the performance of your webpages through real-world user timing data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @link      https://nystudio107.com | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2019 nystudio107 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace nystudio107\webperf\controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use nystudio107\webperf\helpers\Permission as PermissionHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Craft; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use craft\db\Query; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use craft\helpers\UrlHelper; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use craft\web\Controller; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use yii\web\ForbiddenHttpException; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use yii\web\Response; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author    nystudio107 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @package   Webperf | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @since     1.0.0 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | class TablesController extends Controller | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     // Constants | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     // ========================================================================= | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     // Protected Properties | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     // ========================================================================= | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @var    bool|array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     protected $allowAnonymous = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     // Public Methods | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     // ========================================================================= | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * Handle requests for the dashboard statistics table | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * @param string $sort | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param int    $page | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param int    $per_page | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $filter | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @param string $start | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $end | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param int    $siteId | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * @throws ForbiddenHttpException | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function actionPagesIndex( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         string $start = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         string $end = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         string $sort = 'pageLoad|desc', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         int $page = 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         int $per_page = 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $filter = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $siteId = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         PermissionHelper::controllerPermissionCheck('webperf:pages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $sortField = 'pageLoad'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $sortType = 'DESC'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $end = date('Y-m-d', strtotime($end.'+1 day')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         // Figure out the sorting type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if ($sort !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if (strpos($sort, '|') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $sortField = $sort; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 list($sortField, $sortType) = explode('|', $sort); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($sortField === 'totalPageLoad') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $sortField = 'pageLoad'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         // Query the db table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $offset = ($page - 1) * $per_page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $query = (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             ->select([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'url', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 'MIN(title) AS title', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'COUNT(url) AS cnt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 'AVG(pageLoad) AS pageLoad', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 'AVG(domInteractive) AS domInteractive', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 'AVG(firstContentfulPaint) AS firstContentfulPaint', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 'AVG(firstPaint) AS firstPaint', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 'AVG(firstByte) AS firstByte', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'AVG(connect) AS connect', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 'AVG(dns) AS dns', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 'AVG(craftTotalMs) AS craftTotalMs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 'AVG(craftDbCnt) AS craftDbCnt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 'AVG(craftDbMs) AS craftDbMs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 'AVG(craftTwigCnt) AS craftTwigCnt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 'AVG(craftTwigMs) AS craftTwigMs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 'AVG(craftTotalMemory) AS craftTotalMemory', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->from(['{{%webperf_data_samples}}']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->offset($offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->where(['between', 'dateUpdated', $start, $end]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if ((int)$siteId !== 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $query->andWhere(['siteId' => $siteId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($filter !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 ->andWhere(['like', 'url', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 ->orWhere(['like', 'title', $filter]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             ->orderBy("{$sortField} {$sortType}") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             ->groupBy('url') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             ->limit($per_page) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $stats = $query->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         if ($stats) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             // Compute the largest page load time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $maxTotalPageLoad = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             foreach ($stats as &$stat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 // Determine the stat type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     $stat['type'] = 'both'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 if (empty($stat['pageLoad'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                     $stat['type'] = 'craft'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 if (empty($stat['craftTotalMs'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     $stat['type'] = 'frontend'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 if (empty($stat['pageLoad'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     $pageLoad = $stat['craftTotalMs']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     $pageLoad = $stat['pageLoad']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 if ($pageLoad > $maxTotalPageLoad) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                     $maxTotalPageLoad = $pageLoad; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             // Massage the stats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $index = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             foreach ($stats as &$stat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 $stat['id'] = $index++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $stat['cnt'] = (int)$stat['cnt']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 $stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 // If there is no frontend beacon timing, use the Craft timing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 if (empty($stat['pageLoad'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                     $stat['totalPageLoad'] = (int)$stat['craftTotalMs']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                     $stat['totalPageLoad'] = (int)$stat['pageLoad']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 // Decode any emojis in the title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 if (!empty($stat['title'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                     $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 // Set up the appropriate helper links | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 $stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-samples-by-url', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                     'pageUrl' => $stat['url'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                     'siteId' => $siteId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 $stat['detailPageUrl'] = UrlHelper::cpUrl('webperf/page-detail', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                     'pageUrl' => $stat['url'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                     'siteId' => $siteId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 // Override based on permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $user = Craft::$app->getUser()->getIdentity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 if (!$user->can('webperf:delete-data-samples')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                     $stat['deleteLink'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 if (!$user->can('webperf:page-detail')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |                     $stat['detailPageUrl'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             // Format the data for the API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $data['data'] = $stats; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $query = (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 ->from(['{{%webperf_data_samples}}']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 ->groupBy('url'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             if ($filter !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                 $query->where(['like', 'url', $filter]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 $query->orWhere(['like', 'title', $filter]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             $count = $query->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             $data['links']['pagination'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 'total' => $count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                 'per_page' => $per_page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 'current_page' => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |                 'last_page' => ceil($count / $per_page), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |                 'next_page_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                 'prev_page_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |                 'from' => $offset + 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |                 'to' => $offset + ($count > $per_page ? $per_page : $count), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  |         return $this->asJson($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |      * Handle requests for the dashboard statistics table | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  |      * @param string $sort | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 211 |  |  |      * @param int    $page | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 212 |  |  |      * @param int    $per_page | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 213 |  |  |      * @param string $filter | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 214 |  |  |      * @param string $pageUrl | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 215 |  |  |      * @param string $start | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 216 |  |  |      * @param string $end | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 217 |  |  |      * @param int    $siteId | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 218 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 219 |  |  |      * @return Response | 
            
                                                                        
                            
            
                                    
            
            
                | 220 |  |  |      * @throws ForbiddenHttpException | 
            
                                                                        
                            
            
                                    
            
            
                | 221 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     public function actionPageDetail( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |         string $start = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |         string $end = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         string $sort = 'pageLoad|desc', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         int $page = 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         int $per_page = 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |         $filter = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |         $pageUrl = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |         $siteId = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |     ): Response { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         PermissionHelper::controllerPermissionCheck('webperf:pages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |         $sortField = 'pageLoad'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |         $sortType = 'DESC'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |         // Add a day since YYYY-MM-DD is really YYYY-MM-DD 00:00:00 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         $end = date('Y-m-d', strtotime($end.'+1 day')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |         $pageUrl = urldecode($pageUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |         // Figure out the sorting type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         if ($sort !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |             if (strpos($sort, '|') === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |                 $sortField = $sort; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |                 list($sortField, $sortType) = explode('|', $sort); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |         if ($sortField === 'totalPageLoad') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |             $sortField = 'pageLoad'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |         // Query the db table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         $offset = ($page - 1) * $per_page; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         $query = (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |             ->from(['{{%webperf_data_samples}}']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |             ->offset($offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             ->where(['url' => $pageUrl]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |             ->andWhere(['between', 'dateUpdated', $start, $end]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |         if ((int)$siteId !== 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |             $query->andWhere(['siteId' => $siteId]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |         if ($filter !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |             $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |                 ->andWhere(['like', 'device', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |                 ->orWhere(['like', 'os', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |                 ->orWhere(['like', 'browser', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |                 ->orWhere(['like', 'countryCode', $filter]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |         $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |             ->orderBy("{$sortField} {$sortType}") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |             ->limit($per_page) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         $stats = $query->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |         if ($stats) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |             // Compute the largest page load time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |             $maxTotalPageLoad = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |             foreach ($stats as &$stat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |                 // Determine the stat type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |                 if (!empty($stat['pageLoad']) && !empty($stat['craftTotalMs'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                     $stat['type'] = 'both'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |                 if (empty($stat['pageLoad'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |                     $stat['type'] = 'craft'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |                 if (empty($stat['craftTotalMs'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |                     $stat['type'] = 'frontend'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |                 if (empty($stat['pageLoad'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |                     $pageLoad = $stat['craftTotalMs']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |                     $pageLoad = $stat['pageLoad']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |                 if ($pageLoad > $maxTotalPageLoad) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |                     $maxTotalPageLoad = $pageLoad; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |             // Massage the stats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |             foreach ($stats as &$stat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |                 $stat['maxTotalPageLoad'] = (int)$maxTotalPageLoad; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |                 // If there is no frontend beacon timing, use the Craft timing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |                 if (empty($stat['pageLoad'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |                     $stat['totalPageLoad'] = (int)$stat['craftTotalMs']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |                     $stat['totalPageLoad'] = (int)$stat['pageLoad']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |                 // Decode any emojis in the title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |                 if (!empty($stat['title'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |                     $stat['title'] = html_entity_decode($stat['title'], ENT_NOQUOTES, 'UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |                 $stat['deleteLink'] = UrlHelper::actionUrl('webperf/data-samples/delete-sample-by-id', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |                     'id' => $stat['id'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |                 ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |                 // Override based on permissions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |                 $user = Craft::$app->getUser()->getIdentity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |                 if (!$user->can('webperf:delete-data-samples')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |                     $stat['deleteLink'] = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |             // Format the data for the API | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |             $data['data'] = $stats; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |             $query = (new Query()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |                 ->from(['{{%webperf_data_samples}}']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |                 ->where(['url' => $pageUrl]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |             if ($filter !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |                 $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |                     ->andWhere(['like', 'device', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |                     ->orWhere(['like', 'os', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |                     ->orWhere(['like', 'browser', $filter]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |                     ->orWhere(['like', 'countryCode', $filter]) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |                 ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |             $count = $query->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |             $data['links']['pagination'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |                 'total' => $count, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |                 'per_page' => $per_page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |                 'current_page' => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |                 'last_page' => ceil($count / $per_page), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |                 'next_page_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |                 'prev_page_url' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |                 'from' => $offset + 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |                 'to' => $offset + ($count > $per_page ? $per_page : $count), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 347 |  |  |         return $this->asJson($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |     // Protected Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |     // ========================================================================= | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 352 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 353 |  |  |  |