/application/controllers/ProcessController.php

https://bitbucket.org/yeahyahoo/yeahyahoo.bitbucket.org · PHP · 178 lines · 142 code · 30 blank · 6 comment · 29 complexity · 6e416f7e1a8284e9190c84e30a55ccaa MD5 · raw file

  1. <?php
  2. class ProcessController extends Zend_Controller_Action {
  3. //// for pagination, returns rows per page, prev,next buttons.
  4. //// currently, only used for dataset archive tab
  5. public function getpageAction() {
  6. if ($this->getRequest()->isPost()) {
  7. $formData = $this->getRequest()->getPost();
  8. $rows = intval($formData['rows']);
  9. $page = intval($formData['page']);
  10. if ($page == 0){
  11. $page = 1;
  12. }
  13. $type = $formData['type'];
  14. $process_model = new Model_Processes();
  15. $process_cursor = $process_model->find(array('type' => $type,'status'=>array('$in'=>array('done','error'))))
  16. ->limit($rows)->skip(($page - 1) * $rows)->sort(array('start_time'=>-1));
  17. if (!$process_cursor) {
  18. $this->getHelper('json')->sendJson(array(
  19. 'result' => false,
  20. ));
  21. }
  22. if ($page > 1) {
  23. $prev = $page - 1;
  24. } else {
  25. $prev = 0;
  26. }
  27. if ($page * $rows < $process_cursor->count()) {
  28. $next = $page + 1;
  29. } else {
  30. $next = 1;
  31. }
  32. $program_model = new Model_Programs();
  33. $processes = array();
  34. if ($type == 'dataset') {
  35. $model = new Model_Datasets();
  36. foreach ($process_cursor as $proc) {
  37. $cursor = $model->findById($proc['dataset_id']);
  38. $program = $program_model->findById($proc['program_id']);
  39. $proc['dataset_name'] = $cursor['name'];
  40. $proc['program_name'] = $program['name'];
  41. $proc['formated_start_time'] = date('m/d/Y - h:i:s', $proc['start_time']->sec);
  42. if($proc['status']=='done'){
  43. $sec_diff = $proc['end_time']->sec - $proc['start_time']->sec;
  44. $min_str = round($sec_diff/60,1);
  45. $proc['duration'] = $min_str;
  46. }else{
  47. $proc['duration'] = '';
  48. }
  49. $processes[] = $proc;
  50. }
  51. }
  52. $this->getHelper('json')->sendJson(array(
  53. 'processes' => $processes,
  54. 'prev' => $prev,
  55. 'page' => $page,
  56. 'next' => $next,
  57. 'result' => true,
  58. ));
  59. }
  60. }
  61. public function getactiveAction() {
  62. $type = $this->getRequest()->getParam('type');
  63. $process_model = new Model_Processes();
  64. /// receive user
  65. $auth = Zend_Auth::getInstance();
  66. if ($auth->hasIdentity()) {
  67. $user = $auth->getIdentity();
  68. } else {
  69. $this->getHelper('json')->sendJson(array("result" => false));
  70. }
  71. $process_cursor = $process_model->find(array('user_id' => $user['id'], 'type' => $type ,'status'=> 'active'))->sort(array('start_time'=>-1));
  72. $processes = array();
  73. if ($type == 'dataset') {
  74. $model = new Model_Datasets();
  75. $program_model = new Model_Programs();
  76. foreach ($process_cursor as $proc) {
  77. $cursor = $model->findById($proc['dataset_id']);
  78. $proc['id'] = utf8_encode($proc['_id']);
  79. $proc['dataset_name'] = $cursor['name'];
  80. $proc['formated_start_time'] = date('m/d/Y - h:i:s', $proc['start_time']->sec);
  81. $program = $program_model->findById($proc['program_id']);
  82. $proc['program_name'] = $program['name'];
  83. $processes[] = $proc;
  84. }
  85. } else {
  86. $model = new Model_Classifiers();
  87. foreach ($process_cursor as $proc) {
  88. $cursor = $model->findById($proc['classifier_id']);
  89. $proc['id'] = utf8_encode($proc['_id']);
  90. $proc['classifier_name'] = $cursor['name'];
  91. $proc['formated_start_time'] = date('M/d/Y - h:i:s', $proc['start_time']->sec);
  92. $processes[] = $proc;
  93. }
  94. }
  95. $this->getHelper('json')->sendJson(array(
  96. "processes" => $processes,
  97. "result" => true
  98. ));
  99. }
  100. public function stopprocessAction() {
  101. if ($this->getRequest()->isPost()) {
  102. $formData = $this->getRequest()->getPost();
  103. /// Check for authentication
  104. $auth = Zend_Auth::getInstance();
  105. if ($auth->hasIdentity()) {
  106. $user = $auth->getIdentity();
  107. } else {
  108. $this->getHelper('json')->sendJson(array("result" => false));
  109. }
  110. $process_model = new Model_Processes();
  111. $process = $process_model->findById($formData['id']);
  112. $pid = intval($process['pid']);
  113. if ($process['status'] == 'active' && $pid > 0) {
  114. exec("kill $pid");
  115. if (!$process_model->delete($formData['id'])) {
  116. $this->getHelper('json')->sendJson(array("result" => false));
  117. }
  118. if($process['type'] == 'classifier'){
  119. $classifier_model = new Model_Classifiers();
  120. $classifier_model->update(array("_id" => new MongoId($process['classifier_id'])), array('$pull' => array('processes' => $formData['id'])));
  121. //// clean up resources ( json, images)
  122. $user_output_path = APPLICATION_PATH . "/../" . "/user_data/" . $user['id'] . "/out/";
  123. $json_file_path = $user_output_path . $formData['id'] . ".json";
  124. if (file_exists($json_file_path)) {
  125. $decoded_json = json_decode(file_get_contents($json_file_path));
  126. foreach ($decoded_json as $output) {
  127. if ($output->type == 'image_plot') {
  128. $image_path = ($user_output_path . $output->data->name);
  129. if(file_exists($image_path)){
  130. unlink($image_path);
  131. }
  132. }
  133. }
  134. unlink($json_file_path);
  135. }
  136. }
  137. //// set dataset to not being processed
  138. $dataset_model = new Model_Datasets();
  139. $dataset_model->update(array("_id" => new MongoId($process['dataset_id'])), array('$set' =>
  140. array('processing' => false)
  141. ));
  142. $this->getHelper('json')->sendJson(array("result" => true));
  143. }
  144. }
  145. $this->getHelper('json')->sendJson(array("result" => false));
  146. }
  147. }
  148. ?>