tripal_bulk_loader.drush.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * Implements drush integration for this module
  5. *
  6. * @ingroup tripal_bulk_loader
  7. */
  8. /**
  9. * Implements hook_drush_command().
  10. *
  11. * @ingroup tripal_bulk_loader
  12. */
  13. function tripal_bulk_loader_drush_command() {
  14. $items = [];
  15. $items['tripal-loader-progress'] = [
  16. 'description' => dt('Display the progress of any running tripal bulk loading job.'),
  17. 'aliases' => ['trpload-%'],
  18. ];
  19. $items['tripal-loader-view'] = [
  20. // used by drush help
  21. 'description' => dt('Returns the status/details of the specified bulk loading job.'),
  22. 'arguments' => [
  23. 'nid' => dt('The Node ID of the bulk Loading Job'),
  24. ],
  25. 'examples' => [
  26. 'Standard Example' => 'drush tripal-loader-view 5433',
  27. ],
  28. 'aliases' => ['trpload-view'],
  29. ];
  30. $items['tripal-loader-cancel'] = [
  31. // used by drush help
  32. 'description' => dt('Cancels the specified bulk loading job.'),
  33. 'arguments' => [
  34. 'nid' => dt('The Node ID of the bulk Loading Job'),
  35. ],
  36. 'examples' => [
  37. 'Standard Example' => 'drush tripal-loader-cancel 5433',
  38. ],
  39. 'aliases' => ['trpload-cncl'],
  40. ];
  41. $items['tripal-loader-submit'] = [
  42. // used by drush help
  43. 'description' => dt('Submit or Re-submit the given bulk loading job.'),
  44. 'arguments' => [
  45. 'nid' => dt('The Node ID of the bulk Loading Job'),
  46. ],
  47. 'examples' => [
  48. 'Standard Example' => 'drush tripal-loader-submit 5433',
  49. ],
  50. 'aliases' => ['trpload-sbmt'],
  51. ];
  52. $items['tripal-loader-revert'] = [
  53. // used by drush help
  54. 'description' => dt('Revert the records loaded by the last run of the specified loading job. This is only available if the specified loading job is keeping track of inserted IDs.'),
  55. 'arguments' => [
  56. 'nid' => dt('The Node ID of the bulk Loading Job'),
  57. ],
  58. 'examples' => [
  59. 'Standard Example' => 'drush tripal-loader-revert 5433',
  60. ],
  61. 'aliases' => ['trpload-revert'],
  62. ];
  63. return $items;
  64. }
  65. /**
  66. * Code ran for the tripal-loader-progress drush command
  67. * Display the progress of any running tripal bulk loading job.
  68. *
  69. * @ingroup tripal_bulk_loader
  70. */
  71. function drush_tripal_bulk_loader_tripal_loader_progress() {
  72. // determine the progress of any loading jobs
  73. $sql = "SELECT t.loader_name, t.file, t.job_id FROM {tripal_bulk_loader} t WHERE job_status='Loading...'";
  74. $resource = db_query($sql);
  75. while ($r = db_fetch_object($resource)) {
  76. if ($r->job_id) {
  77. $progress = tripal_bulk_loader_progess_file_get_progress($r->job_id);
  78. if ($progress->num_records > 0 AND $progress->total_percent < 100) {
  79. drush_print(
  80. $r->loader_name . "\n"
  81. . str_repeat("-", 40) . "\n"
  82. . "File:" . $r->file . "\n"
  83. . "Current Constant Set:\n"
  84. . "\tLines processed: " . $progress->num_lines . "\n"
  85. . "\tRecord Inserted: " . $progress->num_records . "\n"
  86. . "\tPercent Complete: " . $progress->percent_file . "\n"
  87. . "Number of Constant Sets fully loaded: " . $progress->num_constant_sets_loaded . "\n"
  88. . "Job Percent Complete: " . $progress->total_percent . "\n"
  89. );
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Returns the status/details of the specified bulk loading job.
  96. *
  97. * @param $nid
  98. * The Node ID of the bulk Loading Job
  99. *
  100. * @ingroup tripal_bulk_loader
  101. */
  102. function drush_tripal_bulk_loader_tripal_loader_view($nid) {
  103. $node = node_load($nid);
  104. $author = user_load($node->uid);
  105. drush_print("Job Name: " . $node->loader_name);
  106. drush_print("Submitted By: " . $author->name);
  107. drush_print("Job Creation Date: " . format_date($node->created));
  108. drush_print("Last Updated: " . format_date($node->changed));
  109. drush_print("Template Name: " . $node->template->name);
  110. drush_print("Data File: " . $node->file);
  111. drush_print("Job Status: " . $node->job_status);
  112. }
  113. /**
  114. * Cancels the specified bulk loading job.
  115. *
  116. * @param $nid
  117. * The Node ID of the bulk Loading Job
  118. *
  119. * @ingroup tripal_bulk_loader
  120. */
  121. function drush_tripal_bulk_loader_tripal_loader_cancel($nid) {
  122. $node = node_load($nid);
  123. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Job Cancelled', $node->nid);
  124. tripal_cancel_job($node->job_id, FALSE);
  125. }
  126. /**
  127. * Submit or Re-submit the given bulk loading job.
  128. *
  129. * @param $nid
  130. * The Node ID of the bulk Loading Job
  131. *
  132. * @ingroup tripal_bulk_loader
  133. */
  134. function drush_tripal_bulk_loader_tripal_loader_submit($nid) {
  135. global $user;
  136. if ($node->job_id) {
  137. //Re-submit Tripal Job
  138. tripal_rerun_job($node->job_id);
  139. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
  140. }
  141. else {
  142. //Submit Tripal Job
  143. $node = node_load($nid);
  144. $job_args[1] = $nid;
  145. if (is_readable($node->file)) {
  146. $fname = basename($node->file);
  147. $job_id = tripal_add_job("Bulk Loading Job: $fname", 'tripal_bulk_loader', 'tripal_bulk_loader_load_data', $job_args, $user->uid);
  148. // add job_id to bulk_loader node
  149. $success = db_query("UPDATE {tripal_bulk_loader} SET job_id=%d WHERE nid=%d", $job_id, $nid);
  150. // change status
  151. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Submitted to Queue', $nid);
  152. }
  153. else {
  154. drupal_set_message(t("Can not open %file. Job not scheduled.", ['%file' => $node->file]));
  155. }
  156. }
  157. }
  158. /**
  159. * Revert the records loaded by the last run of the specified loading job. This
  160. * is only available if the specified loading job is keeping track of inserted
  161. * IDs.
  162. *
  163. * @param $nid
  164. * The Node ID of the bulk Loading Job
  165. *
  166. * @ingroup tripal_bulk_loader
  167. */
  168. function drush_tripal_bulk_loader_tripal_loader_revert($nid) {
  169. // Remove the records from the database that were already inserted
  170. $resource = db_query('SELECT * FROM {tripal_bulk_loader_inserted} WHERE nid=%d ORDER BY tripal_bulk_loader_inserted_id DESC', $nid);
  171. while ($r = db_fetch_object($resource)) {
  172. $ids = preg_split('/,/', $r->ids_inserted);
  173. db_query('DELETE FROM {%s} WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted);
  174. $result = db_fetch_object(db_query('SELECT true as present FROM {%s} WHERE %s IN (%s)', $r->table_inserted_into, $r->table_primary_key, $r->ids_inserted));
  175. if (!$result->present) {
  176. drush_print('Successfully Removed data Inserted into the ' . $r->table_inserted_into . ' table.');
  177. db_query('DELETE FROM {tripal_bulk_loader_inserted} WHERE tripal_bulk_loader_inserted_id=%d', $r->tripal_bulk_loader_inserted_id);
  178. }
  179. else {
  180. drush_print('Unable to remove data Inserted into the ' . $r->table_inserted_into . ' table!');
  181. }
  182. }
  183. // reset status
  184. db_query("UPDATE {tripal_bulk_loader} SET job_status='%s' WHERE nid=%d", 'Reverted -Data Deleted', $nid);
  185. }