diff --git a/web/lib/Query.pm b/web/lib/Query.pm index 5bd5013..67740cc 100644 --- a/web/lib/Query.pm +++ b/web/lib/Query.pm @@ -997,7 +997,7 @@ sub _attr { my $self = shift; my $field_name = shift; my $class_id = shift; - + if (defined $Fields::Field_to_order->{$field_name}){ return $Fields::Field_order_to_attr->{ $Fields::Field_to_order->{$field_name} }; } diff --git a/web/lib/Query/Sphinx.pm b/web/lib/Query/Sphinx.pm index 8aab11d..d478554 100644 --- a/web/lib/Query/Sphinx.pm +++ b/web/lib/Query/Sphinx.pm @@ -22,6 +22,9 @@ sub BUILD { my $self = shift; $self->schemas($self->_get_index_groups()); + if (not $self->peer_label){ + $self->peer_label('127.0.0.1'); + } return $self; } @@ -210,12 +213,21 @@ sub _build_query { my $match_str = $self->_get_match_str($class_id, $terms_and_filters->{searches}, $group_key); my $attr_str = $self->_get_attr_tests($class_id, $terms_and_filters->{filters}); $self->log->debug('attr_str: ' . $attr_str); + $self->log->debug('groupby: ' . $self->_get_groupby_clause($class_id)); + $self->log->debug('orderby: ' . $self->_get_orderby_clause($class_id)); + $self->log->debug('class_id: ' . $class_id); my $query = { select => $self->_get_select_clause($class_id, $attr_str), where => $self->_get_where_clause($class_id, $match_str), - groupby => $self->_get_groupby_clause($class_id), - orderby => $self->_get_orderby_clause($class_id), + #groupby => $self->_get_groupby_clause($class_id), + #orderby => $self->_get_orderby_clause($class_id), }; + if ($self->_get_groupby_clause($class_id)){ + $query->{groupby} = $self->_get_groupby_clause($class_id); + } + if ($self->_get_orderby_clause($class_id)){ + $query->{orderby} = $self->_get_orderby_clause($class_id); + } $self->log->debug('query: ' . Dumper($query)); push @queries, $query; } @@ -246,6 +258,7 @@ sub _get_groupby_clause { my $self = shift; my $class_id = shift; + return '_groupby' if $self->groupby eq 'node'; return '' unless $self->groupby; return $self->_attr($self->groupby, $class_id); } @@ -609,8 +622,13 @@ sub _get_select_clause { } if ($self->groupby){ + my $groupby = $self->_attr($self->groupby, $class_id); + if ($groupby eq ''){ + $groupby = 1; + } return { - clause => 'SELECT id, COUNT(*) AS _count, ' . $self->_attr($self->groupby, $class_id) . ' AS _groupby, ' + #clause => 'SELECT id, COUNT(*) AS _count, ' . $self->_attr($self->groupby, $class_id) . ' AS _groupby, ' + clause => 'SELECT id, COUNT(*) AS _count, ' . $groupby . ' AS _groupby, ' . $attr_string . ' AS attr_tests, ' . $import_where . ' AS import_tests', values => [], } @@ -1269,13 +1287,23 @@ sub _format_records_groupby { my $ret = shift; my %agg; - my $total_records = 0; + #my $total_records = 0; # One-off for grouping by node if ($self->groupby eq 'node'){ my $node_label = $self->peer_label ? $self->peer_label : '127.0.0.1'; - $agg{$node_label} = int($ret->{meta}->{total_found}); - next; + $self->log->debug("Formatting node label $node_label"); + if (not scalar keys %{ $ret->{results} }){ + return; + } + + my $key = shift [ keys %{ $ret->{results} } ]; + my $count = $ret->{results}->{$key}->{_count}; + $self->results->total_records($count); + $self->results->add_results({ $self->groupby => [ + { intval => $count, '_groupby' => $node_label, '_count' => $count } + ] }); + return; } foreach my $id (sort { $a <=> $b } keys %{ $ret->{results} }){ my $row = $ret->{results}->{$id}; @@ -1316,7 +1344,7 @@ sub _format_records_groupby { my $increment = $Fields::Time_values->{ $self->groupby }; my $use_gmt = $increment >= 86400 ? 1 : 0; foreach my $key (sort { $a <=> $b } keys %agg){ - $total_records += $agg{$key}; + #$total_records += $agg{$key}; my $unixtime = $key * $increment; my $client_localtime = $unixtime - $self->parser->timezone_diff($unixtime); @@ -1352,7 +1380,7 @@ sub _format_records_groupby { # Sort these in descending value order my @tmp; foreach my $key (sort { $agg{$b} <=> $agg{$a} } keys %agg){ - $total_records += $agg{$key}; + #$total_records += $agg{$key}; push @tmp, { intval => $agg{$key}, '_groupby' => $key, '_count' => $agg{$key} }; last if scalar @tmp >= $self->limit; }