Skip to content

Commit 8e676df

Browse files
authored
Merge pull request #113 from tristanbudd/fix-sourcing-and-rebuild
Fix sourcing and rebuild
2 parents 7f88e16 + 220ebe4 commit 8e676df

9 files changed

Lines changed: 145 additions & 104 deletions

File tree

app/Http/Controllers/Api/VesselController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,19 @@ public function activities(string $mmsi): JsonResponse
624624

625625
$data = $activities->map(function ($activity) {
626626
$details = $activity->details;
627-
$source = $details['source'] ?? 'FleetLeaks';
627+
$source = null;
628+
629+
if ($activity->type === 'port_of_interest') {
630+
static $poiData = null;
631+
if ($poiData === null) {
632+
$path = resource_path('data/ports_of_interest.json');
633+
$poiData = file_exists($path) ? json_decode(file_get_contents($path), true) : [];
634+
}
635+
636+
$portName = $details['port_name'] ?? null;
637+
$matchingPoi = collect($poiData)->firstWhere('name', $portName);
638+
$source = $matchingPoi['source'] ?? ($details['source'] ?? 'FleetLeaks');
639+
}
628640

629641
return [
630642
'id' => $activity->id,
@@ -756,7 +768,6 @@ public function infractionsList(Request $request): JsonResponse
756768
'infractions_count' => (int) $vessel->activities_count,
757769
'highest_severity' => $highestSeverity,
758770
'risk_score' => min(100, (int) $vessel->raw_score),
759-
'source' => 'FleetLeaks',
760771
];
761772
});
762773

app/Models/Vessel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@
1212
* @property int $mmsi
1313
* @property int|null $imo
1414
* @property string|null $name
15+
* @property string|null $call_sign
16+
* @property int|null $type
17+
* @property int|null $navigational_status
1518
* @property float|null $lat
1619
* @property float|null $lng
20+
* @property float|null $speed
21+
* @property float|null $course
22+
* @property int|null $heading
23+
* @property float|null $draught
24+
* @property string|null $destination
25+
* @property Carbon|null $eta
26+
* @property string|null $flag
1727
* @property Carbon|null $last_seen_at
1828
* @property Carbon|null $last_analyzed_at
1929
*/

app/Models/VesselActivity.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,20 @@
22

33
namespace App\Models;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Eloquent\Model;
67

8+
/**
9+
* @property int $id
10+
* @property int $mmsi
11+
* @property string $type
12+
* @property string $severity
13+
* @property string $description
14+
* @property array $details
15+
* @property Carbon $started_at
16+
* @property Carbon|null $ended_at
17+
* @property bool $is_active
18+
*/
719
class VesselActivity extends Model
820
{
921
protected $guarded = [];

app/Models/VesselPosition.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
namespace App\Models;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Eloquent\Casts\Attribute;
67
use Illuminate\Database\Eloquent\Model;
78

9+
/**
10+
* @property int $mmsi
11+
* @property float $lat
12+
* @property float $lng
13+
* @property float|null $speed
14+
* @property float|null $course
15+
* @property int|null $heading
16+
* @property int|null $navigational_status
17+
* @property Carbon $recorded_at
18+
*/
819
class VesselPosition extends Model
920
{
1021
protected $guarded = [];

app/Services/VesselAnalysisService.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ private function detectTransmissionGaps(Vessel $vessel): void
142142
'effective_speed' => round($effectiveSpeed, 2),
143143
'gap_start' => $current->recorded_at->toIso8601String(),
144144
'gap_end' => $next->recorded_at->toIso8601String(),
145+
'start_pos' => ['lat' => round($current->lat, 6), 'lng' => round($current->lng, 6)],
146+
'end_pos' => ['lat' => round($next->lat, 6), 'lng' => round($next->lng, 6)],
145147
'start_speed' => $current->speed,
146148
'end_speed' => $next->speed,
147149
], $current->recorded_at, $next->recorded_at);
@@ -185,6 +187,7 @@ private function detectLoiteringPatterns(Vessel $vessel): void
185187
'duration_hours' => $durationHours,
186188
'lat_span' => round($latRange, 6),
187189
'lng_span' => round($lngRange, 6),
190+
'coordinates' => ['lat' => round($recent->avg('lat'), 6), 'lng' => round($recent->avg('lng'), 6)],
188191
], $recent->last()->recorded_at, $recent->first()->recorded_at);
189192
}
190193
}
@@ -307,10 +310,17 @@ private function persistActivity(
307310
private function resolveDescription(string $type, array $details): string
308311
{
309312
return match ($type) {
310-
'ais_gap' => 'AIS transmission interruption detected ('.MaritimeFormatter::formatDuration($details['duration_minutes'] ?? 0).').',
311-
'loitering' => 'Stationary residency pattern in open-sea transit area.',
312-
'speed_anomaly' => 'Kinematic violation: speed exceeds physical capability ('.round($details['reported_speed'] ?? 0, 1).' kn).',
313-
'port_of_interest' => 'Vessel interaction detected at high-risk maritime hub: '.($details['port_name'] ?? 'Unknown Port').'.',
313+
'ais_gap' => 'AIS transmission interruption detected. From '.
314+
(isset($details['start_pos']) ? "[{$details['start_pos']['lat']}, {$details['start_pos']['lng']}] to [{$details['end_pos']['lat']}, {$details['end_pos']['lng']}]" : 'unknown position').
315+
' ('.MaritimeFormatter::formatDuration($details['duration_minutes'] ?? 0).').',
316+
'loitering' => 'Stationary residency pattern detected at '.
317+
(isset($details['coordinates']) ? "[{$details['coordinates']['lat']}, {$details['coordinates']['lng']}]" : 'unknown area').
318+
' for '.($details['duration_hours'] ?? 0).' hours.',
319+
'speed_anomaly' => 'Kinematic violation at '.
320+
(isset($details['coordinates']) ? "[{$details['coordinates']['lat']}, {$details['coordinates']['lng']}]" : 'unknown location').
321+
': speed exceeds physical capability ('.round($details['reported_speed'] ?? 0, 1).' kn).',
322+
'port_of_interest' => 'Vessel interaction detected at high-risk maritime hub: '.($details['port_name'] ?? 'Unknown Port').
323+
' '.(isset($details['coordinates']) ? "[{$details['coordinates']['lat']}, {$details['coordinates']['lng']}]" : '').'.',
314324
default => 'Anomalous behavioral event detected.',
315325
};
316326
}

docker/entrypoint/start.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if [ "${RUN_DEPLOY_TASKS_ON_STARTUP:-true}" = "true" ]; then
2828
php artisan route:cache
2929
php artisan view:cache
3030
php artisan scribe:generate
31+
php artisan sist:rebuild-analysis
3132
fi
3233

3334
exec "$@"

0 commit comments

Comments
 (0)