Skip to content

Commit

Permalink
editoast: stdcm_log: store requests that cause core errors
Browse files Browse the repository at this point in the history
Fixes #10841.

Signed-off-by: Younes Khoudli <[email protected]>
  • Loading branch information
Khoyo committed Feb 19, 2025
1 parent 07d6bac commit 1d6d796
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DELETE FROM stdcm_logs WHERE response ? 'response' = FALSE;
UPDATE stdcm_logs SET response = response->'response';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE stdcm_logs SET response = jsonb_build_object('response', response);
15 changes: 14 additions & 1 deletion editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11079,7 +11079,7 @@ components:
request:
$ref: '#/components/schemas/StdcmRequest'
response:
$ref: '#/components/schemas/StdcmResponse'
$ref: '#/components/schemas/StdcmResponseOrError'
trace_id:
type: string
nullable: true
Expand Down Expand Up @@ -11347,6 +11347,19 @@ components:
type: string
enum:
- preprocessing_simulation_error
StdcmResponseOrError:
oneOf:
- type: object
required:
- response
properties:
response:
$ref: '#/components/schemas/StdcmResponse'
- type: object
required:
- request_error
properties:
request_error: {}
StdcmSearchEnvironment:
type: object
required:
Expand Down
17 changes: 14 additions & 3 deletions editoast/src/models/stdcm_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ use crate::models::prelude::*;

editoast_common::schemas! {
StdcmLog,
StdcmResponseOrError,
}

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "snake_case")]
// We accepted the difference of memory size taken by variants
// Since there is only on success and others are error cases
#[allow(clippy::large_enum_variant)]
pub enum StdcmResponseOrError {
#[schema(value_type = StdcmResponse)]
Response(Response),
RequestError(serde_json::Value),
}

#[derive(Clone, Debug, Serialize, Deserialize, Model, ToSchema)]
Expand All @@ -25,8 +37,7 @@ pub struct StdcmLog {
#[schema(value_type = StdcmRequest)]
pub request: Request,
#[model(json)]
#[schema(value_type = StdcmResponse)]
pub response: Response,
pub response: StdcmResponseOrError,
pub created: DateTime<Utc>,
pub user_id: Option<i64>,
}
Expand All @@ -36,7 +47,7 @@ impl StdcmLog {
mut conn: DbConnection,
trace_id: Option<String>,
request: Request,
response: Response,
response: StdcmResponseOrError,
user_id: Option<i64>,
) {
let stdcm_log_changeset = StdcmLog::changeset()
Expand Down
17 changes: 13 additions & 4 deletions editoast/src/views/timetable/stdcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::core::AsCoreRequest;
use crate::core::CoreClient;
use crate::error::Result;
use crate::models::prelude::*;
use crate::models::stdcm_log::StdcmLog;
use crate::models::stdcm_log::{StdcmLog, StdcmResponseOrError};
use crate::models::timetable::Timetable;
use crate::models::train_schedule::TrainSchedule;
use crate::models::Infra;
Expand Down Expand Up @@ -284,7 +284,7 @@ async fn stdcm(
.collect(),
};

let stdcm_response = stdcm_request.fetch(core_client.as_ref()).await?;
let stdcm_response = stdcm_request.fetch(core_client.as_ref()).await;

// 6. Log STDCM request and response if logging is enabled
if config.enable_stdcm_logging {
Expand All @@ -301,6 +301,15 @@ async fn stdcm(
},
);

let stdcm_response = match stdcm_response.clone() {
Ok(response) => StdcmResponseOrError::Response(response),
Err(error) => {
StdcmResponseOrError::RequestError(serde_json::to_value(error).unwrap_or(
serde_json::Value::String("Failed to serialize the error".into()),
))
}
};

tokio::spawn(
// We just don't await the creation of the log entry since we want
// the endpoint to return as soon as possible, and because failing
Expand All @@ -309,15 +318,15 @@ async fn stdcm(
conn,
trace_id.map(|trace_id| trace_id.to_string()),
stdcm_request,
stdcm_response.clone(),
stdcm_response,
user_id,
)
.in_current_span(),
);
}

// 7. Handle STDCM Core Response
match stdcm_response {
match stdcm_response? {
crate::core::stdcm::Response::Success {
simulation,
path,
Expand Down
9 changes: 8 additions & 1 deletion front/src/common/api/generatedEditoastApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3628,11 +3628,18 @@ export type StdcmResponse =
error: SimulationResponse;
status: 'preprocessing_simulation_error';
};
export type StdcmResponseOrError =
| {
response: StdcmResponse;
}
| {
request_error: unknown;
};
export type StdcmLog = {
created: string;
id: number;
request: StdcmRequest;
response: StdcmResponse;
response: StdcmResponseOrError;
trace_id?: string | null;
user_id?: number | null;
};
Expand Down

0 comments on commit 1d6d796

Please sign in to comment.