~ubuntu-branches/ubuntu/vivid/libmonitoring-livestatus-class-perl/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/Monitoring/Livestatus/Class/Base/Table.pm

  • Committer: Package Import Robot
  • Author(s): Alexander Wirt
  • Date: 2012-09-23 12:52:44 UTC
  • Revision ID: package-import@ubuntu.com-20120923125244-tj2b60nma3530edj
Tags: upstream-0.3
ImportĀ upstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package # hide from pause
 
2
    Monitoring::Livestatus::Class::Base::Table;
 
3
 
 
4
use Moose;
 
5
use Carp;
 
6
 
 
7
use Monitoring::Livestatus::Class::Abstract::Filter;
 
8
use Monitoring::Livestatus::Class::Abstract::Stats;
 
9
 
 
10
use Monitoring::Livestatus::Class;
 
11
my $TRACE = Monitoring::Livestatus::Class->TRACE() || 0;
 
12
 
 
13
has 'ctx' => (
 
14
    is => 'rw',
 
15
    isa => 'Monitoring::Livestatus::Class',
 
16
    handles => [qw/backend_obj/],
 
17
);
 
18
 
 
19
#
 
20
#  Filter Stuff
 
21
#
 
22
has 'filter_obj' => (
 
23
    is => 'ro',
 
24
    isa => 'Monitoring::Livestatus::Class::Abstract::Filter',
 
25
    builder => '_build_filter',
 
26
    handles => { apply_filer => 'apply' },
 
27
);
 
28
 
 
29
sub _build_filter { return Monitoring::Livestatus::Class::Abstract::Filter->new( ctx => shift ); };
 
30
 
 
31
sub filter {
 
32
    my $self = shift;
 
33
    my $cond = shift;
 
34
 
 
35
    my @statments = $self->apply_filer($cond);
 
36
    my @tmp = @{ $self->statments || [] };
 
37
    push @tmp, @statments;
 
38
    $self->_statments(\@tmp);
 
39
    return $self;
 
40
}
 
41
 
 
42
sub options {
 
43
    my($self, $options) = @_;
 
44
    $self->{'_options'} = $options;
 
45
    return $self;
 
46
}
 
47
 
 
48
#
 
49
#  Stats Stuff
 
50
#
 
51
has 'stats_obj' => (
 
52
    is => 'ro',
 
53
    isa => 'Monitoring::Livestatus::Class::Abstract::Stats',
 
54
    builder => '_build_stats',
 
55
    handles => { apply_stats => 'apply' },
 
56
);
 
57
 
 
58
sub _build_stats { return Monitoring::Livestatus::Class::Abstract::Stats->new( ctx => shift ); };
 
59
 
 
60
sub stats {
 
61
    my $self = shift;
 
62
    my $cond = shift;
 
63
 
 
64
    my @statments = $self->apply_stats($cond);
 
65
    my @tmp = @{ $self->statments || [] };
 
66
    push @tmp, @statments;
 
67
    $self->_statments(\@tmp);
 
68
    return $self;
 
69
}
 
70
 
 
71
has 'table_name' => (
 
72
    is => 'ro',
 
73
    isa => 'Str',
 
74
    builder  => 'build_table_name',
 
75
);
 
76
 
 
77
sub build_table_name { die "build_table_name must be implemented in " . ref(shift) };
 
78
 
 
79
 
80
# Primary key stuff
 
81
 
82
has 'primary_keys' => (
 
83
    is => 'ro',
 
84
    isa => 'ArrayRef[Str]',
 
85
    builder  => 'build_primary_keys',
 
86
);
 
87
 
 
88
sub build_primary_keys { die "build_primary_keys must be implemented in " . ref(shift) };
 
89
 
 
90
sub has_single_primary_key {
 
91
    my $self = shift;
 
92
    if ( scalar @{ $self->primary_keys } == 1 ){
 
93
        return 1
 
94
    }
 
95
    return;
 
96
}
 
97
 
 
98
sub single_primary_key {
 
99
    my $self = shift;
 
100
    if ( $self->has_single_primary_key ){
 
101
        return $self->primary_keys->[0];
 
102
    }
 
103
    return;
 
104
}
 
105
 
 
106
has '_statments' => (
 
107
    is => 'rw',
 
108
    reader => 'statments',
 
109
    isa => 'ArrayRef',
 
110
    default => sub { return []; }
 
111
);
 
112
 
 
113
has '_options' => (
 
114
    is => 'rw',
 
115
    isa => 'HashRef',
 
116
    default => sub { return {}; }
 
117
);
 
118
 
 
119
has '_columns' => (
 
120
    is => 'rw',
 
121
    isa => 'ArrayRef',
 
122
    default => sub { return []; }
 
123
);
 
124
 
 
125
sub columns {
 
126
    my $self = shift;
 
127
    my @columns = @_ ;
 
128
    $self->_columns( \@columns );
 
129
    return $self;
 
130
}
 
131
 
 
132
sub headers{
 
133
    my $self = shift;
 
134
 
 
135
    my $statment = sprintf("GET %s\nLimit: 1",$self->table_name);
 
136
    my ( $hash_ref ) = @{ $self->backend_obj->selectall_arrayref($statment,{ slice => 1}) };
 
137
    my @cols = keys %$hash_ref;
 
138
    return wantarray ? @cols : \@cols;
 
139
}
 
140
 
 
141
sub hashref_array {
 
142
    my $self = shift;
 
143
 
 
144
    my @statments = ();
 
145
    if ( scalar @{ $self->_columns } > 0 ){
 
146
        push @statments, sprintf('Columns: %s',join(' ',@{  $self->_columns  }));
 
147
    }
 
148
    push @statments, @{ $self->statments };
 
149
 
 
150
    my @data =  $self->_execute( @statments );
 
151
    return wantarray ? @data : \@data;
 
152
}
 
153
 
 
154
sub hashref_pk {
 
155
    my $self = shift;
 
156
    my $key  = $self->single_primary_key || shift;
 
157
    unless ( $key ) {
 
158
        croak("There was no single primary key to be found.");
 
159
    };
 
160
    my %indexed;
 
161
    my @data = $self->hashref_array();
 
162
    for my $row (@data) {
 
163
        if(!defined $row->{$key}) {
 
164
            my %possible_keys = keys %{$row};
 
165
            croak("key $key not found in result set, possible keys are: ".join(', ', sort keys %possible_keys));
 
166
        } else {
 
167
            $indexed{$row->{$key}} = $row;
 
168
        }
 
169
    }
 
170
    return wantarray ? %indexed : \%indexed;
 
171
}
 
172
 
 
173
sub _execute {
 
174
    my $self = shift;
 
175
    my @data = @_;
 
176
 
 
177
    my @statments = ();
 
178
    push @statments, sprintf("GET %s",$self->table_name);
 
179
    push @statments, @data;
 
180
 
 
181
    printf STDERR "EXECUTE: %s\n", join("\nEXECUTE: ",@statments)
 
182
        if $TRACE >= 1;
 
183
 
 
184
    my $statment = join("\n",@statments);
 
185
 
 
186
    my $options = $self->{'_options'};
 
187
    $options->{'slice'} = {};
 
188
 
 
189
    my $return = $self->backend_obj->selectall_arrayref($statment, $options);
 
190
 
 
191
    return wantarray ? @{ $return }  : $return;
 
192
}
 
193
 
 
194
1;
 
195
__END__
 
196
=head1 NAME
 
197
 
 
198
Monitoring::Livestatus::Class::Base::Table - Base class for all table objects.
 
199
 
 
200
=head2 SYNOPSIS
 
201
 
 
202
    my $class = Monitoring::Livestatus::Class->new(
 
203
        backend => 'INET',
 
204
        socket => '10.211.55.140:6557',
 
205
    );
 
206
 
 
207
    my $table_obj = $class->table('services');
 
208
 
 
209
    my $data = $table_obj->search( {} )->hashref_array();
 
210
 
 
211
=head1 ATTRIBUTES
 
212
 
 
213
=head2 ctx
 
214
 
 
215
Reference to context object L<Monitoring::Livestatus::Class>
 
216
 
 
217
=head2 filter
 
218
 
 
219
Reference to filter object L<Monitoring::Livestatus::Class>
 
220
 
 
221
=head2 stats
 
222
 
 
223
Reference to filter object L<Monitoring::Livestatus::Class>
 
224
 
 
225
=head2 table_name
 
226
 
 
227
Containts the table name.
 
228
 
 
229
=head2 statments
 
230
 
 
231
Containts all the statments.
 
232
 
 
233
=head2 options
 
234
 
 
235
Containts all the options.
 
236
 
 
237
=head1 METHODS
 
238
 
 
239
=head2 columns
 
240
 
 
241
Arguments: $colA, $colB, ...
 
242
 
 
243
Return: $self
 
244
 
 
245
Set columns...
 
246
 
 
247
=head2 headers
 
248
 
 
249
Returns a array or reference to array, depending on the calling context, of all
 
250
header columns.
 
251
 
 
252
=head2 filter
 
253
 
 
254
Example usage:
 
255
 
 
256
    $table_obj->search( { name => 'localhost' } );
 
257
    $table_obj->search( { name => [ 'localhost', 'gateway' ] } );
 
258
    $table_obj->search( [ { name => 'localhost' }, { name => 'gateway' } ] );
 
259
 
 
260
Returns: $self
 
261
 
 
262
=head2 hashref_array
 
263
 
 
264
Returns a array or reference to array, depending on the calling context.
 
265
 
 
266
Example usage:
 
267
 
 
268
    my $hashref_array = $table_obj->search( { } )->hashref_array;
 
269
    print Dumper $hashref_array;
 
270
 
 
271
 
 
272
=head2 hashref_pk
 
273
 
 
274
Returns a hash of hash references.
 
275
 
 
276
Example usage:
 
277
 
 
278
    my $hashref_pk = $table_obj->search( { } )->hashref_pk();
 
279
    print Dumper $hashref_pk;
 
280
 
 
281
=head2 has_single_primary_key
 
282
 
 
283
=head2 single_primary_key
 
284
 
 
285
=head2 build_table_name
 
286
 
 
287
=head2 build_primary_keys
 
288
 
 
289
=head1 AUTHOR
 
290
 
 
291
See L<Monitoring::Livestatus::Class/AUTHOR> and L<Monitoring::Livestatus::Class/CONTRIBUTORS>.
 
292
 
 
293
=head1 COPYRIGHT & LICENSE
 
294
 
 
295
Copyright 2009 Robert Bohne.
 
296
 
 
297
This program is free software; you can redistribute it and/or modify it
 
298
under the terms of either: the GNU General Public License as published
 
299
by the Free Software Foundation; or the Artistic License.
 
300
 
 
301
See http://dev.perl.org/licenses/ for more information.
 
302
 
 
303
=cut