mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-10 13:10:57 +00:00
fix(monitoring): tolerate null API payloads
Normalize monitoring API responses before rendering so empty or error payloads with data:null cannot crash the dashboard. Also guard chart, token, and box session arrays before reading length/map.
This commit is contained in:
@@ -92,18 +92,46 @@ export function useMonitoringData(filterState: FilterState) {
|
||||
limit: 50,
|
||||
});
|
||||
|
||||
const overview = response?.overview ?? {
|
||||
total_messages: 0,
|
||||
llm_calls: 0,
|
||||
embedding_calls: 0,
|
||||
model_calls: 0,
|
||||
success_rate: 100,
|
||||
active_sessions: 0,
|
||||
};
|
||||
const messages = Array.isArray(response?.messages)
|
||||
? response.messages
|
||||
: [];
|
||||
const llmCalls = Array.isArray(response?.llmCalls)
|
||||
? response.llmCalls
|
||||
: [];
|
||||
const embeddingCalls = Array.isArray(response?.embeddingCalls)
|
||||
? response.embeddingCalls
|
||||
: [];
|
||||
const sessions = Array.isArray(response?.sessions)
|
||||
? response.sessions
|
||||
: [];
|
||||
const errors = Array.isArray(response?.errors) ? response.errors : [];
|
||||
const totalCount = response?.totalCount ?? {
|
||||
messages: messages.length,
|
||||
llmCalls: llmCalls.length,
|
||||
embeddingCalls: embeddingCalls.length,
|
||||
sessions: sessions.length,
|
||||
errors: errors.length,
|
||||
};
|
||||
|
||||
// Transform the response to match MonitoringData interface
|
||||
const transformedData: MonitoringData = {
|
||||
overview: {
|
||||
totalMessages: response.overview.total_messages,
|
||||
llmCalls: response.overview.llm_calls,
|
||||
embeddingCalls: response.overview.embedding_calls || 0,
|
||||
modelCalls:
|
||||
response.overview.model_calls || response.overview.llm_calls,
|
||||
successRate: response.overview.success_rate,
|
||||
activeSessions: response.overview.active_sessions,
|
||||
totalMessages: overview.total_messages,
|
||||
llmCalls: overview.llm_calls,
|
||||
embeddingCalls: overview.embedding_calls || 0,
|
||||
modelCalls: overview.model_calls || overview.llm_calls,
|
||||
successRate: overview.success_rate,
|
||||
activeSessions: overview.active_sessions,
|
||||
},
|
||||
messages: response.messages.map(
|
||||
messages: messages.map(
|
||||
(msg: {
|
||||
id: string;
|
||||
timestamp: string;
|
||||
@@ -136,7 +164,7 @@ export function useMonitoringData(filterState: FilterState) {
|
||||
variables: msg.variables,
|
||||
}),
|
||||
),
|
||||
llmCalls: response.llmCalls.map(
|
||||
llmCalls: llmCalls.map(
|
||||
(call: {
|
||||
id: string;
|
||||
timestamp: string;
|
||||
@@ -173,7 +201,7 @@ export function useMonitoringData(filterState: FilterState) {
|
||||
messageId: call.message_id,
|
||||
}),
|
||||
),
|
||||
embeddingCalls: (response.embeddingCalls || []).map(
|
||||
embeddingCalls: embeddingCalls.map(
|
||||
(call: {
|
||||
id: string;
|
||||
timestamp: string;
|
||||
@@ -208,7 +236,7 @@ export function useMonitoringData(filterState: FilterState) {
|
||||
),
|
||||
// Create merged modelCalls array from llmCalls and embeddingCalls
|
||||
modelCalls: [] as ModelCall[], // Will be populated after transform
|
||||
sessions: response.sessions.map(
|
||||
sessions: sessions.map(
|
||||
(session: {
|
||||
session_id: string;
|
||||
bot_id: string;
|
||||
@@ -236,7 +264,7 @@ export function useMonitoringData(filterState: FilterState) {
|
||||
userId: session.user_id,
|
||||
}),
|
||||
),
|
||||
errors: response.errors.map(
|
||||
errors: errors.map(
|
||||
(error: {
|
||||
id: string;
|
||||
timestamp: string;
|
||||
@@ -264,11 +292,11 @@ export function useMonitoringData(filterState: FilterState) {
|
||||
}),
|
||||
),
|
||||
totalCount: {
|
||||
messages: response.totalCount.messages,
|
||||
llmCalls: response.totalCount.llmCalls,
|
||||
embeddingCalls: response.totalCount.embeddingCalls || 0,
|
||||
sessions: response.totalCount.sessions,
|
||||
errors: response.totalCount.errors,
|
||||
messages: totalCount.messages,
|
||||
llmCalls: totalCount.llmCalls,
|
||||
embeddingCalls: totalCount.embeddingCalls || 0,
|
||||
sessions: totalCount.sessions,
|
||||
errors: totalCount.errors,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user