| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Contains logic for getting/creating/removing profile records. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class Xhgui_Profiles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * @var Xhgui_StorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Xhgui_Profiles constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param Xhgui_StorageInterface $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function __construct(\Xhgui_StorageInterface $storage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param $conditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param null $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function query($conditions, $fields = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return $this->storage->find($conditions, $fields); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Get a single profile run by id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param string $id The id of the profile to get. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return Xhgui_Profile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function get($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return $this->wrap($this->storage->findOne($id)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Get the list of profiles for a simplified url. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $url The url to load profiles for. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param array $options Pagination options to use. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param array $conditions The search options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return MongoCursor | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function getForUrl($url, $options, $conditions = array()) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $conditions = array_merge( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             (array)$conditions, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             array('simple_url' => $url) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $options = array_merge($options, array( | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             'conditions' => $conditions, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return $this->paginate($options); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param Xhgui_Storage_Filter $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function paginate(Xhgui_Storage_Filter $filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $projection = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($projection === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $result = $this->storage->find($filter, null); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $result = $this->storage->find($filter, $projection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $totalRows = $this->storage->count($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $totalPages = max(ceil($totalRows / $filter->getPerPage()), 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'results'       => $this->wrap($result), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'sort'          => $filter->getSort(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'direction'     => $filter->getDirection(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'page'          => $filter->getPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'perPage'       => $filter->getPerPage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             'totalPages'    => $totalPages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * Get the Percentile metrics for a URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * This will group data by date and returns only the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * percentile + date, making the data ideal for time series graphs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param integer $percentile The percentile you want. e.g. 90. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param array $search Search options containing startDate and or endDate | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return array Array of metrics grouped by date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function getPercentileForUrl($percentile, $url, $filter) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $col = '$meta.request_date'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $results = $this->storage->aggregate($filter, $col, $percentile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (empty($results['result'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $keys = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             'wall_times'    => 'wt', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'cpu_times'     => 'cpu', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'mu_times'      => 'mu', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'pmu_times'     => 'pmu' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         foreach ($results['result'] as &$result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             if ($result['_id'] instanceof MongoDate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $result['date'] = date('Y-m-d H:i:s', $result['_id']->sec); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 $result['date'] = $result['_id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             unset($result['_id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $index = max(round($result['raw_index']) - 1, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             foreach ($keys as $key => $out) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 sort($result[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $result[$out] = isset($result[$key][$index]) ? $result[$key][$index] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 unset($result[$key]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         return array_values($results['result']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Get a paginated set of results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param array $options The find options to use. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return array An array of result data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public function getAll($filter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $this->paginate($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * Delete a profile run. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param string $id The profile id to delete. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     public function delete($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $this->storage->remove($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Used to truncate a collection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * Primarly used in test cases to reset the test db. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function truncate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return $this->storage->drop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * Converts arrays + MongoCursors into Xhgui_Profile instances. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @param array|MongoCursor $data The data to transform. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @return Xhgui_Profile|array The transformed/wrapped results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     protected function wrap($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         if ($data === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             throw new Exception('No profile data found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         if (!($data instanceof \Xhgui_Storage_ResultSet)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             return new Xhgui_Profile($data, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         $results = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         foreach ($data as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             $results[] = new Xhgui_Profile($row, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         return $results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @return Xhgui_StorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     public function getStorage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         return $this->storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |      * @param Xhgui_StorageInterface $storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     public function setStorage($storage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $this->storage = $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 208 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |  | 
            
                        
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: