| 1 | package ETLp::Audit::FileProcess; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | use DateTime; | ||
| 5 | |||
| 6 | class ETLp::Audit::FileProcess with (ETLp::Role::Config, ETLp::Role::Schema, | ||
| 7 | ETLp::Role::Audit) { | ||
| 8 | |||
| 9 | use ETLp::Types; | ||
| 10 | use Try::Tiny; | ||
| 11 | use ETLp::Exception; | ||
| 12 | |||
| 13 | has 'item' => | ||
| 14 | (is => 'ro', isa => 'ETLp::Audit::Item', required => 1, weak_ref => 1); | ||
| 15 | has 'filename' => (is => 'ro', isa => 'Str', required => 1); | ||
| 16 | has 'config' => (is => 'ro', isa => 'HashRef', required => 1); | ||
| 17 | |||
| 18 | =head2 method_id | ||
| 19 | |||
| 20 | Return the primary key of the file process record | ||
| 21 | |||
| 22 | =cut | ||
| 23 | method id { | ||
| 24 | return $self->{id}; | ||
| 25 | } | ||
| 26 | |||
| 27 | =head2 canonical_filename | ||
| 28 | |||
| 29 | Returns the canonical filename of the file being processed | ||
| 30 | |||
| 31 | =cut | ||
| 32 | method canonical_filename { | ||
| 33 | return $self->{canonical_filename}; | ||
| 34 | } | ||
| 35 | |||
| 36 | =head2 details | ||
| 37 | |||
| 38 | Returns the current record (as a DBIx::Class Row) | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | method details { | ||
| 43 | return $self->EtlpFileProcess->find($self->id); | ||
| 44 | } | ||
| 45 | |||
| 46 | # create a canonical record for the processed file | ||
| 47 | method _create_canonical_record { | ||
| 48 | my $now = $self->now; | ||
| 49 | |||
| 50 | my $file = $self->EtlpFile->create( | ||
| 51 | { | ||
| 52 | canonical_filename => $self->canonical_filename, | ||
| 53 | date_created => $now, | ||
| 54 | date_updated => $now, | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | |||
| 58 | return $file->file_id; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Given the filename and format regex, determine the file format | ||
| 62 | method _get_canonical_filename(Str $filename) { | ||
| 63 | |||
| 64 | try { | ||
| 65 | my $c_regex = $self->config->{config}->{filename_format}; | ||
| 66 | my $canonical_name; | ||
| 67 | |||
| 68 | $self->logger->debug("c_regex $c_regex"); | ||
| 69 | $self->logger->debug("filename $filename\n"); | ||
| 70 | |||
| 71 | if ($filename =~ /$c_regex/) { | ||
| 72 | $canonical_name = $1; | ||
| 73 | } | ||
| 74 | |||
| 75 | ETLpException->throw(error => | ||
| 76 | "Cannot determine canonical name for $filename, regex: $c_regex" | ||
| 77 | ) unless $canonical_name; | ||
| 78 | |||
| 79 | return $canonical_name; | ||
| 80 | } catch { | ||
| 81 | $self->logger->logdie($_); | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 get_canonical_id | ||
| 86 | |||
| 87 | Return the primary key of the canonical file record | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | method get_canonical_id { | ||
| 92 | my $canonical_file = $self->EtlpFile->search( | ||
| 93 | { | ||
| 94 | canonical_filename => $self->canonical_filename | ||
| 95 | } | ||
| 96 | )->single; | ||
| 97 | |||
| 98 | if ($canonical_file) { | ||
| 99 | return $canonical_file->file_id, | ||
| 100 | } else { | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 update_message | ||
| 106 | |||
| 107 | Saves a message to the process run record | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | method update_message(Str $message) { | ||
| 112 | my $file_process = $self->EtlpFileProcess()->find($self->id); | ||
| 113 | $file_process->message($message); | ||
| 114 | $self->update($file_process, $self->now); | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 update_status | ||
| 118 | |||
| 119 | Sets the status of the process run records | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | method update_status(Str $status_name) { | ||
| 124 | my $file_process = $self->EtlpFileProcess()->find($self->id); | ||
| 125 | $file_process->status_id($self->get_status_id($status_name)); | ||
| 126 | $self->update($file_process, $self->now); | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 update | ||
| 130 | |||
| 131 | Saves the file_process record item to the database | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | method update(ETLp::Schema::Result::EtlpFileProcess $file_process, | ||
| 136 | DateTime $date) { | ||
| 137 | $file_process->date_updated($date); | ||
| 138 | my $item = $self->EtlpItem->find($self->item->id); | ||
| 139 | try { | ||
| 140 | $self->schema->txn_do( | ||
| 141 | sub { | ||
| 142 | $file_process->update; | ||
| 143 | $self->item->update($item, $date); | ||
| 144 | } | ||
| 145 | ) | ||
| 146 | } | ||
| 147 | catch { | ||
| 148 | $self->logger->logdie("Cannot update job item record: $_"); | ||
| 149 | }; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 record_count | ||
| 153 | |||
| 154 | set the number of records loaded | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | method record_count (Int $record_count) { | ||
| 159 | my $file_process = $self->EtlpFileProcess()->find($self->id); | ||
| 160 | $file_process->record_count($record_count); | ||
| 161 | $self->update($file_process, $self->now); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Create a new processing record for the file | ||
| 165 | method _create { | ||
| 166 | my $file_id = $self->get_canonical_id; | ||
| 167 | my $now = $self->db_now; | ||
| 168 | |||
| 169 | unless ($file_id) { | ||
| 170 | $file_id = $self->_create_canonical_record; | ||
| 171 | } | ||
| 172 | |||
| 173 | my $file_process = $self->EtlpFileProcess->create( | ||
| 174 | { | ||
| 175 | item_id => $self->item->id, | ||
| 176 | status_id => $self->get_status_id('running'), | ||
| 177 | filename => $self->filename, | ||
| 178 | file_id => $file_id, | ||
| 179 | date_created => $now, | ||
| 180 | date_updated => $now, | ||
| 181 | } | ||
| 182 | ); | ||
| 183 | |||
| 184 | $self->{id} = $file_process->file_proc_id; | ||
| 185 | } | ||
| 186 | |||
| 187 | method BUILD { | ||
| 188 | my $driver = $self->get_driver; | ||
| 189 | $self->{canonical_filename} = | ||
| 190 | $self->_get_canonical_filename($self->filename); | ||
| 191 | $self->_create; | ||
| 192 | } | ||
| 193 | }; | ||
| 194 | |||
| 195 | =head1 LICENSE AND COPYRIGHT | ||
| 196 | |||
| 197 | Copyright 2010 Redbone Systems Ltd | ||
| 198 | |||
| 199 | This program is free software; you can redistribute it and/or modify it | ||
| 200 | under the terms of either: the GNU General Public License as published | ||
| 201 | by the Free Software Foundation; or the Artistic License. | ||
| 202 | |||
| 203 | The terms are in the LICENSE file that accompanies this application | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | 1; | ||
| 208 |