5e43b2e1f208de13382272d1016d8a77187f98cc
[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 $commit = $self->param('commit');
22         my ( $items, $unique2id );
23         if ( my $apply_on_path = $self->param('apply_on_path') ) {
24                 $items = $MojoFacets::Data::loaded->{$apply_on_path}->{data}->{items};
25                 die "no $apply_on_path" unless $items;
26                 warn "using $items for $apply_on_path\n";
27         }
28         my $invalidate_columns;
29         my $changes;
30         my $stats;
31         my $glob = $self->_changes_path . '/*';
32         foreach my $t ( sort { $a cmp $b } glob $glob ) {
33                 my $e = retrieve($t);
34                 $e->{old} = [ $e->{old} ] unless ref $e->{old} eq 'ARRAY';
35                 if ( $items && exists $e->{unique} ) {
36                         my ($pk,$id) = %{ $e->{unique} };
37                         if ( ! $pk ) {
38                                 $e->{_status} = 'skip';
39                                 $stats->{skip}++;
40                                 push @$changes, $e;
41                                 next;
42                         }
43                         if ( ! defined $unique2id->{$pk} ) {
44                                 warn "unique2id $pk on ", $#$items + 1 ," items\n";
45                                 foreach my $i ( 0 .. $#$items ) {
46                                         $unique2id->{$pk}->{ $items->[$i]->{$pk}->[0] } = $i;
47                                 }
48                         }
49                         my $status = 'missing';
50                         if ( my $i = $unique2id->{$pk}->{$id} ) {
51                                 $status = 'found';
52                                 if ( $commit ) {
53                                         my $column = $e->{column} or die "no column";
54                                         $items->[$i]->{$column} = $e->{new};
55                                         warn "# commit $i $column ",dump( $e->{new} );
56                                         $invalidate_columns->{$column}++;
57                                 }
58                         }
59                         $e->{_status} = $status;
60                         $stats->{$status}++;
61                 } else {
62                         warn "no unique in ", dump($e);
63                 }
64                 push @$changes, $e;
65         }
66
67         foreach my $name ( keys %$invalidate_columns ) {
68                 MojoFacets::Data::__invalidate_path_column( $path, $name );
69         }
70
71         MojoFacets::Data::__path_modified( $path );
72
73         my @loaded = MojoFacets::Data::__loaded_paths();
74         warn "# loaded paths ",dump @loaded;
75
76         $self->render( changes => $changes, loaded => \@loaded, stats => $stats );
77 }
78
79 sub remove {
80         my $self = shift;
81
82         if ( my $t = $self->param('time') ) {
83                 unlink $self->_changes_path . '/' . $t;
84         }
85
86         $self->redirect_to('/changes');
87 }
88
89 1;