Skip to content

Commit

Permalink
feat: export packaging components data (#8362)
Browse files Browse the repository at this point in the history
feat: export packaging components data #8361
  • Loading branch information
stephanegigandet authored Apr 25, 2023
1 parent 1c945a7 commit c84cee3
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions scripts/gen_packaging_stats.pl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ =head1 DESCRIPTION
use JSON::PP;
use Data::DeepAccess qw(deep_exists deep_get deep_set deep_val);
use Getopt::Long;
use Text::CSV;

my $quiet;

Expand Down Expand Up @@ -322,6 +323,45 @@ ($name, $packagings_stats_ref)
return;
}

=head2 export_product_packaging_components_to_csv($csv, $filehandle, $product_ref)
Export each packaging component of the product as one line in the CSV file.
=cut

sub export_product_packaging_components_to_csv ($csv, $filehandle, $product_ref) {

# Go through all packaging components
if (defined $product_ref->{packagings}) {

my $countries_tags;
if (defined $product_ref->{countries_tags}) {
$countries_tags = join(",", @{$product_ref->{countries_tags}});
}

my $categories_tags;
if (defined $product_ref->{categories_tags}) {
$categories_tags = join(",", @{$product_ref->{categories_tags}});
}

foreach my $packaging_ref (@{$product_ref->{packagings}}) {

my $weight = $packaging_ref->{weight_specified} // $packaging_ref->{weight_measured};

my @values = (
$product_ref->{code}, $countries_tags,
$categories_tags, $packaging_ref->{number_of_units},
$packaging_ref->{shape}, $packaging_ref->{material},
$packaging_ref->{recycling}, $packaging_ref->{weight},
$packaging_ref->{weight_measured}, $packaging_ref->{weight_specified},
$packaging_ref->{quantity_per_unit},
);

$csv->print($filehandle, \@values);
}
}
}

=head2 generate_packaging_stats_for_query($name, $query_ref)
Generate packaging stats for products matching a specific query.
Expand All @@ -345,6 +385,7 @@ ($name, $query_ref)

# fields to retrieve
my $fields_ref = {
code => 1,
countries_tags => 1,
categories_tags => 1,
packagings => 1,
Expand All @@ -365,13 +406,38 @@ ($name, $query_ref)

my $packagings_stats_ref = {};

# Export packaging components of all products to a CSV file
my $filehandle;
my $filename = "$www_root/data/$name.csv";
open($filehandle, ">:encoding(UTF-8)", $filename)
or die("Could not write " . $filename . " : $!\n");
my $csv = Text::CSV->new(
{
eol => "\n",
sep => "\t",
quote_space => 0,
binary => 1
}
) or die "Cannot use CSV: " . Text::CSV->error_diag();

# Print the header line with fields names
$csv->print(
$filehandle,
[
"code", "countries_tags", "categories_tags", "number_of_units",
"shape", "material", "recycling", "weight", "weight_measured",
"weight_specified", "quantity_per_unit"
]
);

# Go through all products
while (my $product_ref = $cursor->next) {
$total++;

if ($total % 1000 == 0) {
$quiet or print STDERR "$name: $total / $products_count processed\n";
}
export_product_packaging_components_to_csv($csv, $filehandle, $product_ref);

add_product_to_stats($name, $packagings_stats_ref, $product_ref);
}
Expand Down

0 comments on commit c84cee3

Please sign in to comment.