forked from tarantool/tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: extract alert board into its own module
It encapsulates all the alerts manipulation in one place, splits it from the main config logic and simplifies reading of the relevant code. Several tests are updated to use the public API instead of the internal one. Part of tarantool#9574 Part of tarantool#9586 NO_DOC=refactoring, no user-visible changes NO_CHANGELOG=see NO_DOC NO_TEST=see NO_DOC
- Loading branch information
1 parent
d7b7265
commit 0c9813a
Showing
10 changed files
with
209 additions
and
65 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
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,161 @@ | ||
-- Alert board. | ||
|
||
local datetime = require('datetime') | ||
local log = require('internal.config.utils.log') | ||
|
||
-- Set an alert. | ||
-- | ||
-- The `key` is an unique identifier of the alert. Attempt to set | ||
-- a new alert with the same key replaces the existing alert. | ||
-- | ||
-- The key can be used to drop the alert using :drop(). | ||
-- | ||
-- The `alert` is a table of the following structure. | ||
-- | ||
-- { | ||
-- type = 'warn' or 'error', | ||
-- message = <string>, | ||
-- } | ||
-- | ||
-- More fields may be placed into the table and all these fields | ||
-- are returned from :alerts(). | ||
-- | ||
-- The alert board takes an ownership of the `alert` table and | ||
-- modifies it. | ||
-- | ||
-- A timestamp is saved on the :set() method call to show it in | ||
-- an :alerts() result. | ||
local function aboard_set(self, key, alert) | ||
assert(alert.type == 'error' or alert.type == 'warn') | ||
if alert.type == 'error' then | ||
log.error(alert.message) | ||
else | ||
log.warn(alert.message) | ||
end | ||
alert.timestamp = datetime.now() | ||
self._alerts[key] = alert | ||
end | ||
|
||
-- Return an alert pointed by the given `key` or nil. | ||
local function aboard_get(self, key) | ||
return self._alerts[key] | ||
end | ||
|
||
-- Drop an alert pointed by the given `key`. | ||
-- | ||
-- The function is no-op if the alert doesn't exist. | ||
-- | ||
-- The `on_drop` callback (see the aboard.new() function) is | ||
-- called if the alert had existed. | ||
local function aboard_drop(self, key) | ||
if self._alerts[key] == nil then | ||
return | ||
end | ||
self._alerts[key] = nil | ||
if self._on_drop ~= nil then | ||
self._on_drop(self, key) | ||
end | ||
end | ||
|
||
-- Drop all the alerts. | ||
-- | ||
-- The `on_drop` callback is NOT called. | ||
local function aboard_clean(self) | ||
self._alerts = {} | ||
end | ||
|
||
-- Serialize the alerts to show them to a user. | ||
-- | ||
-- The alerts are sorted by a time of the :set() method call and | ||
-- returned as an array-like table. | ||
local function aboard_alerts(self) | ||
-- Don't return alert keys. | ||
local alerts = {} | ||
for _, alert in pairs(self._alerts) do | ||
table.insert(alerts, alert) | ||
end | ||
-- Sort by timestamps. | ||
table.sort(alerts, function(a, b) | ||
return a.timestamp < b.timestamp | ||
end) | ||
return alerts | ||
end | ||
|
||
-- Return one of three possible statuses: | ||
-- | ||
-- * ready | ||
-- * check_warnings | ||
-- * check_errors | ||
local function aboard_status(self) | ||
local status = 'ready' | ||
|
||
for _, alert in pairs(self._alerts) do | ||
assert(alert.type == 'error' or alert.type == 'warn') | ||
if alert.type == 'error' then | ||
return 'check_errors' | ||
end | ||
|
||
status = 'check_warnings' | ||
end | ||
|
||
return status | ||
end | ||
|
||
-- Return `true` if there are no alerts, `false` otherwise. | ||
local function aboard_is_empty(self) | ||
return next(self._alerts) == nil | ||
end | ||
|
||
local mt = { | ||
__index = { | ||
set = aboard_set, | ||
get = aboard_get, | ||
drop = aboard_drop, | ||
clean = aboard_clean, | ||
alerts = aboard_alerts, | ||
status = aboard_status, | ||
is_empty = aboard_is_empty, | ||
}, | ||
} | ||
|
||
-- Create a new alert board. | ||
-- | ||
-- The `on_drop` callback is called on the :drop() method call. | ||
-- It is NOT called on the :clean() call. | ||
-- | ||
-- The callback should have the following prototype. | ||
-- | ||
-- | local function on_drop_cb(aboard, key) | ||
-- | <...> | ||
-- | end | ||
local function new(opts) | ||
local on_drop | ||
if opts ~= nil then | ||
on_drop = opts.on_drop | ||
end | ||
|
||
return setmetatable({ | ||
-- _alerts is a table of the following structure. | ||
-- | ||
-- { | ||
-- [key1] = { | ||
-- type = 'warn' or 'error', | ||
-- message = <string>, | ||
-- timestamp = <datetime object>, | ||
-- <..arbitrary fields..> | ||
-- }, | ||
-- [key2] = { | ||
-- <...> | ||
-- }, | ||
-- <...> | ||
-- } | ||
-- | ||
-- The key can be a string or a number. | ||
_alerts = {}, | ||
_on_drop = on_drop, | ||
}, mt) | ||
end | ||
|
||
return { | ||
new = new, | ||
} |
Oops, something went wrong.