First of all, I would like to let you know I’m working with Asterisk PBX again (not enough, but that’s ok).
As far we know, Asterisk can store entire call logs in a database – MySQL at this point. So, let we have some fun with Ruby and Asterisk and Mysql too, and all we want is: retrieve all calls’ time, in seconds, just to know how much time we spent on phone.
Let’s rock
I will not cover any installation (asterisk, mysql, ruby, rails, etc).
I have a Trixbox running and it stores cdr in MySQL (I think it’s part of FreePBX). Let’s rock:
mysql> SELECT SUM(duration)
-> FROM cdr
-> WHERE (
-> (dcontext="from-internal" AND dstchannel like "SIP/voip-gw1%")
-> AND (calldate BETWEEN '2008-07-01%' AND '2008-07-30%')
-> AND (disposition="ANSWERED")
-> )
-> GROUP BY dcontext;
+---------------+
| SUM(duration) |
+---------------+
| 90845 |
+---------------+
1 row in set (0.00 sec)
Explaining: we just care about answered calls made in period (between 2008-07-01 and 2008-07-30) via VOIP-GW1 (a trunk which give us N free minutes to talk per month).
Rubyfying
database.yml
We’ll use ActiveRecord as I said. First, we need to setup our database connection, so write a database.yml (like rails’ proj/config/database.yml):
adapter: mysql
database: asterisk
username: asterisk
password: asterisk
host: 127.0.0.1
port: 3306
Running it
#!/usr/bin/env ruby
require 'yaml'
require 'rubygems'
require 'active_record'
database = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(database)
class CallReport < ActiveRecord::Base
set_table_name 'cdr'
set_primary_key 'uniqueid'
def self.used_minutes(startdate,finaldate)
@context = 'from-internal'
@dstchannel = 'SIP/voip-gw1%'
@firstdate = "#{startdate}%"
@lastdate = "#{finaldate}%"
@disposition = 'ANSWERED'
@minutes = CallReport.find(:all,
:select => '*, SUM(duration) as total',
:group => 'dcontext',
:conditions => [
'dcontext = ? AND dstchannel like ? AND calldate BETWEEN ? and ? AND DISPOSITION = ?',
@context,@dstchannel,@firstdate,@lastdate,@disposition])
@minutes.each do |minute|
return minute.total
end
end
end
total_time = CallReport.used_minutes('2008-07-01','2008-07-10')
p total_time
Result
rocha@vorheez:~/devel/ruby/prepaid$ ruby main.rb
"23649"
rocha@vorheez:~/devel/ruby/prepaid$
Posts relacionados:

16 Comentários para “Integrate Ruby and Asterisk (call detail record) using ActiveRecord”
[...] First of all, I would like to let you know I’m working with Asterisk PBX again (not enough, but that’s ok). As far we know, Asterisk can store entire call logs in a database – MySQL at this point. So, let we have some fun with Ruby and …[Continue Reading] [...]
Pingback por Integrate Ruby and Asterisk (Call Detail Record) Using … on 03/12/2008 at 4:59 pm
Хорошо пишете. Я бы конечно некоторые моменты оспорила, ну да ладно.
Comment por SabsnuttCat on 17/12/2008 at 3:16 pm
А есть, какая нибудь альтернатива?
Comment por GonsNuNse on 18/12/2008 at 4:09 pm
Спасибо за информацию.
Comment por VakCokSeero on 18/12/2008 at 5:50 pm
статья оказалась очень полезной.
Comment por adechappy on 19/12/2008 at 10:42 am
Действительно интерестно.
Comment por Impomshoape on 19/12/2008 at 12:53 pm
Отлично…
Comment por Impomshoape on 19/12/2008 at 2:11 pm
I found more here if anyone’s interested
Comment por Jenny on 23/12/2008 at 11:20 am
[...] Best Real Estate Software Normally Cost $429 Now Free of Charge!
Pingback por Selecting And Using A Real Estate Database. | 7Wins.eu on 03/01/2009 at 12:50 pm
ujuoNzsrWr01C
Comment por Reilly on 06/01/2009 at 5:33 pm
Thank you!
Comment por Sergey on 10/01/2009 at 5:40 pm
Кстати, финансовый кризис не коснётся Вас, если будете хорошо спать
Comment por Valert on 28/01/2009 at 6:28 pm
Лучший пост за последний месяц из тех что я вообще читала
Comment por Valert on 28/01/2009 at 11:58 pm
Я думала, что так не бывает
Comment por Valert on 29/01/2009 at 12:38 am
Лучший пост за последний месяц из тех что я вообще читала
Comment por Ира on 01/02/2009 at 7:58 pm
Greatings,
Not enought information
Have a nice day
Bodyc
Comment por Bodyc on 04/02/2009 at 4:42 pm
Deixe Seu Comentário