Linking backend data to frontend

This commit is contained in:
iv_vuytsik
2025-10-15 19:49:19 +03:00
parent ea1f50c1b8
commit 2b19ed246b
28 changed files with 959 additions and 385 deletions

View File

@@ -0,0 +1,27 @@
export const dynamic = 'force-static';
import fs from 'fs';
import path from 'path';
export async function GET(
request: Request,
{ params }: { params: Promise<{ path: string[] }> }
) {
const { path: pathParts } = await params;
const fileName = pathParts.join('/');
const filePath = path.join(process.cwd(), 'assets', 'big-models', fileName);
if (!fs.existsSync(filePath)) {
return new Response('Not Found', { status: 404 });
}
const stat = fs.statSync(filePath);
const stream = fs.createReadStream(filePath);
return new Response(stream as unknown as ReadableStream, {
headers: {
'Content-Length': stat.size.toString(),
'Content-Type': 'model/gltf-binary',
},
});
}

View File

@@ -0,0 +1,114 @@
import { NextResponse, NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { getToken } from 'next-auth/jwt'
export async function GET(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
const authHeader = req.headers.get('authorization') || req.headers.get('Authorization')
const bearer = authHeader && authHeader.toLowerCase().startsWith('bearer ') ? authHeader.slice(7) : undefined
const secret = process.env.NEXTAUTH_SECRET
const token = await getToken({ req, secret }).catch(() => null)
let accessToken = session?.accessToken || bearer || (token as any)?.accessToken
const refreshToken = session?.refreshToken || (token as any)?.refreshToken
if (!accessToken && refreshToken) {
try {
const refreshRes = await fetch(`${process.env.BACKEND_URL}/auth/refresh/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh: refreshToken }),
})
if (refreshRes.ok) {
const refreshed = await refreshRes.json()
accessToken = refreshed.access
}
} catch {}
}
if (!accessToken) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
}
const backendUrl = process.env.BACKEND_URL
if (!backendUrl) {
return NextResponse.json({ success: false, error: 'BACKEND_URL is not configured' }, { status: 500 })
}
const url = new URL(req.url)
const objectId = url.searchParams.get('objectId')
const objectsRes = await fetch(`${backendUrl}/account/get-objects/`, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
cache: 'no-store',
})
const alertsRes = await fetch(`${backendUrl}/account/get-alerts/`, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
cache: 'no-store',
})
const payloadText = await alertsRes.text()
let payload: any
try { payload = JSON.parse(payloadText) } catch { payload = payloadText }
if (!alertsRes.ok) {
const err = typeof payload === 'string' ? payload : JSON.stringify(payload)
return NextResponse.json({ success: false, error: `Backend alerts error: ${err}` }, { status: alertsRes.status })
}
const objectsText = await objectsRes.text()
let objectsPayload: any
try { objectsPayload = JSON.parse(objectsText) } catch { objectsPayload = [] }
const titleToIdMap: Record<string, string> = {}
if (Array.isArray(objectsPayload)) {
for (const obj of objectsPayload) {
if (obj && typeof obj.title === 'string' && typeof obj.id === 'number') {
titleToIdMap[obj.title] = `object_${obj.id}`
}
}
}
const list: any[] = Array.isArray(payload) ? payload : []
const filtered = objectId ? list.filter(a => {
if (a.object && typeof a.object === 'string') {
const mappedId = titleToIdMap[a.object] || a.object
return mappedId === objectId
}
return true
}) : list
const transformed = filtered.map((a) => ({
id: a.id,
detector_name: a.name,
message: a.message,
type: a.severity,
location: a.object,
priority: a.severity,
acknowledged: a.resolved,
timestamp: a.created_at,
}))
return NextResponse.json({ success: true, data: transformed })
} catch (error) {
console.error('Error fetching alerts data:', error)
return NextResponse.json(
{
success: false,
error: 'Failed to fetch alerts data',
},
{ status: 500 }
)
}
}

View File

@@ -1,28 +0,0 @@
import { NextResponse } from 'next/server'
import { readFile } from 'fs/promises'
import { join } from 'path'
export async function GET() {
try {
// Имитация полученных данных в формате json (данные в data/detectors.json)
const filePath = join(process.cwd(), 'frontend', 'data', 'detectors.json')
const fileContent = await readFile(filePath, 'utf8')
const allData = JSON.parse(fileContent)
return NextResponse.json({
success: true,
data: allData,
objectsCount: Object.keys(allData.objects || {}).length,
detectorsCount: Object.keys(allData.detectors || {}).length
})
} catch (error) {
console.error('Error fetching detectors data:', error)
return NextResponse.json(
{
success: false,
error: 'Failed to fetch detectors data'
},
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,88 @@
import { NextResponse } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
export async function GET() {
try {
const session = await getServerSession(authOptions)
if (!session?.accessToken) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
}
const backendUrl = process.env.BACKEND_URL
const [detectorsRes, objectsRes] = await Promise.all([
fetch(`${backendUrl}/account/get-detectors/`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
cache: 'no-store',
}),
fetch(`${backendUrl}/account/get-objects/`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
cache: 'no-store',
}),
])
if (!detectorsRes.ok) {
const err = await detectorsRes.text()
return NextResponse.json({ success: false, error: `Backend detectors error: ${err}` }, { status: detectorsRes.status })
}
if (!objectsRes.ok) {
const err = await objectsRes.text()
return NextResponse.json({ success: false, error: `Backend objects error: ${err}` }, { status: objectsRes.status })
}
const detectorsPayload = await detectorsRes.json()
const objectsPayload = await objectsRes.json()
const titleToIdMap: Record<string, string> = {}
if (Array.isArray(objectsPayload)) {
for (const obj of objectsPayload) {
if (obj && typeof obj.title === 'string' && typeof obj.id === 'number') {
titleToIdMap[obj.title] = `object_${obj.id}`
}
}
}
const statusToColor: Record<string, string> = {
critical: '#b3261e',
warning: '#fd7c22',
normal: '#00ff00',
}
const transformedDetectors: Record<string, any> = {}
const detectorsObj = detectorsPayload?.detectors ?? {}
for (const [key, sensor] of Object.entries<any>(detectorsObj)) {
const color = statusToColor[sensor.status] ?? '#00ff00'
const objectId = titleToIdMap[sensor.object] || sensor.object
transformedDetectors[key] = {
...sensor,
status: color,
object: objectId,
checked: sensor.checked ?? false,
location: sensor.zone ?? '',
}
}
return NextResponse.json({
success: true,
data: { detectors: transformedDetectors },
objectsCount: Array.isArray(objectsPayload) ? objectsPayload.length : 0,
detectorsCount: Object.keys(transformedDetectors).length,
})
} catch (error) {
console.error('Error fetching detectors data:', error)
return NextResponse.json(
{
success: false,
error: 'Failed to fetch detectors data',
},
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,71 @@
import { NextResponse, NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { getToken } from 'next-auth/jwt'
export async function GET(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
const authHeader = req.headers.get('authorization') || req.headers.get('Authorization')
const bearer = authHeader && authHeader.toLowerCase().startsWith('bearer ') ? authHeader.slice(7) : undefined
const secret = process.env.NEXTAUTH_SECRET
const token = secret ? (await getToken({ req, secret }).catch(() => null)) : null
let accessToken: string | undefined = session?.accessToken || bearer || (token as any)?.accessToken
const refreshToken: string | undefined = session?.refreshToken || (token as any)?.refreshToken
if (!accessToken && refreshToken) {
try {
const refreshRes = await fetch(`${process.env.BACKEND_URL}/auth/refresh/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh: refreshToken }),
})
if (refreshRes.ok) {
const refreshed = await refreshRes.json()
accessToken = refreshed.access
}
} catch {}
}
if (!accessToken) {
return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 })
}
const backendUrl = process.env.BACKEND_URL
if (!backendUrl) {
return NextResponse.json({ success: false, error: 'BACKEND_URL is not configured' }, { status: 500 })
}
const objectsRes = await fetch(`${backendUrl}/account/get-objects/`, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
cache: 'no-store',
})
const payloadText = await objectsRes.text()
let payload: any
try { payload = JSON.parse(payloadText) } catch { payload = payloadText }
if (!objectsRes.ok) {
const err = typeof payload === 'string' ? payload : JSON.stringify(payload)
return NextResponse.json({ success: false, error: `Backend objects error: ${err}` }, { status: objectsRes.status })
}
return NextResponse.json({ success: true, data: payload })
} catch (error) {
console.error('Error fetching objects data:', error)
return NextResponse.json(
{
success: false,
error: 'Failed to fetch objects data',
},
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,109 @@
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { NextRequest } from 'next/server'
import { getToken } from 'next-auth/jwt'
export async function POST(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
const authHeader = req.headers.get('authorization') || req.headers.get('Authorization')
const bearer = authHeader && authHeader.toLowerCase().startsWith('bearer ') ? authHeader.slice(7) : undefined
const secret = process.env.NEXTAUTH_SECRET
const token = await getToken({ req, secret }).catch(() => null)
let accessToken = session?.accessToken || bearer || (token as any)?.accessToken
const refreshToken = session?.refreshToken || (token as any)?.refreshToken
if (!accessToken && refreshToken) {
try {
const refreshRes = await fetch(`${process.env.BACKEND_URL}/auth/refresh/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ refresh: refreshToken }),
})
if (refreshRes.ok) {
const refreshed = await refreshRes.json()
accessToken = refreshed.access
}
} catch {}
}
if (!accessToken) {
return new Response(JSON.stringify({ success: false, error: 'Unauthorized' }), {
status: 401,
headers: { 'Content-Type': 'application/json' },
})
}
const backendUrl = process.env.BACKEND_URL
if (!backendUrl) {
return new Response(JSON.stringify({ success: false, error: 'BACKEND_URL is not configured' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
})
}
const body = await req.json().catch(() => ({ })) as { format?: 'csv' | 'pdf', hours?: number }
const reportFormat = (body.format || '').toLowerCase()
const url = new URL(req.url)
const qpFormat = (url.searchParams.get('format') || '').toLowerCase()
const qpHoursRaw = url.searchParams.get('hours')
const qpHours = qpHoursRaw ? Number(qpHoursRaw) : undefined
const finalFormat = reportFormat || qpFormat
const finalHours = typeof body.hours === 'number' ? body.hours : (typeof qpHours === 'number' && !Number.isNaN(qpHours) ? qpHours : 168)
let backendRes = await fetch(`${backendUrl}/account/get-reports/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ report_format: finalFormat, hours: finalHours }),
})
if (!backendRes.ok && backendRes.status === 404) {
backendRes = await fetch(`${backendUrl}/account/get-report/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ report_format: finalFormat, hours: finalHours }),
})
}
if (!backendRes.ok) {
const errText = await backendRes.text().catch(() => '')
const contentType = backendRes.headers.get('Content-Type') || 'text/plain'
return new Response(errText || 'Error generating report', {
status: backendRes.status,
headers: {
'Content-Type': contentType,
},
})
}
const arrayBuffer = await backendRes.arrayBuffer()
const contentType = backendRes.headers.get('Content-Type') || 'application/octet-stream'
const contentDisposition = backendRes.headers.get('Content-Disposition') || backendRes.headers.get('content-disposition')
const headers: Record<string, string> = { 'Content-Type': contentType }
if (contentDisposition) {
headers['Content-Disposition'] = contentDisposition
} else if (finalFormat) {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').replace(/T/g, '_')
headers['Content-Disposition'] = `attachment; filename="alerts_report_${timestamp}.${finalFormat}"`
}
return new Response(arrayBuffer, { status: 200, headers })
} catch (error) {
console.error('Error proxying report export:', error)
return new Response(JSON.stringify({ success: false, error: 'Failed to export report' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
})
}
}