Skip to content
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ top, then by impact. Types follow [Keep a Changelog](https://keepachangelog.com/

### Medium impact

- **Changed:** PDF reports are filed by date inside their outcome folders (2026-11-24)

When a PDF report channel moves an incoming PDF after reading it, the engine now places the
file inside a `YYYY/MM/DD` date folder (using the host computer's local date when the report
was received) under the configured processed, filtered or unreadable folder — for example
`processed/2026/11/24/report.pdf`. The configured folder names stay the same, and the date
folders are created automatically on both local folders and SFTP servers.

Technical notes:

Applies to `pdf_report` sources with `on_consumed: move` on `folder` and `sftp` endpoints,
using the message's `received_at` converted to the host's local calendar date
(`<outcome_directory>/YYYY/MM/DD`). Delimited file sources are unchanged.

- **Fixed:** SFTP endpoints connect to servers with RSA host keys (2026-11-24)

SFTP endpoints now connect to servers that identify themselves with an RSA host key, including
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions changelog.d/2026-11-24-pdf-report-date-folders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
date: 2026-11-24
type: changed
impact: medium
title: PDF reports are filed by date inside their outcome folders
area: Endpoints
link: /endpoints
---
When a PDF report channel moves an incoming PDF after reading it, the engine now places the
file inside a `YYYY/MM/DD` date folder (using the host computer's local date when the report
was received) under the configured processed, filtered or unreadable folder — for example
`processed/2026/11/24/report.pdf`. The configured folder names stay the same, and the date
folders are created automatically on both local folders and SFTP servers.

## Technical notes

Applies to `pdf_report` sources with `on_consumed: move` on `folder` and `sftp` endpoints,
using the message's `received_at` converted to the host's local calendar date
(`<outcome_directory>/YYYY/MM/DD`). Delimited file sources are unchanged.
2 changes: 1 addition & 1 deletion console/help/pdf-reports.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"title": "Folders and embedding",
"paragraphs": [
"On the folder or SFTP endpoint, under After a file is read, a filtered and an unreadable reports folder keep left-out and unreadable reports apart. A mapping embeds the report with report.file_base64; an HL7 v2 files destination writes each message as a file for the receiver to collect."
"On the folder or SFTP endpoint, under After a file is read, a filtered and an unreadable reports folder keep left-out and unreadable reports apart; moved PDF reports are filed under YYYY/MM/DD date folders inside them. A mapping embeds the report with report.file_base64; an HL7 v2 files destination writes each message as a file for the receiver to collect."
]
}
]
Expand Down
6 changes: 3 additions & 3 deletions console/src/features/config/EndpointFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,19 @@ function AfterReading() {
label="Processed folder"
isRequired
validate={subfolder}
description="Inside the endpoint's folder; created when needed."
description="Inside the endpoint's folder; created when needed (PDF reports use YYYY/MM/DD date folders inside it)."
/>
<TextInput
name="filteredDirectory"
label="Filtered reports folder"
validate={subfolder}
description="PDF report sources: reports the channel's filters leave out. Empty: the processed folder."
description="PDF report sources: reports the channel's filters leave out, under YYYY/MM/DD. Empty: the processed folder."
/>
<TextInput
name="invalidDirectory"
label="Unreadable reports folder"
validate={subfolder}
description="PDF report sources: reports that cannot be read with the template. Empty: the processed folder."
description="PDF report sources: reports that cannot be read with the template, under YYYY/MM/DD. Empty: the processed folder."
/>
</>
)}
Expand Down
1 change: 1 addition & 0 deletions crates/packie-routing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license.workspace = true
publish.workspace = true

[dependencies]
jiff = { workspace = true }
rust_decimal = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
108 changes: 86 additions & 22 deletions crates/packie-routing/src/message/pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
//! each complete file is one message, committed whole with the identifiers its template
//! reads; the pipeline parses it on a blocking thread. The file is consumed right after the
//! commit, or, with a filtered or invalid directory, once the pipeline has decided the
//! message, into the directory of its outcome.
//! message, into `YYYY/MM/DD` (local date of receipt) under the directory of its outcome.

use std::sync::Arc;

use jiff::tz::TimeZone;
use packie_config::{Consumption, EndpointConfig, PdfReportSettings};
use packie_core::{MessageId, MessageState, ProtocolKind, Timestamp};
use packie_engine::pdf::PdfReader;
Expand Down Expand Up @@ -112,6 +113,27 @@ enum Outcome {
Invalid,
}

impl Outcome {
fn directory<'a>(self, consumption: &Consumption<'a>) -> &'a str {
match self {
Self::Processed => consumption.processed,
Self::Filtered => consumption.filtered.unwrap_or(consumption.processed),
Self::Invalid => consumption.invalid.unwrap_or(consumption.processed),
}
}
}

/// `<base>/YYYY/MM/DD` for `at` in the computer's time zone.
fn dated_directory(base: &str, at: Timestamp) -> String {
dated_directory_in(base, at, &TimeZone::system())
}

fn dated_directory_in(base: &str, at: Timestamp, zone: &TimeZone) -> String {
let base = base.trim().trim_end_matches(['/', '\\']);
let date = at.as_jiff().to_zoned(zone.clone()).strftime("%Y/%m/%d");
format!("{base}/{date}")
}

/// Commits the report, runs its pipeline and consumes the file.
async fn take_report(
cx: &MessageContext,
Expand All @@ -123,12 +145,16 @@ async fn take_report(
let Some(consumption) = endpoint.consumption() else {
return Ok(());
};
let received_at = Timestamp::now();
let committed = if unit.too_large.is_some() {
commit_too_large(cx, unit).await
commit_too_large(cx, unit, received_at).await
} else {
let identifiers = read_identifiers(reader, unit).await;
cx.store
.commit_received(receipt(cx, unit, identifiers), unit.bytes.clone())
.commit_received(
receipt(cx, unit, identifiers, received_at),
unit.bytes.clone(),
)
.await
.map(Some)
};
Expand All @@ -142,14 +168,16 @@ async fn take_report(
let committed = committed?;
cx.shared.session_stable();
if !consumption.by_outcome() {
consume(cx, source, unit, &consumption, Outcome::Processed).await;
let target = dated_directory(Outcome::Processed.directory(&consumption), received_at);
consume(cx, source, unit, &target).await;
}
let outcome = match committed {
None => Outcome::Invalid,
Some(receipt) => after_commit(cx, &receipt, &unit.bytes).await,
};
if consumption.by_outcome() {
consume(cx, source, unit, &consumption, outcome).await;
let target = dated_directory(outcome.directory(&consumption), received_at);
consume(cx, source, unit, &target).await;
}
Ok(())
}
Expand All @@ -158,10 +186,11 @@ async fn take_report(
async fn commit_too_large(
cx: &MessageContext,
unit: &FileUnit,
received_at: Timestamp,
) -> Result<Option<ReceiptOutcome>, StoreError> {
let message = cx
.store
.begin_transfer(receipt(cx, unit, identifiers(&[])), Vec::new())
.begin_transfer(receipt(cx, unit, identifiers(&[]), received_at), Vec::new())
.await?;
cx.store.abort_transfer(message, "file_too_large").await?;
Ok(None)
Expand Down Expand Up @@ -211,25 +240,16 @@ async fn outcome_of(cx: &MessageContext, original: MessageId) -> Outcome {
}
}

/// Moves (or deletes, or leaves) the file per `on_consumed`; a move goes to the outcome's
/// directory. A file that cannot be consumed is logged; reading it again records a duplicate.
/// Moves (or deletes, or leaves) the file per `on_consumed`; a move goes to `directory`
/// (`<outcome>/YYYY/MM/DD`). A file that cannot be consumed is logged; reading it again
/// records a duplicate.
async fn consume(
cx: &MessageContext,
source: &mut dyn FileSource,
unit: &FileUnit,
consumption: &Consumption<'_>,
outcome: Outcome,
directory: &str,
) {
let directory = match outcome {
Outcome::Processed => None,
Outcome::Filtered => consumption.filtered,
Outcome::Invalid => consumption.invalid,
};
let result = match directory {
Some(directory) => source.consumed_into(unit, directory).await,
None => source.consumed(unit).await,
};
if let Err(error) = result {
if let Err(error) = source.consumed_into(unit, directory).await {
tracing::warn!(
event = "file.consume_failed",
operation = %error.operation,
Expand All @@ -241,7 +261,12 @@ async fn consume(
}
}

fn receipt(cx: &MessageContext, unit: &FileUnit, mut identifiers: Identifiers) -> Receipt {
fn receipt(
cx: &MessageContext,
unit: &FileUnit,
mut identifiers: Identifiers,
received_at: Timestamp,
) -> Receipt {
identifiers.source_identity = Some(unit.name.clone());
Receipt {
channel_id: cx.shared.id,
Expand All @@ -251,7 +276,7 @@ fn receipt(cx: &MessageContext, unit: &FileUnit, mut identifiers: Identifiers) -
protocol: ProtocolKind::PdfReport,
// When the engine takes the file, not the file's modified time: an old file dropped
// today is received today (and retention counts from today).
received_at: Timestamp::now(),
received_at,
config_version: cx.shared.config_version(),
duplicate_detection: cx.channel.source.duplicate_detection,
identifiers,
Expand All @@ -268,3 +293,42 @@ fn receipt(cx: &MessageContext, unit: &FileUnit, mut identifiers: Identifiers) -
}),
}
}

#[cfg(test)]
mod tests {
use super::*;
use packie_config::OnConsumed;

#[test]
fn dated_directories_use_the_local_calendar_date_with_zero_padded_parts() {
let chicago = TimeZone::get("America/Chicago").unwrap();
let bucharest = TimeZone::get("Europe/Bucharest").unwrap();
// 2026-03-05 23:30 UTC: still March 5 in Chicago (-06:00), already March 6 in Bucharest (+02:00).
let at = Timestamp::parse("2026-03-05T23:30:00Z").unwrap();
assert_eq!(
dated_directory_in("processed", at, &chicago),
"processed/2026/03/05"
);
assert_eq!(
dated_directory_in("processed/", at, &bucharest),
"processed/2026/03/06"
);
assert_eq!(
dated_directory_in("errors/pdf\\", at, &bucharest),
"errors/pdf/2026/03/06"
);
}

#[test]
fn unset_outcome_directories_fall_back_to_processed() {
let consumption = Consumption {
on_consumed: OnConsumed::Move,
processed: "processed",
filtered: Some("filtered"),
invalid: None,
};
assert_eq!(Outcome::Processed.directory(&consumption), "processed");
assert_eq!(Outcome::Filtered.directory(&consumption), "filtered");
assert_eq!(Outcome::Invalid.directory(&consumption), "processed");
}
}
Loading
Loading