Bug 34587: Add IR_A1 fields to COUNTER report
[koha.git] / Koha / BackgroundJob / ErmSushiHarvester.pm
1 package Koha::BackgroundJob::ErmSushiHarvester;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Try::Tiny;
21
22 use Koha::DateUtils qw( dt_from_string );
23 use Koha::ERM::EUsage::UsageDataProviders;
24
25 use base 'Koha::BackgroundJob';
26
27 =head1 NAME
28
29 Koha::BackgroundJob::ErmSushiHarvester - Background job derived class to process the ERM Usage Statistics SUSHI Harvester
30 =head1 API
31
32 =head2 Class methods
33
34 =head3 job_type
35
36 Define the job type of this job: erm_sushi_harvester
37
38 =cut
39
40 sub job_type {
41     return 'erm_sushi_harvester';
42 }
43
44 =head3 process
45
46 Koha::BackgroundJob->find($id)->process(
47     {
48         ud_provider_id => $self->erm_usage_data_provider_id
49     }
50 );
51
52 Process the harvesting.
53
54 =cut
55
56 sub process {
57     my ( $self, $args ) = @_;
58
59     if ( $self->status eq 'cancelled' ) {
60         return;
61     }
62
63     $self->{us_report_info} = {
64         skipped_mus         => 0,
65         skipped_yus         => 0,
66         added_mus           => 0,
67         added_yus           => 0,
68         added_usage_objects => 0,
69     };
70
71     # FIXME If the job has already been started, but started again (worker has been restart for instance)
72     # Then we will start from scratch and so double process the same records
73
74     $self->start;
75
76     my $ud_provider = Koha::ERM::EUsage::UsageDataProviders->find( $args->{ud_provider_id} );
77
78     $ud_provider->set_background_job_callbacks(
79         {
80             report_info_callback => sub { $self->report_info(@_); },
81             step_callback        => sub { $self->step; },
82             set_size_callback    => sub { $self->set_job_size(@_); },
83             add_message_callback => sub { $self->add_message(@_); },
84         }
85     );
86
87     my $counter_file;
88     if ( $args->{file_content} ) {
89         my $counter_files_rs = $ud_provider->counter_files(
90             [
91                 {
92                     usage_data_provider_id => $ud_provider->erm_usage_data_provider_id,
93                     file_content           => $args->{file_content},
94                     date_uploaded          => POSIX::strftime( "%Y%m%d%H%M%S", localtime ),
95                     filename               => $ud_provider->name . "_" . $ud_provider->{report_type},
96                 }
97             ]
98         );
99         $counter_file = $counter_files_rs->last;
100     } else {
101         $ud_provider->harvest_sushi(
102             {
103                 begin_date  => $args->{begin_date},
104                 end_date    => $args->{end_date},
105                 report_type => $args->{report_type}
106             }
107         );
108     }
109
110     my $job_report_report_type = $ud_provider->{report_type} || $counter_file->type;
111
112     # Prepare job report
113     my $report = {
114         report_type      => $job_report_report_type,
115         us_report_info   => $self->{us_report_info},
116         ud_provider_id   => $ud_provider->erm_usage_data_provider_id,
117         ud_provider_name => $ud_provider->name,
118     };
119
120     my $data = $self->decoded_data;
121     $data->{report}   = $report;
122     $data->{messages} = \@{ $self->{messages} };
123
124     $self->finish($data);
125 }
126
127 =head3 report_info
128
129 Setter for report_info
130
131 =cut
132
133 sub report_info {
134     my ( $self, $info ) = @_;
135
136     $self->{us_report_info}{$info}++;
137 }
138
139 =head3 set_job_size
140
141 Setter for job_size
142
143 =cut
144
145 sub set_job_size {
146     my ( $self, $size ) = @_;
147
148     $self->size($size)->store();
149 }
150
151 =head3 add_message
152
153     $job->add_message(
154         {
155             type => 'success', # success, warning or error
156             code => 'object_added', # object_added or object_already_exists
157             title => $row->{Title},
158             message => 'message',
159         }
160     );
161
162 Add a new job message
163
164 =cut
165
166 sub add_message {
167     my ( $self, $message ) = @_;
168
169     push @{ $self->{messages} }, $message;
170
171 }
172
173 =head3 enqueue
174
175 Enqueue the new job
176
177 =cut
178
179 sub enqueue {
180     my ( $self, $args ) = @_;
181
182     $self->SUPER::enqueue(
183         {
184             job_size  => 1,
185             job_args  => {%$args},
186             job_queue => 'long_tasks',
187         }
188     );
189 }
190
191 1;