SD/ForeignReplica/PullEncoder.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 149 CC 8 SCAR 19.7
CC
8
Cov
0%
transcode_ticket
12.0
transcode_history
12.0
_only_pull_tickets_modified_after
12.0
CRAP 7.2
1 package App::SD::ForeignReplica::PullEncoder;
2 use Any::Moose;
3 use App::SD::Util;
4 use Params::Validate qw/validate/;
5  
6 with 'Prophet::CLI::ProgressBar';
7  
8 sub run {
9 my $self = shift;
10 my %args = validate( @_, { after => 1});
11  
12 $self->sync_source->log('Finding matching tickets');
13 my $tickets = $self->find_matching_tickets( query => $self->sync_source->query );
14  
15 if ( @$tickets == 0 ) {
16 $self->sync_source->log("No tickets found.");
17 return;
18 }
19  
20 my $counter = 0;
21 $self->sync_source->log_debug("Discovering ticket history");
22  
23 my ( $last_modified, $last_txn, @changesets );
24  
25 my $progress = $self->progress_bar(
26 max => $#$tickets,
27 format => "Fetching ticket history %30b %p Est: %E\r",
28 );
29  
30 for my $ticket (@$tickets) {
31 $counter++;
32 my $changesets;
33 $progress->();
34 $self->sync_source->log_debug( "Fetching $counter of " . scalar @$tickets . " tickets");
35 ( $last_modified, $changesets ) = $self->transcode_ticket( $ticket, $last_modified );
36 unshift @changesets, @$changesets;
37 }
38 my $sorted_changesets = [ sort {
39 $a->original_sequence_no <=> $b->original_sequence_no } @changesets ];
40 return $sorted_changesets;
41 }
42  
43 sub ticket_last_modified { undef}
44  
45 sub transcode_ticket {
46 my $self = shift;
47 my $ticket = shift;
48 my $last_modified = shift;
49 my @changesets;
50  
51 if ( my $ticket_last_modified = $self->ticket_last_modified($ticket) ) {
52  
53 $last_modified = $ticket_last_modified if ( !$last_modified || $ticket_last_modified > $last_modified );
54 }
55  
56 my $transactions = $self->find_matching_transactions(
57 ticket => $ticket,
58 starting_transaction => $self->sync_source->app_handle->handle->last_changeset_from_source(
59 $self->sync_source->uuid_for_remote_id( $self->ticket_id($ticket) )
60 ) || 1
61 );
62  
63 my $changesets;
64 ( $last_modified, $changesets ) = $self->transcode_history( $ticket, $transactions, $last_modified );
65 return ( $last_modified, $changesets );
66 }
67  
68  
69 sub transcode_history {
70 my $self = shift;
71 my $ticket = shift;
72 my $transactions = shift;
73 my $last_modified = shift;
74 my $ticket_id = $self->ticket_id($ticket);
75  
76 my @changesets;
77  
78 # Walk transactions newest to oldest.
79 my $txn_counter = 0;
80 my ($initial_state, $final_state) = $self->translate_ticket_state($ticket, $transactions);
81  
82  
83 for my $txn ( sort { $b->{'serial'} <=> $a->{'serial'} } @$transactions ) {
84 $last_modified = $txn->{timestamp} if ( !$last_modified || ( $txn->{timestamp} > $last_modified ) );
85 $self->sync_source->log_debug( "$ticket_id Transcoding transaction " . ++$txn_counter . " of " . scalar @$transactions );
86  
87 my $changeset = $self->transcode_one_txn( $txn, $initial_state, $final_state );
88 next unless $changeset && $changeset->has_changes;
89  
90 # the changesets are older than the ones that came before, so they go
91 # first
92 unshift @changesets, $changeset;
93 }
94 return ( $last_modified, \@changesets );
95 }
96  
97  
98 sub translate_ticket_state {
99 die 'translate_ticket_state must be implemented';
100 }
101  
102 sub warp_list_to_old_value {
103 my $self = shift;
104 my $current = shift;
105 my $add = shift;
106 my $del = shift;
107 $_ = '' foreach grep !defined, $current, $add, $del;
108  
109 my @new = grep defined && length, split /\s*,\s*/, $current;
110 my @old = grep defined && length && $_ ne $add, (@new, $del);
111 return join( ", ", @old );
112 }
113  
114 =head2 _only_pull_tickets_modified_after
115  
116 If we've previously pulled from this sync source, this routine will
117 return a datetime object. It's safe not to evaluate any ticket last
118 modified before that datetime
119  
120 =cut
121  
122 sub _only_pull_tickets_modified_after {
123 my $self = shift;
124  
125 # last modified date is in GMT and searches are in user-time XXX -check assumption
126 # because of this, we really want to back that date down by one day to catch overlap
127 # XXX TODO we are playing FAST AND LOOSE WITH DATE MATH
128 # XXX TODO THIS WILL HURT US SOME DAY
129 # At that time, Jesse will buy you a beer.
130 my $last_pull = $self->sync_source->upstream_last_modified_date();
131 return undef unless $last_pull;
132 my $before = App::SD::Util::string_to_datetime($last_pull);
133 die "Failed to parse '" . $self->sync_source->upstream_last_modified_date() . "' as a timestamp"
134 unless ($before);
135  
136 # 26 hours ago deals with most any possible timezone/dst edge case
137 $before->subtract( hours => 26 );
138  
139 return $before;
140 }
141  
142 sub new_comment_creation_change {
143 my $self = shift;
144 return Prophet::Change->new(
145 {
146 record_type => 'comment',
147 record_uuid => $self->sync_source->uuid_generator->create_str()
148 , # comments are never edited, we can have a random uuid
149 change_type => 'add_file'
150 }
151 );
152 }
153  
154 __PACKAGE__->meta->make_immutable;
155 no Any::Moose;
156 1;