File Coverage

blib/lib/Hash/Weighted/Categorize.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 22 22 100.0


line stmt bran cond sub pod time code
1             package Hash::Weighted::Categorize;
2             {
3             $Hash::Weighted::Categorize::VERSION = '0.002';
4             }
5              
6             # code eval'ed by Hash::Weighted::Categorize
7             # will have none of the extras lexically provided by Moo
8 4     4   1400 sub __eval { eval shift }
9              
10 1     1   33970 use Moo;
  1         18061  
  1         5  
11              
12 1     1   2982 use Hash::Weighted::Categorize::Parser;
  1         2  
  1         200  
13              
14             has _parser => (
15             is => 'lazy',
16             );
17              
18             sub _build__parser {
19 1     1   666 my ($self) = @_;
20 1         13 return Hash::Weighted::Categorize::Parser->new();
21             }
22              
23             sub parse_to_source {
24 4     4 1 8 my ($self, $input) = @_;
25 4         82 $self->_parser->input( $input );
26 4         110 return $self->_parser->Run;
27             }
28              
29             sub parse {
30 4     4 1 1467 my ($self, $input) = @_;
31 4         18 return __eval $self->parse_to_source($input);
32             }
33              
34             1;
35              
36              
37              
38             =pod
39              
40             =head1 NAME
41              
42             Hash::Weighted::Categorize - Categorize weighted hashes using a Domain Specific Language
43              
44             =head1 VERSION
45              
46             version 0.002
47              
48             =head1 SYNOPSIS
49              
50             # create a parser
51             my $parser = Hash::Weighted::Categorize->new();
52              
53             # generate a scoring function
54             my $score = $parser->parse( << 'CODE' );
55             %OK > 90%, %CRIT < .1: OK;
56             %CRIT > 50%: CRIT;
57             %CRIT > 25%: WARN;
58             UNKN;
59             CODE
60              
61             # OK
62             $status = $score->( { OK => 19, CRIT => 2 } );
63              
64             # WARN
65             $status = $score->( { OK => 14, CRIT => 6, WARN => 1 } );
66              
67             # CRIT
68             $status = $score->( { OK => 8, CRIT => 11, WARN => 1, UNKN => 1 } );
69              
70             # UNKN
71             $status = $score->( { OK => 18, CRIT => 2, WARN => 1 } );
72              
73             =head1 DESCRIPTION
74              
75             Hash::Weighted::Categorize is a tool to easily create scoring functions (think monitoring)
76             based on a simple mini-language. A Hash::Weighted::Categorize object is a parser for this
77             mini-language, that will return coderefs implementing a scoring function
78             written in this language.
79              
80             =head1 METHODS
81              
82             =head2 new()
83              
84             Create a new L object.
85              
86             =head2 parse( $code )
87              
88             Parse the content of C<$code> and return the corresponding code reference.
89              
90             =head2 parse_to_source( $code )
91              
92             Parse the content of C<$code> and return the Perl source code for the
93             code reference that would be returned by C.
94              
95             =head1 DOMAIN SPECIFIC LANGUAGE
96              
97             The I parsed by L
98             is intentionaly very simple. Simple statements consist of boolean
99             expressions separated by commas (C<,> meaning I), and
100             terminated by a colon (C<:>) followed by the result to be returned if
101             the condition is true.
102              
103             In the following example:
104              
105             %OK > 90%, %CRIT < .1: OK;
106             %CRIT > 50%: CRIT;
107             %CRIT > 25%: WARN;
108             UNKN;
109              
110             C, C, C and C are I. On the left-hand side of
111             the C<:>, they are interpreted in relation to the keys of the examined
112             hash. A I by itself is interpreted as the count/weight of this
113             element in the hash. When prefixed by a C<%> sign, the ratio of this
114             category compared to the total is used in the expression.
115              
116             A literal number followed by a C<%> sign is simply divided by C<100>.
117              
118             The currently supported mathematical operators are:
119             C<+>, C<->, C<*> and C.
120              
121             The currently supported comparison operators are:
122             C<< < >>, C<< <= >>, C<==>, C, C<< > >> and C<< >= >>.
123              
124             The mini-language supports the use of brace-delimited blocks, nested at
125             an arbitrary depth, which allows to write complex expressions such as:
126              
127             %CRIT >= 10%: {
128             %CRIT > 20% : CRIT;
129             %OK > 85% : OK;
130             WARN;
131             }
132             WARN > 0 : WARN;
133             OK;
134              
135             which is equivalent to:
136              
137             %CRIT >= 10%, %CRIT > 20% : CRIT;
138             %CRIT >= 10%, %OK > 85% : OK;
139             %CRIT >= 10% : WARN;
140             WARN > 0 : WARN;
141             OK;
142              
143             =head1 BUGS
144              
145             Please report any bugs or feature requests on the bugtracker website
146             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-Weighted-Categorize or by
147             email to bug-hash-weighted-categorize@rt.cpan.org.
148              
149             When submitting a bug or request, please include a test-file or a
150             patch to an existing test-file that illustrates the bug or desired
151             feature.
152              
153             =head1 AUTHOR
154              
155             Philippe Bruhat (BooK)
156              
157             =head1 ACKNOWLEDGMENTS
158              
159             This module was originally developed for Booking.com. With approval from
160             Booking.com, this module was generalized and put on CPAN, for which the
161             author would like to express his gratitude.
162              
163             This module is the result of scratching my colleague Menno Blom's itch
164             during a company-sponsored hackathon. Thanks to everyone involved.
165              
166             The name of this module owes a lot to the C mailing-list,
167             and especially to Aristotle Pagaltzis. Thanks to everyone involved.
168              
169             =head1 COPYRIGHT
170              
171             Copyright 2013 Philippe Bruhat (BooK), all rights reserved.
172              
173             =head1 LICENSE
174              
175             This program is free software; you can redistribute it and/or modify it
176             under the same terms as Perl itself.
177              
178             =cut
179              
180              
181             __END__