tripal_bulk_loader.drush.inc 6.5 KB

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