-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

--[[
Graphs the load average and process count data. Expects to receive messages
containing fields entitled `1MinAvg`, `5MinAvg`, `15MinAvg`, and `NumProcesses`,
such as those generated by the :ref:`config_linux_load_avg_decoder`.

Config:

- sec_per_row (uint, optional, default 60)
    Sets the size of each bucket (resolution in seconds) in the sliding window.

- rows (uint, optional, default 1440)
    Sets the size of the sliding window i.e., 1440 rows representing 60 seconds
    per row is a 24 sliding hour window with 1 minute resolution.

- anomaly_config (string, optional)
    See :ref:`sandbox_anomaly_module`.

- preservation_version (uint, optional, default 0)
    If `preserve_data = true` is set in the SandboxFilter configuration, then
    this value should be incremented every time the `sec_per_row` or `rows`
    configuration is changed to prevent the plugin from failing to start
    during data restoration.

*Example Heka Configuration*

.. code-block:: ini

    [LoadAvgFilter]
    type = "SandboxFilter"
    filename = "lua_filters/loadavg.lua"
    ticker_interval = 60
    preserve_data = true
    message_matcher = "Type == 'stats.loadavg'"

--]]
_PRESERVATION_VERSION = read_config("preservation_version") or 0

require "circular_buffer"
require "string"
local alert         = require "alert"
local annotation    = require "annotation"
local anomaly       = require "anomaly"

local title             = "LoadAvg Stats"
local rows              = read_config("rows") or 1440
local sec_per_row       = read_config("sec_per_row") or 60
local anomaly_config    = anomaly.parse_config(read_config("anomaly_config"))
annotation.set_prune(title, rows * sec_per_row * 1e9)

local field_names = {"1MinAvg", "5MinAvg", "15MinAvg", "NumProcesses"}

cbuf = circular_buffer.new(rows, #field_names, sec_per_row)

local labels = {}

for i, name in ipairs(field_names) do
    labels[#labels+1] = string.format("Fields[%s]", name)
    cbuf:set_header(i, name, "Count", "max")
end

function process_message ()
    local ts = read_message("Timestamp")
    for i, label in ipairs(labels) do
        local val = read_message(label)
        if type(val) ~= "number" then return -1 end
        cbuf:set(ts, i, val)
    end
    return 0
end

function timer_event(ns)
    if anomaly_config then
        if not alert.throttled(ns) then
            local msg, annos = anomaly.detect(ns, title, cbuf, anomaly_config)
            if msg then
                annotation.concat(title, annos)
                alert.send(ns, msg)
            end
        end
        inject_payload("cbuf", title, annotation.prune(title, ns), cbuf)
    else
        inject_payload("cbuf", title, cbuf)
    end
end
