X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2Fbin%2Ffreeside-cdr-asterisk_sql;h=cd765deeef6e9bdbad46e06029914955405342cd;hp=e32ccfe824eccb9dce536036d92df799120755a2;hb=dde3deba8c26a17e14a71786a0dcfa9d83b44771;hpb=a7cd032eb8714905311070b4dd25a21ee42102ee diff --git a/FS/bin/freeside-cdr-asterisk_sql b/FS/bin/freeside-cdr-asterisk_sql index e32ccfe82..cd765deee 100755 --- a/FS/bin/freeside-cdr-asterisk_sql +++ b/FS/bin/freeside-cdr-asterisk_sql @@ -7,13 +7,13 @@ use Date::Format 'time2str'; use FS::UID qw(adminsuidsetup dbh); use FS::Log; use FS::cdr; -use DBI; +use FS::DBI; use Getopt::Std; $DEBUG = 0; my %opt; -getopts('e:H:U:P:D:T:V:', \%opt); +getopts('e:H:U:P:D:T:V:Q', \%opt); my $user = shift or die &usage; my $engine = $opt{e} || 'mysql'; @@ -26,7 +26,7 @@ adminsuidsetup $user; my $log = FS::Log->new( 'freeside-cdr-asterisk_sql' ); -my $dbi = DBI->connect($dsn, $opt{U}, $opt{P}) ; +my $dbi = FS::DBI->connect($dsn, $opt{U}, $opt{P}) ; if ( $dbi ) { log_msg( info => "Established connection to CDR database at dsn($dsn)" ); @@ -34,7 +34,7 @@ if ( $dbi ) { log_and_die( error => sprintf 'Fatal error connecting to CDR database at dsn(%s): %s', $dsn, - $DBI::errstr + $FS::DBI::errstr ); } @@ -56,7 +56,18 @@ if ( $engine =~ /^mysql/ ) { my @cols = (); if ( $opt{V} >= 12 ) { - push @cols, qw( id start answer end ); + + push @cols, qw( id ) + unless $opt{Q}; + + push @cols, qw( start answer ); + + if ( $engine =~ /^Pg/ ) { + push @cols, "'end'"; + } else { + push @cols, "end"; + } + } else { push @cols, qw( calldate ); } @@ -119,10 +130,12 @@ while ( my $row = $sth->fetchrow_hashref ) { $cdr->cdrbatchnum($cdrbatchnum); + $cdr->amaflags('') unless $cdr->amaflags =~ /^(\d+)$/; + if ( my $error = $cdr->insert ) { - log_msg( error => - sprintf 'Non-fatal failure to import acctid(%s) from table(%s): %s', - $row->acctid, + log_and_die( error => + sprintf 'Fatal failure to import uniqueid(%s) from table(%s): %s', + $row->{'uniqueid'}, $table, $error ); @@ -133,8 +146,13 @@ while ( my $row = $sth->fetchrow_hashref ) { my $usql = "UPDATE $table SET freesidestatus = 'done' WHERE "; my @args = (); if ( $opt{V} >= 12 ) { - $usql .= ' id = '. $row->{'id'}; - @args = ( undef ); + if ( $row->{'id'} ) { + $usql .= ' id = '. $row->{'id'}; + @args = ( undef ); + } else { + $usql .= ' uniqueid = ?'; + @args = ( undef, $row->{'uniqueid'} ); + } } else { $usql .= ' calldate = ? AND src = ? AND dst = ?'; @args = ( undef, $row->{'calldate'}, $row->{'src'}, $row->{'dst'}, ); @@ -143,9 +161,9 @@ while ( my $row = $sth->fetchrow_hashref ) { if ( $dbi->do($usql, @args) ) { $updates++; } else { - log_msg( error => - sprintf 'Non-fatal failure set status(done) acctid(%s) table(%s): %s', - $row->acctid, + log_and_die( error => + sprintf 'Fatal failure set status(done) uniqueid(%s) table(%s): %s', + $row->{'uniqueid'}, $table, $dbi->errstr ); @@ -182,7 +200,7 @@ sub log_and_warn { sub usage { - "Usage: \n freeside-cdr-asterisk_sql\n\t-e mysql|Pg|... [ -H host ]n\t-D database\n\t[ -T table ]\n\t[ -V asterisk_version]\n\t-U user\n\t-P password\n\tfreesideuser\n"; + "Usage: \n freeside-cdr-asterisk_sql\n\t-e mysql|Pg|... [ -H host ]\n\t-D database\n\t[ -T table ]\n\t[ -V asterisk_version]\n\t-U user\n\t-P password\n\tfreesideuser\n"; } =head1 NAME @@ -211,6 +229,8 @@ Imports CDR records from an Asterisk SQL database. -V: Asterisk version. Values of 12 or higher have a new database schema which no longer includes calldate. Currently defaults to 11, but may default to 12+ or be required in a future, so best to always specify the version. +-Q: Use uniqueid field instead of id (Asterisk v12 only). Less efficient, but useful if the field is missing. + =cut 1;