Refactor analysis file names

The versioning of analysis input and document files
(`analysis_input_v{N}.json`, `document_v{N}.md`) has been removed. All
analysis inputs will now use `analysis_input.json` and generated
documents will use `document.md`.

This simplifies file management, as there's no longer a need to track
and manage multiple versions of these files within a case directory. The
analysis worker will now overwrite the existing `document.md` if it
exists, ensuring that the latest analysis result is always present. The
`version` field has also been removed from `AnalyzeJob` and
`AnalysisInput`.
This commit is contained in:
2026-04-16 01:56:55 +02:00
parent 928c0659c1
commit 8c6b2eeaa4
9 changed files with 111 additions and 281 deletions
+16 -9
View File
@@ -8,12 +8,12 @@ use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use tracing::{info, warn};
use crate::analyze::{AnalyzeJob, AnalyzeSender};
use crate::analyze::{AnalyzeJob, AnalyzeSender, ANALYSIS_INPUT_FILE, DOCUMENT_FILE};
use crate::auth::AuthenticatedWebUser;
use crate::config::Config;
use crate::error::AppError;
use crate::paths::{write_delete_marker, DeleteMarker};
use crate::routes::case_actions::{build_analysis_input, find_latest_document, write_input_create_new};
use crate::routes::case_actions::{build_analysis_input, write_input_create_new};
use crate::routes::user_web::locate_case;
#[derive(Debug, Deserialize)]
@@ -72,11 +72,19 @@ async fn bulk_analyze(
skipped += 1;
continue;
};
let version = find_latest_document(&case_dir)
.await
.map(|(v, _)| v + 1)
.unwrap_or(1);
let input = match build_analysis_input(&case_dir, version).await {
// Same order as the single-case handler: drop old document first so
// the UI doesn't keep showing "ausgewertet" during re-analysis.
let document_path = case_dir.join(DOCUMENT_FILE);
match tokio::fs::remove_file(&document_path).await {
Ok(_) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => {
warn!(slug = %slug, case_id = %case_id, error = %e, "bulk-analyze: remove old document failed");
skipped += 1;
continue;
}
}
let input = match build_analysis_input(&case_dir).await {
Ok(i) => i,
Err(_) => {
warn!(slug = %slug, case_id = %case_id, "bulk-analyze: build_analysis_input failed");
@@ -84,7 +92,7 @@ async fn bulk_analyze(
continue;
}
};
let input_path = case_dir.join(format!("analysis_input_v{version}.json"));
let input_path = case_dir.join(ANALYSIS_INPUT_FILE);
if write_input_create_new(&input_path, &input).await.is_err() {
warn!(slug = %slug, case_id = %case_id, "bulk-analyze: write input failed");
skipped += 1;
@@ -93,7 +101,6 @@ async fn bulk_analyze(
if analyze_tx
.send(AnalyzeJob {
case_dir: case_dir.clone(),
version,
})
.is_err()
{
+21 -66
View File
@@ -10,7 +10,9 @@ use time::OffsetDateTime;
use tokio::io::AsyncWriteExt;
use tracing::{info, warn};
use crate::analyze::{AnalysisInput, AnalyzeJob, AnalyzeSender, RecordingInput};
use crate::analyze::{
AnalysisInput, AnalyzeJob, AnalyzeSender, RecordingInput, ANALYSIS_INPUT_FILE, DOCUMENT_FILE,
};
use crate::auth::AuthenticatedWebUser;
use crate::config::Config;
use crate::error::AppError;
@@ -21,7 +23,6 @@ use crate::routes::user_web::locate_case;
#[template(path = "document.html")]
struct DocumentTemplate {
case_id: String,
version: u32,
content: String,
}
@@ -56,13 +57,18 @@ pub async fn handle_analyze_case(
}
};
let version = find_latest_document(&case_dir)
.await
.map(|(v, _)| v + 1)
.unwrap_or(1);
// Re-analyze: delete the existing document first so the UI stops showing
// the stale "ausgewertet" state while the worker re-computes. Ignore
// NotFound (first-time analysis path).
let document_path = case_dir.join(DOCUMENT_FILE);
match tokio::fs::remove_file(&document_path).await {
Ok(_) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(AppError::Internal(format!("remove old document: {e}"))),
}
let input = build_analysis_input(&case_dir, version).await?;
let input_path = case_dir.join(format!("analysis_input_v{version}.json"));
let input = build_analysis_input(&case_dir).await?;
let input_path = case_dir.join(ANALYSIS_INPUT_FILE);
write_input_create_new(&input_path, &input).await?;
// Unbounded send: only fails if the worker has gone away (programmer error
@@ -71,7 +77,6 @@ pub async fn handle_analyze_case(
if analyze_tx
.send(AnalyzeJob {
case_dir: case_dir.clone(),
version,
})
.is_err()
{
@@ -81,7 +86,6 @@ pub async fn handle_analyze_case(
info!(
slug = %user.slug,
case_id = %case_id,
version,
recordings = input.recordings.len(),
"analyze requested; analysis enqueued"
);
@@ -110,29 +114,22 @@ pub async fn handle_document_view(
}
};
let (version, content) = find_latest_document(&case_dir)
let content = read_document(&case_dir)
.await
.ok_or_else(|| AppError::NotFound("Noch kein Dokument erzeugt".into()))?;
DocumentTemplate {
case_id,
version,
content,
}
DocumentTemplate { case_id, content }
.render()
.map(Html)
.map_err(|e| AppError::Internal(format!("Template render failed: {e}")))
}
/// Build an `AnalysisInput` for the given case directory at the given version.
/// Build an `AnalysisInput` for the given case directory.
/// Collects all `.m4a` (excluding `.m4a.failed`), reads the matching
/// `.transcript.txt` for each, skips blank transcripts, and computes
/// `last_recording_mtime` as the max mtime over ALL `.m4a` (including blank
/// ones — a late blank addendum still counts as activity).
pub(crate) async fn build_analysis_input(
case_dir: &Path,
version: u32,
) -> Result<AnalysisInput, AppError> {
pub(crate) async fn build_analysis_input(case_dir: &Path) -> Result<AnalysisInput, AppError> {
let m4as = collect_m4as(case_dir).await?;
if m4as.is_empty() {
return Err(AppError::BadRequest("Keine Aufnahmen im Fall".into()));
@@ -170,7 +167,6 @@ pub(crate) async fn build_analysis_input(
.map_err(|e| AppError::Internal(format!("format mtime: {e}")))?;
Ok(AnalysisInput {
version,
last_recording_mtime,
recordings,
})
@@ -242,36 +238,9 @@ fn filename_stem_to_recorded_at(stem: &str) -> String {
}
}
/// Scan `case_dir` for `document_v{N}.md`, pick the highest N, return its
/// content together with N. Returns `None` if no document exists.
pub(crate) async fn find_latest_document(case_dir: &Path) -> Option<(u32, String)> {
let mut highest: Option<u32> = None;
let mut entries = tokio::fs::read_dir(case_dir).await.ok()?;
while let Ok(Some(entry)) = entries.next_entry().await {
let name = entry.file_name();
let Some(name_str) = name.to_str() else { continue };
let Some(version) = parse_document_version(name_str) else {
continue;
};
highest = match highest {
Some(h) if h >= version => Some(h),
_ => Some(version),
};
}
let v = highest?;
let content = tokio::fs::read_to_string(case_dir.join(format!("document_v{v}.md")))
.await
.ok()?;
Some((v, content))
}
fn parse_document_version(filename: &str) -> Option<u32> {
let rest = filename.strip_prefix("document_v")?;
let num = rest.strip_suffix(".md")?;
if num.is_empty() || (num.starts_with('0') && num.len() > 1) {
return None;
}
num.parse::<u32>().ok()
/// Read `case_dir/document.md`. Returns `None` if no document exists.
pub(crate) async fn read_document(case_dir: &Path) -> Option<String> {
tokio::fs::read_to_string(case_dir.join(DOCUMENT_FILE)).await.ok()
}
/// POST /web/cases/{case_id}/delete
@@ -437,18 +406,4 @@ mod tests {
);
}
#[test]
fn parses_valid_document_names() {
assert_eq!(parse_document_version("document_v1.md"), Some(1));
assert_eq!(parse_document_version("document_v42.md"), Some(42));
}
#[test]
fn rejects_invalid_document_names() {
assert_eq!(parse_document_version("document_v01.md"), None);
assert_eq!(parse_document_version("document_v.md"), None);
assert_eq!(parse_document_version("document_vx.md"), None);
assert_eq!(parse_document_version("analysis_input_v1.json"), None);
assert_eq!(parse_document_version(""), None);
}
}
+10 -30
View File
@@ -9,7 +9,7 @@ use tracing::{info, warn};
use time::{format_description::well_known::Rfc3339, Date, OffsetDateTime, UtcOffset};
use crate::analyze::{recovery as analyze_recovery, AnalyzeSender};
use crate::analyze::{recovery as analyze_recovery, AnalyzeSender, ANALYSIS_INPUT_FILE, DOCUMENT_FILE};
use crate::auth::AuthenticatedWebUser;
use crate::config::Config;
use crate::error::AppError;
@@ -173,39 +173,19 @@ async fn compute_flags(
}
}
/// True iff the case directory contains at least one `analysis_input_v*.json`.
/// The worker removes the input after successful document write, so existence
/// implies a pending or in-flight analyze job.
/// True iff `analysis_input.json` exists. The worker removes it after a
/// successful document write, so existence implies a pending or in-flight job.
pub(crate) async fn any_analysis_input_exists(case_dir: &Path) -> bool {
let mut entries = match tokio::fs::read_dir(case_dir).await {
Ok(r) => r,
Err(_) => return false,
};
while let Ok(Some(entry)) = entries.next_entry().await {
let name = entry.file_name();
let Some(name_str) = name.to_str() else { continue };
if name_str.starts_with("analysis_input_v") && name_str.ends_with(".json") {
return true;
}
}
false
tokio::fs::try_exists(case_dir.join(ANALYSIS_INPUT_FILE))
.await
.unwrap_or(false)
}
/// True iff the case directory contains at least one `document_v{N}.md`.
/// We don't care about N here — any version means "Ausgewertet" for the UI.
/// True iff `document.md` exists.
pub(crate) async fn any_document_exists(case_dir: &Path) -> bool {
let mut entries = match tokio::fs::read_dir(case_dir).await {
Ok(r) => r,
Err(_) => return false,
};
while let Ok(Some(entry)) = entries.next_entry().await {
let name = entry.file_name();
let Some(name_str) = name.to_str() else { continue };
if name_str.starts_with("document_v") && name_str.ends_with(".md") {
return true;
}
}
false
tokio::fs::try_exists(case_dir.join(DOCUMENT_FILE))
.await
.unwrap_or(false)
}
/// Self-heal: if a worker is idle but orphans exist on disk for this user,