#!/usr/bin/env ruby
# This file is part of LCR-AGI.
#
# LCR-AGI is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LCR-AGI is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LCR-AGI. If not, see .
#
# Copyright (C) 2009 Alkaloid Networks LLC
# Author: Ben Klang
# Alkaloid Networks LLC - http://projects.alkaloid.net
# $Id$
require 'config'
require 'rubygems'
require 'handlers/lcr_handler'
require 'logger'
require 'dbi'
require 'pp'
$logger = Logger.new($logfile)
$logger.level = $loglevel
$logger.formatter = Logger::Formatter.new
#$dbh = DBI.connect("DBI:Mysql:#{$database[:dbname]}:#{$database[:host]}",
$dbh = DBI.connect("DBI:Mysql:verendus:localhost",
'lcrload', 'tomellyt')
class EOFException < Exception
end
begin
totalcost = 0.0
callcount = 0
totalduration = 0.0
while !(input = gets).nil?
pieces = input.split
if (pieces.length < 2 ||
!(pieces[0] =~ /^[0-9]*$/) ||
!(pieces[1] =~ /^[0-9]*$/))
$logger.error("Invalid line found: #{input} Skipping.")
next
end
callerid = pieces[0]
calldest = pieces[1]
callerid.gsub!(/^011/, '')
callerid.gsub!(/^\+/, '')
calldest.gsub!(/^011/, '')
calldest.gsub!(/^\+/, '')
# Get the routing info from the database
info = LCR.getLowestCostRoute(callerid, calldest)
# If call time was provided on the line, calculate the cost
cost = nil
if (pieces.length > 2)
time = pieces[2].split(':')
# Turn MM:SS into a float minutes
duration = time[0].to_f + (time[1].to_f/60)
cost = duration * info['cost'].to_f
end
puts "#{callerid} -> #{calldest}: #{info['carrier']} #{cost}\n"
# All calls with a duration should have a cost, even if it is 0
if (!cost.nil?)
totalduration += duration
totalcost += cost
end
callcount += 1
end
puts "Total: #{callcount} calls."
if totalcost > 0.0
puts "Total cost: $#{totalcost} for #{totalduration} minutes."
end
end