[asterisk-commits] mnicholson: testsuite/asterisk/trunk r466 - in /asterisk/trunk: asttest/self-...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Jul 9 09:42:06 CDT 2010
Author: mnicholson
Date: Fri Jul 9 09:42:02 2010
New Revision: 466
URL: http://svnview.digium.com/svn/testsuite?view=rev&rev=466
Log:
Added a lua cdr parsing lib and a test for it
Added:
asterisk/trunk/asttest/self-tests/cdrlib/
asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv (with props)
asterisk/trunk/asttest/self-tests/cdrlib/good.csv (with props)
asterisk/trunk/asttest/self-tests/cdrlib/test.lua (with props)
asterisk/trunk/lib/lua/cdr.lua (with props)
Added: asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv?view=auto&rev=466
==============================================================================
--- asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv (added)
+++ asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv Fri Jul 9 09:42:02 2010
@@ -1,0 +1,1 @@
+"","test","100","context","""Test User"" <test>","SIP/test-02897fd8","","Hangup","","2009-01-14 23:51:15","2009-01-14 23:51:15","2009-01-14 23:51:20",5,5,"ANSWERED","DOCUMENTATION","1231977075.0","
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/buggy.csv
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: asterisk/trunk/asttest/self-tests/cdrlib/good.csv
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asttest/self-tests/cdrlib/good.csv?view=auto&rev=466
==============================================================================
--- asterisk/trunk/asttest/self-tests/cdrlib/good.csv (added)
+++ asterisk/trunk/asttest/self-tests/cdrlib/good.csv Fri Jul 9 09:42:02 2010
@@ -1,0 +1,5 @@
+"","test","100","context","""Test User"" <test>","SIP/test-02897fd8","","Hangup","","2009-01-14 23:51:15","2009-01-14 23:51:15","2009-01-14 23:51:20",5,5,"ANSWERED","DOCUMENTATION","1231977075.0",""
+"","test","100","context","""Test User"" <test>","SIP/test-02897fd8","","Hangup","","2009-01-14 23:51:15","2009-01-14 23:51:15","2009-01-14 23:51:20",5,7,"ANSWERED","DOCUMENTATION","1231977075.0",""
+"","test","100","context","""Test User"" <test>","SIP/test-02897fd8","","Hangup","","2009-01-14 23:51:15","2009-01-14 23:51:15","2009-01-14 23:51:20",5,0,"FAILED","DOCUMENTATION","1231977075.0",""
+"","test","100","context","""Test User"" <test>","SIP/test-02897fd8","","Hangup","","2009-01-14 23:51:15","2009-01-14 23:51:15","2009-01-14 23:51:20",5,0,"NO ANSWER","DOCUMENTATION","1231977075.0",""
+"","test","100","context","""Test User"" <test>","SIP/test-02897fd8","","Hangup","","2009-01-14 23:51:15","2009-01-14 23:51:15","2009-01-14 23:51:20",5,0,"BUSY","DOCUMENTATION","1231977075.0",""
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/good.csv
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/good.csv
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/good.csv
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: asterisk/trunk/asttest/self-tests/cdrlib/test.lua
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/asttest/self-tests/cdrlib/test.lua?view=auto&rev=466
==============================================================================
--- asterisk/trunk/asttest/self-tests/cdrlib/test.lua (added)
+++ asterisk/trunk/asttest/self-tests/cdrlib/test.lua Fri Jul 9 09:42:02 2010
@@ -1,0 +1,34 @@
+
+require "cdr"
+
+-- imatate an asterisk object and the asterisk:path() funciton
+function a(file)
+ local a = {}
+ function a:path()
+ return file
+ end
+ return a
+end
+
+function check_record(c, record, field, value)
+ fail_if(c[record][field] ~= value, ("%s of record %s is '%s' instead of '%s'"):format(field, record, tostring(c[record][field]), tostring(value)))
+end
+
+print("parsing a known good csv file")
+c, err = cdr.new(a("good.csv"))
+fail_if(not c, "failed to parse known good csv file, good.csv: " .. tostring(err))
+
+fail_if(c:len() ~= 5, "cdr file contains 5 records, but the parser found " .. c:len() .. " records")
+check_record(c, 1, "disposition", "ANSWERED")
+check_record(c, 2, "billsec", "7")
+
+print("parsing a csv file with errors")
+c, err = cdr.new(a("buggy.csv"))
+fail_if(c, "successfully parsed known buggy csv file, buggy.csv. This should have failed.")
+
+print("attempting to parse a non existant file")
+c, err = cdr.new(a("does not exits.csv"))
+fail_if(c, "successfully parsed non existant file???")
+
+fail_if(ast.cdr ~= cdr, "ast.cdr ~= cdr, the cdr lib is not properly being inserted into the ast lib")
+
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/test.lua
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/test.lua
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: asterisk/trunk/asttest/self-tests/cdrlib/test.lua
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: asterisk/trunk/lib/lua/cdr.lua
URL: http://svnview.digium.com/svn/testsuite/asterisk/trunk/lib/lua/cdr.lua?view=auto&rev=466
==============================================================================
--- asterisk/trunk/lib/lua/cdr.lua (added)
+++ asterisk/trunk/lib/lua/cdr.lua Fri Jul 9 09:42:02 2010
@@ -1,0 +1,153 @@
+-- load asterisk csv cdr file and parse it
+
+module(..., package.seeall)
+
+function new(a)
+ return cdr:new(a)
+end
+
+cdr = {}
+cdr.__metatable = "cdr"
+function cdr:new(a)
+ local c = {
+ path = a:path("/var/log/asterisk/cdr-csv/Master.csv"),
+ _records = {},
+ }
+ setmetatable(c, self)
+
+ local res, err = c:_parse()
+ if not res then
+ return res, err
+ end
+ return c
+end
+
+function cdr:len()
+ return self:__len()
+end
+
+function cdr:__len()
+ return #self._records
+end
+
+function cdr:__index(i)
+ if type(i) == "number" then
+ return rawget(self, "_records")[i]
+ end
+ return rawget(cdr, i)
+end
+
+function cdr:ipairs()
+ return ipairs(self._records)
+end
+
+function cdr:records()
+ local i = 1
+ local f = function(s)
+ local r = s._records[i]
+ i = i + 1
+ return r
+ end
+ return f, self
+end
+
+function cdr:_parse()
+ local f, err = io.open(self.path)
+ if not f then
+ return f, err
+ end
+
+ local count = 1
+ for line in f:lines() do
+ local r, err = cdr_record:new(line)
+ if not r then
+ return r, ("error parsing cdr on line %s (%s)"):format(count, err)
+ end
+ table.insert(self._records, r)
+ count = count + 1
+ end
+ return self
+end
+
+cdr_record = {}
+function cdr_record:new(line)
+ local r = {
+ index = {
+ "accountcode",
+ "src",
+ "dst",
+ "dcontext",
+ "clid",
+ "channel",
+ "dstchannel",
+ "lastapp",
+ "lastdata",
+ "start",
+ "answer",
+ "end",
+ "duration",
+ "billsec",
+ "disposition",
+ "amaflags",
+ "uniqueid",
+ "userfield",
+ },
+ data = {},
+ }
+
+ setmetatable(r, self)
+
+ self._process_index(r)
+ local res, err = self._parse(r, line)
+ if not res then
+ return res, err
+ end
+
+ return r
+end
+
+function cdr_record:_parse(line)
+ -- this algorithm is adapted from Programming in Lua (1st Edition)
+ -- chapter 20, section 4
+
+ line = line .. ',' -- ending comma
+ local start = 1
+ repeat
+ -- next field is quoted? (start with `"'?)
+ if line:find('^"', start) then
+ local a, c
+ local i = start
+ repeat
+ -- find closing quote
+ a, i, c = line:find('"("?),', i+1)
+ until c ~= '"' -- quote not followed by quote?
+ if not i then return nil, 'unmatched "' end
+ local f = line:sub(start+1, i-2)
+ table.insert(self.data, (f:gsub('""', '"')))
+ print(self.data[#self.data])
+ start = i+1
+ else -- unquoted; find next comma
+ local nexti = line:find(',', start)
+ table.insert(self.data, line:sub(start, nexti-1))
+ start = nexti + 1
+ end
+ until start > line:len()
+ return self.data
+end
+
+function cdr_record:_process_index()
+ for k, v in ipairs(self.index) do
+ self.index[v] = k
+ end
+end
+
+function cdr_record:__index(i)
+ if type(i) == "number" then
+ return rawget(self, "data")[i]
+ end
+ return rawget(self, "data")[rawget(self, "index")[i]]
+end
+
+-- put cdr in the ast table too
+_G.ast.cdr = _G.cdr
+
Propchange: asterisk/trunk/lib/lua/cdr.lua
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: asterisk/trunk/lib/lua/cdr.lua
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: asterisk/trunk/lib/lua/cdr.lua
------------------------------------------------------------------------------
svn:mime-type = text/plain
More information about the asterisk-commits
mailing list