From 0da7ecb92ccd671c47cb7fd4d04560cc80d4b520 Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 3 Jan 2009 01:52:59 +0000 Subject: [PATCH] indosoft CDR format, RT#4425 --- FS/FS/Record.pm | 2 +- FS/FS/cdr.pm | 20 ++++++--- FS/FS/cdr/indosoft.pm | 71 +++++++++++++++++++++++++++++++ bin/cdr.http_and_import | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ bin/cdr.import | 5 ++- bin/cdr.sftp_and_import | 6 +-- 6 files changed, 202 insertions(+), 10 deletions(-) create mode 100644 FS/FS/cdr/indosoft.pm create mode 100755 bin/cdr.http_and_import diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index 3327f18dc..16949fa95 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -1505,7 +1505,7 @@ sub batch_import { my $job = $param->{job}; my $file = $param->{file}; my $format = $param->{'format'}; - my $params = $param->{params}; + my $params = $param->{params} || {}; die "unknown format $format" unless exists $formats->{ $format }; diff --git a/FS/FS/cdr.pm b/FS/FS/cdr.pm index 5bfd91da8..3c806f4c5 100644 --- a/FS/FS/cdr.pm +++ b/FS/FS/cdr.pm @@ -634,11 +634,12 @@ sub _cdr_min_parse { sub _cdr_date_parser_maker { my $field = shift; + my @fields = ref($field) ? @$field : ($field); return sub { - my( $cdr, $date ) = @_; - #$cdr->$field( _cdr_date_parse($date) ); - eval { $cdr->$field( _cdr_date_parse($date) ); }; - die "error parsing date for $field from $date: $@\n" if $@; + my( $cdr, $datestring ) = @_; + my $unixdate = eval { _cdr_date_parse($datestring) }; + die "error parsing date for @fields from $datestring: $@\n" if $@; + $cdr->$_($unixdate) foreach @fields; }; } @@ -674,13 +675,22 @@ Imports CDR records. Available options are: =item file +Filename + =item format +=item params + +Hash reference of preset fields, typically cdrbatch + +=item empty_ok + +Set true to prevent throwing an error on empty imports + =back =cut - my %import_options = ( 'table' => 'cdr', diff --git a/FS/FS/cdr/indosoft.pm b/FS/FS/cdr/indosoft.pm new file mode 100644 index 000000000..cb25089e3 --- /dev/null +++ b/FS/FS/cdr/indosoft.pm @@ -0,0 +1,71 @@ +package FS::cdr::indosoft; + +use strict; +use base qw( FS::cdr ); +use vars qw( %info ); +use FS::cdr qw( _cdr_date_parser_maker _cdr_min_parser_maker ); + +%info = ( + 'name' => 'Indosoft Conference Bridge', + 'weight' => 300, + 'header' => 1, + 'type' => 'csv', + + #listref of what to do with each field from the CDR, in order + 'import_fields' => [ + + #cdr_id + 'uniqueid', + + #connect_time + _cdr_date_parser_maker( ['startdate', 'answerdate' ] ), + + #disconnect_time + _cdr_date_parser_maker('enddate'), + + #account_id + 'accountcode', + + #conference_id + 'userfield', + + #client_id + 'charged_party', + + #pin_used + 'dcontext', + + #channel + 'channel', + + #clid + #'src', + sub { my($cdr, $clid) = @_; + $cdr->clid( $clid ); #because they called it 'clid' explicitly + $cdr->src( $clid ); + }, + + #dnis + 'dst', + + #call_status + 'disposition', + + #conf_billing_code + 'lastapp', #arbitrary + + #participant_id + 'lastdata', #arbitrary + + #codr_id + 'dstchannel', #arbitrary + + #call_type + 'description', + + ], + +); + +1; + diff --git a/bin/cdr.http_and_import b/bin/cdr.http_and_import new file mode 100755 index 000000000..5637fa526 --- /dev/null +++ b/bin/cdr.http_and_import @@ -0,0 +1,108 @@ +#!/usr/bin/perl +# +# Usage: +# cdr.http_and_import [ -p prefix ] [ -e extension ] [ -v ] user format URL +# +# -e: file extension, defaults to .csv +# -d: if specified, moves files to the specified folder when done + +use strict; +use Getopt::Std; +use WWW::IndexParser; +#use LWP::UserAgent; +use FS::UID qw(adminsuidsetup datasrc dbh); +use FS::cdr; + +### +# parse command line +### + +use vars qw( $opt_p $opt_e $opt_v ); +getopts('p:e:v'); + +$opt_e ||= 'csv'; +#$opt_e = ".$opt_e" unless $opt_e =~ /^\./; +$opt_e =~ s/^\.//; + +my $user = shift or die &usage; +adminsuidsetup $user; + +# %%%FREESIDE_CACHE%%% +my $cachedir = '/usr/local/etc/freeside/cache.'. datasrc. '/cdrs'; +mkdir $cachedir unless -d $cachedir; + +my $format = shift or die &usage; + +use vars qw( $URL ); +$URL = shift or die &usage; + +### +# get the file list +### + +warn "Retreiving directory listing\n" if $opt_v; + +my @files = WWW::IndexParser->new(url => $URL); + +### +# import each file +### + +foreach my $file ( @files ) { + + my $filename = $file->{filename}; + + if ( $opt_p ) { next unless $filename =~ /^$opt_p/ }; + if ( $opt_e ) { next unless $filename =~ /\.$opt_e$/i }; + + #check and see if we've gotten this file already!!! + #just going to cheat with filenames in the cache for now + if ( -e "$cachedir/$filename" ) { + warn "Already have unprocessed $cachedir/$filename; skipping\n"; # if $opt_v; + next; + } + if ( -e "$cachedir/$filename.DONE" ) { + warn "Already processed $cachedir/$filename; skipping\n" if $opt_v; + next; + } + + warn "Downloading $filename\n" if $opt_v; + + #get the file + + my $ua = LWP::UserAgent->new; + my $response = $ua->get("$URL/$filename"); + + unless ( $response->is_success ) { + die "Error retreiving $URL/$filename: ". $response->status_line; + } + + open(FILE, ">$cachedir/$filename") + or die "can't open $cachedir/$filename: $!"; + print FILE $response->content; + close FILE or die "can't close $cachedir/$filename: $!"; + + warn "Processing $filename\n" if $opt_v; + + my $error = FS::cdr::batch_import( { + 'file' => "$cachedir/$filename", + 'format' => $format, + 'params' => { 'cdrbatch' => $filename }, + 'empty_ok' => 1, + } ); + die $error if $error; + + close FILE; + + rename("$cachedir/$filename", "$cachedir/$filename.DONE"); + +} + +### +# sub +### + +sub usage { + "Usage: \n cdr.http_and_import [ -p prefix ] [ -e extension ] [ -v ] user format URL\n"; +} + diff --git a/bin/cdr.import b/bin/cdr.import index b2e8d9127..a17417b85 100644 --- a/bin/cdr.import +++ b/bin/cdr.import @@ -13,9 +13,12 @@ adminsuidsetup $user; my $format = shift or die &usage; +my $file = shift; + my $error = FS::cdr::batch_import( { - 'file' => shift, + 'file' => $file, 'format' => $format, + 'params' => { 'cdrbatch' => $file }, } ); die $error if $error; diff --git a/bin/cdr.sftp_and_import b/bin/cdr.sftp_and_import index 57d4e871e..79e743f39 100755 --- a/bin/cdr.sftp_and_import +++ b/bin/cdr.sftp_and_import @@ -16,8 +16,8 @@ use FS::cdr; # parse command line ### -use vars qw( $opt_e $opt_d $opt_u $opt_v ); -getopts('e:d:u:v'); +use vars qw( $opt_e $opt_d $opt_v ); +getopts('e:d:v'); $opt_e ||= 'csv'; #$opt_e = ".$opt_e" unless $opt_e =~ /^\./; @@ -64,7 +64,7 @@ foreach my $file ( @$ls ) { my $error = FS::cdr::batch_import( { 'file' => "$cachedir/$filename" 'format' => $format, - 'cdrbatch' => $filename, + 'params' => { 'cdrbatch' => $filename, }, 'empty_ok' => 1, } ); die $error if $error; -- 2.11.0