package Ernad::IssueState; use strict; use warnings; use Carp qw(confess); use Data::Dumper; use File::Path; use Encode; use XML::LibXML; use Time::Duration; use Ernad::Erimp; use Ernad::Common; $|=3; my $e=$Erimp::e // $main::e; ## form issue nucleus sub state_summary_doc { my $repcode=shift; my $e=shift // ''; if(not $e) { $e=$Erimp::e // $main::e; } if(not ref($e) eq 'Ernad::Erimp') { confess "I can't see the erimp."; } if($e->{'testing'}) { my $log_file=$e->{'dir'}->{'test'}.'/state_summary_doc.log'; open O, "> $log_file"; print O "start\n"; } my $rerc=$e->{'report'}->{$repcode}; if(not $rerc) { if($e->{'r'}) { ## Osborne transitory code $e->{'r'}->load($repcode); $rerc=$e->{'report'}->{$repcode}; } else { confess "I don't see your report $repcode"; } } ## Osborne transitory code my $repdoc=$rerc->{'repdoc'} // $rerc->{'doc'}; if(not $repdoc) { confess "I don't see your report $repcode"; } my $issue_doc=$rerc->{'repdoc'}->cloneNode(1); my $dir=$rerc->{'dir'}; my $ernad_ns=$e->{'const'}->{'ernad_ns'}; my $issues_element=$issue_doc->createElementNS( $ernad_ns, 'issues'); my $r; my $sent_issues=find_sent_dates($e,$repcode,0); my $dh; if($e->{'testing'}) { print O "reading \n"; } foreach my $sosta (keys %{$e->{'const'}->{'source_states'}}) { if($e->{'testing'}) { print O Dumper $e->{'const'}->{'source_states'}; print O "sosta is $sosta\n"; } my $file; if(not -d $dir->{$sosta}) { mkpath($dir->{$sosta}); } opendir($dh, $dir->{$sosta}) or confess "I could not open the $sosta directory ".$dir->{$sosta}; if($e->{'testing'}) { print O "reading ". $dir->{$sosta}."\n"; } while ( $file = readdir $dh) { if($e->{'testing'}) { print O "found file $file\n"; } if( $file eq ".." || $file eq "." ) { next; } # split riff name into path, date and timestamp my ( $path, $issuedate, $ts ); ## check if split of RIF works #if(not &Ernad::Common::split_rif_name( $file, \$path, \$issuedate, \$ts ) ) { if(not $e->{'f'}->split_rif_name( $file, \$path, \$issuedate, \$ts ) ) { if($e->{'testing'}) { print O "split rif failed on $file\n"; } next; } ## check if we don't have a RIF with the same date in the sent dir if($e->{'testing'}) { print O "issuedate is $issuedate\n"; } if($sent_issues->{$issuedate}) { if($e->{'testing'}) { print O "issuedate $issuedate has been sent.\n"; } next; } $r->{'sosta'}->{$sosta}->{$issuedate}=1; push(@{$r->{'files'}->{$issuedate}->{$sosta}},$file); my $fs; $fs->{'sosta'}=$sosta; $fs->{'ts'}=$ts; my $delay_with_words=&delay_with_words($ts); $fs->{'ago'}=$delay_with_words; $r->{'dates'}->{$issuedate}->{$file}->{$sosta}=$fs; } } if($e->{'testing'}) { my $r_string=Dumper $r; print O $r_string; } my $count_issues=0; foreach my $date (reverse sort {$b cmp $a} keys %{$r->{'dates'}}) { if($e->{'testing'}) { print O "I found date $date\n"; } my $issue_element=$issue_doc->createElementNS( $ernad_ns, 'issue'); $issue_element->setAttribute('date',$date); $issue_element->appendText("\n"); ## put the display as an attribute my $display=$date; $display=~s|-|\x{2012}|g; $issue_element->setAttribute('display',$display); #foreach my $sosta (@sostas) { # ## indicate that we have something available here. # #if(defined($r->{'sosta'}->{$sosta}->{$date})) { # # $issue_element->setAttribute($sosta,$sosta); # #} #} $count_issues++; foreach my $file (keys %{$r->{'dates'}->{$date}}) { foreach my $sosta (keys %{$r->{'dates'}->{$date}->{$file}}) { my $fs=$r->{'dates'}->{$date}->{$file}->{$sosta}; my $file_element=$issue_doc->createElementNS($ernad_ns,'file'); $file_element->setAttribute('name',$file); $file_element->setAttribute('time',$fs->{'ts'}); $file_element->setAttribute('ago',$fs->{'ago'}); $file_element->setAttribute('sosta',$fs->{'sosta'}); $issue_element->appendChild($file_element); $issue_element->appendText("\n"); } $issues_element->appendChild($issue_element); $issue_element->appendText("\n"); } } ## if element is not there, report no issues #my $collection_element=&Ernad::Common::find_collection_element_from_repdoc($issue_doc); my $collection_element=$e->{'x'}->find_collection_element_from_repdoc($issue_doc); if($count_issues) { $collection_element->appendChild($issues_element); } if($e->{'testing'}) { $issue_doc->toFile('/tmp/issuedoc.xml'); print O "the end\n"; close O; } return $issue_doc; } ## find out the sent files sub find_sent_dates { my $e=shift or confess 'I need an erimp here.'; my $repcode=shift or confess 'I need a repcode here.'; my $out; if($e->{'testing'}) { open L, '> /tmp/find_sent_dates.log'; } my $rerc=$e->{'report'}->{$repcode}; my $dir=$rerc->{'dir'}; my $sent_dir=$dir->{'sent'} or die "no sent dir"; if(not -d $sent_dir) { confess "no such sent_dir $sent_dir"; } my ($dh, $file, $path, $issuedate, $ts); opendir($dh,$sent_dir); if($e->{'testing'}) { print L "reading $sent_dir\n"; } while ( $file = readdir $dh) { if($e->{'testing'}) { print L "found file $file\n"; } if($file eq '..' or $file eq '.') { if($e->{'testing'}) { print L "skipped $file\n"; next; } } #if(not &Ernad::Common::split_rif_name( $file, \$path, \$issuedate, \$ts ) ) { if(not $e->{'f'}->split_rif_name( $file, \$path, \$issuedate, \$ts ) ) { if($e->{'testing'}) { print "split rif failed on $file\n"; } next; } $out->{$issuedate}=$ts; } my $out_string=Dumper $out; if($e->{'testing'}) { print L $out_string; close L; } return $out; } sub delay_with_words { my $in=shift; my $ago=ago(time-$in); $ago=~s| ago$||; return $ago; } 1;