Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send events when editing products #7303

Merged
merged 10 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ MONGODB_CACHE_SIZE=8 # GB
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=test
ROBOTOFF_URL=http://api:5500 # connect to Robotoff running in separate docker-compose deployment
EVENTS_URL=
GOOGLE_CLOUD_VISION_API_KEY=
CROWDIN_PROJECT_IDENTIFIER=
CROWDIN_PROJECT_KEY=
Expand Down
7 changes: 6 additions & 1 deletion cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use ProductOpener::ForestFootprint qw/:all/;
use ProductOpener::Web qw(get_languages_options_list);
use ProductOpener::Text qw/:all/;
use ProductOpener::Events qw/:all/;

use Apache2::RequestRec ();
use Apache2::Const ();
Expand Down Expand Up @@ -1500,6 +1501,9 @@ ($product_ref, $field, $language)
# Notify robotoff
send_notification_for_product_change($product_ref, "updated");

# Create an event
send_event( { user_id => $User_id, event_type => "product_edited", barcode => $code, points => 5});

$template_data_ref_process->{display_random_sample_of_products_after_edits_options}
= $options{display_random_sample_of_products_after_edits};

Expand All @@ -1518,10 +1522,11 @@ ($product_ref, $field, $language)
);

display_product(\%request);

}
}

$log->debug("product edited", { code => $code }) if $log->is_debug();

$template_data_ref_process->{edited_product_url} = $edited_product_url;
process_template('web/pages/product_edit/product_edit_form_process.tt.html', $template_data_ref_process, \$html)
or $html = "<p>" . $tt->error() . "</p>";
Expand Down
3 changes: 3 additions & 0 deletions conf/apache.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ PerlPassEnv PRODUCT_OPENER_DOMAIN
PerlPassEnv PRODUCT_OPENER_PORT
PerlPassEnv PRODUCERS_PLATFORM
PerlPassEnv ROBOTOFF_URL
PerlPassEnv EVENTS_URL
PerlPassEnv EVENTS_USERNAME
PerlPassEnv EVENTS_PASSWORD
PerlPassEnv MONGODB_HOST
PerlPassEnv GOOGLE_CLOUD_VISION_API_KEY
PerlPassEnv CROWDIN_PROJECT_IDENTIFIER
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ x-backend-conf: &backend-conf
- POSTGRES_USER
- POSTGRES_PASSWORD
- ROBOTOFF_URL
- EVENTS_URL
- EVENTS_USERNAME
- EVENTS_PASSWORD
- GOOGLE_CLOUD_VISION_API_KEY
- CROWDIN_PROJECT_IDENTIFIER
- CROWDIN_PROJECT_KEY
Expand Down
9 changes: 9 additions & 0 deletions lib/ProductOpener/Config2_docker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ BEGIN
$crowdin_project_identifier
$crowdin_project_key
$robotoff_url
$events_url
$events_username
$events_password
%server_options
);
%EXPORT_TAGS = (all => [@EXPORT_OK]);
Expand Down Expand Up @@ -93,6 +96,12 @@ my $postgres_url = "postgresql://${postgres_user}:${postgres_password}\@${postgr
# enable an in-site robotoff-asker in the product page
$robotoff_url = $ENV{ROBOTOFF_URL};

# Set this to your instance of https://github.com/openfoodfacts/openfoodfacts-events
# enable creating events for some actions (e.g. when a product is edited)
$events_url = $ENV{EVENTS_URL};
$events_username = $ENV{EVENTS_USERNAME};
$events_password = $ENV{EVENTS_PASSWORD};

%server_options = (
private_products => $producers_platform, # 1 to make products visible only to the owner (producer platform)
producers_platform => $producers_platform,
Expand Down
10 changes: 9 additions & 1 deletion lib/ProductOpener/Config2_sample.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ BEGIN
$crowdin_project_identifier
$crowdin_project_key
$robotoff_url

$events_url
$events_username
$events_password
%server_options

);
Expand Down Expand Up @@ -74,6 +76,12 @@ $crowdin_project_key = '';
# enable an in-site robotoff-asker in the product page
$robotoff_url = '';

# Set this to your instance of https://github.com/openfoodfacts/openfoodfacts-events
# enable creating events for some actions (e.g. when a product is edited)
$events_url = '';
$events_username = '';
$events_password = '';

%server_options = (

cookie_domain => "openfoodfacts.dev", # if not set, default to $server _domain
Expand Down
11 changes: 11 additions & 0 deletions lib/ProductOpener/Config_obf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ BEGIN
$crowdin_project_key

$robotoff_url
$events_url
$events_username
$events_password

$mongodb
$mongodb_host
Expand Down Expand Up @@ -199,8 +202,16 @@ $google_cloud_vision_api_key = $ProductOpener::Config2::google_cloud_vision_api_
$crowdin_project_identifier = $ProductOpener::Config2::crowdin_project_identifier;
$crowdin_project_key = $ProductOpener::Config2::crowdin_project_key;

# Set this to your instance of https://github.com/openfoodfacts/robotoff/ to
# enable an in-site robotoff-asker in the product page
$robotoff_url = $ProductOpener::Config2::robotoff_url;

# Set this to your instance of https://github.com/openfoodfacts/openfoodfacts-events
# enable creating events for some actions (e.g. when a product is edited)
$events_url = $ProductOpener::Config2::events_url;
$events_username = $ProductOpener::Config2::events_username;
$events_password = $ProductOpener::Config2::events_password;

# server options

%server_options = %ProductOpener::Config2::server_options;
Expand Down
11 changes: 11 additions & 0 deletions lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ BEGIN
$crowdin_project_key

$robotoff_url
$events_url
$events_username
$events_password

$mongodb
$mongodb_host
Expand Down Expand Up @@ -345,8 +348,16 @@ $google_cloud_vision_api_key = $ProductOpener::Config2::google_cloud_vision_api_
$crowdin_project_identifier = $ProductOpener::Config2::crowdin_project_identifier;
$crowdin_project_key = $ProductOpener::Config2::crowdin_project_key;

# Set this to your instance of https://github.com/openfoodfacts/robotoff/ to
# enable an in-site robotoff-asker in the product page
$robotoff_url = $ProductOpener::Config2::robotoff_url;

# Set this to your instance of https://github.com/openfoodfacts/openfoodfacts-events
# enable creating events for some actions (e.g. when a product is edited)
$events_url = $ProductOpener::Config2::events_url;
$events_username = $ProductOpener::Config2::events_username;
$events_password = $ProductOpener::Config2::events_password;

# server options

%server_options = %ProductOpener::Config2::server_options;
Expand Down
11 changes: 11 additions & 0 deletions lib/ProductOpener/Config_opf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ BEGIN
$crowdin_project_key

$robotoff_url
$events_url
$events_username
$events_password

$mongodb
$mongodb_host
Expand Down Expand Up @@ -197,8 +200,16 @@ $google_cloud_vision_api_key = $ProductOpener::Config2::google_cloud_vision_api_
$crowdin_project_identifier = $ProductOpener::Config2::crowdin_project_identifier;
$crowdin_project_key = $ProductOpener::Config2::crowdin_project_key;

# Set this to your instance of https://github.com/openfoodfacts/robotoff/ to
# enable an in-site robotoff-asker in the product page
$robotoff_url = $ProductOpener::Config2::robotoff_url;

# Set this to your instance of https://github.com/openfoodfacts/openfoodfacts-events
# enable creating events for some actions (e.g. when a product is edited)
$events_url = $ProductOpener::Config2::events_url;
$events_username = $ProductOpener::Config2::events_username;
$events_password = $ProductOpener::Config2::events_password;

# server options

%server_options = %ProductOpener::Config2::server_options;
Expand Down
11 changes: 11 additions & 0 deletions lib/ProductOpener/Config_opff.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ BEGIN
$crowdin_project_key

$robotoff_url
$events_url
$events_username
$events_password

$mongodb
$mongodb_host
Expand Down Expand Up @@ -195,8 +198,16 @@ $google_cloud_vision_api_key = $ProductOpener::Config2::google_cloud_vision_api_
$crowdin_project_identifier = $ProductOpener::Config2::crowdin_project_identifier;
$crowdin_project_key = $ProductOpener::Config2::crowdin_project_key;

# Set this to your instance of https://github.com/openfoodfacts/robotoff/ to
# enable an in-site robotoff-asker in the product page
$robotoff_url = $ProductOpener::Config2::robotoff_url;

# Set this to your instance of https://github.com/openfoodfacts/openfoodfacts-events
# enable creating events for some actions (e.g. when a product is edited)
$events_url = $ProductOpener::Config2::events_url;
$events_username = $ProductOpener::Config2::events_username;
$events_password = $ProductOpener::Config2::events_password;

# server options

%server_options = %ProductOpener::Config2::server_options;
Expand Down
143 changes: 143 additions & 0 deletions lib/ProductOpener/Events.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# This file is part of Product Opener.
#
# Product Opener
# Copyright (C) 2011-2022 Association Open Food Facts
# Contact: [email protected]
# Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France
#
# Product Opener is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

=head1 NAME

ProductOpener::Events - Send events to https://events.openfoodfacts.org

=head1 SYNOPSIS

C<ProductOpener::Events> is used to create events to https://events.openfoodfacts.org

use ProductOpener::Events qw/:all/;

# TODO

=head1 DESCRIPTION

See https://github.com/openfoodfacts/openfoodfacts-events

=cut

package ProductOpener::Events;

use ProductOpener::PerlStandards;
use Exporter qw< import >;

BEGIN {
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS);
@EXPORT_OK = qw(
&send_event
); # symbols to export on request
%EXPORT_TAGS = (all => [@EXPORT_OK]);
}

use vars @EXPORT_OK;

use Log::Any qw($log);

use Encode;
use JSON::PP;
use LWP::UserAgent;
use HTTP::Request::Common;

use ProductOpener::Config qw/:all/;
use ProductOpener::Display qw/display_date_iso/;

=head1 FUNCTIONS

=head2 send_event ( $event_ref )

=head3 Arguments

Arguments are passed through a single hash reference with the following keys:

=head4 event_type - required - string

Type of the event (e.g. "product_edited")

=head4 barcode - required - string

Barcode of the product.

=head4 user id - required

=cut

sub send_event ($event_ref) {
alexgarel marked this conversation as resolved.
Show resolved Hide resolved

if ((defined $events_url) and ($events_url ne "")) {

# Add timestamp if we event does not contain one already
if (not defined $event_ref->{timestamp}) {
$event_ref->{timestamp} = display_date_iso(time());
}

my $ua = LWP::UserAgent->new();
my $endpoint = "$events_url/events";
$ua->timeout(2);

my $request = POST $endpoint, $event_ref;
$request->header('content-type' => 'application/json');
$request->content(decode_utf8(encode_json($event_ref)));

# Add basic HTTP authentification credentials if we have some
# (as of August 2022, they are required to post to /events)
if ((defined $events_username) and ($events_username ne "")) {
$request->authorization_basic($events_username, $events_password);
}

$log->debug("send_event request", {endpoint => $endpoint, event => $event_ref}) if $log->is_debug();
my $response = $ua->request($request);

if ($response->is_success) {
$log->debug(
"send_event response ok",
{
endpoint => $endpoint,
event => $event_ref,
is_success => $response->is_success,
code => $response->code,
status_line => $response->status_line
}
) if $log->is_debug();
}
else {
$log->warn(
"send_event response not ok",
{
endpoint => $endpoint,
event => $event_ref,
is_success => $response->is_success,
code => $response->code,
status_line => $response->status_line,
response => $response
}
) if $log->is_warn();
}
}
else {
$log->debug("send_event EVENTS_URL not defined", {events_url => $events_url}) if $log->is_debug();
}

return;
}

1;