Skip to content

Commit

Permalink
Combine all the separate importer views into a single one.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawngee committed Nov 5, 2018
1 parent f1fd408 commit c7c4569
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 418 deletions.
6 changes: 5 additions & 1 deletion classes/Tasks/BackgroundProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,9 @@ public function cancel_process() {
*/
abstract public function task( $item );

abstract public static function cancelAll();
/**
* Override in subclasses to handle this process being cancelled.
*/
public static function cancelAll() {
}
}
10 changes: 3 additions & 7 deletions classes/Tasks/BatchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public static function boot() {
});

add_action('ilab_media_tools_run_batch_hook', function(){
Logger::info('Running ilab_media_tools_run_batch_hook');
BatchManager::instance()->dispatchBatchesIfNeeded();
});

Expand Down Expand Up @@ -272,23 +271,23 @@ public function stats($batch) {
if (($totalTime > 0) && ($current > 1)) {
$postsPerSecond = ($totalTime / ($current - 1));
if ($postsPerSecond > 0) {
$postsPerMinute = 60 / $postsPerMinute;
$postsPerMinute = 60 / $postsPerSecond;
$eta = ($total - $current) / $postsPerMinute;
}
}

return [
'running' => $this->status($batch),
'current' => $current,
'file' => $this->currentFile($batch),
'currentFile' => $this->currentFile($batch),
'total' => $total,
'totalTime' => $totalTime,
'lastTime' => $this->lastTime($batch),
'lastUpdate' => $this->lastUpdate($batch),
'eta' => $eta,
'progress' => $progress,
'postsPerMinute' => $postsPerMinute,
'cancelling' => $this->shouldCancel($batch)
'shouldCancel' => $this->shouldCancel($batch)
];
}

Expand Down Expand Up @@ -347,12 +346,9 @@ public function addToBatchAndRun($batch, $postIDs) {
public function dispatchBatchesIfNeeded() {
foreach(static::$batchClasses as $batch => $batchClass) {
if ($this->status($batch)) {
Logger::info("ilab_media_tools_run_batch_hook - $batch running");
$lastRun = $this->lastRun($batch);
Logger::info("ilab_media_tools_run_batch_hook - Last run ".(time() - $lastRun)." seconds ago.");

if ((time() - $lastRun) > 60) {
Logger::info("ilab_media_tools_run_batch_hook - Restarting");
$this->setLastRun($batch, time());

/** @var BackgroundProcess $process */
Expand Down
16 changes: 13 additions & 3 deletions classes/Tools/Rekognition/RekognitionTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,20 @@ public function renderImporter() {
$stats['total'] = count($attachments);
}

$stats['running'] = ($stats['running']) ? 'running' : 'idle';
$stats['status'] = ($stats['running']) ? 'running' : 'idle';
$stats['enabled'] = $this->enabled();

echo View::render_view( 'rekognizer/ilab-rekognizer-processor.php', $stats);
$stats['title'] = 'Rekognizer Importer';
$stats['disabledText'] = 'enable Rekognizer';
$stats['instructions'] = View::render_view('importer/rekognition-instructions.php', []);
$stats['commandLine'] = 'wp rekognition process';
$stats['commandTitle'] = 'Process Images';
$stats['cancelCommandTitle'] = 'Cancel Import';
$stats['cancelAction'] = 'ilab_rekognizer_cancel_process';
$stats['startAction'] = 'ilab_rekognizer_process_images';
$stats['progressAction'] = 'ilab_rekognizer_process_progress';

echo View::render_view('importer/importer.php', $stats);
}

/**
Expand Down Expand Up @@ -578,7 +588,7 @@ public function processImages() {
*/
public function processProgress() {
$stats = BatchManager::instance()->stats('rekognizer');
$stats['running'] = ($stats['running']) ? 'running' : 'idle';
$stats['status'] = ($stats['running']) ? 'running' : 'idle';

header('Content-type: application/json');
echo json_encode($stats);
Expand Down
32 changes: 26 additions & 6 deletions classes/Tools/Storage/StorageTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -1618,10 +1618,20 @@ public function renderRegenerator() {
$stats['total'] = count($attachments);
}

$stats['running'] = ($stats['running']) ? 'running' : 'idle';
$stats['status'] = ($stats['running']) ? 'running' : 'idle';
$stats['enabled'] = $this->enabled();

echo View::render_view('storage/regenerator.php', $stats);
$stats['title'] = 'Regenerate Thumbnails';
$stats['instructions'] = View::render_view('importer/regeneration-instructions.php', []);
$stats['disabledText'] = 'enable Storage';
$stats['commandLine'] = 'wp mediacloud regenerate';
$stats['commandTitle'] = 'Regenerate Thumbnails';
$stats['cancelCommandTitle'] = 'Cancel Regeneration';
$stats['cancelAction'] = 'ilab_media_cloud_cancel_regenerate';
$stats['startAction'] = 'ilab_media_cloud_regenerate_files';
$stats['progressAction'] = 'ilab_media_cloud_regenerate_progress';

echo View::render_view('importer/importer.php', $stats);
}

/**
Expand All @@ -1630,7 +1640,7 @@ public function renderRegenerator() {
public function regenerateProgress() {
$stats = BatchManager::instance()->stats('thumbnails');

$stats['running'] = ($stats['running']) ? 'running' : 'idle';
$stats['status'] = ($stats['running']) ? 'running' : 'idle';
$stats['enabled'] = $this->enabled();

header('Content-type: application/json');
Expand Down Expand Up @@ -1691,10 +1701,20 @@ public function renderImporter() {
$stats['total'] = count($attachments);
}

$stats['running'] = ($stats['running']) ? 'running' : 'idle';
$stats['status'] = ($stats['running']) ? 'running' : 'idle';
$stats['enabled'] = $this->enabled();

echo View::render_view('storage/ilab-storage-importer.php', $stats);
$stats['title'] = 'Storage Importer';
$stats['instructions'] = View::render_view('importer/storage-importer-instructions.php', []);
$stats['disabledText'] = 'enable Storage';
$stats['commandLine'] = 'wp mediacloud import';
$stats['commandTitle'] = 'Import Uploads';
$stats['cancelCommandTitle'] = 'Cancel Import';
$stats['cancelAction'] = 'ilab_s3_cancel_import';
$stats['startAction'] = 'ilab_s3_import_media';
$stats['progressAction'] = 'ilab_s3_import_progress';

echo View::render_view('importer/importer.php', $stats);
}

/**
Expand All @@ -1703,7 +1723,7 @@ public function renderImporter() {
public function importProgress() {
$stats = BatchManager::instance()->stats('storage');

$stats['running'] = ($stats['running']) ? 'running' : 'idle';
$stats['status'] = ($stats['running']) ? 'running' : 'idle';
$stats['enabled'] = $this->enabled();

header('Content-type: application/json');
Expand Down
56 changes: 33 additions & 23 deletions views/storage/regenerator.php → views/importer/importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
}

.s3-importer-progress-container {
display: relative;
position: relative;
width: 100%;
height: 18px;
background: lightgray;
border-radius: 8px;
height: 32px;
background: #AAA;
border-radius: 16px;
overflow: hidden;
}

Expand Down Expand Up @@ -46,41 +46,51 @@
#s3-timing-stats {
display: none;
}

#s3-importer-status-text {
position: absolute;
left: 16px; top:0px; bottom: 0px; right: 16px;
display: flex;
align-items: center;
color: white;
font-weight: bold;
}
</style>
<div class="settings-container">
<header>
<img src="{{ILAB_PUB_IMG_URL}}/icon-cloud.svg">
<h1>Regenerate Thumbnails</h1>
<h1>{{$title}}</h1>
</header>
<div class="settings-body">
<div id="s3-importer-instructions" {{($status=="running") ? 'style="display:none"':''}}>
<p>This tool will rebuild all of the thumbnails for all of your images.</p>
<p>Depending on the number of items you have, this could take anywhere from a minute to several hours. This process runs in the background until it's finished. Once you've started the process, please check this page for progress.</p>
<p>If you don't have any of the source files on your WordPress server, this will download what it can from your storage service. Obviously this can be very slow going if you are processsing a lot of images. If you only want to regenerate thubmnails for a select group of images, use the bulk action in the media library's list view.</p>
{{$instructions}}
<div class="wp-cli-callout">
<h3>Using WP-CLI</h3>
<p>You can run this importer process from the command line using WP-CLI:</p>
<code>
wp mediacloud regenerate
{{$commandLine}}
</code>
</div>
<div style="margin-top: 2em;">
<?php if($enabled): ?>
<a href="#" class="ilab-ajax button button-primary">Regenerate Thumbnails</a>
<a href="#" class="ilab-ajax button button-primary">{{$commandTitle}}</a>
<?php else: ?>
<strong class="tool-disabled">Please <a href="admin.php?page=media-tools-top">enable storage</a> before using this tool.</strong>
<strong class="tool-disabled">Please <a href="admin.php?page=media-tools-top">{{$disabledText}}</a> before using this tool.</strong>
<?php endif ?>
</div>
</div>
<div id="s3-importer-progress" {{($status!="running") ? 'style="display:none"':''}}>
<div id="s3-importer-progress-text">
<div id="s3-importer-progress-text">
<p id="s3-importer-cancelling-text" style="display:{{($shouldCancel) ? 'block':'none'}}">Cancelling ... This may take a minute ...</p>
<p id="s3-importer-status-text" style="display:{{($shouldCancel) ? 'none':'block'}}">The thumbnail regeneration is currently running. Regenerating thumbnails for '<span id="s3-importer-current-file">{{$currentFile}}</span>' (<span id="s3-importer-current">{{$current}}</span> of <span id="s3-importer-total">{{$total}}</span>). <span id="s3-timing-stats"><span id="s3-timing-ppm">{{number_format($postsPerMinute, 1)}}</span> posts per minute, ETA: <span id="s3-timing-eta">{{number_format($eta, 2)}}</span>.</span></p>
</div>
<div class="s3-importer-progress-container">
<div class="s3-importer-progress-container">
<div id="s3-importer-progress-bar"></div>
<div id="s3-importer-status-text" style="visibility:{{($shouldCancel) ? 'hidden':'visible'}}">
<div>Processing '<span id="s3-importer-current-file">{{$currentFile}}</span>' (<span id="s3-importer-current">{{$current}}</span> of <span id="s3-importer-total">{{$total}}</span>). <span id="s3-timing-stats"><span id="s3-timing-ppm">{{number_format($postsPerMinute, 1)}}</span> posts per minute, ETA: <span id="s3-timing-eta">{{number_format($eta, 2)}}</span>.</span></div>
</div>
</div>
<button id="s3-importer-cancel-import" class="button button-warning" title="Cancel">Cancel Regeneration</button>
<button id="s3-importer-cancel-import" class="button button-warning" title="Cancel">{{$cancelCommandTitle}}</button>
</div>
</div>
</div>
<script>
Expand All @@ -91,14 +101,14 @@
$('#s3-importer-cancel-import').on('click', function(e){
e.preventDefault();

if (confirm("Are you sure you want to cancel the thumbnail regeneration?")) {
if (confirm("Are you sure you want to cancel?")) {
var data={
action: 'ilab_media_cloud_cancel_regenerate'
action: '{{$cancelAction}}'
};

$.post(ajaxurl,data,function(response){
$('#s3-importer-cancelling-text').css({'display':'block'});
$('#s3-importer-status-text').css({'display':'none'});
$('#s3-importer-status-text').css({'visibility':'hidden'});
$('#s3-importer-cancel-import').attr('disabled', true);
console.log(response);
});
Expand All @@ -117,14 +127,14 @@
importing=true;

var data={
action: 'ilab_media_cloud_regenerate_files'
action: '{{$startAction}}'
};

$.post(ajaxurl,data,function(response){
if (response.status == 'running') {
$('#s3-importer-cancel-import').attr('disabled', false);
$('#s3-importer-cancelling-text').css({'display':'none'});
$('#s3-importer-status-text').css({'display':'block'});
$('#s3-importer-status-text').css({'visibility':'visible'});

$('#s3-importer-instructions').css({display: 'none'});
$('#s3-importer-progress').css({display: 'block'});
Expand All @@ -136,16 +146,16 @@
var checkStatus = function() {
if (importing) {
var data={
action: 'ilab_media_cloud_regenerate_progress'
action: '{{$progressAction}}'
};

$.post(ajaxurl,data,function(response){
if (response.shouldCancel) {
$('#s3-importer-cancelling-text').css({'display':'block'});
$('#s3-importer-status-text').css({'display':'none'});
$('#s3-importer-status-text').css({'visibility':'hidden'});
} else {
$('#s3-importer-cancelling-text').css({'display':'none'});
$('#s3-importer-status-text').css({'display':'block'});
$('#s3-importer-status-text').css({'visibility':'visible'});
}

if (response.status != 'running') {
Expand Down
3 changes: 3 additions & 0 deletions views/importer/regeneration-instructions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>This tool will rebuild all of the thumbnails for all of your images.</p>
<p>Depending on the number of items you have, this could take anywhere from a minute to several hours. This process runs in the background until it's finished. Once you've started the process, please check this page for progress.</p>
<p>If you don't have any of the source files on your WordPress server, this will download what it can from your storage service. Obviously this can be very slow going if you are processsing a lot of images. If you only want to regenerate thubmnails for a select group of images, use the bulk action in the media library's list view.</p>
8 changes: 8 additions & 0 deletions views/importer/rekognition-instructions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>This tool will run any images that have already been uploaded to S3 through Amazon Rekognizer.</p>
<p>Depending on the number of items you have, this could take anywhere from a minute to several hours. This process runs in the background until it's finished. Once you've started the process, please check this page for progress.</p>
<p><strong>Note:</strong></p>
<ol>
<li>You <strong>MUST HAVE</strong> Rekognizer enabled and working in <a href="admin.php?page=media-tools-top">Tools Settings</a> before running this task.</li>
<li>You <strong>MUST</strong> be using Amazon S3 for cloud storage.</li>
<li>Your S3 bucket must be in a region that Rekognition can be used in.</li>
</ol>
8 changes: 8 additions & 0 deletions views/importer/storage-importer-instructions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>This tool will import any media and documents you are currently hosting on this server to your cloud storage service.</p>
<p>Depending on the number of items you have, this could take anywhere from a minute to several hours. This process runs in the background until it's finished. Once you've started the process, please check this page for progress.</p>
<p><strong>Note:</strong></p>
<ol>
<li>If you upload any files while this process is running, you'll need to run this tool again after it finishes.</li>
<li>This process DOES NOT delete your files on your server, you'll have to do that yourself manually.</li>
<li>You <strong>MUST HAVE</strong> storage enabled and working in <a href="admin.php?page=media-tools-top">Tools Settings</a> before running this task.</li>
</ol>
Loading

0 comments on commit c7c4569

Please sign in to comment.