fix quotes ("" for single quote)
[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 $apply = $self->param('apply');
24         my ( $items, $unique2id );
25         if ( $on_path ) {
26                 $items = $MojoFacets::Data::loaded->{$on_path}->{data}->{items};
27                 if ( ! $items ) {
28                         warn "$on_path not loaded";
29                         $self->redirect_to('/data/index?path=' . $on_path);
30                         return;
31                 }
32                 warn "using ", $#$items + 1, " items from $on_path\n";
33         }
34         my $invalidate_columns;
35         my $changes;
36         my $stats;
37         my $glob = $self->_changes_path . '/*';
38         my $status = 'unknown';
39         foreach my $t ( sort { $a cmp $b } glob $glob ) {
40                 my $e = retrieve($t);
41                 $e->{old} = [ $e->{old} ] unless ref $e->{old} eq 'ARRAY';
42                 if ( $items && exists $e->{unique} ) {
43                         my ($pk,$id) = %{ $e->{unique} };
44                         if ( ! $pk ) {
45                                 $e->{_status} = 'skip';
46                                 $stats->{skip}++;
47                                 push @$changes, $e;
48                                 next;
49                         }
50                         if ( ! defined $unique2id->{$pk} ) {
51                                 warn "unique2id $pk on ", $#$items + 1 ," items\n";
52                                 foreach my $i ( 0 .. $#$items ) {
53                                         $unique2id->{$pk}->{ $items->[$i]->{$pk}->[0] } = $i;
54                                 }
55                         }
56                         $status = 'missing';
57                         if ( my $i = $unique2id->{$pk}->{$id} ) {
58                                 $status = 'found';
59                                 if ( $commit ) {
60                                         my $column = $e->{column} or die "no column";
61                                         $items->[$i]->{$column} = $e->{new};
62                                         warn "# commit $i $column ",dump( $e->{new} );
63                                         $invalidate_columns->{$column}++;
64                                 }
65                         }
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                         $status = 'code';
81                         if ( ( $apply || $commit ) && $e->{commit_changed} ) {
82                                 $status = 'found';
83                                 foreach my $c ( keys %{ $e->{commit_changed} } ) {
84                                         $status = 'missing' unless defined $MojoFacets::Data::loaded->{$path}->{stats}->{$c};
85                                 }
86                         }
87                 } else {
88                         $status = 'unknown';
89                 }
90
91                 $e->{_status} = $status;
92                 $stats->{$status}++;
93
94                 push @$changes, $e;
95         }
96
97
98         foreach my $name ( keys %$invalidate_columns ) {
99                 MojoFacets::Data::__invalidate_path_column( $on_path, $name );
100         }
101
102         MojoFacets::Data::__path_modified( $on_path );
103
104         my @loaded = MojoFacets::Data::__loaded_paths();
105         warn "# loaded paths ",dump @loaded;
106
107         $self->render(
108                 on_path => $on_path || $path,
109                 changes => $changes,
110                 loaded => \@loaded,
111                 stats => $stats,
112         );
113 }
114
115 sub remove {
116         my $self = shift;
117
118         if ( my $t = $self->param('time') ) {
119                 unlink $self->_changes_path . '/' . $t;
120         }
121
122         $self->redirect_to('/changes');
123 }
124
125 1;