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 $on_path = $self->param('on_path');
22 my $commit = $self->param('commit');
23 my $apply = $self->param('apply');
24 my ( $items, $unique2id );
26 $items = $MojoFacets::Data::loaded->{$on_path}->{data}->{items};
28 warn "$on_path not loaded";
29 return $self->redirect_to('/data/index?path=' . $on_path);
31 warn "using ", $#$items + 1, " items from $on_path\n";
33 my $invalidate_columns;
36 my $glob = $self->_changes_path . '/*';
37 my $status = 'unknown';
38 foreach my $t ( sort { $a cmp $b } glob $glob ) {
40 $e->{old} = [ $e->{old} ] unless ref $e->{old} eq 'ARRAY';
41 if ( $items && exists $e->{unique} ) {
42 my ($pk,$id) = %{ $e->{unique} };
44 $e->{_status} = 'skip';
49 if ( ! defined $unique2id->{$pk} ) {
50 warn "unique2id $pk on ", $#$items + 1 ," items\n";
51 foreach my $i ( 0 .. $#$items ) {
52 $unique2id->{$pk}->{ $items->[$i]->{$pk}->[0] } = $i;
56 if ( my $i = $unique2id->{$pk}->{$id} ) {
59 my $column = $e->{column} or die "no column";
60 $items->[$i]->{$column} = $e->{new};
61 warn "# commit $i $column ",dump( $e->{new} );
62 $invalidate_columns->{$column}++;
65 } elsif ( my $code = $e->{code} ) {
69 foreach my $i ( 0 .. $#$items ) {
70 MojoFacets::Data::__commit_path_code( $on_path, $i, $code, \$commit_changed );
73 $self->stash( 'commit_changed', $commit_changed );
74 warn "commit_changed in $t s ",dump( $e->{commit_changed}, $commit_changed );
75 $e->{commit_changed_this} = $commit_changed;
76 MojoFacets::Data::__invalidate_path_column( $on_path, $_ ) foreach keys %$commit_changed;
77 MojoFacets::Data::__path_rebuild_stats( $on_path );
80 if ( ( $apply || $commit ) && $e->{commit_changed} ) {
82 foreach my $c ( keys %{ $e->{commit_changed} } ) {
83 $status = 'missing' unless defined $MojoFacets::Data::loaded->{$path}->{stats}->{$c};
90 $e->{_status} = $status;
97 foreach my $name ( keys %$invalidate_columns ) {
98 MojoFacets::Data::__invalidate_path_column( $on_path, $name );
101 MojoFacets::Data::__path_modified( $on_path );
103 my @loaded = MojoFacets::Data::__loaded_paths();
104 warn "# loaded paths ",dump @loaded;
107 on_path => $on_path || $path,
117 if ( my $t = $self->param('time') ) {
118 unlink $self->_changes_path . '/' . $t;
121 return $self->redirect_to('/changes');