Snowflake/Fact.pm

filter: Statementstmt 100% 64 / 64 Branchbran 100% 10 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 100% 84 / 84 CC 11 SCAR 12.7
CC
11
Cov
100%
generate_report
5.0
_convert_joins
5.0
_resolve_types
3.0
CRAP 3.6
« Snowflake.pm ↑ Summary Snowflake/Dimension.pm »
1 package DBIx::Class::Snowflake::Fact;
2 4 our $VERSION = '0.10';
3  
4  
5 =head1 NAME
6  
7 DBIx::Class::Snowflake::Fact
8  
9 =head1 VERSION
10  
11 version 0.10
12  
13 =head1 ABSTRACT
14  
15 DBIx::Class::Snowflake::Fact - Load this for any fact tables.
16  
17 =cut
18  
19 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use base qw( DBIx::Class::Snowflake );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 DBIx::Class::Fact - Make your table a star/snowflake fact table
28  
29 =head1 SYNOPSIS
30  
31 __PACKAGE__->load_components(qw/ Snowflakee::Fact /);
32  
33 =head1 DESCRIPTION
34  
35 Component for DBIx::Class that makes it easier to develop star or snowflake schemas.
36 This will provide the class with useful accessors to get a list of dimensions and easily search given a dimension.
37  
38 =head1 METHODS
39  
40 =cut
41  
42 =head2 attributes
43  
44 Returns the attributes of the fact excluding any ignore columns.
45 It returns them as an array containing hashes each
46 containing the data type and the accesssor name.
47  
48 =cut
49  
50 =head2 attrs
51  
52 Convenience alias to attributes
53  
54 =cut
55 sub attrs
56 4 ▶{
Subroutine attrs: called CC=1 SCAR=0.0
Pod documented
57 4 shift->attributes(@_);
58 }
59  
60 =head2 generate_report
61  
62 B<generate_report> returns a resultset for the report requested.
63 Each item in the array is a row in the report and each element in
64 the hash is one of the metrics requested.
65  
66 Usage:
67 $fact->generate_report({'filters' => {'dimdate.day_of_week' => 3}, 'metric' => {'dimline.line_velocity' => 1}})
68  
69 =cut
70 sub generate_report
71 8 ▶{
Subroutine generate_report: called CC=5 SCAR=16.1
Pod documented
72 8 my $self = shift;
73 8 my $hash = shift;
74 8 my $filters = $hash->{'filters'};
75 8 my $metrics = $hash->{'metric'};
76 8 my ($temp, $results, %row, @joins, @names);
77  
78 8 $metrics = $self->_resolve_types( $metrics );
79 8 $filters = $self->_resolve_types( $filters );
80  
81 #convert our WHERE
82 8 foreach my $attr ( keys( %{$filters->{'dimensions'}} ) )
83 {
84 $temp =
85 $self->_resolve_dimension_to_attribute( $attr,
86 6 $filters->{'dimensions'}{$attr} );
87 6 ▶ if ( defined($temp) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $temp) { } 4 2 6
88 {
89 4 $filters->{'attributes'} = {%{$filters->{'attributes'}}, %{$temp}};
90 }
91 else
92 {
93 2 $self->throw_exception("Unable to resolve dimension '$attr', does not exist in snowflake.");
94 }
95 }
96  
97 #convert our SELECT
98 6 foreach my $metric ( keys( %{$metrics->{'dimensions'}} ) )
99 {
100 14 my $temp = $self->_resolve_metrics($metric);
101 14 ▶ if( defined($temp) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $temp) { } 12 2 14
102 {
103 12 push( @names, pop(@$temp) );
104 12 push( @joins, $temp );
105 }
106 else
107 {
108 2 $self->throw_exception("Unable to resolve dimension '$metric', does not exist in snowflake.");
109 }
110 }
111  
112 4 $self->_convert_joins(\@joins);
113  
114 $results = $self->result_source->resultset->search(
115 4 $filters->{'attributes'},
116 {
117 'join' => \@joins,
118 '+select' => \@names
119 }
120 );
121 }
122  
123 sub _convert_joins
124 14 ▶{
Subroutine _convert_joins: called CC=5 SCAR=16.1
125 14 my $self = shift;
126 14 my $joins = shift;
127  
128 14 foreach my $join (@$joins)
129 {
130 # if there is only 1 or 0 elements then don't worry about it, it's formatted
131 26 ▶ if ( @$join > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@$join > 1) 10 16 26
132 {
133 # temp is what our join will be when we are done building it
134 10 my $temp;
135 10 ▶ if ( @$join > 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (@$join > 2) { } 4 6 10
136 {
137 # inner temp is going to always reference the deepest embedded hash while
138 # we loop
139 4 my $inner_temp = {};
140 # temp references the top of this structure
141 4 $temp = $inner_temp;
142 4 my $join_part = 0;
143 # we don't want the last two elements because the second to last element
144 # is going to an array ref instead of a hash ref and the last element is
145 # going to be a scalar.
146 4 foreach $join_part ( 0 .. @$join - 3 )
147 {
148 4 $inner_temp->{ $join->[$join_part] } = {};
149 4 $inner_temp = $inner_temp->{ $join->[$join_part] };
150 }
151 # the last step is to make the deepest hash contain a key,value of the
152 # second to last element with the value being an arrayref that contains
153 # only the last element
154 4 $inner_temp->{ $join->[ $join_part + 1 ] } = [ $join->[-1] ];
155 }
156 else # there are only two elements
157 {
158 # simple edge case, just makes { 'foo' => [ 'bar' ] }
159 6 $temp = { $join->[0] => [ $join->[1] ] };
160 }
161 10 $join = $temp;
162 }
163 }
164 14 return $joins;
165 }
166  
167 =head2 _resolve_types
168  
169 The resolve types subroutine attempts to determine if the value passed
170 in is the name of a dimension or an attribute given the columns of
171 the current table.
172  
173 In this case the term attribute is described as column on a fact table
174 that does not reference another table. So, if there was a fact table
175 fact_produced that had two columns, date_id and widget_id, and date referred
176 to the date_id column of the date table which had a column dow and widget_id did not refer to
177 anything then the following call:
178 $fact->_resolve_types({'dow' => 3, 'widget_id' => 6});
179 the following hash would be returned:
180 { 'attributes' => { 'widget_id' => 6}, 'dimensions' => { 'dow' => 3 }}
181  
182 B<NOTE> If the column does not exist it assumes it is a dimension, IT
183 DOES NOT SEARCH FOR IT TO VERIFY.
184 =cut
185 sub _resolve_types
186 18 ▶{
Subroutine _resolve_types: called CC=3 SCAR=11.0
187 18 my $self = shift;
188  
189 # list of possibly dimensions, possibly attributes
190 18 my $dimensions = shift;
191 18 my %columns = $self->_columns_as_hash();
192 18 my ( %attrs, %dims );
193 18 foreach my $dim ( keys(%$dimensions) )
194 {
195 28 ▶ if ( defined( $columns{$dim} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $columns{$dim}) { } 6 22 28
196 {
197 6 $attrs{$dim} = $dimensions->{$dim};
198 }
199 else
200 {
201 22 $dims{$dim} = $dimensions->{$dim};
202 }
203 }
204 18 return { 'attributes' => \%attrs, 'dimensions' => \%dims };
205 }
206  
207 4 1;
« Snowflake.pm ↑ Summary Snowflake/Dimension.pm »