phase 7
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AnalyticsEvent extends Model
|
||||
{
|
||||
const UPDATED_AT = null; // Only created_at timestamp
|
||||
|
||||
protected $fillable = [
|
||||
'event_type',
|
||||
'job_id',
|
||||
'session_id',
|
||||
'ip_address',
|
||||
'user_agent',
|
||||
'referer',
|
||||
'event_data',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'event_data' => 'array',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
|
||||
// Event types
|
||||
const EVENT_JOB_VIEW = 'job_view';
|
||||
const EVENT_JOB_APPLY_CLICK = 'job_apply_click';
|
||||
const EVENT_SEARCH = 'search';
|
||||
const EVENT_FILTER = 'filter';
|
||||
const EVENT_CATEGORY_VIEW = 'category_view';
|
||||
|
||||
public function job(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Job::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get events by type
|
||||
*/
|
||||
public function scopeOfType($query, string $type)
|
||||
{
|
||||
return $query->where('event_type', $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get events within a date range
|
||||
*/
|
||||
public function scopeWithinDays($query, int $days)
|
||||
{
|
||||
return $query->where('created_at', '>=', now()->subDays($days));
|
||||
}
|
||||
|
||||
/**
|
||||
* Log an event
|
||||
*/
|
||||
public static function logEvent(string $eventType, ?int $jobId = null, array $eventData = []): void
|
||||
{
|
||||
self::create([
|
||||
'event_type' => $eventType,
|
||||
'job_id' => $jobId,
|
||||
'session_id' => session()->getId(),
|
||||
'ip_address' => request()->ip(),
|
||||
'user_agent' => request()->userAgent(),
|
||||
'referer' => request()->header('referer'),
|
||||
'event_data' => $eventData,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ApiLog extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'job_source_id',
|
||||
'endpoint',
|
||||
'method',
|
||||
'status_code',
|
||||
'response_time_ms',
|
||||
'success',
|
||||
'error_message',
|
||||
'request_params',
|
||||
'response_data',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'success' => 'boolean',
|
||||
'request_params' => 'array',
|
||||
'response_data' => 'array',
|
||||
];
|
||||
|
||||
public function source(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(JobSource::class, 'job_source_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only successful logs
|
||||
*/
|
||||
public function scopeSuccessful($query)
|
||||
{
|
||||
return $query->where('success', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only failed logs
|
||||
*/
|
||||
public function scopeFailed($query)
|
||||
{
|
||||
return $query->where('success', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs within a date range
|
||||
*/
|
||||
public function scopeWithinDays($query, int $days)
|
||||
{
|
||||
return $query->where('created_at', '>=', now()->subDays($days));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class JobFetchLog extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'job_source_id',
|
||||
'success',
|
||||
'jobs_fetched',
|
||||
'jobs_created',
|
||||
'jobs_updated',
|
||||
'jobs_skipped',
|
||||
'duration_seconds',
|
||||
'error_message',
|
||||
'metadata',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'success' => 'boolean',
|
||||
'metadata' => 'array',
|
||||
];
|
||||
|
||||
public function source(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(JobSource::class, 'job_source_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only successful logs
|
||||
*/
|
||||
public function scopeSuccessful($query)
|
||||
{
|
||||
return $query->where('success', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get only failed logs
|
||||
*/
|
||||
public function scopeFailed($query)
|
||||
{
|
||||
return $query->where('success', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to get logs within a date range
|
||||
*/
|
||||
public function scopeWithinDays($query, int $days)
|
||||
{
|
||||
return $query->where('created_at', '>=', now()->subDays($days));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total jobs processed
|
||||
*/
|
||||
public function getTotalJobsProcessedAttribute(): int
|
||||
{
|
||||
return $this->jobs_created + $this->jobs_updated + $this->jobs_skipped;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user