mirror of
https://github.com/OpenSquawk/OpenSquawk
synced 2026-08-05 00:46:00 +08:00
merge
This commit is contained in:
107
server/api/admin/logs/sessions.get.ts
Normal file
107
server/api/admin/logs/sessions.get.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { defineEventHandler, getQuery } from 'h3'
|
||||
import { requireAdmin } from '../../../utils/auth'
|
||||
import { TransmissionLog } from '../../../models/TransmissionLog'
|
||||
|
||||
interface SessionSummaryEntry {
|
||||
sessionId: string
|
||||
startedAt: string | null
|
||||
updatedAt: string | null
|
||||
entryCount: number
|
||||
callsign?: string
|
||||
lastMessage?: {
|
||||
text: string
|
||||
role: string
|
||||
channel: string
|
||||
createdAt: string
|
||||
}
|
||||
}
|
||||
|
||||
function toISO(value: any): string | null {
|
||||
if (!value) {
|
||||
return null
|
||||
}
|
||||
const date = new Date(value)
|
||||
return Number.isNaN(date.valueOf()) ? null : date.toISOString()
|
||||
}
|
||||
|
||||
function extractCallsign(entry: any): string | undefined {
|
||||
const fromMetadata = (payload: any) =>
|
||||
payload?.metadata?.context?.variables?.callsign ||
|
||||
payload?.metadata?.context?.variables?.CALLSIGN ||
|
||||
payload?.metadata?.context?.variables?.Callsign
|
||||
|
||||
return (
|
||||
fromMetadata(entry.lastEntry) ||
|
||||
fromMetadata(entry.firstEntry) ||
|
||||
entry.lastEntry?.metadata?.variables?.callsign ||
|
||||
entry.firstEntry?.metadata?.variables?.callsign ||
|
||||
undefined
|
||||
)
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdmin(event)
|
||||
|
||||
const query = getQuery(event)
|
||||
const page = Math.max(parseInt(String(query.page ?? '1'), 10) || 1, 1)
|
||||
const pageSizeRaw = parseInt(String(query.pageSize ?? query.limit ?? '20'), 10)
|
||||
const pageSize = Math.min(Math.max(pageSizeRaw || 20, 1), 100)
|
||||
const skip = (page - 1) * pageSize
|
||||
|
||||
const matchStage = { sessionId: { $exists: true, $nin: [null, ''] } }
|
||||
|
||||
const [sessionDocs, totalCountAgg] = await Promise.all([
|
||||
TransmissionLog.aggregate([
|
||||
{ $match: matchStage },
|
||||
{ $sort: { sessionId: 1, createdAt: 1 } },
|
||||
{
|
||||
$group: {
|
||||
_id: '$sessionId',
|
||||
firstEntry: { $first: '$$ROOT' },
|
||||
lastEntry: { $last: '$$ROOT' },
|
||||
count: { $sum: 1 },
|
||||
},
|
||||
},
|
||||
{ $sort: { 'lastEntry.createdAt': -1 } },
|
||||
{ $skip: skip },
|
||||
{ $limit: pageSize },
|
||||
]).exec(),
|
||||
TransmissionLog.aggregate([
|
||||
{ $match: matchStage },
|
||||
{ $group: { _id: '$sessionId' } },
|
||||
{ $count: 'count' },
|
||||
]).exec(),
|
||||
])
|
||||
|
||||
const total = totalCountAgg?.[0]?.count || 0
|
||||
|
||||
const items: SessionSummaryEntry[] = sessionDocs.map((entry: any) => {
|
||||
const first = entry.firstEntry || {}
|
||||
const last = entry.lastEntry || {}
|
||||
return {
|
||||
sessionId: entry._id,
|
||||
startedAt: toISO(first.createdAt),
|
||||
updatedAt: toISO(last.createdAt),
|
||||
entryCount: entry.count || 0,
|
||||
callsign: extractCallsign(entry),
|
||||
lastMessage: last.text
|
||||
? {
|
||||
text: last.text,
|
||||
role: last.role,
|
||||
channel: last.channel,
|
||||
createdAt: toISO(last.createdAt) || new Date().toISOString(),
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
items,
|
||||
pagination: {
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
pages: Math.ceil(total / pageSize) || 1,
|
||||
},
|
||||
}
|
||||
})
|
||||
85
server/api/admin/logs/sessions/[sessionId].get.ts
Normal file
85
server/api/admin/logs/sessions/[sessionId].get.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { defineEventHandler, createError } from 'h3'
|
||||
import { requireAdmin } from '../../../../utils/auth'
|
||||
import { TransmissionLog } from '../../../../models/TransmissionLog'
|
||||
|
||||
function toISO(value: any): string | null {
|
||||
if (!value) {
|
||||
return null
|
||||
}
|
||||
const date = new Date(value)
|
||||
return Number.isNaN(date.valueOf()) ? null : date.toISOString()
|
||||
}
|
||||
|
||||
function mapEntry(doc: any) {
|
||||
return {
|
||||
id: String(doc._id),
|
||||
role: doc.role,
|
||||
channel: doc.channel,
|
||||
direction: doc.direction,
|
||||
text: doc.text,
|
||||
normalized: doc.normalized || undefined,
|
||||
createdAt: toISO(doc.createdAt) || new Date().toISOString(),
|
||||
metadata: doc.metadata || undefined,
|
||||
sessionId: doc.sessionId || undefined,
|
||||
user: doc.user
|
||||
? {
|
||||
id: String(doc.user._id),
|
||||
email: doc.user.email,
|
||||
name: doc.user.name || undefined,
|
||||
role: doc.user.role,
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function extractCallsign(entries: any[]): string | undefined {
|
||||
for (const entry of [...entries].reverse()) {
|
||||
const callsign =
|
||||
entry.metadata?.context?.variables?.callsign ||
|
||||
entry.metadata?.context?.variables?.CALLSIGN ||
|
||||
entry.metadata?.variables?.callsign
|
||||
if (callsign) {
|
||||
return callsign
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireAdmin(event)
|
||||
const sessionId = event.context.params?.sessionId
|
||||
|
||||
if (!sessionId || typeof sessionId !== 'string') {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Session ID is required' })
|
||||
}
|
||||
|
||||
const items = await TransmissionLog.find({ sessionId })
|
||||
.sort({ createdAt: 1 })
|
||||
.populate('user', 'email name role')
|
||||
.lean()
|
||||
.exec()
|
||||
|
||||
if (!items.length) {
|
||||
return {
|
||||
sessionId,
|
||||
startedAt: null,
|
||||
updatedAt: null,
|
||||
entryCount: 0,
|
||||
callsign: undefined,
|
||||
entries: [],
|
||||
}
|
||||
}
|
||||
|
||||
const startedAt = toISO(items[0].createdAt)
|
||||
const updatedAt = toISO(items[items.length - 1].createdAt)
|
||||
const callsign = extractCallsign(items)
|
||||
|
||||
return {
|
||||
sessionId,
|
||||
startedAt,
|
||||
updatedAt,
|
||||
entryCount: items.length,
|
||||
callsign,
|
||||
entries: items.map(mapEntry),
|
||||
}
|
||||
})
|
||||
@@ -17,6 +17,7 @@ type TransmissionListItem = {
|
||||
createdAt: string
|
||||
metadata?: Record<string, any>
|
||||
user?: { id: string; email: string; name?: string; role: string }
|
||||
sessionId?: string
|
||||
}
|
||||
|
||||
function escapeRegExp(input: string) {
|
||||
@@ -33,6 +34,7 @@ function mapTransmission(doc: any): TransmissionListItem {
|
||||
normalized: doc.normalized || undefined,
|
||||
createdAt: doc.createdAt ? new Date(doc.createdAt).toISOString() : new Date().toISOString(),
|
||||
metadata: doc.metadata || undefined,
|
||||
sessionId: doc.sessionId || undefined,
|
||||
user: doc.user
|
||||
? {
|
||||
id: String(doc.user._id),
|
||||
|
||||
Reference in New Issue
Block a user