Skip to content

Commit

Permalink
#25 Fixed groupby node
Browse files Browse the repository at this point in the history
  • Loading branch information
mcholste committed Nov 10, 2015
1 parent fbc3cc9 commit 5ac6e90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion web/lib/Query.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} };
}
Expand Down
44 changes: 36 additions & 8 deletions web/lib/Query/Sphinx.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 => [],
}
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5ac6e90

Please sign in to comment.