indosoft CDR format, RT#4425
authorivan <ivan>
Sat, 3 Jan 2009 01:52:59 +0000 (01:52 +0000)
committerivan <ivan>
Sat, 3 Jan 2009 01:52:59 +0000 (01:52 +0000)
FS/FS/Record.pm
FS/FS/cdr.pm
FS/FS/cdr/indosoft.pm [new file with mode: 0644]
bin/cdr.http_and_import [new file with mode: 0755]
bin/cdr.import
bin/cdr.sftp_and_import

index 3327f18..16949fa 100644 (file)
@@ -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 };
 
index 5bfd91d..3c806f4 100644 (file)
@@ -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 (file)
index 0000000..cb25089
--- /dev/null
@@ -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 (executable)
index 0000000..5637fa5
--- /dev/null
@@ -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";
+}
+
index b2e8d91..a17417b 100644 (file)
@@ -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;
 
index 57d4e87..79e743f 100755 (executable)
@@ -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;