all values must be arrays
[MojoFacets.git] / lib / MojoFacets / Actions.pm
1 package MojoFacets::Actions;
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 index {
13         my $self = shift;
14
15         my $max = $self->param('max') || 50;
16         my $action_regex = join('|', $self->param('action_filter'));
17         warn "# action_regex $action_regex\n";
18
19         my $actions;
20
21         my $stats;
22         foreach my $path ( sort { $b cmp $a } glob '/tmp/actions/*' ) {
23                 if ( $path =~ m{/((\d+\.\d+)\.data\.(.+))$} ) {
24                         my ( $uid, $t, $action ) = ( $1, $2, $3 );
25                         $stats->{$action}++;
26                         next if $action_regex && $action !~ m/^($action_regex)$/;
27                         push @$actions, { uid => $uid, t => $t, action => $action }
28                                 if $#$actions < $max;
29                 } else {
30                         warn "ignore: $path\n";
31                 }
32         }
33
34         # Render template "actions/index.html.ep" with message
35         $self->render(actions => $actions, stats => $stats );
36 }
37
38
39 sub view {
40         my $self = shift;
41         my $uid = $self->param('uid');
42         $self->render( change => retrieve( "/tmp/actions/$uid" ), uid => $uid );
43 }
44
45 sub _changes_path {
46         my $self = shift;
47         my $path = $self->param('path') || $self->session('path');
48         $self->app->home->rel_dir('data') . '/' . $path . '.changes';
49 }
50
51 sub changes {
52         my ( $self ) = @_;
53         my $path = $self->param('path') || $self->session('path');
54         my $commit = $self->param('commit');
55         my ( $items, $unique2id );
56         if ( my $apply_on_path = $self->param('apply_on_path') ) {
57                 $items = $MojoFacets::Data::loaded->{$apply_on_path}->{data}->{items};
58                 die "no $apply_on_path" unless $items;
59                 warn "using $items for $apply_on_path\n";
60         }
61         my $changes;
62         my $stats;
63         my $glob = $self->_changes_path . '/*';
64         foreach my $t ( sort { $a cmp $b } glob $glob ) {
65                 my $e = retrieve($t);
66                 if ( $items ) {
67                         die "no unique in ", dump($e) unless exists $e->{unique};
68                         my ($pk,$id) = %{ $e->{unique} };
69                         if ( ! $pk ) {
70                                 $e->{_status} = 'skip';
71                                 $stats->{skip}++;
72                                 push @$changes, $e;
73                                 next;
74                         }
75                         if ( ! defined $unique2id->{$pk} ) {
76                                 warn "unique2id $pk on ", $#$items + 1 ," items\n";
77                                 foreach my $i ( 0 .. $#$items ) {
78                                         $unique2id->{$pk}->{ $items->[$i]->{$pk}->[0] } = $i;
79                                 }
80                         }
81                         my $status = 'missing';
82                         if ( my $i = $unique2id->{$pk}->{$id} ) {
83                                 $status = 'found';
84                                 $items->[$i]->{$pk} = $e->{new} if $commit;
85                         }
86                         $e->{_status} = $status;
87                         $stats->{$status}++;
88                 }
89                 push @$changes, $e;
90         }
91
92         my @loaded = MojoFacets::Data::__loaded_paths();
93         warn "# loaded paths ",dump @loaded;
94
95         $self->render( changes => $changes, loaded => \@loaded, stats => $stats );
96 }
97
98 sub remove {
99         my $self = shift;
100
101         if ( my $t = $self->param('time') ) {
102                 unlink $self->_changes_path . '/' . $t;
103         }
104
105         $self->redirect_to('/actions/changes');
106 }
107
108 1;