Skip to content

Instantly share code, notes, and snippets.

@arussellsaw
Created July 1, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arussellsaw/e2a44b3010156ac5d2fc to your computer and use it in GitHub Desktop.
Save arussellsaw/e2a44b3010156ac5d2fc to your computer and use it in GitHub Desktop.
matching limitations UDF
local function map_results(record)
info("MAP ROW")
return map {agency_key=record.agency_key,campaign_key=record.campaign_key,zone_key=record.zone_key,server=record.server,a_id=record.a_id,win_c_id=record.win_c_id,win_c_type=record.win_c_type,z_id=record.z_id,geo_c_code=record.geo_c_code,dev_limit=record.dev_limit,pp=record.pp,vol=record.vol}
end
function match(stream, slimitation)
JSON = require("JSON")
local debugvar
local limitation = JSON:decode(slimitation)
local splits = {}
local function split(string, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
string:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
local function match_list(row, record)
for i = 1, table.getn(splits) do
val = splits[i]
if val == record[row["limit_type"]] then
return true
end
end
return false
end
local function not_match_list(row, record)
for i = 1, table.getn(splits) do
val = splits[i]
if val == record[row["limit_type"]] then
return false
end
end
return true
end
local function equal(row, record)
if row["limit_data"][1] == record[row["limit_type"]] then
return true
else
return false
end
end
local function not_equal(row, record)
if row["limit_data"][1] == record[row["limit_type"]] then
return false
else
return true
end
end
local function match_filter(record)
matched = false
results = {}
logic = {}
for i = 1, table.getn(limitation["banner_limits"]) do
row = limitation["banner_limits"][i]
splits = row["limit_data"]
if row["limit_comparison"] == "=~" then matched = match_list(row, record)
elseif row["limit_comparison"] == "!~" then matched = not_match_list(row, record)
elseif row["limit_comparison"] == "==" then matched = match_list(row, record)
elseif row["limit_comparison"] == "!=" then matched = not_match_list(row, record)
end
results[i] = matched
logic[i] = row["limit_logic"]
end
match = true
for i = 1, table.getn(results) do
if logic[i] == "and" then
match = match and results[i]
else
match = not (match and results[i])
end
end
if match == true then
info("MATCHED ROW")
end
return match
end
local function debug_map(record)
return map {debug=debugvar}
end
return stream : filter(match_filter) : map(map_results)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment