small circular points
[MojoFacets.git] / lib / MojoFacets / Changes.pm
index 9028236..c1ddc04 100644 (file)
@@ -6,65 +6,132 @@ use warnings;
 use base 'Mojolicious::Controller';
 
 use Storable;
+use Data::Dump qw(dump);
+use MojoFacets::Data;
 
-sub index {
+sub _changes_path {
        my $self = shift;
+       my $path = $self->param('path') || $self->session('path');
+       $self->app->home->rel_file('data') . '/' . $path . '.changes';
+}
 
-       my $max = $self->param('max') || 50;
-       my $action_regex = join('|', $self->param('action_filter'));
-       warn "# action_regex $action_regex\n";
+sub _hash_eq {
+       my ( $a_ref, $b_ref ) = @_;
 
-       my $actions;
+       warn "# _hash_eq ",dump($a_ref,$b_ref);
 
+       local $Storable::canonical = 1;
+       return eval { Storable::freeze( $a_ref ) } eq eval { Storable::freeze( $b_ref ) };
+}
+
+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 $apply = $self->param('apply');
+       my ( $items, $unique2id );
+       if ( $on_path ) {
+               $items = $MojoFacets::Data::loaded->{$on_path}->{data}->{items};
+               if ( ! $items ) {
+                       warn "$on_path not loaded";
+                       return $self->redirect_to('/data/index?path=' . $on_path);
+               }
+               warn "using ", $#$items + 1, " items from $on_path\n";
+       }
+       my $invalidate_columns;
        my $changes;
-       foreach my $path ( sort { $b cmp $a } glob '/tmp/changes/*' ) {
-               if ( $path =~ m{/((\d+\.\d+)\.data\.(.+))$} ) {
-                       my ( $uid, $t, $action ) = ( $1, $2, $3 );
-                       $actions->{$action}++;
-                       next if $action_regex && $action !~ m/^($action_regex)$/;
-                       push @$changes, { uid => $uid, t => $t, action => $action }
-                               if $#$changes < $max;
+       my $stats;
+       my $glob = $self->_changes_path . '/*';
+       my $status = 'unknown';
+       foreach my $t ( sort { $a cmp $b } glob $glob ) {
+               my $e = retrieve($t);
+               $e->{old} = [ $e->{old} ] unless ref $e->{old} eq 'ARRAY';
+               if ( $items && exists $e->{unique} ) {
+                       my ($pk,$id) = %{ $e->{unique} };
+                       if ( ! $pk ) {
+                               $e->{_status} = 'skip';
+                               $stats->{skip}++;
+                               push @$changes, $e;
+                               next;
+                       }
+                       if ( ! defined $unique2id->{$pk} ) {
+                               warn "unique2id $pk on ", $#$items + 1 ," items\n";
+                               foreach my $i ( 0 .. $#$items ) {
+                                       $unique2id->{$pk}->{ $items->[$i]->{$pk}->[0] } = $i;
+                               }
+                       }
+                       $status = 'missing';
+                       if ( my $i = $unique2id->{$pk}->{$id} ) {
+                               if ( _hash_eq( $e->{old}, $items->[$i]->{$e->{column}} ) ) {
+                                       $status = 'found';
+                                       if ( $commit ) {
+                                               my $column = $e->{column} or die "no column";
+                                               $items->[$i]->{$column} = $e->{new};
+                                               warn "# commit $i $column ",dump( $e->{new} );
+                                               $invalidate_columns->{$column}++;
+                                       }
+                               } else {
+                                       $status = 'source-changed';
+                               }
+                       }
+               } 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 );
+                       }
+                       $status = 'code';
+                       if ( ( $apply || $commit ) && $e->{commit_changed} ) {
+                               $status = 'found';
+                               foreach my $c ( keys %{ $e->{commit_changed} } ) {
+                                       $status = 'missing' unless defined $MojoFacets::Data::loaded->{$path}->{stats}->{$c};
+                               }
+                       }
                } else {
-                       warn "ignore: $path\n";
+                       $status = 'unknown';
                }
+
+               $e->{_status} = $status;
+               $stats->{$status}++;
+
+               push @$changes, $e;
        }
 
-       # Render template "changes/index.html.ep" with message
-       $self->render(message => 'Latest Changes', changes => $changes, actions => $actions );
-}
 
+       foreach my $name ( keys %$invalidate_columns ) {
+               MojoFacets::Data::__invalidate_path_column( $on_path, $name );
+       }
 
-sub view {
-       my $self = shift;
-       my $uid = $self->param('uid');
-       $self->render( change => retrieve( "/tmp/changes/$uid" ), uid => $uid );
-}
+       MojoFacets::Data::__path_modified( $on_path );
 
-sub _edit_path {
-       my $self = shift;
-       my $path = $self->param('path') || $self->session('path');
-       $self->app->home->rel_dir('data') . '/' . $path . '.edits';
-}
+       my @loaded = MojoFacets::Data::__loaded_paths();
+       warn "# loaded paths ",dump @loaded;
 
-sub edits {
-       my ( $self ) = @_;
-       my $path = $self->param('path') || $self->session('path');
-       my $edits;
-       my $glob = $self->_edit_path . '/*';
-       foreach my $t ( sort { $b cmp $a } glob $glob ) {
-               push @$edits, retrieve("$t");
-       }
-       $self->render( edits => $edits );
+       $self->render(
+               on_path => $on_path || $path,
+               changes => $changes,
+               loaded => \@loaded,
+               stats => $stats,
+       );
 }
 
-sub edit {
+sub remove {
        my $self = shift;
 
-       if ( my $t = $self->param('remove') ) {
-               unlink $self->_edit_path . '/' . $t;
+       if ( my $t = $self->param('time') ) {
+               unlink $self->_changes_path . '/' . $t;
        }
 
-       $self->redirect_to('/changes/edits');
+       return $self->redirect_to('/changes');
 }
 
 1;