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