Created
October 18, 2017 16:54
-
-
Save gregglind/0bfe73857b019c22d6b4032baa8ed6f8 to your computer and use it in GitHub Desktop.
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
-- | |
-- General Filter for 'seen today' for all Study and Shield addons | |
-- | |
-- | |
require "cjson" | |
require "hyperloglog" | |
require "string" | |
addonId = "[email protected]" | |
countStr = "57-perception-shield-study_count" | |
addon_day_counts = {} | |
-- date => count | |
addon_day_hlls = {} | |
-- proposed structure: | |
-- | |
-- ADDON_ID: { date => count} | |
-- | |
-- CEP: process an individual message | |
function process_message() | |
local addonJson = read_message("Fields[environment.addons]") | |
if not addonJson then | |
return -1 | |
end | |
-- exact match, wants to be regex | |
if string.find(addonJson, addonId, 1, true) ~= nil then | |
local day = read_message("Fields[submissionDate]") | |
local hll = addon_day_hlls[day] | |
if not hll then | |
hll = hyperloglog.new() | |
addon_day_hlls[day] = hll | |
end | |
hll:add(read_message("Fields[clientId]")) | |
end | |
return 0 | |
end | |
-- CEP: once in a while | |
function timer_event() | |
local count = 0 | |
local earliest_day = nil | |
for day, hll in pairs(addon_day_hlls) do | |
addon_day_counts[day] = hll:count() | |
count = count + 1 | |
if not earliest_day or day < earliest_day then | |
earliest_day = day | |
end | |
end | |
inject_payload("json", countStr, cjson.encode(addon_day_counts)) | |
if count > 30 then | |
addon_day_hlls[earliest_day] = nil | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment