HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
SitesView.php
Go to the documentation of this file.
1 <?php
2 
3 class SitesView
4 {
5  private $opLog = 'SITE_FIND';
6  public $responseFile;
7  public $requestFile;
8  public $single;
9 
10  public function dccCheck($userId)
11  {
12  $this->createRequestSingle($userId, '0', true);
13  $json = $this->commandStatus($userId);
14 // Logger::log("Response -> \n" . $json, false);
16  return $json;
17  }
18 
19  public function createRequestSingle($userId, $siteId, $init = false)
20  {
21  $json = array(
22  "deleteTaskId" => null,
23  "id" => $siteId
24  );
25  $json = CJSON::encode($json);
26  $this->opLog = "SITE_STATUS";
27  if (!$init) {
28  Logger::log("Opertion ->" . $this->opLog, false);
29  Logger::log("Request -> \n" . $json, true);
30  }
31  $path = Yii::app()->getBasePath() . '/json_temp';
32  $reqFile = tempnam($path, '');
33  $this->requestFile = $reqFile;
34  $file = fopen($reqFile, 'w');
35  fwrite($file, $json);
36  return $reqFile;
37  }
38 
39  public function commandFind($userId)
40  {
41  $api = Yii::app()->params['api'];
42  $path = Yii::app()->getBasePath() . '/shell/';
43  $pathJson = Yii::app()->getBasePath() . '/json_temp/';
44  $cmd = "sh " . $path . "site_find_by_criterions.sh $api $this->requestFile";
45  $json = shell_exec($cmd);
46  $resFile = tempnam($pathJson, '');
47  $this->responseFile = $resFile;
48  $file = fopen($resFile, 'w');
49  fwrite($file, $json);
50  fclose($file);
51  return $this->getResponse($userId);
52  }
53 
54  public function commandStatus($userId)
55  {
56  $api = Yii::app()->params['api'];
57  $path = Yii::app()->getBasePath() . '/shell/';
58  $pathTemp = Yii::app()->getBasePath() . '/json_temp/';
59  $cmd = "sh " . $path . "site_status.sh $api $this->requestFile";
60  $json = shell_exec($cmd);
61  $resFile = tempnam($pathTemp, '');
62  $this->responseFile = $resFile;
63  $file = fopen($resFile, 'w');
64  fwrite($file, $json);
65  fclose($file);
66  return $this->getResponse($userId);
67  }
68 
69  public function getResponse()
70  {
71  return file_get_contents($this->responseFile);
72  }
73 
74  public function findSite($userId, $reqFile, $one = false)
75  {
76  $data = array();
77  if ($one !== false) {
78  $json = $this->commandStatus($userId);
79  } else {
80  $json = $this->commandFind($userId);
81  }
82  Logger::log("Response -> \n" . $json, false);
83  $error = Errors::isError($json);
84  if ($error !== 0) {
85  $data['manError']['isError'] = true;
86  $data['manError']['errorBody'] = $error;
87  return $data;
88  }
89  if ($one !== false) {
90  return $this->getSingleData($userId, $json, $one);
91  }
92  return $this->getMultiData($userId, $json);
93  }
94 
95  public function getSingleData($userId, $json, $one)
96  {
97  $urls = "";
98  $links = "";
99  $data = array();
100  $dataProvider = CJSON::decode($json);
101  $data[0] = $dataProvider["itemsList"][0]["itemObject"];
102  $userDataStr = $data[0]['userId'];
103  $userData = Dictionary::getUserData($data[0]['userId']);
104  foreach ($userData as $v) {
105  $userDataStr .= ' / ' . $v;
106  }
107  foreach ($data[0]["urls"] as $url) {
108  if ($url != '')
109  $urls = $urls . $url["url"] . "<br>";
110  $links = $links . '<a class = "ext" target = "_blank" href="' . $url["url"] . '">' . $url["url"] . '</a>' . "<br>";
111  }
112  $data[0]["urls"] = $urls;
113  $data[0]["urls_act"] = $links;
114  $data[0]["state_str"] = $this->getState($userId, 0);
115  $data[0]["urlType_str"] = $this->getURLType($userId, 0);
116  $data[0]["fetchType_str"] = $this->getFetchType($userId, 0);
117  $data[0]["userData"] = $userDataStr;
118  return new CArrayDataProvider($data);
119  }
120 
121  public function getState($userId, $n)
122  {
123  $json = $this->getResponse($userId, $this->responseFile, true);
124  $data = CJSON::decode($json);
125  if(isset($data['itemsList'][0]['itemObject'][$n])){
126  $state = $data['itemsList'][0]['itemObject'][$n]['state'];
127  } else {
128  $state = $data['itemsList'][0]['itemObject']['state'];
129  }
130  switch ($state) {
131  case '1':
132  return 'Active';
133  break;
134  case '2':
135  return 'Disabled';
136  break;
137  case '3':
138  return 'Suspended';
139  break;
140  }
141  }
142 
143  public function getURLType($userId, $n)
144  {
145  $json = $this->getResponse($userId, $this->responseFile, true);
146  $data = CJSON::decode($json);
147  if(isset($data['itemsList'][0]['itemObject'][$n])){
148  $state = $data['itemsList'][0]['itemObject'][$n]['urlType'];
149  } else {
150  $state = $data['itemsList'][0]['itemObject']['urlType'];
151  }
152  switch ($state) {
153  case '0':
154  return 'Regular';
155  break;
156  case '1':
157  return 'Single';
158  break;
159  case '3':
160  return 'All';
161  break;
162  }
163  }
164 
165  public function getFetchType($userId, $n)
166  {
167  $json = $this->getResponse($userId, $this->responseFile, true);
168  $data = CJSON::decode($json);
169  if(isset($data['itemsList'][0]['itemObject'][$n])){
170  $state = $data['itemsList'][0]['itemObject'][$n]['fetchType'];
171  } else {
172  $state = $data['itemsList'][0]['itemObject']['fetchType'];
173  }
174  switch ($state) {
175  case '1':
176  return 'Static';
177  break;
178  case '2':
179  return 'Dynamic';
180  break;
181  case '3':
182  return 'External';
183  break;
184  }
185  }
186 
187  public function getCounters($userId, $siteId)
188  {
189  $counters = array();
190  $counters['collectedURLs'] = 0;
191  $counters['deletedURLs'] = 0;
192  $counters['contents'] = 0;
193  $counters['errors'] = 0;
194  $counters['iterations'] = 0;
195  $counters['newURLs'] = 0;
196  $counters['size'] = 0;
197  $counters['resources'] = 0;
198  $counters['avgSpeed'] = 0;
199  $counters['avgSpeedCNT'] = 0;
200  $json = CJSON::decode($this->getResponse($userId));
201  foreach ($json['itemsList'] as $item) { // FIXME: add single site support
202  foreach ($item['itemObject'] as $siteItem) {
203  if ($siteItem['id'] == $siteId) {
204  $counters['collectedURLs'] += $siteItem['collectedURLs'];
205  $counters['deletedURLs'] += $siteItem['deletedURLs'];
206  $counters['contents'] += $siteItem['contents'];
207  $counters['errors'] += $siteItem['errors'];
208  $counters['iterations'] += $siteItem['iterations'];
209  $counters['newURLs'] += $siteItem['newURLs'];
210  $counters['size'] += $siteItem['size'];
211  $counters['resources'] += $siteItem['resources'];
212  $counters['avgSpeed'] += $siteItem['avgSpeed'];
213  $counters['avgSpeedCNT']++;
214  }
215  }
216  }
217  $counters['avgSpeed'] = round($counters['avgSpeed'] / $counters['avgSpeedCNT']);
218  return $counters;
219  }
220 
221  public function getMultiData($userId, $json)
222  {
223  $urls = "";
224  $links = "";
225  $data = array();
226  $dataProvider = CJSON::decode($json);
227  foreach ($dataProvider["itemsList"][0]["itemObject"] as $key => $item) {
228  foreach ($item["urls"] as $url) {
229  if ($url != '')
230  $urls = $urls . $url["url"] . "<br>";
231  $links = $links . '<a class = "ext" target = "_blank" href="' . $url["url"] . '">' . $url["url"] . '</a>' . "<br>";
232  }
233  $item["urls"] = $urls;
234  $item["urls_act"] = $links;
235  $item["nn"] = $key;
236  $item["state_str"] = $this->getState($userId, $key);
237  $urls = "";
238  $links = "";
239  $data[] = $item;
240 
241  }
242  $data['manError']['isError'] = false;
243  return $data;
244  }
245 
246  public function createRequest($pattern, $state, $limit, $uid, $pN, $sortBy, $sortDirection)
247  {
248  $uid = Dictionary::getUIDByString($uid);
249  $from = $pN*$limit-$limit;
250  if ($state == "all") {
251  $state = "";
252  } else {
253  $state = "AND `sites`.`State`=$state";
254  }
255  $criterions = array(
256  'url' => null,
257  'criterions' => array(
258  "WHERE" =>
259  "`sites`.`User_Id`=$uid AND `sites`.`Id`=`sites_urls`.`Site_Id` $state AND `sites_urls`.`URL` LIKE '%$pattern%' GROUP BY sites.Id",
260  "LIMIT" => "$from, $limit",
261  "ORDER BY" => "sites.$sortBy $sortDirection"
262  )
263  );
264  $json = json_encode($criterions);
265  Logger::log("Opertion ->" . $this->opLog, false);
266  Logger::log("Request -> \n" . $json, true);
267  $path = Yii::app()->getBasePath() . '/json_temp/';
268  $reqFile = tempnam($path, '');
269  $file = fopen($reqFile, 'w');
270  $this->requestFile = $reqFile;
271  fwrite($file, $json);
272  }
273 
274 
275 
276  public function getFilters($userId, $file, $n = 0)
277  {
278  $counter = 0;
279  $filtersItems = array();
280  $json = $this->getResponse($userId, $file, true);
281  $data = CJSON::decode($json);
282  $filters = $data['itemsList'][0]['itemObject']['filters'];
283  foreach ($filters as $item) {
284  $item["n"] = $counter++;
285  $item["nn"] = $n;
286  $filtersItems[] = $item;
287  }
288  $filtersProvider = new CArrayDataProvider($filtersItems, array(
289  'keyField' => 'type'
290  ));
291  return $filtersProvider;
292  }
293 
294  public function getProperties($userId, $file, $n)
295  {
296  $propsProvider = array();
297  $json = $this->getResponse($userId, $file, true);
298  $data = CJSON::decode($json);
299  $props = $data['itemsList'][0]['itemObject']['properties'];
300  $propsProvider = $props;
301  return new CArrayDataProvider($propsProvider, array(
302  'keyField' => 'name',
303  'pagination' => false
304  ));
305  }
306 
307  public function getLimits($userId, $file, $n)
308  {
309  $limitsProvider = array();
310  $json = $this->getResponse($userId, $file, true);
311  $data = CJSON::decode($json);
312  $limitsProvider[] = array(
313  'limit_name' => 'Priority',
314  'limit_value' => $data['itemsList'][0]['itemObject']['priority'],
315  'limit_name_f' => 'priority'
316  );
317  $limitsProvider[] = array(
318  'limit_name' => 'Max Urls',
319  'limit_value' => $data['itemsList'][0]['itemObject']['maxURLs'],
320  'limit_name_f' => 'maxURLs'
321  );
322  $limitsProvider[] = array(
323  'limit_name' => 'Max resources',
324  'limit_value' => $data['itemsList'][0]['itemObject']['maxResources'],
325  'limit_name_f' => 'maxResources'
326  );
327  $limitsProvider[] = array(
328  'limit_name' => 'Max Errors',
329  'limit_value' => $data['itemsList'][0]['itemObject']['maxErrors'],
330  'limit_name_f' => 'maxErrors'
331  );
332  $limitsProvider[] = array(
333  'limit_name' => 'Max resource size',
334  'limit_value' => $data['itemsList'][0]['itemObject']['maxResourceSize'],
335  'limit_name_f' => 'maxResourceSize'
336  );
337  $limitsProvider[] = array(
338  'limit_name' => 'Max URLs from page',
339  'limit_value' => $data['itemsList'][0]['itemObject']['maxURLsFromPage'],
340  'limit_name_f' => 'maxURLsFromPage'
341  );
342  $limitsProvider[] = array(
343  'limit_name' => 'Processing delay, ms',
344  'limit_value' => $data['itemsList'][0]['itemObject']['processingDelay'],
345  'limit_name_f' => 'processingDelay'
346  );
347  $limitsProvider[] = array(
348  'limit_name' => 'Request delay, ms',
349  'limit_value' => $data['itemsList'][0]['itemObject']['requestDelay'],
350  'limit_name_f' => 'requestDelay'
351  );
352  $limitsProvider[] = array(
353  'limit_name' => 'HTTP timeout, ms',
354  'limit_value' => $data['itemsList'][0]['itemObject']['httpTimeout'],
355  'limit_name_f' => 'httpTimeout'
356  );
357  return new CArrayDataProvider($limitsProvider, array(
358  'keyField' => 'limit_name',
359  'pagination' => false
360  ));
361  }
362 
363  public function getUserId()
364  {
365  if (!isset(Yii::app()->request->cookies['UserId'])) {
366  $userId = $this->generateUID();
367  $cookie = new CHttpCookie('UserId', $userId);
368  $cookie->expire = time() + 31536000;
369  Yii::app()->request->cookies['UserId'] = $cookie;
370  return $userId;
371  }
372  return Yii::app()->request->cookies['UserId']->value;
373  }
374 
375  public function generateUID()
376  {
377  return md5(Yii::app()->session->sessionID);
378  }
379 
380  public function getErrorsTypes($userId, $file, $n)
381  {
382  $eTypes = array();
383  $json = $this->getResponse($userId, $file, true);
384  $data = CJSON::decode($json);
385  $mask = $data['itemsList'][0]['itemObject']['errorMask'];
386  for ($power = 0; $power <= 64; $power++) {
387  $error = $mask & pow(2, $power);
388  if ($error != 0) {
389  $eTypes[] = $power;
390  }
391  }
392  $errors = Dictionary::getErrorsByMask($eTypes);
393  return new CArrayDataProvider($errors, array(
394  'keyField' => 'errorType',
395  'pagination' => false
396  ));
397  }
398 
399  public function checkForErrors($json)
400  {
401  $data = CJSON::decode($json);
402  if (is_null($data)) {
403  return true;
404  }
405  if (!is_array($data["itemsList"][0]["itemObject"])) {
406  return true;
407  }
408  // if (empty($data["itemsList"][0]["itemObject"])) {
409  // echo "shit";
410  // return true;
411  // }
412  return false;
413  }
414  public function rmTmpData()
415  {
416  unlink($this->requestFile);
417  unlink($this->responseFile);
418  }
419  public function integrityCheck($siteId)
420  {
421  $ret = array();
422  $urls = array(
423  'Urls' => $this->urlFetch($siteId),
424  'Sites' => $this->siteStatus($siteId),
425  );
426 
427  $absInSites = array_diff($urls['Urls'], $urls['Sites']);
428  $absInUrls = array_diff($urls['Sites'], $urls['Urls']);
429  if (empty($absInSites) && empty($absInUrls)){
430  return array(
431  'code'=>0
432  );
433  }
434  if (!empty($absInSites) && !isset($absInSites['empty'])) {
435  foreach ($absInSites as $k => $v) {
436  $ret[] = array(
437  'URLs' => $v,
438  'arrow' => '<div class = "arrows">&#8680;</div>',
439  'Sites' => 'Absent'
440  );
441  }
442  }
443  if (!empty($absInUrls) && !isset($absInUrls['empty'])) {
444  foreach ($absInUrls as $k => $v) {
445  $ret[] = array(
446  'Sites' => $v,
447  'arrow' => '<div class = "arrows">&#8680;</div>',
448  'URLs' => 'Absent'
449 
450  );
451  }
452  }
453  return $ret;
454  }
455  public function syncUrls()
456  {
457 
458  }
459  public function siteStatus($siteId)
460  {
461  $siteStatus = new SitesView();
462  $siteStatus->createRequestSingle(false, $siteId, true);
463  $json = $siteStatus->commandStatus(false);
464  $jsonDecoded = CJSON::decode($json);
465  if (empty($jsonDecoded['itemsList'][0]['itemObject']['urls'])) {
466  $URLs['empty'] = 'empty';
467  }
468  foreach ($jsonDecoded['itemsList'][0]['itemObject']['urls'] as $url) {
469  $URLs[$url['urlMd5']] = $url['url'];
470  }
472  return $URLs;
473  }
474  public function urlFetch($siteId)
475  {
476  $urlFetch = new UrlsView();
477  $userId = Yii::app()->user->id;
478  $json = $urlFetch->fetch($userId, array(
479  'siteId'=>$siteId,
480  'onlyRoot'=>true,
481  'pN'=>1,
482  'limit'=>MAX_URLS_INTEGRITY_CHECK,
483  'status' =>'',
484  'sortBy' => '',
485  'sortDirection' => '',
486  ));
487  unset($json['manError']);
488  if (empty($json)) {
489  $URLs['empty'] = 'empty';
490  }
491  foreach ($json as $url) {
492  $URLs[$url['urlMd5']] = $url['url'];
493  }
495  return $URLs;
496  }
497 }