Skip to content

Commit

Permalink
Create a run all transformer area with ui updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 8, 2023
1 parent 317d13f commit e9caa85
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 16 deletions.
55 changes: 55 additions & 0 deletions app/Events/TransformersDoneEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Events;

use App\Models\Project;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TransformersDoneEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(
public Project $project,
public string $transformer_name)
{

}

public function broadcastWith(): array
{
return [
'project_id' => $this->project->id,
'transformer_name' => $this->transformer_name,
];
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('projects.'.$this->project->id),
];
}

/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'transformers.done';
}
}
33 changes: 33 additions & 0 deletions app/Http/Controllers/TransformersRunController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers;

use App\Jobs\RunTransformerJob;
use App\Models\Project;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
use Throwable;

class TransformersRunController extends Controller
{
public function run(Project $project)
{
$jobs = [];

foreach ($project->transformers->sortBy('order') as $transformer) {
$jobs[] = new RunTransformerJob($transformer);
}

$batch = Bus::batch($jobs)->then(function (Batch $batch) {
// Execute when batch is finished
})->catch(function (Batch $batch, Throwable $e) use ($project) {
logger('Errors with project '.$project->id);
})->finally(function (Batch $batch) {

})->dispatch();

request()->session()->flash('flash.banner', 'Running transformers');

return back();
}
}
35 changes: 35 additions & 0 deletions app/Jobs/RunTransformerJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Jobs;

use App\Events\TransformersDoneEvent;
use App\Models\Transformer;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class RunTransformerJob implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct(public Transformer $transformer)
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$this->transformer->run();

TransformersDoneEvent::dispatch($this->transformer->project, str($this->transformer->type->name)->headline());
}
}
Empty file modified artisan
100755 → 100644
Empty file.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"league/csv": "^9.0",
"opcodesio/log-viewer": "^2.4",
"openai-php/laravel": "^0.4.1",
"php-http/discovery": "^1.18",
"pusher/pusher-php-server": "^7.2",
"roach-php/core": "^2.0",
"roach-php/laravel": "^2.0",
Expand Down Expand Up @@ -78,6 +79,8 @@
"@php artisan key:generate --ansi"
],
"fix": "vendor/bin/pint",
"test": "vendor/bin/pest --bail --retry",
"horizon": "php artisan horizon:watch",
"stan": "vendor/bin/phpstan analyse --memory-limit 2G"
},
"extra": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions config/larachain.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
'name' => 'CsvTransformer',
'description' => 'Transform CSV Source to Document Chunks',
'class' => 'App\\Transformer\\Types\\CsvTransformer',
'background' => 'bg-gray-500',
'icon' => 'PhoneIcon',
'requires' => [
],
'active' => 1,
Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,26 @@ services:
- '${DB_USERNAME}'
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test:
- CMD
- redis-cli
- ping
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-pgsql:
driver: local
sail-redis:
driver: local
2 changes: 2 additions & 0 deletions resources/js/Components/GetIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ArrowsRightLeftIcon,
PhoneArrowUpRightIcon,
ArrowUpTrayIcon,
PhoneIcon,
DocumentIcon,
Bars4Icon,
GlobeAltIcon,
Expand All @@ -24,6 +25,7 @@ export default {
Bars4Icon,
DocumentIcon,
GlobeAltIcon,
PhoneIcon,
PhoneArrowUpRightIcon,
DocumentTextIcon,
ArrowDownTrayIcon,
Expand Down
53 changes: 44 additions & 9 deletions resources/js/Pages/Transformers/Partials/ExistingTransformers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
</div>
</Link>
<div class="flex justify-end w-full">

<RunButton
type="button"
@click="run(element)">
<Spinner
class="mr-0"
v-if="formRun.processing&& running === element.id"/>
<PlayIcon v-else class="w-4 h-4"/></RunButton>

<DeleteButton
type="button"
@click="deleteTransformer(element)">
Expand All @@ -41,6 +32,17 @@
</div>
</VueDraggableNext>

<div class="flex justify-between items-center mt-4">
<div>Run all Transformers</div>
<RunButton
type="button"
@click="runAll()">
<Spinner
class="mr-0"
v-if="formRunAll.processing || transformersRunning.length > transformersComlete.length"/>
<PlayIcon v-else class="w-4 h-4"/></RunButton>
</div>

</div>
<div v-else class="text-gray-400">👇Choose a Transformer below to get started</div>

Expand All @@ -65,6 +67,17 @@ const props = defineProps({
project: Object
})
onMounted(() => {
Echo.private(`projects.${props.project.id}`)
.listen('.transformers.done', (e) => {
console.log(e)
toast(e.transformer_name + " Run Complete 🥂");
//remove one item from transformersRunning array and do not worry about the id right now
transformersRunning.value.shift();
});
})
const form = useForm({});
const items = ref([])
Expand All @@ -75,6 +88,28 @@ onMounted(() => {
items.value = props.project.transformers;
})
const transformersRunning = ref([])
const transformersComlete = ref([])
const formRunAll = useForm({})
const runAll = () => {
transformersRunning.value = items.value;
transformersComlete.value = [];
formRunAll.post(route('transformers.run', {
project: props.project.id
}), {
errorBag: "transformerEmbedTransformer",
preserveScroll: true,
onError: params => {
toast.error("Error running job 🤦🏻‍")
},
onSuccess: params => {
toast("All Transformers Batched up 🏃‍♂️")
router.reload()
}
});
}
const deleteTransformer = (transformers) => {
form.delete(route('transformers.delete', {
Expand Down
12 changes: 12 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Http\Controllers\Sources\WebFileSourceController;
use App\Http\Controllers\Transformers\EmbedTransformerController;
use App\Http\Controllers\Transformers\PdfTransformerController;
use App\Http\Controllers\TransformersRunController;
use App\Models\Project;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -95,6 +96,17 @@ function () {
}
);

Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified',
])->controller(TransformersRunController::class)->group(
function () {
Route::post('/projects/{project}/transformers/run', 'run')
->name('transformers.run');
}
);

Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/Http/Controllers/TransformersRunControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Project;
use App\Models\Source;
use App\Models\Transformer;
use App\Models\User;
use Illuminate\Support\Facades\Bus;
use Tests\TestCase;

class TransformersRunControllerTest extends TestCase
{
public function test_runs()
{
Bus::fake();
$project = Project::factory()->create();
$source = Source::factory()->create([
'project_id' => $project->id,
]);
$transformer1 = Transformer::factory()->create([
'project_id' => $project->id,
'order' => 1,
]);
$transformer2 = Transformer::factory()->create([
'project_id' => $project->id,
'order' => 2,
]);
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('transformers.run', $project));
Bus::assertBatchCount(1);
}
}

0 comments on commit e9caa85

Please sign in to comment.