| 1 | package App::SD::Replica::rt; | ||
| 2 | use Any::Moose; | ||
| 3 | extends qw/App::SD::ForeignReplica/; | ||
| 4 | |||
| 5 | use Params::Validate qw(:all); | ||
| 6 | use File::Temp 'tempdir'; | ||
| 7 | use Memoize; | ||
| 8 | use Try::Tiny; | ||
| 9 | |||
| 10 | use constant scheme => 'rt'; | ||
| 11 | use constant pull_encoder => 'App::SD::Replica::rt::PullEncoder'; | ||
| 12 | use constant push_encoder => 'App::SD::Replica::rt::PushEncoder'; | ||
| 13 | |||
| 14 | |||
| 15 | use Prophet::ChangeSet; | ||
| 16 | |||
| 17 | has rt => ( isa => 'RT::Client::REST', is => 'rw'); | ||
| 18 | has remote_url => ( isa => 'Str', is => 'rw'); | ||
| 19 | has rt_queue => ( isa => 'Str', is => 'rw'); | ||
| 20 | has query => ( isa => 'Str', is => 'rw'); | ||
| 21 | has foreign_username => ( isa => 'Str', is => 'rw' ); | ||
| 22 | |||
| 23 | sub BUILD { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | # Require rather than use to defer load | ||
| 27 | try { | ||
| 28 | require RT::Client::REST; | ||
| 29 | require RT::Client::REST::User; | ||
| 30 | require RT::Client::REST::Ticket; | ||
| 31 | } catch { | ||
| 32 | warn $_ if $ENV{PROPHET_DEBUG}; | ||
| 33 | die "RT::Client::REST is required to sync with RT foreign replicas.\n"; | ||
| 34 | }; | ||
| 35 | |||
| 36 | my ( $server, $type, $query ) = $self->{url} =~ m{^rt:(https?://.*?)\|(.*?)\|(.*)$} | ||
| 37 | or die "Can't parse RT server spec. Expected 'rt:http://example.com|QUEUE|QUERY'.\n" | ||
| 38 | ."Try: 'rt:http://example.com/|General|'.\n"; | ||
| 39 | my $uri = URI->new($server); | ||
| 40 | |||
| 41 | my ($username, $password) = $self->extract_auth_from_uri($server); | ||
| 42 | |||
| 43 | $self->rt_queue($type); | ||
| 44 | $self->query( ( $query ? "($query) AND " :"") . " Queue = '$type'" ); | ||
| 45 | $self->rt( RT::Client::REST->new( server => $server ) ); | ||
| 46 | |||
| 47 | if ( $password ) { | ||
| 48 | try { | ||
| 49 | $self->rt->login( username => $username, password => $password ); | ||
| 50 | } catch { | ||
| 51 | die "Bad username or password specified in URL! ". | ||
| 52 | "Error message was:\n$_\n"; | ||
| 53 | }; | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | ($username, $password) = $self->login_loop( | ||
| 57 | uri => $uri, | ||
| 58 | username => $username, | ||
| 59 | login_callback => sub { | ||
| 60 | my ($self, $username, $password) = @_; | ||
| 61 | |||
| 62 | $self->rt->login( username => $username, password => $password ); | ||
| 63 | }, | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub foreign_username { return shift->rt_username(@_) } | ||
| 69 | |||
| 70 | sub get_txn_list_by_date { | ||
| 71 | my $self = shift; | ||
| 72 | my $ticket = shift; | ||
| 73 | my @txns = map { | ||
| 74 | my $txn_created_dt = App::SD::Util::string_to_datetime( $_->created ); | ||
| 75 | unless ($txn_created_dt) { | ||
| 76 | die "Couldn't parse '" . $_->created . "' as a timestamp"; | ||
| 77 | } | ||
| 78 | my $txn_created = $txn_created_dt->epoch; | ||
| 79 | |||
| 80 | return { id => $_->id, creator => $_->creator, created => $txn_created } | ||
| 81 | } | ||
| 82 | |||
| 83 | sort { $b->id <=> $a->id } | ||
| 84 | RT::Client::REST::Ticket->new( rt => $self->rt, id => $ticket )->transactions->get_iterator->(); | ||
| 85 | return @txns; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 uuid | ||
| 89 | |||
| 90 | Return the replica's UUID | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub _uuid_url { | ||
| 95 | my $self = shift; | ||
| 96 | return join( '/', $self->remote_url, $self->query ); | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | sub remote_uri_path_for_id { | ||
| 101 | my $self = shift; | ||
| 102 | my $id = shift; | ||
| 103 | return "/ticket/".$id; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head1 NOTES ON PUSH | ||
| 107 | |||
| 108 | If the remote storage (RT) can not represent a whole changeset along with the prophet changeset uuid, then we need to | ||
| 109 | create a seperate locally(?) stored map of: | ||
| 110 | remote-subchangeset-identifier to changeset uuid. | ||
| 111 | remote id to prophet record uuid | ||
| 112 | |||
| 113 | |||
| 114 | For each sync of the same remote source (RT), we need a unique prophet database domain. | ||
| 115 | |||
| 116 | if clkao syncs from RT, jesse can sync with clkao but not with RT directly with the same database. | ||
| 117 | |||
| 118 | |||
| 119 | Push to rt algorithm | ||
| 120 | |||
| 121 | apply a single changeset that's part of the push: | ||
| 122 | - for each record in that changeset: | ||
| 123 | - pull the record's txn list from the server | ||
| 124 | - for each txn we don't know we've already seen, look at it | ||
| 125 | - if it is from the changeset we just pushed, then | ||
| 126 | store the id of the new transaction and originating uuid in the push-ticket store. | ||
| 127 | - does that let us specify individual txns? or is it a high-water mark? | ||
| 128 | - if it is _not_ from the changeset we just pushed, then | ||
| 129 | do we just ignore it? | ||
| 130 | how do we mark an out-of-order transaction as not-pulled? | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | Changesets we want to push from SD to RT and how they map | ||
| 135 | |||
| 136 | |||
| 137 | what do we do with cfs rt doesn't know about? | ||
| 138 | |||
| 139 | |||
| 140 | |||
| 141 | SD::Source::RT->recode_ticket | ||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | __PACKAGE__->meta->make_immutable; | ||
| 148 | no Any::Moose; | ||
| 149 | |||
| 150 | 1; |