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;
}
}
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 {
stats => $stats,
full_path => $full_path,
size => -s $full_path,
+ mtime => (stat($full_path))[9],
data => $data,
};
;
warn "sorted: $order numeric: $numeric items: ", $#sorted + 1, "\n";
- warn "# sorted ",dump( @sorted );
+ #warn "# sorted ",dump( @sorted );
$loaded->{$path}->{sorted}->{$order} = [ @sorted ];
}
}
}
- 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} );
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};
$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,