we really need separate on_path for apply
[MojoFacets.git] / lib / MojoFacets / Changes.pm
index 5e43b2e..fc664fb 100644 (file)
@@ -18,12 +18,17 @@ sub _changes_path {
 sub index {
        my ( $self ) = @_;
        my $path = $self->param('path') || $self->session('path');
+       my $on_path = $self->param('on_path');
        my $commit = $self->param('commit');
        my ( $items, $unique2id );
-       if ( my $apply_on_path = $self->param('apply_on_path') ) {
-               $items = $MojoFacets::Data::loaded->{$apply_on_path}->{data}->{items};
-               die "no $apply_on_path" unless $items;
-               warn "using $items for $apply_on_path\n";
+       if ( $on_path ) {
+               $items = $MojoFacets::Data::loaded->{$on_path}->{data}->{items};
+               if ( ! $items ) {
+                       warn "$on_path not loaded";
+                       $self->redirect_to('/data/index?path=' . $on_path);
+                       return;
+               }
+               warn "using ", $#$items + 1, " items from $on_path\n";
        }
        my $invalidate_columns;
        my $changes;
@@ -58,22 +63,43 @@ sub index {
                        }
                        $e->{_status} = $status;
                        $stats->{$status}++;
+               } elsif ( my $code = $e->{code} ) {
+                       if ( $commit ) {
+                               my $commit_changed;
+                               my $t = time();
+                               foreach my $i ( 0 .. $#$items ) {
+                                       MojoFacets::Data::__commit_path_code( $on_path, $i, $code, \$commit_changed );
+                               }
+                               $t = time() - $t;
+                               $self->stash( 'commit_changed', $commit_changed );
+                               warn "commit_changed in $t s ",dump( $e->{commit_changed}, $commit_changed );
+                               $e->{commit_changed_this} = $commit_changed;
+                               MojoFacets::Data::__invalidate_path_column( $on_path, $_ ) foreach keys %$commit_changed;
+                               MojoFacets::Data::__path_rebuild_stats( $on_path );
+                       }
+                       $stats->{code}++;
                } else {
                        warn "no unique in ", dump($e);
+                       $stats->{no_unique}++;
                }
                push @$changes, $e;
        }
 
        foreach my $name ( keys %$invalidate_columns ) {
-               MojoFacets::Data::__invalidate_path_column( $path, $name );
+               MojoFacets::Data::__invalidate_path_column( $on_path, $name );
        }
 
-       MojoFacets::Data::__path_modified( $path );
+       MojoFacets::Data::__path_modified( $on_path );
 
        my @loaded = MojoFacets::Data::__loaded_paths();
        warn "# loaded paths ",dump @loaded;
 
-       $self->render( changes => $changes, loaded => \@loaded, stats => $stats );
+       $self->render(
+               on_path => $on_path || $path,
+               changes => $changes,
+               loaded => \@loaded,
+               stats => $stats,
+       );
 }
 
 sub remove {