HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
DefaultController.php
Go to the documentation of this file.
1 <?php
2 
10 class DefaultController extends CController
11 {
12  public $layout = 'main';
13  public $summary;
14 
18  public function getOwner()
19  {
20  return $this->getModule()->owner;
21  }
22 
27  public function getComponent()
28  {
29  return $this->getModule()->owner;
30  }
31 
35  public function actionIndex()
36  {
37  $this->render('index', array(
38  'manifest' => $this->getManifest(),
39  ));
40  }
41 
47  public function actionView($tag = null, $panel = null)
48  {
49  if ($tag === null) {
50  $tags = array_keys($this->getManifest());
51  $tag = reset($tags);
52  $this->redirect(array('view', 'tag' => $tag, 'panel' => $panel));
53  }
54  $this->loadData($tag);
55  if (isset($this->component->panels[$panel])) {
56  $activePanel = $this->getOwner()->panels[$panel];
57  } else {
58  $activePanel = $this->getOwner()->panels['request'];
59  }
60  $this->render('view', array(
61  'tag' => $tag,
62  'summary' => $this->summary,
63  'manifest' => $this->getManifest(),
64  'panels' => $this->getOwner()->panels,
65  'activePanel' => $activePanel,
66  ));
67  }
68 
73  public function actionLock($tag)
74  {
75  $lock = $this->getOwner()->getLock($tag);
76  $this->getOwner()->setLock($tag, !$lock);
77  echo !$lock;
78  }
79 
87  public function actionExplain($tag, $num, $connection)
88  {
89  $this->loadData($tag);
90 
91  $dbPanel = $this->getOwner()->panels['db'];
92  if (!($dbPanel instanceof Yii2DbPanel)) {
93  throw new Exception('Yii2DbPanel not found');
94  }
95  if (!$dbPanel->canExplain) {
96  throw new CHttpException(403, 'Forbidden');
97  }
98  $message = $dbPanel->messageByNum($num);
99  if ($message === null) {
100  throw new Exception("Not found query by number $num");
101  }
102  $query = $dbPanel->formatSql($message, true);
103  /* @var CDbConnection $db */
104  $db = Yii::app()->getComponent($connection);
105 
106  if (!Yii::app()->request->isAjaxRequest) {
107  $this->getOwner()->setLock($tag, true);
108  $this->render('explain', array(
109  'tag' => $tag,
110  'summary' => $this->summary,
111  'manifest' => $this->getManifest(),
112  'panels' => $this->getOwner()->panels,
113  'dbPanel' => $dbPanel,
114  'connection' => $db,
115  'procedure' => Yii2DbPanel::getExplainQuery($query, $db->driverName),
116  'explainRows' => Yii2DbPanel::explain($query, $db),
117  ));
118  } else {
119  $this->renderPartial('_explain', array(
120  'connection' => $db,
121  'explainRows' => Yii2DbPanel::explain($query, $db),
122  ));
123  }
124  }
125 
130  public function actionToolbar($tag)
131  {
132  $this->loadData($tag);
133  $this->renderPartial('toolbar', array(
134  'tag' => $tag,
135  'panels' => $this->getOwner()->panels,
136  ));
137  }
138 
139  public function actionPhpinfo()
140  {
141  phpinfo();
142  }
143 
144  private $_manifest;
145 
146  protected function getManifest()
147  {
148  if ($this->_manifest === null) {
149  $path = $this->getOwner()->logPath;
150  $indexFile = "$path/index.data";
151  if (is_file($indexFile)) {
152  $this->_manifest = array_reverse(unserialize(file_get_contents($indexFile)), true);
153  } else {
154  $this->_manifest = array();
155  }
156  }
157  return $this->_manifest;
158  }
159 
160  protected function loadData($tag)
161  {
162  $manifest = $this->getManifest();
163  if (isset($manifest[$tag])) {
164  $path = $this->getOwner()->logPath;
165  $dataFile = "$path/$tag.data";
166  $data = unserialize(file_get_contents($dataFile));
167  foreach ($this->getOwner()->panels as $id => $panel) {
168  if (isset($data[$id])) {
169  $panel->load($data[$id], $tag);
170  } else {
171  // remove the panel since it has not received any data
172  unset($this->getOwner()->panels[$id]);
173  }
174  }
175  $this->summary = $data['summary'];
176  } else {
177  throw new CHttpException(404, "Unable to find debug data tagged with '$tag'.");
178  }
179  }
180 
181  public function actionConfig()
182  {
183  if (!$this->getOwner()->showConfig) {
184  throw new CHttpException(403, 'Forbidden');
185  }
186  $components = array();
187  foreach (Yii::app()->getComponents(false) as $id => $config) {
188  try {
189  $components[$id] = Yii::app()->getComponent($id);
190  } catch (Exception $e) {
191  assert(is_array($config));
192  $components[$id] = array_merge($config, array(
193  '_error_' => $e->getMessage(),
194  ));
195  }
196  }
197  ksort($components);
198  $modules = Yii::app()->modules;
199  ksort($modules);
200  $data = $this->hideConfigData(
201  array(
202  'app' => Yii2Debug::prepareData(Yii::app()),
203  'components' => Yii2Debug::prepareData($components),
204  'modules' => Yii2Debug::prepareData($modules),
205  'params' => Yii2Debug::prepareData(Yii::app()->params),
206  ),
207  $this->getOwner()->hiddenConfigOptions
208  );
209  $this->render('config', $data);
210  }
211 
217  private function hideConfigData($config, $options)
218  {
219  foreach ($options as $option) {
220  $item = &$config;
221  foreach (explode('/', $option) as $key) {
222  if (is_array($item) && isset($item[$key])) {
223  $item = &$item[$key];
224  } else {
225  unset($item);
226  break;
227  }
228  }
229  if (isset($item)) {
230  $item = '**********';
231  unset($item);
232  }
233  }
234  return $config;
235  }
236 }