Skip to content

Instantly share code, notes, and snippets.

@nicomen
Last active December 7, 2016 18:38
Show Gist options
  • Save nicomen/c372928cf354203d0311646d3dda93b9 to your computer and use it in GitHub Desktop.
Save nicomen/c372928cf354203d0311646d3dda93b9 to your computer and use it in GitHub Desktop.
package RG::Charter::Tree;
# FIXME: Remove me when frontend does not use this in production anymore
use Moo;
use 5.010;
use Clone 'clone';
use Data::Rmap;
use Mojo::JSON::MaybeXS;
use Mojo::JSON qw/encode_json/;
use RG::Charter::Search;
use RG::Model::Destination;
use RG::Model::DestTranslationType;
use RG::Model::DestTranslation;
use RG::General::Tools;
with 'RG::Role::WithContext';
with 'RG::Role::WithLogger';
has 'dest' => ( is => 'lazy', default => sub { RG::Model::Destination->new( logger => shift->logger ); } );
has 'tree' => ( is => 'lazy', default => sub { my ($self) = @_; $self->dest->get_tree({ lang => $self->context->language }); } );
has 'solr' => ( is => 'lazy', default => sub { RG::Charter::Search->new( context => shift->context )->engine(); } );
=head2 get_offer_tree($opt)
$opt->{query} - Array ref of query options to send to solr
See L<RG::General::Tools::generate_tree> for additional options
=cut
sub get_offer_tree {
my ($self, $opt) = @_;
$opt->{keep_fields} //= [ qw/id name offer_cnt children parent/ ];
my $query_options = {
'json.facet' => encode_json ({
destination_id => {
type => 'terms',
limit => -1,
field => 'destination_id',
facet => {
uniq => 'unique(hotel_id)',
}
},
}),
'rows' => 0,
fq => (join ' AND ', @{ $opt->{query} || [ 'origin_departure_date: [ NOW/HOUR+1HOUR TO * ]', 'offer_type: SPECIFIED' ] }),
};
my $query = 'active: true';
my $response = $self->solr->search( $query, $query_options, { return_original_response => 1 } );
return $self->logger->error_undef("Failed to fetch offers from Solr") unless $response;
my $dest_offers = {
map { $_->{val} => $_->{uniq}; } @{ $response->{'facets'}->{'destination_id'}->{buckets} }
};
my $max_price = $response->{'facets'}->{'max_price'};
my $min_price = $response->{'facets'}->{'min_price'};
my $wanted_root;
my $tree = RG::General::Tools::generate_tree( $self->tree, { %{ $opt }, post_process_cb => sub {
for my $ele ($_) {
$wanted_root = $_ if $_->{id} eq $opt->{root_id};
if (my $ele_offers = $dest_offers->{ $_->{id} }) {
$_->{offer_cnt} = $ele_offers;
$dest_offers->{ delete $_->{parent} } += $ele_offers if $_->{parent};
}
}
} });
return $wanted_root || $tree;
}
1;
$ time RG_MODE=prod perl -Ilib -wle 'use DDP; use RG::Charter::Tree; p(RG::Charter::Tree->new( context => RG::Context->new_from_url( { url => "solfaktor.no" }) )->get_offer_tree( { root_id => 19795 } ) );'
Subroutine JSON::PP::Boolean::(-- redefined at /usr/share/perl/5.22/overload.pm line 50, <DATA> line 2231.
Subroutine JSON::PP::Boolean::(0+ redefined at /usr/share/perl/5.22/overload.pm line 50, <DATA> line 2231.
Subroutine JSON::PP::Boolean::(++ redefined at /usr/share/perl/5.22/overload.pm line 50, <DATA> line 2231.
Loaded configs: share/etc/system.conf, share/etc/system_prod.conf
Wide character in print at /usr/share/perl5/Data/Printer.pm line 181.
\ {
children [
[0] {
children [
[0] {
id 20719,
name "Burgaskusten",
offer_cnt 4
},
[1] {
id 38533,
name "Elenite",
offer_cnt 1
},
[2] {
id 38530,
name "Nesebăr",
offer_cnt 28
},
[3] {
id 38534,
name "Obzor",
offer_cnt 9
},
[4] {
id 40269,
name "Pomorie",
offer_cnt 5
},
[5] {
id 38523,
name "Primorsko",
parent 38524
},
[6] {
id 38527,
name "Saint Vlas",
offer_cnt 7
},
[7] {
id 38531,
name "Sinemorec",
offer_cnt 1
},
[8] {
id 38535,
name "Sozopol",
offer_cnt 4
},
[9] {
id 38532,
name "Sunny Beach",
offer_cnt 123
},
[10] {
id 38529,
name "Tsarevo",
offer_cnt 2
}
],
id 38524,
name "Burgaskusten",
offer_cnt 184
},
[1] {
id 20713,
name "Sofia",
offer_cnt 31
},
[2] {
id 20710,
name "Varnakusten",
offer_cnt 9
},
[3] {
children [
[0] {
id 38537,
name "Albena",
offer_cnt 23
},
[1] {
id 38539,
name "Baltchik",
offer_cnt 6
},
[2] {
id 38538,
name "Golden Sands",
offer_cnt 40
},
[3] {
id 31482,
name "Varna Airport",
parent 38536
}
],
id 38536,
name "Varnakusten",
offer_cnt 69
}
],
id 19795,
name "Bulgarien",
offer_cnt 293
}
real 0m2.245s
user 0m0.824s
sys 0m0.040s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment