/usr/share/perl5/Lire/Aggregator.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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 | package Lire::Aggregator;
use strict;
use base qw/ Lire::ReportOperator /;
use Lire::ReportOperator;
use constant MERGE_INITED => 1;
use constant MERGE_FINISHED => 2;
use Carp;
=pod
=head1 NAME
Lire::Aggregator - Base class for all aggregator operators
=head1 SYNOPSIS
use base qw/ Lire::Aggregator /;
=head1 DESCRIPTION
The Lire::Aggregator is the base class for all the aggregator
operators available in Lire. It implements behavior common to all the
aggregator as well as defining some methods that need to be
implemented by subclasses.
In Lire, an aggregator is an operator which will group DLF records.
Other operators like avg or sum will then compute values in these
groups of DLF records. Aggregators can be nested to compute values on
hierarchical groups.
=head1 METHODS
=head2 ops( [$new_ops] )
Returns the operators contained in this aggregator. The returned value
is a reference to an array of Lire::ReportOperator objects.
If the $new_ops is used, it changes the content of this aggragagor to
these new values. $new_ops should be a reference to an array
containing Lire::ReportOperator objects.
=cut
sub ops {
my ( $self, $ops ) = @_;
if ( @_ == 2 ) {
croak "$ops isn't an array reference"
unless UNIVERSAL::isa( $ops, "ARRAY" );
croak "fields array is empty"
if @$ops == 0;
foreach my $op ( @$ops ) {
croak "$op isn't of type Lire::ReportOperator"
unless UNIVERSAL::isa( $op, "Lire::ReportOperator" );
}
$self->{'ops'} = $ops;
}
return $self->{'ops'};
}
=pod
=head2 op_by_name( $name )
Returns the operator named $name in this aggregator. An exception is
thrown if there is no such operator.
=cut
sub op_by_name {
my ( $self, $name ) = @_;
# Check in ops
foreach my $op ( @{$self->{'ops'}} ) {
return $op if $op->name eq $name;
}
croak "No operation named $name\n";
}
=pod
=head2 is_name_defined( $name )
Returns true if this aggregator contains an operator named $name.
=cut
sub is_name_defined {
my ( $self, $name ) = @_;
return 1 if $name eq $self->name();
# Check in ops and aggregator's children
foreach my $p ( @{$self->{'ops'}} ) {
return 1 if $p->name() eq $name;
if ( $p->isa( 'Lire::Aggregator' ) ) {
return 1 if $p->is_name_defined( $name );
}
}
return 0;
}
=pod
=head1 METHODS FOR SUBCLASSES
=cut
#------------------------------------------------------------------------
# Method init( %params )
#
# Initialize the ops attribute.
sub init {
my ($self, %params) = @_;
$self->{'ops'} = [];
$self->SUPER::init( %params );
return;
}
=pod
=head2 print( $fh, $pfx )
This methods implements the print() method required by
Lire::ReportOpetor. It prints the XML element named after op() and
takes care of writing the XML representation of all the children
operation. It also takes care of writing the name and label attribute.
Other attributes can be added to the XML element by overriding the
xml_attrs() method. Other children elements could be added to the
output stream by overriding the print_content() method.
=cut
sub print {
my ( $self, $fh, $pfx ) = @_;
$fh ||= \*STDOUT;
$pfx ||= 0;
my $prefix = " " x $pfx;
my $attrs = $self->xml_attrs;
print $fh $prefix, '<lire:', $self->op;
print $fh ' ', $attrs
if length $attrs;
print $fh qq{ label="$self->{'label'}"}
if $self->{'label'};
print $fh ">\n";
$self->print_content( $fh, $pfx + 1);
print $fh $prefix, "</lire:", $self->op, ">\n";
}
=pod
=head2 xml_attrs()
This method can be used to write additional XML attributes. The
returned string will be output in the XML element.
=cut
sub xml_attrs {
return "";
}
=pod
=head2 print_content( $fh, $pfx )
This method prints the operators contained in this aggregator. It can
be overriden to add some other elements.
=cut
sub print_content {
my ( $self, $fh, $pfx ) = @_;
foreach my $o ( @{$self->{'ops'}} ) {
$o->print( $fh, $pfx );
}
}
=pod
=head2 create_group_info( $info )
FIXME
Subclasses have to override the create_categorical_info() method for
this implementation.
=cut
sub create_group_info {
my ( $self, $info ) = @_;
$info = $self->maybe_nest_group_info( $info );
$self->create_categorical_info( $info );
foreach my $op ( @{$self->ops} ) {
if ( $op->isa( 'Lire::Aggregator' ) ) {
$op->create_group_info( $info );
} else {
$op->create_numerical_info( $info );
}
}
}
=pod
=head2 create_categorical_info( $info )
This method is used by the implementation of create_group_info() to add
the categorical ColumnInfo provided by the aggregator.
=cut
sub create_categorical_info {
croak "unimplemented create_categorical_info() in ", ref $_[0];
}
#------------------------------------------------------------------------
# Method maybe_nest_group_info( $info )
#
# This method should be used by subclasses in their create_group_info()
# implementation to select the GroupInfo to which they should add their
# ColumnInfo.
#
# This method takes care of creating a GroupInfo if the aggregator has a
# parent. It returns the GroupInfo object to which the aggregator should
# add its ColumnInfo objects.
#
sub maybe_nest_group_info {
my ( $self, $info ) = @_;
if ( $self->parent ) {
return $info->create_group_info( $self->name );
} else {
return $info;
}
}
=pod
=head2 create_entries( $subreport )
This method is used by Lire::ReportSpec to fill the
Lire::Report::Subreport with the entries when creating the subreport.
The $subreport parameter contains the Subreport object to which the
subreport's entries should get added.
This method will only be called on the top-level aggregator in the
report.
=cut
sub create_entries {
my ( $self, $subreport ) = @_;
if ( $self->{'store'} ) {
my $query = new Lire::DlfQuery( $self->report_spec()->schema()->id() );
foreach my $schema ( @{ $self->report_spec()->joined_schemas() } ) {
$query->join_stream( $schema );
}
$self->build_query( $query );
my $expr = $self->report_spec()->filter_spec();
if ( $expr ) {
$query->set_filter_clause( $expr->sql_expr(),
@{$expr->sql_params()} );
}
$self->build_table( $self->{'store'}, $query, $subreport );
} elsif ( defined $self->{'_state'} ) {
# Merging
croak "end_merge() wasn't called"
unless $self->{'_state'} == MERGE_FINISHED;
$subreport->nrecords( $self->{'data'}[1] );
$subreport->missing_cases( $self->{'data'}[3] );
$self->set_summary_values( $subreport, $self->{'data'}[0] );
$self->create_group_entries( $subreport, $self->{'data'}[2] );
}
return;
}
=pod
=head2 build_query( $query )
FIXME
=cut
sub build_query {
my ($self, $query ) = @_;
$query->add_aggr_field( '_lr_nrecords', 'count(*)' )
unless grep { $_->isa( 'Lire::Aggregator' ) } @{$self->ops()};
foreach my $op ( @{ $self->ops() }) {
$op->build_query( $op->isa( 'Lire::Aggregator' )
? $query->create_nested_query()
: $query );
}
}
sub build_table_summary {
my ( $self, $store, $query, $table ) = @_;
my $result = $query->execute_summary( $store );
my $summary = $result->next_row();
$table->nrecords( $summary->{'_lr_nrecords'} );
$self->set_group_summary( $table, $summary );
}
=pod
=head2 set_group_summary( $group, $row )
FIXME
=cut
sub set_group_summary {
my ( $self, $group, $row ) = @_;
$group->nrecords( $row->{'_lr_nrecords'} );
$self->_set_aggregate_summary_values( $group, $row );
}
sub _set_aggregate_summary_values {
my ( $self, $group, $row ) = @_;
foreach my $op ( @{$self->ops()} ) {
if ( $op->isa( 'Lire::Aggregator' ) ) {
$op->_set_aggregate_summary_values( $group, $row );
} else {
my $parent = $group->parent_entry()
? $group->parent_entry()->group() : $group;
my $value = $op->create_value( $parent, $row );
$group->set_summary_value( $op->name(), %$value );
}
}
}
sub _set_store {
my ( $self, $store ) = @_;
$self->{'store'} = $store;
return;
}
sub build_table {
my ( $self, $store, $query, $table ) = @_;
$self->build_table_summary( $store, $query, $table )
unless $self->parent();
my $result = $query->execute( $store );
ROW:
while (defined (my $row = $result->next_row() ) ) {
my $group;
if ( $self->parent() ) {
my $p_name = defined $self->parent()->parent()
? $self->parent()->name() : 'table';
my $p_entry = $table->find_entry( $p_name, $row );
next ROW unless defined $p_entry;
$group = $p_entry->data_by_name( $self->name() );
} else {
$group = $table;
}
my $entry = $self->create_entry( $group, $row );
next ROW unless defined $entry;
foreach my $op ( @{$self->ops()} ) {
if ( $op->isa( 'Lire::Aggregator' ) ) {
my $group = $entry->create_group();
$op->set_group_summary( $group, $row );
} else {
my $value = $op->create_value( $entry->group(), $row );
$entry->add_value( $value );
}
}
}
my $i=0;
my $nqueries = $query->nested_queries();
foreach my $op ( grep { $_->isa( 'Lire::Aggregator' ) } @{$self->ops()}) {
$op->build_table( $store, $nqueries->[$i++], $table );
}
}
=pod
=head2 create_entry( $group, $row )
FIXME
=cut
sub create_entry {
my ( $self, $group, $row ) = @_;
croak( "Unimplemented create_entry() in ", ref $self );
}
=pod
=head1 MERGING AGGRATOR API
It defines additional methods required by Aggregator implementation to
be able to merge data..
The base Aggregator implementation takes care of merging the summary
information included in the Lire XML reports.
The merging specifics to the aggregator should be implemented in
the init_agggregator_data(), update_aggregator_data() and
end_aggregator_data() methods.
This class also takes care of the case when the aggregator is the
top-level aggregator, that is the immediate child of the
report-calc-spec element in the report specification),
=head1 IMPLEMENTATION OF Lire::ReportOperator MERGING METHODS
=pod
=head1 init_merge( $period_start, $period_end )
The default implementation makes sure that all contained operators are
inited.
Implementation of specific aggregator must chain up to this method, if
they override it.
=cut
sub init_merge {
my $self = $_[0];
foreach my $op ( @{$self->ops()} ) {
$op->init_merge();
}
$self->{'data'} = $self->init_group_data();
$self->{'_state'} = MERGE_INITED;
return $self;
}
#------------------------------------------------------------------------
# Methode merge_subreport( $subreport )
#
# This method will be called once for every subreport to be merged.
#
# $subreport is the Lire::Report::Subreport to merged.
#
# Method called by lr_xml_merge(1)
sub merge_subreport {
my ( $self, $subreport ) = @_;
croak "init_merge() wasn't called"
unless $self->{'_state'} == MERGE_INITED;
$self->merge_group_data( $subreport, $self->{'data'} );
return $self;
}
=pod
=head2 end_merge()
The default implementation makes sure that all operators gets the
end_report() event.
Subclasses should chain up to this method, if they override it.
=cut
sub end_merge {
my ( $self ) = @_;
croak "init_merge() wasn't called"
unless $self->{'_state'} == MERGE_INITED;
$self->end_group_data( $self->{'data'} );
$self->{'_state'} = MERGE_FINISHED;
return $self;
}
=pod
=head2 init_group_data()
The Aggregator implements init_group_data(). It takes care of
computing the summary information. Subclass does the equivalent in
init_aggregator_data().
=cut
sub init_group_data {
my $self = $_[0];
# Elements of the array
# 0 = summary data
# 1 = nrecords
# 2 = subclass' data
# 3 = missing-cases
return [ $self->init_summary_data(), 0,
$self->init_aggregator_data(), 0 ];
}
=pod
=head2 merge_group_data( $value, $data )
The Aggregator implements merge_group_data(). It takes care of merging
the summary information. Subclass does the equivalent in
merge_aggregator_data().
=cut
sub merge_group_data {
my ( $self, $value, $data ) = @_;
croak "value should be of type Lire::Report::Group, not $value\n"
unless UNIVERSAL::isa( $value, "Lire::Report::Group" );
$data->[1] += $value->nrecords();
$data->[3] += $value->missing_cases();
$self->merge_summary_data( $value, $data->[0] );
$self->merge_aggregator_data( $value, $data->[2] );
return;
}
=pod
=head2 end_group_data($data)
The Aggregator implements end_group_data(). It takes care of
computing the summary information. Subclass does the equivalent in
end_aggregator_data().
=cut
sub end_group_data {
my ( $self, $data ) = @_;
$self->end_summary_data( $data->[0] );
$self->end_aggregator_data( $data->[2] );
return
}
=pod
=head2 add_entry_value( $entry, $data )
This method will make sure that the entries of nested aggregator are
wrapped up in a Lire::Report::Group element.
There is no reason to override that method since the entries of the
aggregator are added in the create_group_entries() method.
=cut
sub add_entry_value {
my ( $self, $entry, $data ) = @_;
my $group = $entry->create_group();
$group->nrecords( $data->[1] );
$group->missing_cases( $data->[3] );
$self->set_summary_values( $group, $data->[0] );
$self->create_group_entries( $group, $data->[2] );
return;
}
# ------------------------------------------------------------------------
# METHODS FOR SUMMARY STATISTICS COMPUTATION
#
# This class defines several methods that are to be used in subclasses
# to compute the summary statistics that are included in Lire XML
# report. Those summary statistics are only computed by Aggregate
# operator (those that compute an aggregated value like avg, sum,
# etc.). The summary value is computed over all the records seen by the
# aggregator instead of only the grouped records.
#------------------------------------------------------------------------
# Method init_summary_data ()
#
# Returns a summary data structure.
sub init_summary_data {
my ( $self, $data ) = @_;
$data ||= {};
foreach my $op ( @{$self->ops()} ) {
if ( $op->isa( 'Lire::Aggregate' ) ) {
$data->{$op->name} = $op->init_group_data();
} elsif ( $op->isa( 'Lire::Aggregator' ) ) {
$op->init_summary_data( $data );
}
}
return $data;
}
#------------------------------------------------------------------------
# Method merge_summary_data ( $group, $summary_data )
#
# This method updates the Aggregate's summary data structures.
sub merge_summary_data {
my ( $self, $group, $data ) = @_;
foreach my $op ( @{$self->ops()} ) {
if ( $op->isa( 'Lire::Aggregate' ) ) {
my $value = $group->get_summary_value( $op->name() );
unless ($value) {
warn( "missing summary value for ", $op->name(), " operator");
next;
}
$op->merge_group_data( $value, $data->{$op->name()} );
} elsif ( $op->isa( 'Lire::Aggregator' ) ) {
$op->merge_summary_data( $group, $data );
}
}
}
#------------------------------------------------------------------------
# end_summary_data( $summary_data )
#
# This method calls end_group_data() on all aggregates' data structures.
sub end_summary_data {
my ( $self, $data ) = @_;
foreach my $op ( @{$self->ops()} ) {
if ( $op->isa( 'Lire::Aggregate' ) ) {
$op->end_group_data( $data->{$op->name} );
} elsif ( $op->isa( 'Lire::Aggregator' ) ) {
$op->end_summary_data( $data );
}
}
}
#------------------------------------------------------------------------
# set_summary_values( $group, $summary_data )
#
# Make sure all operators sets their summary value
sub set_summary_values {
my ( $self, $group, $data ) = @_;
foreach my $op ( @{$self->ops()} ) {
if ( $op->isa( 'Lire::Aggregate' ) ) {
my $v = $op->create_value( $group,
$op->data2dlf( $data->{$op->name()} ) );
$group->set_summary_value( $op->name(), %$v );
} elsif ( $op->isa( 'Lire::Aggregator' ) ) {
$op->set_summary_values( $group, $data );
}
}
}
#------------------------------------------------------------------------
# Method get_summary_value_string( $name )
#
# Returns a code string which can be used to access the summary
# data item of operator $name.
sub get_summary_value_string {
my ( $self, $name ) = @_;
return "->{'" . $name . "'}";
}
=pod
=head1 METHODS THAT SHOULD BE IMPLEMENTED BY SUBCLASSES FOR MERGING
=head2 init_aggregator_data()
This is the equivalent of init_group_data() and is called from
Aggregator's implementation of init_group_data().
=cut
sub init_aggregator_data {
croak "Unimplemented init_aggregator_data() method in ", ref $_[0], "\n";
}
=pod
=head2 merge_aggregator_data( $value, $data )
This method is the equivalent than merge_group_data() and is called
from Aggregator's implementation of merge_group_data().
=cut
sub merge_aggregator_data {
croak "Unimplemented merge_aggregator_data() method in ", ref $_[0], "\n";
}
=pod
=head2 end_aggregator_data( $data )
This method is the equivalent of the end_group_data() and is called
from Aggregator's implementation of end_group_data().
=cut
sub end_aggregator_data {
croak "Unimplemented end_aggregator_data() method in ", ref $_[0], "\n";
}
=head2 create_group_entries( $group, $data )
In this method, the aggregator should add one Lire::Report::Entry
object for every group merged by the aggregator.
$group is an instance of Lire::Report::Group to which the entries
should be added. $data is the data structure returned by
init_group_data() for the group in which the aggregator is nested (or
the only structure that was created when the aggregator is the
top-level aggregator).
In this method, the aggregator must make sure to call
add_entry_value() on its contained operators for each entry created.
=cut
sub create_group_entries {
croak "Unimplemented create_group_entries method in ", ref $_[0], "\n";
}
# keep perl happy
1;
__END__
=pod
=head1 SEE ALSO
Lire::ReportSpec(3pm), Lire::Aggregate(3pm), Lire::ReportOperator(3pm),
Lire::Group(3pm), Lire::Timegroup(3pm), Lire::Timeslot(3pm),
Lire::Rangegroup(3pm)
=head1 AUTHORS
Francis J. Lacoste <flacoste@logreport.org>
Wolgang Sourdeau <wsourdeau@logreport.org>
=head1 VERSION
$Id: Aggregator.pm,v 1.29 2006/07/23 13:16:27 vanbaal Exp $
=head1 COPYRIGHT
Copyright (C) 2001-2004 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
|