diff options
| author | Paul Buetow <paul@buetow.org> | 2025-06-30 10:13:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-06-30 10:13:40 +0300 |
| commit | d7359e7d3114954f3b92a2a28995b26e206d37b7 (patch) | |
| tree | f5fb58dc94b6b2ea8f9fb4ff34cafa8c70b3000b | |
| parent | 4704952f8c33bacf35973ce932bb8ecae78e5a3f (diff) | |
feat: Add conditional daily report generation based on age
Only generate/regenerate daily reports when:
- File doesn't exist (always generate)
- Data is within 3 days (always regenerate to keep recent data fresh)
- Data older than 3 days with existing file (skip to preserve)
This optimization reduces unnecessary file writes while ensuring
recent reports stay up-to-date. Added informative logging to show
when reports are generated, regenerated, or skipped.
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
| -rw-r--r-- | foostats.pl | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/foostats.pl b/foostats.pl index 8873df4..d74a25b 100644 --- a/foostats.pl +++ b/foostats.pl @@ -726,6 +726,30 @@ package Foostats::Reporter { my ( $year, $month, $day ) = $date =~ /(\d{4})(\d{2})(\d{2})/; + # Check if .gmi file exists and its age based on date in filename + my $gemtext_dir = "$stats_dir/gemtext"; + my $report_path = "$gemtext_dir/$date.gmi"; + + # Calculate age of the data based on date in filename + my $today = Time::Piece->new(); + my $file_date = Time::Piece->strptime($date, '%Y%m%d'); + my $age_days = ($today - $file_date) / (24 * 60 * 60); + + if (-e $report_path) { + # File exists + if ($age_days <= 3) { + # Data is recent (within 3 days), regenerate it + say "Regenerating daily report for $year-$month-$day (data age: " . sprintf("%.1f", $age_days) . " days)"; + } else { + # Data is old (older than 3 days), skip if file exists + say "Skipping daily report for $year-$month-$day (file exists, data age: " . sprintf("%.1f", $age_days) . " days)"; + next; + } + } else { + # File doesn't exist, generate it + say "Generating new daily report for $year-$month-$day (file doesn't exist, data age: " . sprintf("%.1f", $age_days) . " days)"; + } + my $report_content = ""; $report_content .= "## Stats for $year-$month-$day @@ -841,10 +865,9 @@ package Foostats::Reporter { $report_content .= "=> ./30day_summary_$current_month.gmi 30-Day Summary Report\n\n"; # Ensure gemtext directory exists - my $gemtext_dir = "$stats_dir/gemtext"; mkdir $gemtext_dir unless -d $gemtext_dir; - my $report_path = "$gemtext_dir/$date.gmi"; + # $report_path already defined above say "Writing report to $report_path"; FileHelper::write( $report_path, $report_content ); } |
