Skip to content

Commit

Permalink
first commit of multi-class diversion
Browse files Browse the repository at this point in the history
  • Loading branch information
BG2BKK committed Apr 26, 2016
1 parent 5dcd1af commit 37d8166
Show file tree
Hide file tree
Showing 126 changed files with 20,580 additions and 1,401 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 渣浪研发中心技术保障部移动端保障团队
Copyright (c) 2016 CNSRE 技术保障团队

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
321 changes: 123 additions & 198 deletions README.md

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions admin/ab_action.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
local policyModule = require('abtesting.adapter.policy')
local redisModule = require('abtesting.utils.redis')
local systemConf = require('abtesting.utils.init')
local handler = require('abtesting.error.handler').handler
local utils = require('abtesting.utils.utils')
local log = require('abtesting.utils.log')
local ERRORINFO = require('abtesting.error.errcode').info
local policy = require("admin.policy")
local runtime = require('admin.runtime')
local policygroup = require("admin.policygroup")

local cjson = require('cjson.safe')
local doresp = utils.doresp
local dolog = utils.dolog

local redisConf = systemConf.redisConf
local divtypes = systemConf.divtypes
local prefixConf = systemConf.prefixConf
local policyLib = prefixConf.policyLibPrefix
local runtimeLib = prefixConf.runtimeInfoPrefix
local domain_name = prefixConf.domainname

local ab_action = {}

ab_action.policy_check = policy.check
ab_action.policy_set = policy.set
ab_action.policy_get = policy.get
ab_action.policy_del = policy.del

ab_action.runtime_set = runtime.set
ab_action.runtime_del = runtime.del
ab_action.runtime_get = runtime.get


ab_action.policygroup_check = policygroup.check
ab_action.policygroup_set = policygroup.set
ab_action.policygroup_get = policygroup.get
ab_action.policygroup_del = policygroup.del


local get_uriargs_error = function()
local info = ERRORINFO.ACTION_BLANK_ERROR
local response = doresp(info, 'user req')
log:errlog(dolog(info, desc))
ngx.say(response)
return
end

local get_action_error = function()
local info = ERRORINFO.ACTION_BLANK_ERROR
local response = doresp(info, 'user req')
log:errlog(dolog(info, desc))
ngx.say(response)
return
end

local do_action_error = function(action)
local info = ERRORINFO.DOACTION_ERROR
local desc = action
local response = doresp(info, desc)
local errlog = dolog(info, desc)
log:errlog(errlog)
ngx.say(response)
return
end

local red = redisModule:new(redisConf)
local ok, err = red:connectdb()
if not ok then
local info = ERRORINFO.REDIS_CONNECT_ERROR
local response = doresp(info, err)
log:errlog(dolog(info, desc))
ngx.say(response)
return
end

local args = ngx.req.get_uri_args()
if args then
local action = args.action
local do_action = ab_action[action]
if do_action then
do_action({['db']=red})
-- local ok, info = do_action(policy, {['db']=red})
-- if not ok then
-- do_action_error()
-- end
else
do_action_error(action)
end
else
get_uriargs_error()
end
236 changes: 236 additions & 0 deletions admin/policy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
---
-- @classmod abtesting.adapter.policy
-- @release 0.0.1
local modulename = "abtestingAdminPolicy"

local _M = { _VERSION = "0.0.1" }
local mt = { __index = _M }

local ERRORINFO = require('abtesting.error.errcode').info

local runtimeModule = require('abtesting.adapter.runtime')
local policyModule = require('abtesting.adapter.policy')
local redisModule = require('abtesting.utils.redis')
local systemConf = require('abtesting.utils.init')
local handler = require('abtesting.error.handler').handler
local utils = require('abtesting.utils.utils')
local log = require('abtesting.utils.log')
local ERRORINFO = require('abtesting.error.errcode').info

local cjson = require('cjson.safe')
local doresp = utils.doresp
local dolog = utils.dolog
local doerror = utils.doerror

local redisConf = systemConf.redisConf
local divtypes = systemConf.divtypes
local prefixConf = systemConf.prefixConf
local policyLib = prefixConf.policyLibPrefix
local runtimeLib = prefixConf.runtimeInfoPrefix
local domain_name = prefixConf.domainname

local getPolicyId = function()
local policyID = tonumber(ngx.var.arg_policyid)

if not policyID or policyID < 0 then
local info = ERRORINFO.PARAMETER_TYPE_ERROR
local desc = "policyID invalid"
local response = doresp(info, desc)
log:errlog(dolog(info, desc))
ngx.say(response)
return nil
end
return policyID
end

local getPolicy = function()

local request_body = ngx.var.request_body
local postData = cjson.decode(request_body)

if not request_body then
-- ERRORCODE.PARAMETER_NONE
local errinfo = ERRORINFO.PARAMETER_NONE
local desc = 'request_body or post data'
local response = doresp(errinfo, desc)
log:errlog(dolog(errinfo, desc))
ngx.say(response)
return nil
end

if not postData then
-- ERRORCODE.PARAMETER_ERROR
local errinfo = ERRORINFO.PARAMETER_ERROR
local desc = 'postData is not a json string'
local response = doresp(errinfo, desc)
log:errlog(dolog(errinfo, desc))
ngx.say(response)
return nil
end

local divtype = postData.divtype
local divdata = postData.divdata

if not divtype or not divdata then
-- ERRORCODE.PARAMETER_NONE
local errinfo = ERRORINFO.PARAMETER_NONE
local desc = "policy divtype or policy divdata"
local response = doresp(errinfo, desc)
log:errlog(dolog(errinfo, desc))
ngx.say(response)
return nil
end

if not divtypes[divtype] then
-- ERRORCODE.PARAMETER_TYPE_ERROR
local errinfo = ERRORINFO.PARAMETER_TYPE_ERROR
local desc = "unsupported divtype"
local response = doresp(errinfo, desc)
log:errlog(dolog(errinfo, desc))
ngx.say(response)
return nil
end

return postData

end

_M.check = function(option)
local db = option.db

local policy = getPolicy()
if not policy then
return false
end

local pfunc = function()
local policyMod = policyModule:new(db.redis, policyLib)
return policyMod:check(policy)
end

local status, info = xpcall(pfunc, handler)
if not status then
local response = doerror(info)
ngx.say(response)
return false
end

local chkout = info
local valid = chkout[1]
local err = chkout[2]
local desc = chkout[3]

local response
if not valid then
log:errlog(dolog(err, desc))
response = doresp(err, desc)
else
response = doresp(ERRORINFO.SUCCESS)
end
ngx.say(response)
return true

end

_M.set = function(option)
local db = option.db

local policy = getPolicy()
if not policy then
return false
end

local pfunc = function()
policyMod = policyModule:new(db.redis, policyLib)
return policyMod:check(policy)
end

local status, info = xpcall(pfunc, handler)
if not status then
local response = doerror(info)
ngx.say(response)
return false
end

local chkout = info
local valid = chkout[1]
local err = chkout[2]
local desc = chkout[3]

if not valid then
log:errlog(dolog(err, desc))
local response = doresp(err, desc)
ngx.say(response)
return false
end

local pfunc = function() return policyMod:set(policy) end
local status, info = xpcall(pfunc, handler)
if not status then
local response = doerror(info)
ngx.say(response)
return false
end
local data
if info then
data = ' the id of new policy is '..info
end

local response = doresp(ERRORINFO.SUCCESS, data)
ngx.say(response)
return true

end

_M.del = function(option)
local db = option.db

local policyId = getPolicyId()
if not policyId then
return false
end

local pfunc = function()
local policyMod = policyModule:new(db.redis, policyLib)
return policyMod:del(policyId)
end

local status, info = xpcall(pfunc, handler)
if not status then
local response = doerror(info)
ngx.say(response)
return false
end
local response = doresp(ERRORINFO.SUCCESS)
ngx.say(response)
return true
end

_M.get = function(option)
local db = option.db

local policyId = getPolicyId()
if not policyId then
return false
end

local pfunc = function()
local policyIO = policyModule:new(db.redis, policyLib)
return policyIO:get(policyId)
end

local status, info = xpcall(pfunc, handler)
if not status then
local response = doerror(info)
ngx.say(response)
return false
else
local response = doresp(ERRORINFO.SUCCESS, nil, info)
log:errlog(dolog(ERRORINFO.SUCCESS, nil))
ngx.say(response)
return true
end

end

return _M
Loading

0 comments on commit 37d8166

Please sign in to comment.