pub fn current_time_slot() -> i32 {
    let now = Local::now();
    let hour = now.hour() as i32;
    let minute = now.minute() as i32;
    hour * 2 + if minute >= 30 { 1 } else { 0 }
}

So what: 현재 시각을 preprocess.py의 time_to_slot과 동일한 30분 단위 슬롯으로 변환한다

So why: AI 모델은 학습 때 사용한 슬롯 번호 체계로만 예측할 수 있다. Python과 Rust가 각자 다른 방식으로 슬롯을 계산할 경우 학습데이터와 예측 입력이 어긋나서 모델이 엉뚱한 값으로 반환하게 된다. 그래서 Rust에 Python로직을 그대로 옮겼다.

 

pub fn current_day_type() -> String {
    let now = Local::now();
    match now.weekday() {
        chrono::Weekday::Sat => "토요일".to_string(),
        chrono::Weekday::Sun => "일요일".to_string(),
        _ => "평일".to_string(),
    }
}

 

So what: 현재 요일을 preprocess.py의 인코더가 학습한 값 중 하나로 반환한다.

So why: 원본 CSV의 요일구분 컬럼 값을 정확히 맞추기 위해서이다

 

/// 단건 예측
pub async fn predict(&self, req: CongestionRequest) -> Result<CongestionResponse, reqwest::Error>

/// 배치 예측 - 여러 구간 한 번에 요청 (경로 정렬용)
pub async fn predict_batch(&self, reqs: Vec<CongestionRequest>) -> Result<Vec<CongestionResponse>, reqwest::Error>

So what: 단건과 배치 예측을 별도 메서드로 분리했다

So why: 단건은 api/realtime 같이 단일 구간 조회할 때 따로 쓸 수 있게 남겨둔거고 여러 구간은 predict_batch로 한번에 보내면 네트워크 왕복이 1회로 줄고, FastAPI쪽도 모델 추론을 벡터 연산으로 처리할 여지가 생긴다.

 

+ Recent posts