Skip to content

Commit

Permalink
Track Statistics (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschucki authored Dec 24, 2024
1 parent 9287e7f commit b245504
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 19 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class HomeController extends Controller
{
public function __invoke(Request $request)
{
return Inertia::render('Temp');
return Inertia::render('Converter/Show');
}
}
50 changes: 50 additions & 0 deletions app/Models/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,54 @@ public function getFormatOperations(): array

return $operations;
}

public function statistic(): BelongsTo
{
return $this->belongsTo(Statistic::class);
}

public function trackStatistic(): void
{
$conversionEnd = $this->status === ConversionStatus::FINISHED ? now() : null;

$updateValues = [
'mime_type' => $this->file->mime_type ?? '',
'extension' => $this->file->extension ?? '',
'size' => $this->file->size ?? 0,
'status' => $this->status,
'keep_resolution' => $this->keep_resolution,
'audio' => $this->audio,
'auto_crop' => $this->auto_crop,
'watermark' => $this->watermark,
'interpolation' => $this->interpolation,
'audio_quality' => $this->audio_quality,
'trim_start' => $this->trim_start,
'trim_end' => $this->trim_end,
'max_size' => $this->max_size,
'url' => $this->url,
'conversion_started_at' => $this->created_at,
'conversion_ended_at' => $conversionEnd,
];

$stat = Statistic::where('conversion_id', $this->id)->first();

if ($stat) {
if ($stat->mime_type !== '' && $stat->mime_type !== $updateValues['mime_type']) {
$updateValues['mime_type'] = $stat->mime_type;
}
if ($stat->extension !== '' && $stat->extension !== $updateValues['extension']) {
$updateValues['extension'] = $stat->extension;
}
if ($stat->size !== 0 && $stat->size !== $updateValues['size']) {
$updateValues['size'] = $stat->size;
}
if ($stat->size === 0) {
$updateValues['size'] = $this->file->size ?? 0;
}

$stat->update($updateValues);
} else {
Statistic::create(array_merge($updateValues, ['conversion_id' => $this->id]));
}
}
}
30 changes: 30 additions & 0 deletions app/Models/Statistic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Statistic extends Model
{
use HasUuids;

protected $guarded = [];

protected $casts = [
'audio' => 'boolean',
'interpolation' => 'boolean',
'auto_crop' => 'boolean',
'max_size' => 'integer',
'audio_quality' => 'float',
'watermark' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

public function conversion(): BelongsTo
{
return $this->belongsTo(Conversion::class);
}
}
9 changes: 9 additions & 0 deletions app/Observers/ConversionObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class ConversionObserver
{
public function updated(Conversion $conversion): void
{
$conversion->trackStatistic();

ConversionUpdated::dispatch($conversion->id);

if ($conversion->status === ConversionStatus::FINISHED) {
Expand All @@ -21,9 +23,16 @@ public function updated(Conversion $conversion): void

public function created(Conversion $conversion): void
{
$conversion->trackStatistic();

if ($conversion->status === ConversionStatus::PENDING && $conversion->url !== null && $conversion->file_id === null) {
//DownloadVideoJob::dispatchSync($conversion->id);
DownloadVideoJob::dispatch($conversion->id)->onQueue('downloader');
}
}

public function deleting(Conversion $conversion): void
{
$conversion->trackStatistic();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function up(): void
$table->foreign('session_id')
->references('id')
->on('sessions')
->cascadeOnDelete()
->cascadeOnUpdate()
->cascadeOnDelete();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function up(): void
$table->foreign('session_id')
->references('id')
->on('sessions')
->cascadeOnDelete()
->cascadeOnUpdate()
->cascadeOnDelete();
});
}
Expand Down
43 changes: 43 additions & 0 deletions database/migrations/2024_12_24_220649_create_statistics_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::create('statistics', static function (Blueprint $table) {
$table->comment('Each row represents a single conversion');

$table->uuid('id')->primary();
$table->foreignUuid('conversion_id')->nullable()->constrained()->nullOnDelete()->cascadeOnUpdate();
$table->string('mime_type');
$table->string('extension');
$table->string('status');
$table->boolean('keep_resolution')->default(false);
$table->boolean('audio')->default(true);
$table->boolean('auto_crop')->default(false);
$table->boolean('watermark')->default(false);
$table->boolean('interpolation')->default(false);
$table->float('audio_quality');
$table->unsignedInteger('trim_start')->nullable();
$table->unsignedInteger('trim_end')->nullable();
$table->unsignedInteger('max_size')->nullable();
$table->text('url')->nullable();
$table->bigInteger('size');
$table->dateTime('conversion_started_at')->nullable();
$table->dateTime('conversion_ended_at')->nullable();
// create a new generated column for the time difference between conversion_started_at and conversion_ended_at
$table->integer('conversion_time')->virtualAs('TIMESTAMPDIFF(SECOND, conversion_started_at, conversion_ended_at)');
// virtualAs Column for
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('statistics');
}
};
4 changes: 3 additions & 1 deletion resources/js/Pages/Converter/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const { isOverDropZone } = useDropZone(dropZoneRef, {
const formSchema = toTypedSchema(
z.object({
file: z.any().optional(),
url: z.string().url('Keine valide URL').optional().default(''),
url: z.string().url('Keine valide URL').nullish().optional().default(null),
// keepResolution: z.boolean().default(false),
audio: z.boolean().default(true),
audioQuality: z
Expand All @@ -74,6 +74,7 @@ const formSchema = toTypedSchema(
const form = useForm({
validationSchema: formSchema,
name: 'converterSettings',
validateOnMount: true,
});
const inertiaForm = useInertiaForm({
Expand All @@ -90,6 +91,7 @@ const inertiaForm = useInertiaForm({
});
const onSubmit = form.handleSubmit(async (values) => {
console.log(values);
if (inertiaForm.processing) {
return;
}
Expand Down
27 changes: 12 additions & 15 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@
return redirect()->route('home');
})->name('login');

Route::middleware('auth')->group(function () {
Route::get('/conversions', [ListConverterController::class, 'index'])
->name('conversions.list');
Route::get('/conversions', [ListConverterController::class, 'index'])
->name('conversions.list');

Route::middleware([VerifyCsrfToken::class])
->group(function () {
Route::post('/conversions', [ListConverterController::class, 'myConversions'])
->name('conversions.my');
Route::post('/converter/start', StartConverterController::class)
->name('converter.start')
->middleware(['throttle']);
});

Route::get('conversions/download/{conversion}', [ConversionController::class, 'download'])
->name('conversions.download');
});
Route::middleware([VerifyCsrfToken::class])
->group(function () {
Route::post('/conversions', [ListConverterController::class, 'myConversions'])
->name('conversions.my');
Route::post('/converter/start', StartConverterController::class)
->name('converter.start')
->middleware(['throttle']);
});

Route::get('conversions/download/{conversion}', [ConversionController::class, 'download'])
->name('conversions.download');

0 comments on commit b245504

Please sign in to comment.