unpack numeric values separated by commas
[MojoFacets.git] / lib / MojoFacets / Data.pm
index 51e02f9..e69c16e 100644 (file)
@@ -47,11 +47,15 @@ sub index {
 sub _load_path {
        my ( $self, $path ) = @_;
 
-       return if defined $loaded->{$path}->{data};
-
        my $full_path = $self->app->home->rel_file( 'data/' . $path );
        die "$full_path $!" unless -r $full_path;
 
+       if ( defined $loaded->{$path}->{data} ) {
+               my $mtime = (stat($full_path))[9];
+               return if $loaded->{$path}->{mtime} == $mtime;
+               warn "reload $full_path, modified ", time() - $mtime, " seconds ago\n";
+       }
+
        # we could use Mojo::JSON here, but it's too slow
 #      $data = from_json read_file $path;
        my $data = read_file $full_path;
@@ -91,7 +95,12 @@ sub _load_path {
                                }
                        }
                        my $item;
-                       $item->{ $header[$_] || "f_$_" } = [ $v[$_] ] foreach ( 0 .. $#v );
+                       foreach my $i ( 0 .. $#v ) {
+                               my $v = $v[$i];
+                               # unpack numeric values separated by commas
+                               my $a = $v =~ m/\d+\s*,\s*\d+/ ? [ split(/\,\s*/,$v) ] : [ $v ];
+                               $item->{ $header[$i] || "f_$i" } = $a;
+                       }
                        push @{ $data->{items} }, $item;
                }
        } else {
@@ -149,6 +158,7 @@ sub _load_path {
                stats  => $stats,
                full_path => $full_path,
                size => -s $full_path,
+               mtime => (stat($full_path))[9],
                data => $data,
        };
 
@@ -405,7 +415,7 @@ sub _data_sorted_by {
        ;
 
        warn "sorted: $order numeric: $numeric items: ", $#sorted + 1, "\n";
-       warn "# sorted ",dump( @sorted );
+       #warn "# sorted ",dump( @sorted );
 
        $loaded->{$path}->{sorted}->{$order} = [ @sorted ];
 }
@@ -446,7 +456,7 @@ sub items {
                }
        }
 
-       my $all_filters = join(' ',sort @filter_names);
+       my $all_filters = join(' ',sort @filter_names,'order:',$order);
 
 #      warn "# all_filters $all_filters ", dump( $loaded->{$path}->{filtered}->{$all_filters} );
 
@@ -563,8 +573,7 @@ sub facet {
        my $data = $self->_loaded('data');
 
        my $filters = $self->_current_filters;
-       my $all_filters = join(' ',sort keys %$filters);
-#      warn "# all_filters $all_filters ", dump( $loaded->{$path}->{filtered}->{$all_filters} );
+       my $all_filters = join(' ',sort keys %$filters,'order:',$self->session('order'));
        my $filtered = $loaded->{$path}->{filtered}->{$all_filters}
                if defined $loaded->{$path}->{filtered}->{$all_filters};
 
@@ -596,22 +605,26 @@ sub facet {
 
        $checked = $self->_checked( @{ $filters->{$name} } ) if defined $filters->{$name};
 
-       my $sort = $self->param('sort') || 'c';
-
-       # sort facet numerically if more >50% elements are numeric
        my $numeric = $self->_is_numeric($name);
 
+       my $sort = $self->param('sort');
+       # sort numeric facets with more than 5 values ascending
+       $sort ||= $numeric && $#facet_names > 4 ? 'a' : 'c';
+
        @facet_names = sort {
-               if ( $sort =~ m/a/i ) {
-                       $numeric ? $a <=> $b : lc $a cmp lc $b;
-               } elsif ( $sort =~ m/d/i ) {
-                       $numeric ? $b <=> $a : lc $b cmp lc $a;
-               } elsif ( $sort =~ m/c/i ) {
-                       ( $facet->{$b} || -1 ) <=> ( $facet->{$a} || -1 )
+               my $result;
+               if ( $sort eq 'a' ) {
+                       $result = $numeric ? $a <=> $b : lc $a cmp lc $b;
+               } elsif ( $sort eq 'd' ) {
+                       $result = $numeric ? $b <=> $a : lc $b cmp lc $a;
+               } elsif ( $sort eq 'c' ) {
+                       $result = ( $facet->{$b} || -1 ) <=> ( $facet->{$a} || -1 )
                } else {
                        warn "unknown sort: $sort";
-                       $a cmp $b;
+                       $result = $a cmp $b;
                }
+               $result = $a cmp $b unless defined $result; # FIXME cludge for numeric facets with invalid data
+               $result;
        } @facet_names;
 
        $self->render( name => $name, facet => $facet, checked => $checked,