]> gitweb.factorcode.org Git - factor.git/blob - basis/logging/analysis/analysis.factor
2b812924228f86246a37be25af47c4a89ca03c42
[factor.git] / basis / logging / analysis / analysis.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs io io.styles kernel logging
4 logging.parser math namespaces prettyprint sequences sorting
5 splitting ;
6 IN: logging.analysis
7
8 SYMBOL: word-names
9 SYMBOL: errors
10 SYMBOL: word-histogram
11 SYMBOL: message-histogram
12
13 : analyze-entry ( entry -- )
14     dup level>> { ERROR CRITICAL } member-eq? [ dup errors get push ] when
15     dup word-name>> word-histogram get inc-at
16     dup word-name>> word-names get member? [
17         dup [ level>> ] [ word-name>> ] [ message>> ] tri 3array
18         message-histogram get inc-at
19     ] when
20     drop ;
21
22 : recent-histogram ( assoc n -- alist )
23     [ sort-values <reversed> ] dip cramp head ;
24
25 : analyze-entries ( entries word-names -- errors word-histogram message-histogram )
26     [
27         word-names set
28         V{ } clone errors set
29         H{ } clone word-histogram set
30         H{ } clone message-histogram set
31
32         [ analyze-entry ] each
33
34         errors get
35         word-histogram get 10 recent-histogram
36         message-histogram get 10 recent-histogram
37     ] with-scope ;
38
39 : histogram. ( assoc quot -- )
40     standard-table-style [
41         [
42             [ swapd with-cell pprint-cell ] with-row
43         ] curry assoc-each
44     ] tabular-output ; inline
45
46 : 10-most-recent ( errors -- errors )
47     10 tail* "Only showing 10 most recent errors" print nl ;
48
49 : errors. ( errors -- )
50     dup length 10 >= [ 10-most-recent ] when
51     log-entries. ;
52
53 : analysis. ( errors word-histogram message-histogram -- )
54     nl "==== FREQUENT MESSAGES:" print nl
55     "Total: " write dup values sum . nl
56     [
57         [ first name>> write bl ]
58         [ second write ": " write ]
59         [ third join-lines write ]
60         tri
61     ] histogram.
62     nl nl
63     "==== FREQUENT WORDS:" print nl
64     [ write ] histogram.
65     nl nl
66     "==== ERRORS:" print nl
67     errors. ;
68
69 : analyze-log ( lines word-names -- )
70     [ parse-log ] dip analyze-entries analysis. ;
71
72 : analyze-log-file ( service word-names -- )
73     [ parse-log-file ] dip analyze-entries analysis. ;