Accessing Reports and Logs using FTP with your Tropo Account
August 24th, 2011 by kbondThere’s a significant amount of logging available to Tropo users in their account, and as of last month, reports showing production usage on a day to day basis have also been added. It’s possible to access this information manually, by logging into your Tropo account, then going to Your Hosted Files, then “root” followed by either “logs” or “reports”:
This requires quite a bit of click through and even then, you’re getting data piece by piece. Tropo offers access to these files via FTP, as well, which opens up quite bit of possibilities. To access your logs and reports through the browser, enter the following: ftp://user:password@ftp.tropo.com. The username and password will be the same as your Tropo login credentials. This opens the door to the files, and then this blog will specifically discuss two Ruby programs that accesses the logs and daily reporting and combines them into more comprehensive reports.
These two programs use the net/ftp library – which is very powerful and easy to use. To start, both programs will initiate this library as follows.
require 'net/ftp'
require 'json'
#create a ftp reference
ftp = Net::FTP.new
#connect to FTP
ftp.connect("ftp.tropo.com")
#login with Tropo credentials
ftp.login("username","password")
We can now access anything and everything in the root folder – reporting, logs, etc. This app is built to retrieve gateway CDRs from the most current log. Gateway CDRs contain a common request for SMS – the statusCode field, which indicates delivery status. If it’s 0, the SMS was delivered to the carrier correctly, anything else and a problem occurred. Note that this is not handset delivery status – that’s not something most carriers support, so we can’t either. It just means we handed it off to the carrier for delivery successfully.
The next step to retrieve this information is to enter into the logs directory and read the latest log:
#Go to the logs directry
ftp.chdir("/logs")
#get a list of all of the logs
list = ftp.nlst
#saving a text file of the newest log to the computer
ftp.gettextfile(list[list.length - 1])
The method gettextfile saves the individual log straight to your computer, storing it at the same path as your app. Next, we have to read this file to get the contents of it:
#opening the saved file
file = File.open("/path/to/file/#{list[list.length - 1]}")
#reading the entire file
contents = file.read
#closing the file
file.close
Once we have the contents of this log file, we now have to decipher the CDRs from the rest of the logs and save them to a text file. To do this, the app splits the varaibale contents, which is the logs, into an array using the word “CDR”. This way every time an index holds the word “CDR”, it will be stored.
#opening a file that will hold all of the CDRs
cdrFile = File.open("CDR.txt", "a")
#spliting the file into an array
check = contents.split("CDR ")
In this log, we can have multiple CDRs because each log holds up to an hour’s worth of data. Because of this, we now have to search through our array to find if there are multiple CDRs. Once we find one, we need to chomp that string so that the only thing that we send to the file is the CDR alone.
#searching through each array for the CDRs
check.each do |l|
#If l[3] == 'h', it is a cdr
if l[3] == "h"
#saving the cdr
cdr = l[0..(l.rindex("\"}\n") + 1)]
#parsing the cdr to JSON
cdr = JSON.parse(cdr)
#savinf the CDR to the file
cdrFile.write(cdr)
cdrFile.write("\n\n")
end
end
#finally, close the file
cdrFile.close
Finally, once you close the newly made file, you can access it and see every CDR for that log. The contents of the file should look something like this:
{"channel"=>"SMS", "status"=>"success", "deliveryStatus"=>"MessageWaiting", "statusCode"=>0, "callId"=>"3fc334f025d225021059c194dc123456", "transactionId"=>"b3a53b01abc05da8122db6cf4e123456", "applicationId"=>"000111", "accountId"=>"111000", "serviceId"=>"101010", "PPID"=>"123", "carrierId"=>"1", "from"=>"0987654321", "to"=>"1234567890", "startTime"=>"2011-08-11T20:26:14.685+0000", "endTime"=>"2011-08-11T20:26:14.744+0000", "count"=>"1", "direction"=>"in", "testNumber"=>"false", "host"=>"10.72.208.102", "text"=>"testing"}
The next sample program accesses reporting in almost the same fashion as we just did with the logs. However, this program is built so you can retrieve a date range of report. The reports are formatted in JSON and contains information such as the network (voice or text), the duration of the call, the cost, and more.
To start, we need to get in the right directory.
#Go to the reporting directory
ftp.chdir("/reports")
#get a list - which are sorted by years
list = ftp.nlst
#Since you know that you want this year - go to the next directory
ftp.chdir(list[list.length-1])
Unlike logs, reporting requires you to enter more than one directory to get to the files that you want. The directory list is as follows: reports/year/month/file.json. As of now, we are in the month directory. Next, we set 3 variables – month, startDay, endDay. These variables can be changed to whatever date range you want.
#set the month variable month = '08' #entering that months directory ftp.chdir(month) #setting the beginning of the report startDay = 1 #setting the end of the report endDay = 13
After the month is received, the app proceeds to the matching directory, which allows us to get to the needed files. These files will be deciphered through the startDay and endDay. The app opens a file, which will be used to save the data, followed by listing the potential accessible information:
#opening a new file to safe all of the data
file = File.open("reporting.txt", "a")
#listing all of the files
list = ftp.nlst
The app looks at every item in the list to check to see if it is in the specified date range. The report’s file name corresponds to the date that it was created, so the app extracts the day by splitting it in an array and grabbing the two digit number. Now that we have the day that the file was made, all you have to do is to see if it is between startDay and endDay. If it is, save that file to your computer, read it and save it to the new file. Finally, close both of the files:
#read each file
list.each { |index|
#extracting only the day of this file
day = [index.split(//)[8], index.split(//)[9]].join
#If that day is between startDay and endDay - save it
if day.to_i > (startDay - 1) && day.to_i < (endDay + 1)
#Check to see if it is a json file
if index == "2011-#{month}-#{day}.cdr.json"
#Save the file to your computer
ftp.gettextfile(index)
#open that file
report = File.open("path/to/file/2011-#{month}-#{day}.cdr.json", "r")
#get the contents of that file
contents = report.read
#got the contents - now close file
report.close
#save the contents to the new file
file.write("2011-#{month}-#{day}.cdr.json:\n" + contents + "\n\n")
end
end
}
#close the newly made file
file.close
Reopen the newly created text file in a text editor and check out your comprehensive report! The content will look something like this:
2011-08- 1.cdr.json:
[{"features":{"unit":"minutes","amount":60,"cost":0.03,"name":"local"}, "duration":60,"start":"2011-08-02T04:15:53+00:00","channel":"voice","network":"phone", "to":"+19996131234","cost":0.03,"direction":"in"},{"features":{"unit":"minutes","amount":60,"cost":0.03,"name":"local"}, "duration":60,"start":"2011-08-02T04:12:30+00:00","channel":"voice","network":"phone", "to":"+19996131234","cost":0.03,"direction":"in"}]
2011-08- 1.cdr.json:
[{"features":{"unit":"minutes","amount":60,"cost":0.03,"name":"local"}, "duration":60,"start":"2011-08-02T04:15:53+00:00","channel":"voice","network":"phone", "to":"+19996131234","cost":0.03,"direction":"in"},{"features":{"unit":"minutes","amount":60,"cost":0.03,"name":"local"}, "duration":60,"start":"2011-08-02T04:12:30+00:00","channel":"voice","network":"phone", "to":"+19996131234","cost":0.03,"direction":"in"}]
Both of these programs can be modified easily to incorporate any task you want to accomplish using these files. Below are two links to see my full programs on github.com.
Reports Application
Logs Application
For more information on logs and reports, check out this page of the Tropo docs.
Related posts:

