From 6b8e701d26e9d69cf27e16fa9e7fe811740b7109 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Sat, 30 Apr 2011 19:48:44 +0200 Subject: [PATCH] [fluksod] join the fill and truncate operations in a single method --- .../package/flukso/luasrc/flukso/data.lua | 25 ++++++++--------- .../openwrt/package/flukso/luasrc/fluksod.lua | 28 ++++++------------- 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua b/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua index d864b0e..1f8401e 100644 --- a/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua +++ b/mote/v2/openwrt/package/flukso/luasrc/flukso/data.lua @@ -90,29 +90,28 @@ function filter(M, span, offset) end end -function truncate(M, cutoff) - for sensor, T in pairs(M) do - local H = timestamps(T) - - for i = H[1], os.time() - cutoff do - T[i] = nil - end - end -end - -function fill(M) +function polish(M, cutoff) + local now = os.time() + for sensor, T in pairs(M) do local H = timestamps(T) + -- fill up the holes first for i = H[#H]-1, H[1]+1, -1 do if T[i] == nil or T[i] == '"nan"' then T[i] = T[i+1] end end - for i = H[#H]+1, os.time() do + -- make sure the tail stretches up to 'now' + for i = H[#H]+1, now do T[i] = '"nan"' end + + -- truncate the head + for i = H[1], now - cutoff do + T[i] = nil + end end end @@ -127,7 +126,7 @@ function json_encode(M) SB[#SB+1] = '[' .. timestamp .. ',' .. T[timestamp] .. '],' end - SB[#SB] = SB[#SB]:sub(1, -2) -- remove the trialing comma from the last entry + SB[#SB] = SB[#SB]:sub(1, -2) -- remove the trailing comma from the last entry SB[#SB+1] = ']' J[sensor] = table.concat(SB) end diff --git a/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua b/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua index cde8468..fbadfd3 100755 --- a/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua +++ b/mote/v2/openwrt/package/flukso/luasrc/fluksod.lua @@ -311,30 +311,20 @@ function lan_buffer(child) end) end -function polish(child, cutoff) +function publish(child) return coroutine.create(function(measurements) - while true do - measurements:fill() - measurements:truncate(cutoff) - coroutine.resume(child, measurements) - measurements = coroutine.yield() - end - end) -end + nixio.fs.mkdirr(LAN_PUBLISH_PATH) -function publish(child, dir) - return coroutine.create(function(measurements) - nixio.fs.mkdirr(dir) - - for file in nixio.fs.dir(dir) do + for file in nixio.fs.dir(LAN_PUBLISH_PATH) do nixio.fs.unlink(file) end while true do + measurements:polish(LAN_POLISH_CUTOFF) local measurements_json = measurements:json_encode() for sensor_id, json in pairs(measurements_json) do - local file = dir .. '/' .. sensor_id + local file = LAN_PUBLISH_PATH .. '/' .. sensor_id nixio.fs.unlink(file) fd = nixio.open(file, O_RDWR_CREAT) @@ -381,11 +371,9 @@ local wan_chain = local lan_chain = lan_buffer( - polish( - publish( - debug(nil) - , LAN_PUBLISH_PATH) - , LAN_POLISH_CUTOFF) + publish( + debug(nil) + ) ) local chain = dispatch(wan_chain, lan_chain)