]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline-status.pl
allowing clients to create their own session names
[Evergreen.git] / Open-ILS / src / offline / offline-status.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3
4 # --------------------------------------------------------------------
5 # Loads the offline script files for a given org, sorts and runs the 
6 # scripts, and returns the exception list
7 # --------------------------------------------------------------------
8
9 our $U;
10 our $logger;
11 require 'offline-lib.pl';
12 &execute();
13
14
15 sub execute {
16         my $evt = $U->check_perms(&offline_requestor->id, &offline_org, 'OFFLINE_VIEW');
17         handle_event($evt) if $evt;
18         &report_json(&gather_workstations) if &offline_cgi->param('detail');
19         &report_sessions() if &offline_cgi->param('seslist');
20         &report_json_summary; 
21 }
22
23
24 sub report_sessions {
25         my $sessions = &offline_org_sessions(&offline_org);
26         my $results = [];
27         for my $s (@$sessions) {
28                 my $name = $$s[0];
29                 my $file = $$s[1];
30                 my $meta = &_offline_file_to_perl("$file/meta", 'workstation');
31                 my $done = ($file =~ m#/archive/#o) ? 1 : 0;
32                 push( @$results, { session => $name, meta => $meta, complete => $done } );
33         }
34         &offline_handle_json($results);
35 }
36
37
38 # --------------------------------------------------------------------
39 # Collects a list of workstations that have pending files
40 # --------------------------------------------------------------------
41 sub gather_workstations {
42         my $dir = &offline_pending_dir;
43         $dir = &offline_archive_dir unless -e $dir;
44         return [] unless -e $dir;
45         my @files =  <$dir/*.log>;
46         $_ =~ s/\.log//og for @files; # remove .log
47         $_ =~ s#/.*/(\w+)#$1#og for @files; # remove leading file path
48         return \@files;
49 }
50
51
52 # --------------------------------------------------------------------
53 # Just resturns whether or not the transaction is complete and how
54 # many items have been processed
55 # --------------------------------------------------------------------
56 sub report_json_summary {
57
58         my $complete = 0;
59         my $results;
60
61         if( -e &offline_pending_dir ) {
62                 $results = &offline_read_results
63
64         } elsif( -e &offline_archive_dir ) {
65                 $results  = &offline_read_archive_results;
66                 $complete = 1;
67
68         } else {
69                 handle_event(OpenILS::Event->new('OFFLINE_SESSION_NOT_FOUND'));
70         }
71
72         &offline_handle_json(
73                 {complete => $complete, num_complete => scalar(@$results)});
74 }
75
76
77 # --------------------------------------------------------------------
78 # Reports the workstations and their results as JSON
79 # --------------------------------------------------------------------
80 sub report_json { 
81         my $wslist = shift;
82         my @data;
83
84         my $meta = &offline_read_meta;
85         my $results = &offline_read_results;
86         my $complete = 0;
87
88         if(!$$meta[0]) {
89                 $logger->debug("offline: attempting to report on archived files for session ".&offline_seskey);
90                 $meta           = &offline_read_archive_meta;
91                 $results  = &offline_read_archive_results;
92                 $complete = 1;
93         }
94
95         for my $ws (@$wslist) {
96                 my ($m) = grep { $_ and $_->{'log'} and $_->{'log'} =~ m#/.*/$ws.log# } @$meta;
97                 my @res = grep { $_->{command}->{_workstation} eq $ws } @$results;
98                 delete $m->{'log'};
99                 push( @data, { meta => $m, workstation => $ws, results =>  \@res } );
100         }
101
102         &offline_handle_json({ complete => $complete, data => \@data});
103 }
104
105
106