Skip to content

Commit

Permalink
editoast: endpoints to retrieve stdcm payloads
Browse files Browse the repository at this point in the history
Signed-off-by: hamz2a <[email protected]>
  • Loading branch information
hamz2a committed Dec 31, 2024
1 parent 67d9838 commit a539bb6
Show file tree
Hide file tree
Showing 12 changed files with 452 additions and 58 deletions.
3 changes: 2 additions & 1 deletion editoast/editoast_common/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ pub struct TracingConfig {

pub fn create_tracing_subscriber<T: SpanExporter + 'static>(
tracing_config: TracingConfig,
log_level: tracing_subscriber::filter::LevelFilter,
exporter: T,
) -> impl tracing::Subscriber {
let env_filter_layer = tracing_subscriber::EnvFilter::builder()
// Set the default log level to 'info'
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into())
.with_default_directive(log_level.into())
.from_env_lossy();
let fmt_layer = tracing_subscriber::fmt::layer()
.pretty()
Expand Down
61 changes: 61 additions & 0 deletions editoast/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,42 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/StdcmSearchEnvironment'
/stdcm_logs:
get:
tags:
- stdcm_log
responses:
'200':
description: The list of Stdcm Logs
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/StdcmLog'
/stdcm_logs/{trace_id}:
get:
tags:
- stdcm_log
parameters:
- name: trace_id
in: path
required: true
schema:
type: string
responses:
'200':
description: The stdcm log
content:
application/json:
schema:
$ref: '#/components/schemas/StdcmLog'
'404':
description: The stdcm log was not found
content:
application/json:
schema:
$ref: '#/components/schemas/InternalError'
/temporary_speed_limit_group:
post:
tags:
Expand Down Expand Up @@ -4741,6 +4777,7 @@ components:
- $ref: '#/components/schemas/EditoastStdcmErrorRollingStockNotFound'
- $ref: '#/components/schemas/EditoastStdcmErrorTimetableNotFound'
- $ref: '#/components/schemas/EditoastStdcmErrorTowedRollingStockNotFound'
- $ref: '#/components/schemas/EditoastStdcmLogErrorNotFound'
- $ref: '#/components/schemas/EditoastStudyErrorNotFound'
- $ref: '#/components/schemas/EditoastStudyErrorStartDateAfterEndDate'
- $ref: '#/components/schemas/EditoastTemporarySpeedLimitErrorNameAlreadyUsed'
Expand Down Expand Up @@ -5960,6 +5997,30 @@ components:
type: string
enum:
- editoast:stdcm_v2:TowedRollingStockNotFound
EditoastStdcmLogErrorNotFound:
type: object
required:
- type
- status
- message
properties:
context:
type: object
required:
- trace_id
properties:
trace_id:
type: string
message:
type: string
status:
type: integer
enum:
- 404
type:
type: string
enum:
- editoast:stdcm_log:NotFound
EditoastStudyErrorNotFound:
type: object
required:
Expand Down
7 changes: 6 additions & 1 deletion editoast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ async fn run() -> Result<(), Box<dyn Error + Send + Sync>> {
stream: EditoastMode::from_client(&client).into(),
telemetry,
};
create_tracing_subscriber(tracing_config, exporter).init();
create_tracing_subscriber(
tracing_config,
tracing_subscriber::filter::LevelFilter::INFO,
exporter,
)
.init();

let pg_config = client.postgres_config;
let db_pool =
Expand Down
33 changes: 24 additions & 9 deletions editoast/src/models/stdcm_log.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::ops::DerefMut;

use chrono::DateTime;
use chrono::Utc;
use diesel::ExpressionMethods;
use diesel::OptionalExtension;
use diesel::QueryDsl;
use diesel_async::RunQueryDsl;
use editoast_derive::Model;
use editoast_models::DbConnection;
use opentelemetry::trace::TraceContextExt;
use serde::Deserialize;
use serde::Serialize;
use tracing_opentelemetry::OpenTelemetrySpanExt;
use utoipa::ToSchema;

use crate::core::stdcm::Request;
Expand All @@ -18,7 +22,7 @@ editoast_common::schemas! {

#[derive(Clone, Debug, Serialize, Deserialize, Model, ToSchema)]
#[model(table = editoast_models::tables::stdcm_logs)]
#[model(gen(ops = c))]
#[model(gen(ops = cd, list))]
pub struct StdcmLog {
pub id: i64,
pub trace_id: String,
Expand All @@ -33,22 +37,33 @@ pub struct StdcmLog {
impl StdcmLog {
pub async fn log(
mut conn: DbConnection,
trace_id: String,
request: Request,
response: Response,
user_id: Option<i64>,
) {
let trace_id = tracing::Span::current()
.context()
.span()
.span_context()
.trace_id();
let stdcm_log_changeset = StdcmLog::changeset()
.trace_id(trace_id.to_string())
.trace_id(trace_id)
.request(request)
.response(response.clone())
.user_id(user_id);
if let Err(e) = stdcm_log_changeset.create(&mut conn).await {
tracing::error!("Failed during log operation: {e}");
}
}

pub async fn find_by_trace_id(
conn: &mut DbConnection,
trace_id: &str,
) -> crate::error::Result<Option<StdcmLog>> {
use editoast_models::tables::stdcm_logs::dsl;

dsl::stdcm_logs
.filter(dsl::trace_id.eq(trace_id))
.first::<StdcmLogRow>(conn.write().await.deref_mut())
.await
.map(Into::into)
.optional()
.map_err(Into::into)
}
}
2 changes: 2 additions & 0 deletions editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod scenario;
pub mod search;
pub mod speed_limit_tags;
pub mod sprites;
pub mod stdcm_logs;
pub mod stdcm_search_environment;
pub mod study;
pub mod temporary_speed_limits;
Expand Down Expand Up @@ -111,6 +112,7 @@ crate::routes! {
&train_schedule,
&timetable,
&path,
&stdcm_logs,
&scenario,
}

Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ pub mod tests {
.roles(HashSet::from([BuiltinRole::OpsRead]))
.build();

let request = app.post("/projects").with_user(user).json(&json!({
let request = app.post("/projects").with_user(&user).json(&json!({
"name": "test_project_failed",
"description": "",
"objectives": "",
Expand Down
Loading

0 comments on commit a539bb6

Please sign in to comment.