-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #580 from 3scale/extract-rules-matcher-and-usage
Extract MappingRulesMatcher and Usage modules
- Loading branch information
Showing
10 changed files
with
330 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- Mapping rules matcher | ||
-- @module mapping_rules_matcher | ||
-- Matches a request against a set of mapping rules and calculates the usage | ||
-- that needs to be authorized and reported according to the rules that match. | ||
|
||
local ipairs = ipairs | ||
local insert = table.insert | ||
local Usage = require('apicast.usage') | ||
|
||
local _M = {} | ||
|
||
--- Calculate usage from matching mapping rules. | ||
-- Matches a request against a set of mapping rules and returns the resulting | ||
-- usage and the matched rules. | ||
-- @tparam string method HTTP method. | ||
-- @tparam string uri URI. | ||
-- @tparam table args Request arguments. | ||
-- @tparam table rules Mapping rules to be matched. | ||
-- @treturn Usage Calculated usage. | ||
-- @treturn table Matched rules. | ||
function _M.get_usage_from_matches(method, uri, args, rules) | ||
local usage = Usage.new() | ||
local matched_rules = {} | ||
|
||
for _, rule in ipairs(rules) do | ||
if rule:matches(method, uri, args) then | ||
-- Some rules have no delta. Send 0 in that case. | ||
usage:add(rule.system_name, rule.delta or 0) | ||
insert(matched_rules, rule) | ||
end | ||
end | ||
|
||
return usage, matched_rules | ||
end | ||
|
||
--- Check if there is a mapping rule that matches. | ||
-- @tparam string method HTTP method. | ||
-- @tparam string uri URI. | ||
-- @tparam table args Request arguments. | ||
-- @tparam table rules Mapping rules to be matched. | ||
-- @treturn boolean Whether there is a match. | ||
-- @treturn integer|nil Index of the first matched rule. | ||
function _M.matches(method, uri, args, rules) | ||
for i, rule in ipairs(rules) do | ||
if rule:matches(method, uri, args) then | ||
return true, i | ||
end | ||
end | ||
|
||
return false | ||
end | ||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- Usage | ||
-- @module usage | ||
-- Usage to be authorized and reported against 3scale's backend. | ||
|
||
local setmetatable = setmetatable | ||
local ipairs = ipairs | ||
local insert = table.insert | ||
|
||
local _M = {} | ||
|
||
local mt = { __index = _M } | ||
|
||
--- Initialize a usage. | ||
-- @return usage New usage. | ||
function _M.new() | ||
local self = setmetatable({}, mt) | ||
|
||
-- table where the keys are metrics and the values their deltas. | ||
self.deltas = {} | ||
|
||
-- table that contains the metrics that have a delta associated. | ||
-- It's useful to iterate over the deltas without using '.pairs'. | ||
-- That's what we are doing in .merge(). | ||
-- We want to avoid using '.pairs' because is not jitted, '.ipairs' is. | ||
self.metrics = {} | ||
|
||
return self | ||
end | ||
|
||
--- Add a metric usage. | ||
-- Increases the usage of the given metric by the given value. If the metric | ||
-- is not in the usage, it will be included. | ||
-- Note that this mutates self. | ||
-- @tparam string metric Metric. | ||
-- @tparam integer value Value. | ||
function _M:add(metric, value) | ||
if self.deltas[metric] then | ||
self.deltas[metric] = self.deltas[metric] + value | ||
else | ||
self.deltas[metric] = value | ||
insert(self.metrics, metric) | ||
end | ||
end | ||
|
||
--- Merge usages | ||
-- Merges two usages. This means that: | ||
-- | ||
-- 1) When a metric appears in both usages, its delta is updated in self by | ||
-- adding the two values. | ||
-- 2) When a metric does not appear in self, it is added in self. | ||
-- | ||
-- Note that this mutates self. | ||
-- @tparam another_usage Usage Usage. | ||
function _M:merge(another_usage) | ||
local another_usage_metrics = another_usage.metrics | ||
local another_usage_deltas = another_usage.deltas | ||
|
||
for _, metric in ipairs(another_usage_metrics) do | ||
local delta = another_usage_deltas[metric] | ||
self:add(metric, delta) | ||
end | ||
end | ||
|
||
return _M |
Oops, something went wrong.