commit 8c899a15a8c3a97753bad9e53c1f4f3389d503b4
parent ddf6ef593ff469f2061cd0e6a52f04ba4e077725
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Sat, 18 Apr 2015 01:20:33 -0600
gen_html: unite all folder generation
Diffstat:
M | gen_html | | | 230 | +++++++++++++++++++++++++++++++------------------------------------------------ |
1 file changed, 90 insertions(+), 140 deletions(-)
diff --git a/gen_html b/gen_html
@@ -40,177 +40,68 @@ my $template = Template->new($config)
# manufacturers
#
my $stale_clause = $args{a} ? "" : "and products.svg_stale = 1";
-my $sql = qq{select distinct prices.manufacturer from prices, products where
+my $sql_1 = qq{select distinct prices.manufacturer from prices, products where
prices.manufacturer = products.manufacturer and
prices.part_num = products.part_num $stale_clause
group by prices.manufacturer having count(distinct prices.part_num) > 1};
-my $manufacturers = $dbh->selectcol_arrayref($sql);
-my ($num_manuf, %summary, %logo_file) = (scalar @$manufacturers, (), ());
-print "info: manufacturers: " if ($args{v});
-for my $manufacturer (@$manufacturers) {
- spin() if ($args{v});
-
- my $logo_file = get_logo(lc($manufacturer));
- $logo_file{$manufacturer} = $logo_file;
-
- # must have at least one price to show up from this
- $sql = qq{select distinct products.type from prices, products where
- prices.manufacturer = products.manufacturer and
- prices.part_num = products.part_num and prices.manufacturer = ?};
- my $types = $dbh->selectcol_arrayref($sql, undef, $manufacturer);
-
- # show product charts categorized first by type and then manufacturer
- my $manufacturer_link = linkify($manufacturer);
- for my $type (sort @$types) {
- $sql = qq{select distinct manufacturer, part_num
- from products where type = ? and manufacturer = ?};
- my $products = $dbh->selectall_arrayref($sql, undef, $type, $manufacturer);
- $_->[2] = get_description($_->[0], $_->[1]) for (@$products);
-
- my $type_link = linkify($type);
- $type = PL($type, scalar @$products);
- $summary{$manufacturer}{$type}{num} = scalar @$products;
- $summary{$manufacturer}{$type}{link} = "$manufacturer_link/$type_link";
-
- my $vars = {
- name => $manufacturer, type => $type,
- products => $products, logo_file => $logo_file
- };
- my $out_path = "manufacturers/$manufacturer_link/$type_link.html";
- $template->process("chart_list.tt", $vars, $out_path)
- or die "template: " . $template->error() . "\n";
- }
-
- # print Dumper($summary{$manufacturer});
- my $vars = {
- type => "manufacturer", name => $manufacturer,
- info => \$summary{$manufacturer}, logo => $logo_file
- };
- $template->process("summary.tt", $vars, "manufacturers/$manufacturer_link.html")
- or die "template: " . $template->error() . "\n";
-}
-print "\b$num_manuf processed\n" if ($args{v});
+my $sql_2 = qq{select distinct products.type from prices, products where
+ prices.manufacturer = products.manufacturer and
+ prices.part_num = products.part_num and prices.manufacturer = ?};
-my $vars = { name => "Manufacturers", list => \%summary, logo_file => \%logo_file };
-$template->process("coarse_list.tt", $vars, "manufacturers.html")
- or die "template: " . $template->error() . "\n";
+my $sql_3 = qq{select distinct manufacturer, part_num
+ from products where type = ? and manufacturer = ?};
+generate_folder($sql_1, $sql_2, $sql_3, "manufacturers", "Manufacturers");
#
# retailers
#
if ($args{a}) {
- $sql = "select distinct retailer from prices";
+ $sql_1 = "select distinct retailer from prices";
} else {
- $sql = qq{select distinct prices.retailer from prices, products where
+ $sql_1 = qq{select distinct prices.retailer from prices, products where
prices.manufacturer = products.manufacturer and
prices.part_num = products.part_num and products.svg_stale = 1};
}
-my $retailers = $dbh->selectcol_arrayref($sql);
-
-my $num_retailers = scalar @$retailers;
-(%summary, %logo_file) = ((), ());
-print "info: retailers: " if ($args{v});
-for my $retailer (@$retailers) {
- my $retailer_lc = lc($retailer);
- spin() if ($args{v});
- my $logo_file = get_logo($retailer);
+$sql_2 = qq{select distinct products.type from prices, products where
+ prices.manufacturer = products.manufacturer and
+ prices.part_num = products.part_num and prices.retailer = ?};
- $sql = qq{select distinct products.type from prices, products where
- prices.manufacturer = products.manufacturer and
- prices.part_num = products.part_num and prices.retailer = ?};
- my $types = $dbh->selectcol_arrayref($sql, undef, $retailer);
-
- for my $type (sort @$types) {
- $sql = qq{select distinct prices.manufacturer, prices.part_num
- from prices, products where
- prices.part_num = products.part_num and
- products.manufacturer = prices.manufacturer
- and products.type = ? and prices.retailer = ?};
- my $products = $dbh->selectall_arrayref($sql, undef, $type, $retailer);
- $_->[2] = get_description($_->[0], $_->[1]) for (@$products);
-
- my $type_link = linkify($type);
- my $retailer_link = linkify($retailer);
- $type = PL($type, scalar @$products);
- $summary{$retailer}{$type}{num} = scalar @$products;
- $summary{$retailer}{$type}{link} = "$retailer_link/$type_link";
+$sql_3 = qq{select distinct prices.manufacturer, prices.part_num
+ from prices, products where
+ prices.part_num = products.part_num and
+ products.manufacturer = prices.manufacturer
+ and products.type = ? and prices.retailer = ?};
- my $vars = {
- name => $retailer, type => $type,
- products => $products, logo_file => $logo_file
- };
- $template->process("chart_list.tt", $vars,
- "retailers/$retailer_link/$type_link.html")
- or die "template: " . $template->error() . "\n";
- }
-}
-print "\b$num_retailers processed\n" if ($args{v});
-
-$vars = { name => "Retailers", list => \%summary, logo_file => \%logo_file };
-$template->process("coarse_list.tt", $vars, "retailers.html")
- or die "template: " . $template->error() . "\n";
+generate_folder($sql_1, $sql_2, $sql_3, "retailers", "Retailers");
#
# product types
#
$stale_clause = $args{a} ? "" : "and products.svg_stale = 1";
-$sql = qq{select distinct products.type from products, prices where
+$sql_1 = qq{select distinct products.type from products, prices where
products.manufacturer = prices.manufacturer and
products.part_num = prices.part_num $stale_clause};
-my $types = $dbh->selectcol_arrayref($sql);
-
-my $num_types = scalar @$types;
-print "info: types: " if ($args{v});
-(%summary, %logo_file) = ((), ());
-for my $type (@$types) {
- my $type_lc = lc($type);
- spin() if ($args{v});
- my $logo_file = "";
-
- $sql = qq{select distinct products.manufacturer from prices, products where
- prices.manufacturer = products.manufacturer and
- prices.part_num = products.part_num and products.type = ?};
- my $manufacturers = $dbh->selectcol_arrayref($sql, undef, $type);
-
- my $type_link = linkify($type);
- for my $manufacturer (sort @$manufacturers) {
- $sql = qq{select distinct prices.manufacturer, prices.part_num
- from prices, products where
- prices.part_num = products.part_num and
- products.manufacturer = prices.manufacturer and
- products.manufacturer = ?};
- my $products = $dbh->selectall_arrayref($sql, undef, $manufacturer);
- $_->[2] = get_description($_->[0], $_->[1]) for (@$products);
-
- my $manuf_link = linkify($manufacturer);
- $manufacturer = PL($manufacturer, scalar @$products);
- $summary{$type}{$manufacturer}{num} = scalar @$products;
- $summary{$type}{$manufacturer}{link} = "$type_link/$manuf_link";
+$sql_2 = qq{select distinct products.manufacturer from prices, products where
+ prices.manufacturer = products.manufacturer and
+ prices.part_num = products.part_num and products.type = ?};
- my $vars = {
- name => $manufacturer, type => $type,
- products => $products, logo_file => $logo_file
- };
- $template->process("chart_list.tt", $vars,
- "types/$type_link/$manuf_link.html")
- or die "template: " . $template->error() . "\n";
- }
-}
-print "\b$num_types processed\n" if ($args{v});
+$sql_3 = qq{select distinct prices.manufacturer, prices.part_num
+ from prices, products where
+ prices.part_num = products.part_num and
+ products.manufacturer = prices.manufacturer and
+ products.manufacturer = ? and products.type = ?};
-$vars = { name => "Types", list => \%summary, logo_file => \%logo_file };
-$template->process("coarse_list.tt", $vars, "types.html")
- or die "template: " . $template->error() . "\n";
+generate_folder($sql_1, $sql_2, $sql_3, "types", "Types");
#
# products
#
$stale_clause = $args{a} ? "" : "where svg_stale = 1";
-$sql = "select * from products $stale_clause";
+my $sql = "select * from products $stale_clause";
my $products = $dbh->selectall_hashref($sql, "part_num");
my $num_products = scalar keys %$products;
@@ -240,8 +131,8 @@ my $upds = $dbh->selectall_arrayref($sql, undef, time - (1 * 60 * 60));
#
# index.html
#
-$vars = {
- nret => $num_retailers, nmanuf => $num_manuf, nprod => $num_products,
+my $vars = {
+ nret => 66, nmanuf => 99, nprod => $num_products,
nnew => scalar @$news, news => $news, nupd => scalar @$upds, upds => $upds
};
print "info: index.html\n" if ($args{v});
@@ -335,6 +226,65 @@ $dbh->do("update products set svg_stale = 0");
$dbh->disconnect();
+
+#
+# generate an entire tree of html structure
+#
+sub generate_folder
+{
+ my $sql_1 = shift;
+ my $sql_2 = shift;
+ my $sql_3 = shift;
+ my $name_lc = shift;
+ my $name = shift;
+
+ my $coarse_list = $dbh->selectcol_arrayref($sql_1);
+
+ my ($num, %summary, %logo_file) = (scalar @$coarse_list, (), ());
+ print "info: $name_lc: " if ($args{v});
+ for my $it (@$coarse_list) {
+ spin() if ($args{v});
+
+ my $logo_file = get_logo(lc($it));
+ $logo_file{$it} = $logo_file;
+
+ my $it_link = linkify($it);
+ my $types = $dbh->selectcol_arrayref($sql_2, undef, $it);
+
+ # show product charts categorized first by type and then manufacturer
+ for my $type (sort @$types) {
+ my $products = $dbh->selectall_arrayref($sql_3, undef, $type, $it);
+ $_->[2] = get_description($_->[0], $_->[1]) for (@$products);
+
+ my $type_link = linkify($type);
+ $type = PL($type, scalar @$products);
+ $summary{$it}{$type}{num} = scalar @$products;
+ $summary{$it}{$type}{link} = "$it_link/$type_link";
+
+ my $vars = {
+ name => $it, type => $type,
+ products => $products, logo_file => $logo_file
+ };
+ my $out_path = "$name_lc/$it_link/$type_link.html";
+ $template->process("chart_list.tt", $vars, $out_path)
+ or die "template: " . $template->error() . "\n";
+ }
+
+ # print Dumper($summary{$manufacturer});
+ my $vars = {
+ type => "manufacturer", name => $it,
+ info => \$summary{$it}, logo => $logo_file
+ };
+ $template->process("summary.tt", $vars, "$name_lc/$it_link.html")
+ or die "template: " . $template->error() . "\n";
+ }
+ print "\b$num processed\n" if ($args{v});
+
+ my $vars = { name => $name, list => \%summary, logo_file => \%logo_file };
+ $template->process("coarse_list.tt", $vars, "$name_lc.html")
+ or die "template: " . $template->error() . "\n";
+}
+
sub get_logo
{
my $name = shift;