MT#12707 - add recentcalls lua (store in redis)

Change-Id: Ie1d900c491bbc27f8319d2b323d991d84c567862
changes/90/2390/6
Kirill Solomko 10 years ago
parent 7a52fc6ad5
commit 2bfe436066

@ -0,0 +1,96 @@
--
-- Copyright 2015 SipWise Team <development@sipwise.com>
--
-- This program 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.
--
-- This package 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 this program. If not, see <http://www.gnu.org/licenses/>.
-- .
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
local NGCPRecentCalls = {
__class__ = 'NGCPRecentCalls'
}
local redis = require 'redis';
require 'ngcp.utils';
_ENV = NGCPRecentCalls
-- class NGCPRecentCalls
local NGCPRecentCalls_MT = { __index = NGCPRecentCalls }
NGCPRecentCalls_MT.__tostring = function (t)
return string.format("config:%s central:%s",
table.tostring(t.config), table.tostring(t.central))
end
function NGCPRecentCalls.new()
local t = NGCPRecentCalls.init();
setmetatable( t, NGCPRecentCalls_MT )
return t;
end
function NGCPRecentCalls.init()
local t = {
config = {
central = {
host = '127.0.0.1',
port = 6379,
db = "7"
},
expire = 7200
},
central = {},
};
return t;
end
function NGCPRecentCalls._test_connection(client)
if not client then return nil end
local ok, _ = pcall(client.ping, client)
return ok
end
function NGCPRecentCalls._connect(config)
local client = redis.connect(config.host,config.port)
client:select(config.db)
sr.log("info", string.format("connected to redis server %s:%d at %s\n",
config.host, config.port, config.db))
return client
end
function NGCPRecentCalls:set_by_uuid(uuid, callid, start_time,
duration, caller, callee)
if not self._test_connection(self.central) then
self.central = self._connect(self.config.central)
end
local res = self.central:hmset(uuid, "callid", callid,
"start_time", start_time,
"duration", duration,
"caller", caller,
"callee", callee)
if res then
self.central:expire(uuid, self.config.expire)
end
sr.log("info", string.format("central:hset[%s]=>[%s] callid: %s start_time: %s duration: %d caller: %s callee: %s expire: %d\n",
uuid, tostring(res),
callid,
start_time, duration,
caller, callee,
self.config.expire))
return res
end
-- class
return NGCPRecentCalls
--EOF

@ -0,0 +1,102 @@
--
-- Copyright 2014 SipWise Team <development@sipwise.com>
--
-- This program 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.
--
-- This package 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 this program. If not, see <http://www.gnu.org/licenses/>.
-- .
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
local lemock = require('lemock')
require('luaunit')
require 'ngcp.utils'
if not sr then
require 'mocks.sr'
sr = srMock:new()
else
argv = {}
end
local mc
TestNGCPRecentCalls = {} --class
function TestNGCPRecentCalls:setUp()
mc = lemock.controller()
self.fake_redis = mc:mock()
self.central = mc:mock()
package.loaded.redis = self.fake_redis
local NGCPRecentCalls = require 'ngcp.recentcalls'
self.rcalls = NGCPRecentCalls.new()
assertTrue(self.rcalls)
self.rcalls.central = self.central;
end
function TestNGCPRecentCalls:test_connection_ok()
local prev = self.central
self.central:ping() ;mc :returns(true)
mc:replay()
local ok = self.rcalls._test_connection(self.central)
mc:verify()
assertTrue(ok)
assertIs(prev, self.central)
end
function TestNGCPRecentCalls:test_connection_fail()
local prev = self.central
self.central:ping() ;mc :error("error")
mc:replay()
local res = self.rcalls._test_connection(self.central)
mc:verify()
assertFalse(res)
assertIs(prev, self.central)
end
function TestNGCPRecentCalls:test_set_by_uuid()
local ttl = 7200
local uuid = "9bcb88b6-541a-43da-8fdc-816f5557ff93"
local callid = "12345-67890"
local start_time = "1439911398"
local duration = 11
local caller = "437712345"
local callee = "437754321"
self.central:ping() ;mc :returns(true)
self.central:hmset(uuid, "callid", callid,
"start_time", start_time,
"duration", duration,
"caller", caller,
"callee", callee) ;mc :returns(true)
self.central:expire(uuid, ttl) ;mc :returns(1)
mc:replay()
local res = self.rcalls:set_by_uuid(uuid, callid,
start_time, duration,
caller, callee)
mc:verify()
assertTrue(res)
assertIs(self.rcalls.central, self.central)
end
-- class TestNGCPRecentCalls
--EOF
Loading…
Cancel
Save