-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvertical-slice.patch
More file actions
240 lines (240 loc) · 7.05 KB
/
Copy pathvertical-slice.patch
File metadata and controls
240 lines (240 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
*** Begin Patch
*** Add File: apps/api/src/main.ts
+import { Module, Controller, Post, UploadedFile, UseInterceptors, Get } from '@nestjs/common';
+import { FileInterceptor } from '@nestjs/platform-express';
+import { NestFactory } from '@nestjs/core';
+import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
+import { Pool } from 'pg';
+
+@Controller('ingest')
+class IngestController {
+ @Post('synthetic-ais')
+ @UseInterceptors(FileInterceptor('file'))
+ async uploadSynthetic(@UploadedFile() file: Express.Multer.File) {
+ // Placeholder: store raw capture, create raw_observation record
+ return { status: 'received', size: file?.size ?? 0 };
+ }
+}
+
+@Controller()
+class VesselsController {
+ pool: Pool;
+ constructor() {
+ const connectionString = process.env.DATABASE_URL || 'postgres://gm_user:gm_pass@localhost:5432/global_monitor';
+ this.pool = new Pool({ connectionString });
+ }
+
+ @Get('vessels')
+ async listVessels() {
+ const res = await this.pool.query('SELECT id, imo, mmsi, name, created_at FROM vessels ORDER BY created_at DESC LIMIT 100');
+ return res.rows;
+ }
+
+ @Get('observations')
+ async listObservations() {
+ const res = await this.pool.query('SELECT id, mmsi, imo, name, lat, lon, observed_at FROM ais_observations ORDER BY observed_at DESC LIMIT 100');
+ return res.rows;
+ }
+}
+
+@Module({ controllers: [IngestController, VesselsController] })
+class AppModule {}
+
+async function bootstrap() {
+ const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
+ app.enableShutdownHooks();
+ await app.listen(3001, '0.0.0.0');
+ console.log('API listening on 3001');
+}
+
+bootstrap();
+
*** End Patch
*** Begin Patch
*** Add File: apps/api/src/vessels.controller.ts
+import { Controller, Get } from '@nestjs/common';
+import { Pool } from 'pg';
+
+@Controller()
+export class VesselsController {
+ pool: Pool;
+ constructor() {
+ const connectionString = process.env.DATABASE_URL || 'postgres://gm_user:gm_pass@localhost:5432/global_monitor';
+ this.pool = new Pool({ connectionString });
+ }
+
+ @Get('vessels')
+ async listVessels() {
+ const res = await this.pool.query('SELECT id, imo, mmsi, name, created_at FROM vessels ORDER BY created_at DESC LIMIT 100');
+ return res.rows;
+ }
+
+ @Get('observations')
+ async listObservations() {
+ const res = await this.pool.query('SELECT id, mmsi, imo, name, lat, lon, observed_at FROM ais_observations ORDER BY observed_at DESC LIMIT 100');
+ return res.rows;
+ }
+}
+
*** End Patch
*** Begin Patch
*** Add File: .github/workflows/ci.yml
+name: CI
+
+on:
+ push:
+ branches: [ main ]
+ pull_request:
+ branches: [ main ]
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ services:
+ postgres:
+ image: postgis/postgis:15-3.4
+ env:
+ POSTGRES_USER: gm_user
+ POSTGRES_PASSWORD: gm_pass
+ POSTGRES_DB: global_monitor
+ ports:
+ - 5432:5432
+ options: >-
+ --health-cmd="pg_isready -U gm_user" --health-interval=10s --health-timeout=5s --health-retries=5
+ minio:
+ image: minio/minio
+ ports:
+ - 9000:9000
+ options: >-
+ --entrypoint "" --health-cmd='curl -f http://localhost:9000/minio/health/ready || exit 1' --health-interval=10s --health-timeout=5s --health-retries=5
+ env:
+ MINIO_ROOT_USER: minioadmin
+ MINIO_ROOT_PASSWORD: minioadmin
+ redpanda:
+ image: vectorized/redpanda:latest
+ ports:
+ - 9092:9092
+
+ steps:
+ - uses: actions/checkout@v4
+
+ - name: Setup Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: '20'
+
+ - name: Setup pnpm
+ uses: pnpm/action-setup@v2
+ with:
+ pnpm-version: 8
+
+ - name: Install dependencies
+ run: pnpm install
+
+ - name: Run lint
+ run: pnpm lint
+
+ - name: Run TypeScript type check
+ run: pnpm typecheck
+
+ - name: Wait for Postgres
+ run: |
+ for i in {1..30}; do
+ if pg_isready -h localhost -p 5432 -U gm_user; then
+ echo "Postgres ready"
+ break
+ fi
+ echo "Waiting for Postgres..."
+ sleep 2
+ done
+ env:
+ PGPASSWORD: gm_pass
+
+ - name: Apply migrations
+ run: |
+ sudo apt-get update && sudo apt-get install -y postgresql-client
+ export DATABASE_URL="postgres://gm_user:gm_pass@localhost:5432/global_monitor"
+ chmod +x infra/scripts/run_migrations.sh
+ ./infra/scripts/run_migrations.sh
+ env:
+ PGPASSWORD: gm_pass
+
+ - name: Build API
+ run: pnpm --filter api build
+
+ - name: Build web
+ run: pnpm --filter web build
+
*** End Patch
*** Begin Patch
*** Add File: apps/web/src/pages/index.tsx
+import React, { useEffect, useState } from 'react';
+
+export default function Home() {
+ const [vessels, setVessels] = useState<any[]>([]);
+ const [observations, setObservations] = useState<any[]>([]);
+
+ useEffect(() => {
+ fetch('/api/proxy/vessels').then(r => r.json()).then(setVessels).catch(() => {});
+ fetch('/api/proxy/observations').then(r => r.json()).then(setObservations).catch(() => {});
+ }, []);
+
+ return (
+ <div>
+ <h1>GLOBAL MONITOR — Web UI (placeholder)</h1>
+ <section>
+ <h2>Vessels</h2>
+ <ul>
+ {vessels.map(v => (
+ <li key={v.id}>{v.name || v.imo || v.mmsi} — {v.imo || v.mmsi}</li>
+ ))}
+ </ul>
+ </section>
+
+ <section>
+ <h2>Recent Observations (delayed view)</h2>
+ <ul>
+ {observations.map(o => (
+ <li key={o.id}>{o.name || o.mmsi} @ {o.lat},{o.lon} at {o.observed_at}</li>
+ ))}
+ </ul>
+ </section>
+ </div>
+ );
+}
+
*** End Patch
*** Begin Patch
*** Add File: apps/web/src/pages/api/proxy/[...catchall].ts
+// Simple API proxy for development to avoid CORS and to call backend running on 3001
+import type { NextApiRequest, NextApiResponse } from 'next'
+
+const BACKEND = process.env.BACKEND_URL || 'http://localhost:3001'
+
+export default async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const url = `${BACKEND}${req.url?.replace('/api/proxy', '')}`;
+ const r = await fetch(url);
+ const data = await r.json();
+ res.status(200).json(data);
+}
+
*** End Patch
*** Begin Patch
*** Add File: infra/scripts/run_migrations_ci.sh
+#!/usr/bin/env bash
+set -euo pipefail
+
+# Wait for Postgres before applying migrations
+
+echo "Waiting for Postgres..."
+until pg_isready -h "${PGHOST:-localhost}" -p "${PGPORT:-5432}" -U "${PGUSER:-gm_user}" >/dev/null 2>&1; do
+ sleep 1
+done
+
+echo "Applying migrations (CI runner)..."
+psql "${DATABASE_URL:-postgres://gm_user:gm_pass@localhost:5432/global_monitor}" -f infra/migrations/0001_init.sql
+psql "${DATABASE_URL:-postgres://gm_user:gm_pass@localhost:5432/global_monitor}" -f infra/migrations/0002_ais_observations.sql
+
+echo "Migrations complete."
+
*** End Patch