/usr/share/perl5/Lire/Count.pm is in lire 2:2.1.1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | package Lire::Count;
use strict;
use base qw/ Lire::Aggregate /;
use Carp;
use Lire::DataTypes qw/ format_numeric_type /;
use Lire::Utils qw/ ratio100 sql_quote_name check_object_param/;
=pod
=head1 NAME
Lire::Count - Lire class that implements the count operator
=head1 SYNOPSIS
use Lire::Count
=head1 DESCRIPTION
Class that implements the count operator. This operator counts the
number of DLF records in the current group.
If the fields attribute is set, the count statistics will be equals to
the number of different values present in the fields specified by that
attribute.
Its also possible to express the count as a ratio of the total count
for the group or table.
=head1 METHODS
=head2 new( %params )
Creates a new Lire::Count object. In addition to the values
supported by its parents, the fields and ratio attributes will be
initialized to the values specified in the %params argument.
=cut
sub new {
my $self = bless { 'ratio' => 'none', }, shift;
$self->init( @_, 'op' => 'count' );
my %args = @_;
$self->fields( $args{'fields'} )
if exists $args{'fields'};
$self->ratio( $args{'ratio'} )
if exists $args{'ratio'};
return $self;
}
=pod
=head2 fields( [$new_fields] )
Returns the fields for which we are counting the different values.
This a reference to an array of DLF field names.
If the $new_fields parameter is set, it will be used as the new
$fields value. It must be an array reference and should only contains
valid field names for the current report specification's schema.
=cut
sub fields {
my ( $self, $fields ) = @_;
if ( @_ == 2 ) {
if ( defined $fields ) {
check_object_param( $fields, 'fields', 'ARRAY' );
foreach my $f ( @$fields ) {
croak "'$f' isn't a defined field in the specification's schemas"
unless $self->report_spec()->has_field( $f );
}
}
$self->{'fields'} = $fields;
}
$self->{'fields'};
}
=pod
=head2 ratio([$new_ratio])
Returns how the count will be expressed. This can one of three
possible values:
=over
=item none
Default. The absolute count will be used.
=item group
The count will be expressed as a percentage of the group's count.
=item table
The count will be expressed as a percentage of the table's total
count.
=back
=cut
sub ratio {
my ( $self, $ratio ) = @_;
if ( @_ == 2 ) {
croak "invalid value for ratio attribute: $ratio, should be one of none, group or table"
unless $ratio =~ /^(none|group|table)$/;
$self->{'ratio'} = $ratio;
}
$self->{'ratio'};
}
#------------------------------------------------------------------------
# Method xml_attrs()
#
# Implementation needed by Lire::Aggregate
sub xml_attrs {
my ($self) = @_;
my $attr = "";
if ( exists $self->{'fields'} ) {
my $fields = join " ", @{$self->{'fields'}};
$attr .= qq{fields="$fields"};
}
$attr .= qq{ ratio="$self->{'ratio'}"};
$attr;
}
# Implementats Lire::Aggregate::create_numerical_info
sub create_numerical_info {
my ( $self, $group_info ) = @_;
$group_info->create_column_info( $self->name, 'numerical', 'int',
$self->label );
}
# Overrides Lire::SimpleStat::sql_required_fields
sub sql_required_fields {
my $self = $_[0];
return $self->{'fields'} || [];
}
# Implements Lire::Aggregate::sql_aggr_expr
sub sql_aggr_expr {
my $self = $_[0];
if (defined $self->{'fields'}) {
my @fields = map { sql_quote_name( $_ ) } @{ $self->{'fields'} };
return 'lr_count_uniq(' . join (',', @fields) .')';
} else {
return 'count(*)';
}
}
# Implements Lire::Aggregate::create_value()
sub create_value {
my ( $self, $parent_group, $row ) = @_;
my %value;
my $name = $self->name();
$value{'value'} = $row->{$name};
$self->set_missing_cases_value( $row, \%value );
if ( $self->{'ratio'} eq 'none' ) {
$value{'content'} = format_numeric_type( $row->{$name}, 'int' );
} else {
my $group_sum = $self->_get_ratio_denominator( $parent_group );
my $total = defined $group_sum->{'value'} ?
$group_sum->{'value'} : $row->{$name};
$value{'content'} = ratio100( $row->{$name}, $total );
}
return \%value;
}
sub _get_ratio_denominator {
my ( $self, $parent_group ) = @_;
return ( $self->{'ratio'} eq 'table' || ! $parent_group->parent_entry() )
? $parent_group->subreport()->get_summary_value( $self->name())
: $parent_group->get_summary_value( $self->name() );
}
# Implements Lire::ReportOperator::init_group_data
sub init_group_data {
my ( $self ) = @_;
if ( $self->{'key_maker'} ) {
return [ 0, {} ];
} else {
my $scalar = 0;
return \$scalar;
}
}
# Implements Lire::ReportOperator::merge_group_data
sub merge_group_data {
my ( $self, $value, $data ) = @_;
# FIXME: We should add a note when a key_maker was used. For
# example, there is no guarentee that the sum of the number of
# hosts that visited during two reports is the same than the
# number of hosts calculated over the period spanned by the two
# reports.
# To merge two counts, we simply add them
$$data += $value->{'value'};
return;
}
# Implements Lire::ReportOperator::end_group_data
sub end_group_data {
my ( $self, $data ) = @_;
$data->[1] = undef
if $self->{'key_maker'};
return;
}
# Implements Lire::Aggregate::data2dlf()
sub data2dlf {
my ($self, $data) = @_;
my $name = $self->name();
return { "$name" => $self->{'key_maker'} ? $data->[0] : $$data,
"_lr_${name}_mc" => $self->missing_cases( $data ),
};
}
# keep perl happy
1;
__END__
=head1 SEE ALSO
Lire::ReportSpec(3pm), Lire::ReportOperator(3pm), Lire::Aggregator(3pm), Lire::Aggregate(3pm).
=head1 AUTHOR
Francis J. Lacoste <flacoste@logreport.org>
=head1 VERSION
$Id: Count.pm,v 1.16 2008/03/09 19:27:31 vanbaal Exp $
=head1 COPYRIGHT
Copyright (C) 2001, 2002 Stichting LogReport Foundation LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html.
=cut
|