blib/lib/App/RecordStream/Manual/Story.pm untested

filter: Statementstmt 0.0% 0 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 2 Podpod n/a totaltotal 0.0% 0 / 8 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use strict;
2 use warnings;
3 package App::RecordStream::Manual::Story;
4  
5 =head1 NAME
6  
7 App::RecordStream::Manual::Story - A humorous introduction to recs
8  
9 =head1 DESCRIPTION
10  
11 This was a humorous article written to give an introduction to the RecordStream
12 tools, please read with tongue firmly planted in cheek.
13  
14 =head1 And So It Begins
15  
16 Every ninja knows the value of tools. How could one accomplish anything without
17 a mouse quenched in the blood of a live and willing dragon or a monitor whose
18 CRT is hewn from a single enormous diamond, formed in the fire of Hades?
19 Indeed, most ninja scholars agree unanimously that the hundred-handed
20 conference room reservation prana cannot be done with out a keyboard each key
21 of which is greased with the blood of a different enemy of your ancestors'.
22  
23 Given that you have already collected the 12 ancient swords of the sun and the
24 IBM Model M keyboard, you must now choose wisely which to use in each of your
25 challenges. When one wrestles Leviathan one does not do it with chopsticks.
26  
27 Similarly, when a ninja faces a complex dataset they do not come at it with
28 grep (well, many times you start with grep). The true ninja runs atop the
29 cubicle dividers, slaughtering all until the dataset is rendered meaningless.
30 code ninjas use recs.
31  
32 =head1 On the first day, we analyzed an access log
33  
34 Say you have only seconds to report URL statistics from an apache access log
35 before the ancient sea wyrm of Atlantis raises from the Puget Sound and
36 destroys Seattle entirely. Then you might type something like this:
37  
38 recs-frommultire --re 'latency=TIME: (\d*)' --re 'method,url="([^" ]*) ([^" ?]*)' access.log \
39 | recs-xform '$r->{url} =~ s/(get\.cgi)\/.*/$1/;' \
40 | recs-collate -k url --perfect -a 'avg,latency' -a count \
41 | recs-sort -k 'avg_latency=-n' \
42 | head -n 5 \
43 | recs-totable
44  
45 Scared yet? A proper tool should always inspire fear in the weak. With
46 appropriate mastery, you too can learn to banish ancient horrors using recs.
47 But one does not learn recs-jitsu all at once; one must learn it kata by kata.
48  
49 First one must understand the principles and overall form of this arcane art.
50 Recs, or RecordStream is a collection of scripts that facilitates the parsing
51 of files into JSON records and the transformation of those records. Many common
52 UNIX programs like grep, sort, and uniq have recs analogs and several recs
53 scripts allow transformations unheard of using typical UNIX tools. In general
54 the tools fall into three categories: those that produce JSON records, those
55 that operate on JSON records, and those that convert JSON records into output.
56 A typical use of recs will consist of one of the first type, one, or more of
57 the second type, and one of the third type. To begin using recs, you'll have to
58 decide on how to get your data into JSON. There are several scripts available
59 to do this, one of the most powerful of which is recs-frommultire. It allows
60 you to write multiple regular expressions to capture fields.
61  
62 =head1 recs-frommultire - parsing data into JSON
63  
64 To understand how our invocation of recs-frommultire was written, you'll want
65 to see our access log. Here are four sample lines:
66  
67 192.168.151.55 - - [10/Sep/2007:01:01:55 -0700] "GET /view_image.cgi?uid=bernard&badge=1 HTTP/1.1" 200 3528 TIME: 0
68 192.168.153.89 - - [10/Sep/2007:01:02:28 -0700] "GET /x.gif HTTP/1.1" 304 - TIME: 0
69 192.168.153.105 - - [10/Sep/2007:01:02:32 -0700] "GET /dbfiles/get.cgi/data.xml HTTP/1.1" 200 7338 TIME: 1
70 192.168.151.66 - - [10/Sep/2007:01:02:41 -0700] "GET /helpdesk.html HTTP/1.1" 200 40 TIME: 1
71  
72 For reference the invocation was:
73  
74 recs-frommultire --re 'latency=TIME: (\d*)' --re 'method,url="([^" ]*) ([^" ?]*)' access.log
75  
76 The first option specifies one field, named "latency" which is the only capture
77 group of the first regular expression. The second option specifies two fields,
78 named "method" and "url" which are the two capture groups of the second regular
79 expression. The final argument is the file to parse. Each regular expression is
80 run against each line. When a field would be duplicated, all matches so far are
81 flushed as a record.
82  
83 The output from recs-frommultire looks like:
84  
85 {"url":"/view_image.cgi","method":"GET","latency":"0"}
86 {"url":"/x.gif","method":"GET","latency":"0"}
87 {"url":"/dbfiles/get.cgi/data.xml","method":"GET","latency":"1"}
88 {"url":"/helpdesk.html","method":"GET","latency":"1"}
89  
90 =head1 recs-xform - arbitrary manipulation of records
91  
92 JSON is mostly human readable and as you can see each record has three fields,
93 "url", "method", and "latency". Unfortunately "url" isn't quite as we want it.
94 As it stands the key for get.cgi requests is included in the URL but that will
95 mess up our statistics so we'd like to get rid of it which brings us to our
96 next stage in the pipeline:
97  
98 recs-xform '$r->{url} =~ s/(get\.cgi)\/.*/$1/;'
99  
100 recs-xform is both simple and powerful: it executes arbitrary, inline perl on
101 each record. The record is represented as a App::RecordStream::Record object in the scalar
102 $r, but all the fields can be accessed as if it were no more than a hashref. In
103 this case we are using a substitution command to strip the key off of get.cgi
104 requests. At this point our data is ready to be aggregated and made into
105 statistics.
106  
107 =head1 recs-collate - Generate aggregate statistics
108  
109 recs-collate -k url --perfect -a avg,latency -a count
110  
111 recs-collate is the crown jewel of recs analysis. It groups records from input
112 together, computes aggregate information about them, and dumps this aggregate
113 information as output records. "-k url" requests that records be grouped by
114 their "url" field. "--perfect" indicates that they should be grouped together
115 even if they are not adjacent in input (adjacent only is the default). "-a
116 avg,latency" requests that the average aggregator be used on the latency field.
117 "-a count" requests that the count aggregator be used.
118  
119 Aggregators are one of the most powerful features of recs. As of writing there
120 are 21 distinct aggregators ready for use. Some of the most powerful are:
121  
122 average: averages provided field
123 count: counts (non-unique) records
124 distinctcount: count unique values from provided field
125 maximum: maximum value for a field
126 percentile: value of pXX for field
127 sum: sums provided field
128  
129 You can find out what all of them are with `recs-collate --list-aggregators`.
130  
131 Here are a few sample records from after the collate step:
132  
133 {"count":11,"url":"/dbfiles/list.cgi","avg_latency":21.0909090909091}
134 {"count":2,"url":"/linkGenerator/Host.cgi","avg_latency":0.5}
135 {"count":3,"url":"/view_image.cgi","avg_latency":0.333333333333333}
136 {"count":21,"url":"/dbfiles/check.cgi","avg_latency":0.476190476190476}
137  
138 =head1 recs-sort - ordering records in a stream
139  
140 Now that the collation has been done the records have the numbers we desire, but they are neither in a useful order, nor a pretty format.
141  
142 The first we rectify with recs-sort:
143  
144 recs-sort -k 'avg_latency=-n'
145  
146 We have specified that the records are to be sorted by their avg_latency field
147 and they are to be sorted numerically, descending (negative n)
148  
149 =head1 recs-totable - pretty output of data
150  
151 Finally, we convert JSON back to something slightly more human readable:
152  
153 head -n 5 | recs-totable
154  
155 Since JSON records are one to a line, we can use good ol' UNIX head to take the
156 5 top offenders. And we use recs-totable to convert those top ten to a nicely
157 formatted text table:
158  
159 avg_latency ct url
160 ----------------- ---- -----------------------
161 21.0909090909091 11 /dbfiles/list.cgi
162 1.36368901114811 6907 /view_image.cgi
163 1.02898550724638 345 /helpdesk.html
164 1 1 /dbfiles/
165 0.727272727272727 11 /linkGenerator/Host.cgi
166  
167 =head1 And so it ends
168  
169 When faced with awesome prowess like this, what can a 346-foot, 26000-ton sea
170 monster from beyond the stars do but slink back to its cave and bide its time
171 beneath downtown Seattle?
172  
173 Should you find yourself locked in mortal combat with a unspeakable horror of
174 your own you can always turn to --help. All recs scripts come equipped with
175 detailed usage instructions triggered by the --help option. You can also turn
176 to `man recs` (if the man file is installed correctly)
177  
178 =head1 SEE ALSO
179  
180 =over
181  
182 =item * See L<App::RecordStream> for an overview of the scripts and the system
183  
184 =item * Run C<recs examples> or see L<App::RecordStream::Manual::Examples> for a set of simple recs examples
185  
186 =back
187  
188 =cut
189  
190 1;