]> gitweb.factorcode.org Git - factor.git/blob - extra/spider/report/report.factor
7779b233f9adda966f9a6d37ef6ec31c4022fe33
[factor.git] / extra / spider / report / report.factor
1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators kernel math
4 math.statistics namespaces sequences sorting xml.syntax
5 spider urls html ;
6 IN: spider.report
7
8 SYMBOL: network-failures
9 SYMBOL: broken-pages
10 SYMBOL: timings
11
12 : record-broken-page ( url spider-result -- )
13     headers>> [ code>> ] [ message>> ] bi 2array 2array
14     broken-pages push ;
15
16 : record-page-timings ( url spider-result -- )
17     fetched-in>> 2array timings get push ;
18
19 : record-network-failure ( url -- )
20     network-failures get push ;
21
22 : process-result ( url spider-result -- )
23     {
24         { f [ record-network-failure ] }
25         [
26             dup headers>> code>> 200 =
27             [ record-page-timings ] [ record-broken-page ] if
28         ]
29     } case ;
30
31 CONSTANT: slowest 5
32
33 SYMBOL: slowest-pages
34 SYMBOL: mean-time
35 SYMBOL: median-time
36 SYMBOL: time-std
37
38 : process-timings ( -- )
39     timings get sort-values
40     [ slowest short tail* reverse slowest-pages set ]
41     [
42         values [
43             [ mean 1000000 /f mean-time set ]
44             [ median 1000000 /f median-time set ]
45             [ std 1000000 /f time-std set ] tri
46         ] unless-empty
47     ] bi ;
48
49 : process-results ( results -- )
50     V{ } clone network-failures set
51     V{ } clone broken-pages set
52     V{ } clone timings set
53     [ process-result ] assoc-each
54     process-timings ;
55
56 : info-table ( alist -- html )
57     [
58         first2 dupd 1000000 /f
59         [XML
60         <tr><td><a href=<->><-></a></td><td><-> seconds</td></tr>
61         XML]
62     ] map [XML <table border="1"><-></table> XML] ;
63
64 : report-broken-pages ( -- html )
65     broken-pages get info-table ;
66
67 : report-network-failures ( -- html )
68     network-failures get [
69         dup [XML <li><a href=<->><-></a></li> XML]
70     ] map [XML <ul><-></ul> XML] ;
71
72 : slowest-pages-table ( -- html )
73     slowest-pages get info-table ;
74
75 : timing-summary-table ( -- html )
76     mean-time get
77     median-time get
78     time-std get
79     [XML
80     <table border="1">
81     <tr><th>Mean</th><td><-> seconds</td></tr>
82     <tr><th>Median</th><td><-> seconds</td></tr>
83     <tr><th>Standard deviation</th><td><-> seconds</td></tr>
84     </table>
85     XML] ;
86
87 : report-timings ( -- html )
88     slowest-pages-table
89     timing-summary-table
90     [XML
91     <h3>Slowest pages</h3>
92     <->
93
94     <h3>Summary</h3>
95     <->
96     XML] ;
97
98 : generate-report ( -- html )
99     url get dup
100     report-broken-pages
101     report-network-failures
102     report-timings
103     [XML
104     <h1>Spider report</h1>
105     URL: <a href=<->><-></a>
106
107     <h2>Broken pages</h2>
108     <->
109
110     <h2>Network failures</h2>
111     <->
112
113     <h2>Load times</h2>
114     <->
115     XML] ;
116
117 : spider-report ( spider -- html )
118     [ "Spider report" f ] dip
119     [
120         [ base>> url set ]
121         [ spidered>> process-results ] bi
122         generate-report
123     ] with-scope
124     simple-page ;