Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
Boris Gorbylev committed Jun 3, 2016
1 parent bae2be1 commit d7c18a1
Show file tree
Hide file tree
Showing 19 changed files with 1,256 additions and 1,264 deletions.
190 changes: 95 additions & 95 deletions nginx-metrix/collectors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,115 +12,115 @@ table.remove(collectors, 1) -- removing dummy item
-- @return bool
---
local collector_exists = function(collector)
return index(collector.name, collectors_iter:map(function(c) return c.name end)) ~= nil
return index(collector.name, collectors_iter:map(function(c) return c.name end)) ~= nil
end

---
-- @param collector table
---
local collector_validate = function(collector)
assert(
type(collector) == 'table',
('Collector MUST be a table, got %s: %s'):format(type(collector), inspect(collector))
)

assert(
type(collector.name) == 'string',
('Collector must have string property "name", got %s: %s'):format(type(collector.name), inspect(collector))
)

-- collector exists
assert(
not collector_exists(collector),
('Collector<%s> already exists'):format(collector.name)
)

assert(
type(collector.ngx_phases) == 'table',
('Collector<%s>.ngx_phases must be an array, given: %s'):format(collector.name, type(collector.ngx_phases))
)

assert(
all(function(phase) return type(phase) == 'string' end, collector.ngx_phases),
('Collector<%s>.ngx_phases must be an array of strings, given: %s'):format(collector.name, inspect(collector.ngx_phases))
)

assert(
is.callable(collector.on_phase),
('Collector<%s>:on_phase must be a function or callable table, given: %s'):format(collector.name, type(collector.on_phase))
)

assert(
type(collector.fields) == 'table',
('Collector<%s>.fields must be a table, given: %s'):format(collector.name, type(collector.fields))
)

assert(
all(function(field, params) return type(field) == 'string' and type(params) == 'table' end, collector.fields),
('Collector<%s>.fields must be an table[string, table], given: %s'):format(collector.name, inspect(collector.fields))
)
assert(
type(collector) == 'table',
('Collector MUST be a table, got %s: %s'):format(type(collector), inspect(collector))
)

assert(
type(collector.name) == 'string',
('Collector must have string property "name", got %s: %s'):format(type(collector.name), inspect(collector))
)

-- collector exists
assert(
not collector_exists(collector),
('Collector<%s> already exists'):format(collector.name)
)

assert(
type(collector.ngx_phases) == 'table',
('Collector<%s>.ngx_phases must be an array, given: %s'):format(collector.name, type(collector.ngx_phases))
)

assert(
all(function(phase) return type(phase) == 'string' end, collector.ngx_phases),
('Collector<%s>.ngx_phases must be an array of strings, given: %s'):format(collector.name, inspect(collector.ngx_phases))
)

assert(
is.callable(collector.on_phase),
('Collector<%s>:on_phase must be a function or callable table, given: %s'):format(collector.name, type(collector.on_phase))
)

assert(
type(collector.fields) == 'table',
('Collector<%s>.fields must be a table, given: %s'):format(collector.name, type(collector.fields))
)

assert(
all(function(field, params) return type(field) == 'string' and type(params) == 'table' end, collector.fields),
('Collector<%s>.fields must be an table[string, table], given: %s'):format(collector.name, inspect(collector.fields))
)
end

---
-- @param collector table
-- @return table
---
local collector_extend = function(collector)
local _metatable = {
init = function(self, storage)
self.storage = storage
end,

handle_ngx_phase = function(self, phase)
self:on_phase(phase)
end,

aggregate = function(self)
iter(self.fields):each(function(field, params)
if params.mean then
self.storage:mean_flush(field)
elseif params.cyclic then
self.storage:cyclic_flush(field)
end
end)
end,

get_raw_stats = function(self)
return iter(self.fields):map(function(k, _)
return k, (self.storage:get(k) or 0)
end)
end,

get_text_stats = function(self, output_helper)
return output_helper.render_stats(self)
end,

get_html_stats = function(self, output_helper)
return output_helper.render_stats(self)
local _metatable = {
init = function(self, storage)
self.storage = storage
end,

handle_ngx_phase = function(self, phase)
self:on_phase(phase)
end,

aggregate = function(self)
iter(self.fields):each(function(field, params)
if params.mean then
self.storage:mean_flush(field)
elseif params.cyclic then
self.storage:cyclic_flush(field)
end
}
_metatable.__index = _metatable
end)
end,

setmetatable(collector, _metatable)
get_raw_stats = function(self)
return iter(self.fields):map(function(k, _)
return k, (self.storage:get(k) or 0)
end)
end,

return collector
get_text_stats = function(self, output_helper)
return output_helper.render_stats(self)
end,

get_html_stats = function(self, output_helper)
return output_helper.render_stats(self)
end
}
_metatable.__index = _metatable

setmetatable(collector, _metatable)

return collector
end

---
-- @param collector table
-- @return table
---
local collector_register = function(collector)
collector_validate(collector)
collector_validate(collector)

collector = collector_extend(collector)
collector = collector_extend(collector)

local storage = storage_collector_wrapper_factory.create(collector)
collector:init(storage)
local storage = storage_collector_wrapper_factory.create(collector)
collector:init(storage)

table.insert(collectors, collector)
table.insert(collectors, collector)

return collector
return collector
end

--------------------------------------------------------------------------------
Expand All @@ -130,19 +130,19 @@ exports.register = collector_register
exports.all = collectors_iter

if __TEST__ then
exports.__private__ = {
collectors = function(value)
if value ~= nil then
local count = length(collectors)
while count > 0 do table.remove(collectors); count = count - 1 end
iter(value):each(function(collector) table.insert(collectors, collector) end)
end
return collectors
end,
collector_exists = collector_exists,
collector_extend = collector_extend,
collector_validate = collector_validate,
}
exports.__private__ = {
collectors = function(value)
if value ~= nil then
local count = length(collectors)
while count > 0 do table.remove(collectors); count = count - 1 end
iter(value):each(function(collector) table.insert(collectors, collector) end)
end
return collectors
end,
collector_exists = collector_exists,
collector_extend = collector_extend,
collector_validate = collector_validate,
}
end

return exports
24 changes: 12 additions & 12 deletions nginx-metrix/lib/is.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ local exports = {}

local callable
callable = function(obj)
return type(obj) == 'function' or type(obj) == 'table' and getmetatable(obj) and callable(getmetatable(obj).__call)
return type(obj) == 'function' or type(obj) == 'table' and getmetatable(obj) and callable(getmetatable(obj).__call)
end

exports.callable = callable

setmetatable(exports, {
__call = function(t)
if _G.is == nil or type(_G.is) ~= 'table' then
_G.is = t
else
for k, v in pairs(t) do
_G.is[k] = v
end
end
return exports
end,
__call = function(t)
if _G.is == nil or type(_G.is) ~= 'table' then
_G.is = t
else
for k, v in pairs(t) do
_G.is[k] = v
end
end
return exports
end,
})

return exports
return exports
58 changes: 28 additions & 30 deletions nginx-metrix/listener.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local inspect = require 'inspect'

local phases = {
[[init]],
[[access]],
[[content]],
[[log]],
[[body-filter]],
[[header-filter]],
[[rewrite]],
[[ssl_certificate]],
[[init_worker]],
'init',
'access',
'content',
'log',
'body-filter',
'header-filter',
'rewrite',
'ssl_certificate',
'init_worker',
}

local handlers = zip(phases, duplicate({})):tomap()
Expand All @@ -18,30 +18,28 @@ local handlers = zip(phases, duplicate({})):tomap()
-- @param collector table
--
local attach_collector = function(collector)
iter(collector.ngx_phases):each(function(phase)
assert(
index(phase, phases) ~= nil,
('Collector<%s>.ngx_phases[%s] invalid, phase "%s" does not exists'):format(collector.name, phase, phase)
)

table.insert(handlers[phase], collector)
end)
iter(collector.ngx_phases):each(function(phase)
assert(index(phase, phases) ~= nil,
('Collector<%s>.ngx_phases[%s] invalid, phase "%s" does not exists'):format(collector.name, phase, phase))

table.insert(handlers[phase], collector)
end)
end

---
-- @param phase string
---
local handle_phase = function(phase)
phase = phase or ngx.get_phase()
phase = phase or ngx.get_phase()

assert(
type(phase) == 'string' and index(phase, phases) ~= nil,
('Invalid ngx phase %s (%s)'):format(inspect(phase), type(phase))
)
assert(
type(phase) == 'string' and index(phase, phases) ~= nil,
('Invalid ngx phase %s (%s)'):format(inspect(phase), type(phase))
)

iter(handlers[phase]):each(function(collector)
collector:handle_ngx_phase(phase)
end)
iter(handlers[phase]):each(function(collector)
collector:handle_ngx_phase(phase)
end)
end

--------------------------------------------------------------------------------
Expand All @@ -52,10 +50,10 @@ exports.attach_collector = attach_collector
exports.handle_phase = handle_phase

if __TEST__ then
exports.__private__ = {
phases = phases,
get_handlers = function() return handlers end,
}
exports.__private__ = {
phases = phases,
get_handlers = function() return handlers end,
}
end

return exports
return exports

0 comments on commit d7c18a1

Please sign in to comment.