9 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
10 |
use Time::HiRes qw/time/; |
use Time::HiRes qw/time/; |
11 |
|
|
12 |
|
print qq{Content-type: text/html\r\n\r\n}; |
13 |
|
|
14 |
our $dsn = 'DBI:Pg:dbname=syslog'; |
our $dsn = 'DBI:Pg:dbname=syslog'; |
15 |
our $user = 'dpavlin'; |
our $user = 'dpavlin'; |
16 |
|
our $table = 'log'; |
17 |
|
our $limit = 1000; |
18 |
|
|
19 |
|
our $group_by_join = { |
20 |
|
feed_id => [ 'feeds', 'id', 'title', 'link', 'timestamp' ], |
21 |
|
}; |
22 |
|
|
23 |
require 'config.pl' if -e 'config.pl'; |
require 'config.pl' if -e 'config.pl'; |
24 |
|
|
25 |
my $table = param('table') || 'log'; |
$table = param('table') || $table; |
26 |
my @columns = param('columns'); |
my @columns = param('columns'); |
27 |
@columns = ('*') unless @columns; |
@columns = ('*') unless @columns; |
28 |
my $limit = param('limit') || 1000; |
$limit = param('limit') || $limit; |
29 |
my $offset = param('offset') || 0; |
my $offset = param('offset') || 0; |
30 |
|
|
31 |
my @where_parts = param('where_parts'); |
my @where_parts = param('where_parts'); |
32 |
|
|
33 |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
my $dbh = DBI->connect( $dsn, $user, '', { RaiseError => 1 } ) || die $DBI::errstr; |
34 |
|
|
35 |
if ( my $group_by = param('add_group_by') ) { |
if ( my $group_by = param('lookup_col') ) { |
36 |
my $sth = $dbh->prepare(qq{ |
|
37 |
select $group_by,count($group_by) |
my @cols = ( $group_by, "count($group_by)" ); |
38 |
|
my @group_by = ( $group_by ); |
39 |
|
|
40 |
|
my $join = ''; |
41 |
|
my @join = @{ $group_by_join->{$group_by} } if defined $group_by_join->{$group_by}; |
42 |
|
if ( @join ) { |
43 |
|
warn "## join ",dump( @join ); |
44 |
|
my $join_table = shift @join; |
45 |
|
my $col = shift @join; |
46 |
|
$join = qq{ join $join_table on $table.$group_by = $join_table.$col }; |
47 |
|
my @join_cols = map { $join_table . '.' . $_ } @join; |
48 |
|
push @cols, @join_cols; |
49 |
|
push @group_by, @join_cols; |
50 |
|
} |
51 |
|
|
52 |
|
my $sql = join("\n", |
53 |
|
'select', join(',', @cols), qq{ |
54 |
from $table |
from $table |
55 |
group by $group_by |
$join |
56 |
|
}, 'group by', join(',', @group_by), qq{ |
57 |
order by count($group_by) desc |
order by count($group_by) desc |
58 |
limit 10 |
limit 10 |
59 |
}); |
} |
60 |
|
); |
61 |
|
|
62 |
|
warn "# join SQL: $sql\n"; |
63 |
|
|
64 |
|
my $t = time(); |
65 |
|
my $sth = $dbh->prepare( $sql ); |
66 |
$sth->execute; |
$sth->execute; |
67 |
print header, qq|<table><tr><th>count</th><th>$group_by</th><tr>|; |
$t = time() - $t; |
68 |
|
print qq|$t<table><tr><th>|, join(qq|</th><th>|, @cols), qq|</th></tr>|; |
69 |
while ( my @row = $sth->fetchrow_array ) { |
while ( my @row = $sth->fetchrow_array ) { |
70 |
my ( $n, $c ) = @row; |
my $n = shift @row; |
71 |
$n = 'NULL' unless defined $n; |
$n = 'NULL' unless defined $n; |
72 |
print qq|<tr><td>$c</td><td><a href="#">$n</a></td></tr>|; |
print qq|<tr><td><a href="#">$n</a></td><td>|, join(qq|</td><td>|, @row), qq|</td></tr>|; |
73 |
} |
} |
74 |
print qq|</table>|; |
print qq|</table>|; |
75 |
|
print qq|<code>$sql</code>|; |
76 |
exit; |
exit; |
77 |
} |
} |
78 |
|
|
79 |
print header, q| |
print q| |
80 |
|
|
81 |
<html> |
<html> |
82 |
<head> |
<head> |
171 |
|
|
172 |
print |
print |
173 |
qq|</table>| |
qq|</table>| |
174 |
, start_form( -id => 'sql' ) |
, start_form( -id => 'sql', -class => 'fixed' ) |
175 |
|
|
176 |
, qq|<a href="#" onclick="\$('form#sql').toggleClass('visible'); return false;" class=close title="hide sql editor">[x]</a>| |
, qq|<input type=button value="[=]" onclick="\$('form#sql').toggleClass('fixed'); return false;" title="toggle fixed position" class="right">| |
177 |
|
, qq|<input type=button value="[x]" onclick="\$('form#sql').toggleClass('visible'); return false;" title="hide sql editor" class="right">| |
178 |
|
|
179 |
, qq|<label for=columns>select</label>| |
, qq|<label for=columns>select</label>| |
180 |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
, checkbox_group( -name => 'columns', -values => [ @columns ], -defaults => [ @columns ] ) |
181 |
|
|
182 |
, qq|<label for=from>from</label>| |
, qq|<label for=from>from</label>| |
183 |
, textfield( -name => 'from', -value => $table, -default => 'log' ) |
, textfield( -name => 'from', -value => $table, -default => $table ) |
184 |
|
|
185 |
, qq|<label for=where>where</label>| |
, qq|<label for=where>where</label>| |
186 |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
, checkbox_group( -name => 'where_parts', -values => [ @where_parts ], -defaults => [ @where_parts ] ) |
189 |
, textfield( -name => 'where_value' ) |
, textfield( -name => 'where_value' ) |
190 |
, qq| |
, qq| |
191 |
<span> |
<span> |
192 |
<input type=button name=add_group_by> |
<input type=button name=lookup_col title="lookup column details"> |
193 |
<input type=button name=close_group_by value="[x]" disabled=1> |
<input type=button name=close_group_by value="[x]" disabled=1> |
194 |
<div id="lookup"></div> |
<div id="lookup"></div> |
195 |
<span> |
<span> |
202 |
, textfield( -name => 'order_by' ) |
, textfield( -name => 'order_by' ) |
203 |
|
|
204 |
, qq|<label for=limit>limit</label>| |
, qq|<label for=limit>limit</label>| |
205 |
, textfield( -name=> 'limit', -default => 1000, -size => 4 ) |
, textfield( -name=> 'limit', -default => $limit, -size => 4 ) |
206 |
|
|
207 |
, qq|<label for=offset>offset</label>| |
, qq|<label for=offset>offset</label>| |
208 |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |
, textfield( -name=> 'offset', -default => 0, -size => 4 ) |