From 98f37c1019ea0e80d5a45a5633a0a182a43cbf9a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Thu, 15 Nov 2012 17:17:20 +0100 Subject: [PATCH] Progress genauer anzeigen --- SL/BackgroundJob/CsvImport.pm | 40 +++++++++++++++---- SL/Controller/CsvImport.pm | 16 ++++++-- SL/Controller/CsvImport/Base.pm | 22 +++++----- SL/Controller/CsvImport/Part.pm | 4 +- SL/DB/BackgroundJob.pm | 2 + .../csv_import/_deferred_results.html | 15 +++++-- 6 files changed, 73 insertions(+), 26 deletions(-) diff --git a/SL/BackgroundJob/CsvImport.pm b/SL/BackgroundJob/CsvImport.pm index 60147f8e2..66d830f90 100644 --- a/SL/BackgroundJob/CsvImport.pm +++ b/SL/BackgroundJob/CsvImport.pm @@ -59,23 +59,49 @@ sub do_import { my ($self) = @_; my $c = SL::Controller::CsvImport->new; + my $job = $self->{db_obj}; $c->profile($self->profile); - $c->type($self->{db_obj}->data_as_hash->{type}); + $c->type($job->data_as_hash->{type}); + + my $test = $job->data_as_hash->{test}; + + $self->track_progress( + progress => 0, + plan => { + 'parsing csv' => 1, + 'building data' => 2, + ( 'saving data' => 3, )x!!$test, + 'building report' => ($test ? 3 : 4), + }, + num_phases => ($test ? 3 : 4), + ); $c->add_progress_tracker($self); - $c->test_and_import(test => 1, session_id => $self->{db_obj}->data_as_hash->{session_id}); - my $report_id = $c->save_report; - $self->{db_obj}->set_data(report_id => $report_id); - $self->{db_obj}->save; + $c->test_and_import(test => 1, session_id => $job->data_as_hash->{session_id}); + + if ($c->errors) { + $job->set_data( + errors => $c->errors, + progress => -1, + )->save; + } else { - $c->track_progress(100); + my $report_id = $c->save_report; + $job->set_data(report_id => $report_id)->save; + + $c->track_progress(finished => 1); + } } sub track_progress { - my ($self, $progress) = @_; + my ($self, %params) = @_; + + my $data = $self->{db_obj}->data_as_hash; + my $progress = $data->{progress} || {}; + $progress->{$_} = $params{$_} for keys %params; $self->{db_obj}->set_data(progress => $progress); $self->{db_obj}->save; } diff --git a/SL/Controller/CsvImport.pm b/SL/Controller/CsvImport.pm index 4db025e7d..29947a19b 100644 --- a/SL/Controller/CsvImport.pm +++ b/SL/Controller/CsvImport.pm @@ -89,7 +89,11 @@ sub action_result { $self->profile($profile); - if ($data->{progress} < 100) { + if ($data->{errors} and my $first_error = $data->{errors}->[0]) { + flash('error', $::locale->text('There was an error parsing the csv file: #1 in line #2.', $first_error->[2], $first_error->[0])); + } + + if (!$data->{progress}{finished}) { $self->render('csv_import/_deferred_results', { no_layout => 1 }); } else { $self->action_report(report_id => $data->{report_id}, no_layout => 1); @@ -206,6 +210,7 @@ sub test_and_import_deferred { file => $self->csv_file_name, profile => $self->profile, type => $self->profile->type, + test => $params{test}, )->save; SL::System::TaskServer->start_if_not_running; @@ -234,6 +239,8 @@ sub test_and_import { $worker->run; + return if $self->errors; + $self->num_imported(0); $worker->save_objects if !$params{test}; @@ -313,6 +320,8 @@ sub char_map { sub save_report { my ($self, $report_id) = @_; + $self->track_progress(phase => 'building report', progress => 0); + my $clone_profile = $self->profile->clone_and_reset_deep; $clone_profile->save; # weird bug. if this isn't saved before adding it to the report, it will default back to the last profile. @@ -349,6 +358,7 @@ sub save_report { my $o2 = $o1 + @methods; for my $row (0 .. $#{ $self->data }) { + $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 100 == 0; my $data_row = $self->{data}[$row]; $sth->execute($report->id, $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods; @@ -393,10 +403,10 @@ sub setup_help { } sub track_progress { - my ($self, $progress) = @_; + my ($self, %params) = @_; for my $tracker ($self->progress_tracker) { - $tracker->track_progress($progress); + $tracker->track_progress(%params); } } diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index 67ca19185..4de38edcf 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -22,7 +22,7 @@ use Rose::Object::MakeMethods::Generic sub run { my ($self) = @_; - $self->controller->track_progress(0); + $self->controller->track_progress(phase => 'parsing csv', progress => 0); my $profile = $self->profile; $self->csv(SL::Helper::Csv->new(file => $self->file->file_name, @@ -35,14 +35,14 @@ sub run { map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char), )); - $self->controller->track_progress(1); + $self->controller->track_progress(progress => 10); my $old_numberformat = $::myconfig{numberformat}; $::myconfig{numberformat} = $self->controller->profile->get('numberformat'); $self->csv->parse; - $self->controller->track_progress(3); + $self->controller->track_progress(progress => 50); $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors; @@ -55,21 +55,17 @@ sub run { $self->controller->raw_data_headers({ used => { }, headers => [ ] }); $self->controller->info_headers({ used => { }, headers => [ ] }); - $::lxdebug->dump(0, "self", $self->controller->info_headers); - $::lxdebug->dump(0, "self", $self->controller->headers); - $::lxdebug->dump(0, "self", $self->controller->raw_data_headers); - my @objects = $self->csv->get_objects; - $self->controller->track_progress(4); + $self->controller->track_progress(progress => 70); my @raw_data = @{ $self->csv->get_data }; - $self->controller->track_progress(4.5); + $self->controller->track_progress(progress => 80); $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]); - $self->controller->track_progress(5); + $self->controller->track_progress(progress => 90); $self->check_objects; if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) { @@ -78,7 +74,7 @@ sub run { } $self->fix_field_lengths; - $self->controller->track_progress(99); + $self->controller->track_progress(progress => 100); $::myconfig{numberformat} = $old_numberformat; } @@ -387,6 +383,8 @@ sub save_objects { return unless $data->[0]; return unless $data->[0]{object}; + $self->controller->track_progress(phase => 'saving objects', progress => 0); # scale from 45..95%; + my $dbh = $data->[0]{object}->db; $dbh->begin_work; @@ -404,7 +402,7 @@ sub save_objects { } continue { if ($entry_index % 100 == 0) { $dbh->commit; - $self->controller->track_progress(45 + $entry_index/scalar(@$data) * 50); # scale from 45..95%; + $self->controller->track_progress(progress => $entry_index/scalar(@$data) * 100); # scale from 45..95%; $dbh->begin_work; } } diff --git a/SL/Controller/CsvImport/Part.pm b/SL/Controller/CsvImport/Part.pm index 36dccc62a..30c3719ca 100644 --- a/SL/Controller/CsvImport/Part.pm +++ b/SL/Controller/CsvImport/Part.pm @@ -103,12 +103,14 @@ sub check_objects { return unless @{ $self->controller->data }; + $self->controller->track_progress(phase => 'building data', progress => 0); + $self->makemodel_columns({}); my $i; my $num_data = scalar @{ $self->controller->data }; foreach my $entry (@{ $self->controller->data }) { - $self->controller->track_progress(8 + ($i/$num_data * 40)) if $i % 100 == 0; # scale from 5..45% + $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0; $self->check_buchungsgruppe($entry); $self->check_type($entry); diff --git a/SL/DB/BackgroundJob.pm b/SL/DB/BackgroundJob.pm index 1a9c86922..c83868502 100644 --- a/SL/DB/BackgroundJob.pm +++ b/SL/DB/BackgroundJob.pm @@ -83,6 +83,8 @@ sub set_data { my $data = YAML::Load($self->data); $data->{$_} = $data{$_} for keys %data; $self->data(YAML::Dump($data)); + + $self; } sub validate { diff --git a/templates/webpages/csv_import/_deferred_results.html b/templates/webpages/csv_import/_deferred_results.html index f4267b377..bc6d68379 100644 --- a/templates/webpages/csv_import/_deferred_results.html +++ b/templates/webpages/csv_import/_deferred_results.html @@ -1,5 +1,12 @@ +[%- USE L %] +[%- USE T8 %] +[%- USE HTML %] + +

[% 'Import Status' | $T8 %]

+ +[% PROCESS 'common/flash.html' %] +
-[% IF progress < 100 %] -[% END %] -- 2.20.1