2009-06-20 12:57:22 +00:00
|
|
|
--
|
|
|
|
-- data.lua: property and methods for manipulating incoming measurements
|
|
|
|
-- Copyright (c) 2009 jokamajo.org
|
2010-04-20 13:38:53 +00:00
|
|
|
-- 2010 flukso.net
|
2009-06-20 12:57:22 +00:00
|
|
|
--
|
|
|
|
-- This program is free software; you can redistribute it and/or
|
|
|
|
-- modify it under the terms of the GNU General Public License
|
|
|
|
-- as published by the Free Software Foundation; either version 2
|
|
|
|
-- of the License, or (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU General Public License
|
|
|
|
-- along with this program; if not, write to the Free Software
|
|
|
|
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
--
|
2009-06-22 12:54:51 +00:00
|
|
|
-- $Id$
|
2009-06-20 12:57:22 +00:00
|
|
|
--
|
|
|
|
|
2010-04-20 22:07:47 +00:00
|
|
|
local os, math, table, string =
|
|
|
|
os, math, table, string
|
2009-06-20 12:57:22 +00:00
|
|
|
|
2010-04-20 22:07:47 +00:00
|
|
|
local getfenv, setmetatable, pairs, ipairs =
|
|
|
|
getfenv, setmetatable, pairs, ipairs
|
2009-06-20 12:57:22 +00:00
|
|
|
|
|
|
|
module (...)
|
|
|
|
local modenv = getfenv() -- module environment
|
|
|
|
|
2010-04-20 22:32:02 +00:00
|
|
|
-- private
|
|
|
|
local function timestamps(T)
|
|
|
|
local H = {} -- helper table, an indexed array containing all the measurement's timestamps
|
|
|
|
for timestamp in pairs(T) do H[#H+1] = timestamp end
|
|
|
|
table.sort(H) -- sort in ascending order, oldest timestamps will be treated first
|
|
|
|
return H
|
|
|
|
end
|
|
|
|
|
2009-06-20 12:57:22 +00:00
|
|
|
function new()
|
|
|
|
return setmetatable({}, {__index = modenv})
|
|
|
|
end
|
|
|
|
|
|
|
|
function add(M, meter, timestamp, value)
|
|
|
|
if not M[meter] then M[meter] = {} end
|
|
|
|
M[meter][timestamp] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
function clear(M)
|
|
|
|
for meter in pairs(M) do
|
|
|
|
M[meter] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function filter(M, span, offset)
|
|
|
|
for meter, T in pairs(M) do
|
2010-04-20 13:38:53 +00:00
|
|
|
local H = timestamps(T)
|
2009-06-20 12:57:22 +00:00
|
|
|
local i = 2
|
|
|
|
while not (H[i+1] == nil or H[i] > os.time()-offset) do
|
|
|
|
if math.floor(H[i-1]/span) == math.floor(H[i]/span) and math.floor(H[i]/span) == math.floor(H[i+1]/span) then
|
|
|
|
T[H[i]] = nil
|
|
|
|
table.remove(H, i)
|
|
|
|
else
|
|
|
|
i = i+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-06-22 12:54:51 +00:00
|
|
|
end
|
2010-04-20 13:38:53 +00:00
|
|
|
|
|
|
|
function truncate(M, cutoff)
|
|
|
|
for meter, T in pairs(M) do
|
|
|
|
local H = timestamps(T)
|
2010-10-01 18:05:05 +00:00
|
|
|
for i = H[1], os.time() - cutoff do
|
2010-04-20 13:38:53 +00:00
|
|
|
T[i] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function fill(M)
|
|
|
|
for meter, T in pairs(M) do
|
|
|
|
local H = timestamps(T)
|
2010-09-09 20:07:02 +00:00
|
|
|
for i = H[#H]-1, H[1]+1, -1 do
|
2010-10-03 19:12:01 +00:00
|
|
|
if T[i] == nil or T[i] == '"nan"' then T[i] = T[i+1] end
|
2010-10-01 18:05:05 +00:00
|
|
|
end
|
|
|
|
for i = H[#H]+1, os.time() do
|
2010-10-03 19:12:01 +00:00
|
|
|
T[i] = '"nan"'
|
2010-04-20 13:38:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function json_encode(M)
|
2010-04-20 22:07:47 +00:00
|
|
|
local J = {}
|
2010-04-20 13:38:53 +00:00
|
|
|
for meter, T in pairs(M) do
|
|
|
|
local H = timestamps(T)
|
2010-05-22 09:01:15 +00:00
|
|
|
local SB = {'['} -- use a string buffer for building up the JSON string
|
2010-04-20 22:07:47 +00:00
|
|
|
for k, timestamp in ipairs(H) do
|
2010-05-22 09:01:15 +00:00
|
|
|
SB[#SB+1] = '[' .. timestamp .. ',' .. T[timestamp] .. '],'
|
2010-04-20 13:38:53 +00:00
|
|
|
end
|
2010-05-22 09:01:15 +00:00
|
|
|
SB[#SB] = SB[#SB]:sub(1, -2) -- remove the trialing comma from the last entry
|
|
|
|
SB[#SB+1] = ']'
|
|
|
|
J[meter] = table.concat(SB)
|
2010-04-20 13:38:53 +00:00
|
|
|
end
|
|
|
|
return J
|
|
|
|
end
|