Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editoast: stdcm_log: store requests that cause core errors #10847

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 15 additions & 4 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,13 +47,13 @@ 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()
.trace_id(trace_id)
.request(request)
.response(response.clone())
.response(response)
.user_id(user_id);
if let Err(e) = stdcm_log_changeset.create(&mut conn).await {
tracing::error!("Failed during log operation: {e}");
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
Loading