1 package MojoFacets::Changes;
6 use base 'Mojolicious::Controller';
9 use Data::Dump qw(dump);
14 my $path = $self->param('path') || $self->session('path');
15 $self->app->home->rel_dir('data') . '/' . $path . '.changes';
20 my $path = $self->param('path') || $self->session('path');
21 my $commit = $self->param('commit');
22 my ( $items, $unique2id );
24 $items = $MojoFacets::Data::loaded->{$path}->{data}->{items};
26 warn "$path not loaded";
27 $self->session('path', $path);
28 $self->redirect_to('/data/index');
31 warn "using $items for $path\n";
33 my $invalidate_columns;
36 my $glob = $self->_changes_path . '/*';
37 foreach my $t ( sort { $a cmp $b } glob $glob ) {
39 $e->{old} = [ $e->{old} ] unless ref $e->{old} eq 'ARRAY';
40 if ( $items && exists $e->{unique} ) {
41 my ($pk,$id) = %{ $e->{unique} };
43 $e->{_status} = 'skip';
48 if ( ! defined $unique2id->{$pk} ) {
49 warn "unique2id $pk on ", $#$items + 1 ," items\n";
50 foreach my $i ( 0 .. $#$items ) {
51 $unique2id->{$pk}->{ $items->[$i]->{$pk}->[0] } = $i;
54 my $status = 'missing';
55 if ( my $i = $unique2id->{$pk}->{$id} ) {
58 my $column = $e->{column} or die "no column";
59 $items->[$i]->{$column} = $e->{new};
60 warn "# commit $i $column ",dump( $e->{new} );
61 $invalidate_columns->{$column}++;
64 $e->{_status} = $status;
66 } elsif ( my $code = $e->{code} ) {
70 foreach my $i ( 0 .. $#$items ) {
71 MojoFacets::Data::__commit_path_code( $path, $i, $code, \$commit_changed );
74 $self->stash( 'commit_changed', $commit_changed );
75 warn "commit_changed in $t s ",dump( $e->{commit_changed}, $commit_changed );
76 $e->{commit_changed_this} = $commit_changed;
77 MojoFacets::Data::__invalidate_path_column( $path, $_ ) foreach keys %$commit_changed;
78 MojoFacets::Data::__path_rebuild_stats( $path );
82 warn "no unique in ", dump($e);
83 $stats->{no_unique}++;
88 foreach my $name ( keys %$invalidate_columns ) {
89 MojoFacets::Data::__invalidate_path_column( $path, $name );
92 MojoFacets::Data::__path_modified( $path );
94 my @loaded = MojoFacets::Data::__loaded_paths();
95 warn "# loaded paths ",dump @loaded;
97 $self->render( path => $path, changes => $changes, loaded => \@loaded, stats => $stats );
103 if ( my $t = $self->param('time') ) {
104 unlink $self->_changes_path . '/' . $t;
107 $self->redirect_to('/changes');