we really need separate on_path for apply
[MojoFacets.git] / lib / MojoFacets / Changes.pm
1 package MojoFacets::Changes;
2
3 use strict;
4 use warnings;
5
6 use base 'Mojolicious::Controller';
7
8 use Storable;
9 use Data::Dump qw(dump);
10 use MojoFacets::Data;
11
12 sub _changes_path {
13         my $self = shift;
14         my $path = $self->param('path') || $self->session('path');
15         $self->app->home->rel_dir('data') . '/' . $path . '.changes';
16 }
17
18 sub index {
19         my ( $self ) = @_;
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 ( $items, $unique2id );
24         if ( $on_path ) {
25                 $items = $MojoFacets::Data::loaded->{$on_path}->{data}->{items};
26                 if ( ! $items ) {
27                         warn "$on_path not loaded";
28                         $self->redirect_to('/data/index?path=' . $on_path);
29                         return;
30                 }
31                 warn "using ", $#$items + 1, " items from $on_path\n";
32         }
33         my $invalidate_columns;
34         my $changes;
35         my $stats;
36         my $glob = $self->_changes_path . '/*';
37         foreach my $t ( sort { $a cmp $b } glob $glob ) {
38                 my $e = retrieve($t);
39                 $e->{old} = [ $e->{old} ] unless ref $e->{old} eq 'ARRAY';
40                 if ( $items && exists $e->{unique} ) {
41                         my ($pk,$id) = %{ $e->{unique} };
42                         if ( ! $pk ) {
43                                 $e->{_status} = 'skip';
44                                 $stats->{skip}++;
45                                 push @$changes, $e;
46                                 next;
47                         }
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;
52                                 }
53                         }
54                         my $status = 'missing';
55                         if ( my $i = $unique2id->{$pk}->{$id} ) {
56                                 $status = 'found';
57                                 if ( $commit ) {
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}++;
62                                 }
63                         }
64                         $e->{_status} = $status;
65                         $stats->{$status}++;
66                 } elsif ( my $code = $e->{code} ) {
67                         if ( $commit ) {
68                                 my $commit_changed;
69                                 my $t = time();
70                                 foreach my $i ( 0 .. $#$items ) {
71                                         MojoFacets::Data::__commit_path_code( $on_path, $i, $code, \$commit_changed );
72                                 }
73                                 $t = time() - $t;
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( $on_path, $_ ) foreach keys %$commit_changed;
78                                 MojoFacets::Data::__path_rebuild_stats( $on_path );
79                         }
80                         $stats->{code}++;
81                 } else {
82                         warn "no unique in ", dump($e);
83                         $stats->{no_unique}++;
84                 }
85                 push @$changes, $e;
86         }
87
88         foreach my $name ( keys %$invalidate_columns ) {
89                 MojoFacets::Data::__invalidate_path_column( $on_path, $name );
90         }
91
92         MojoFacets::Data::__path_modified( $on_path );
93
94         my @loaded = MojoFacets::Data::__loaded_paths();
95         warn "# loaded paths ",dump @loaded;
96
97         $self->render(
98                 on_path => $on_path || $path,
99                 changes => $changes,
100                 loaded => \@loaded,
101                 stats => $stats,
102         );
103 }
104
105 sub remove {
106         my $self = shift;
107
108         if ( my $t = $self->param('time') ) {
109                 unlink $self->_changes_path . '/' . $t;
110         }
111
112         $self->redirect_to('/changes');
113 }
114
115 1;