diff src/report.js.luan @ 0:8f4df159f06b

start public repo
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Jul 2025 20:57:49 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/report.js.luan	Fri Jul 11 20:57:49 2025 -0600
@@ -0,0 +1,82 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local stringify = Luan.stringify or error()
+local String = require "luan:String.luan"
+local to_number = String.to_number or error()
+local Number = require "luan:Number.luan"
+local long = Number.long or error()
+local Table = require "luan:Table.luan"
+local copy = Table.copy or error()
+local Io = require "luan:Io.luan"
+local Http = require "luan:http/Http.luan"
+local Reporting = require "site:/lib/Reporting.luan"
+local db = Reporting.db or error()
+local Utils = require "site:/lib/Utils.luan"
+local to_list = Utils.to_list or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "report.js"
+
+
+-- modified from Lucene.luan
+local function get_document(query)
+	local doc
+	local function fn(_,doc_fn,_)
+		doc = doc_fn()
+	end
+	local total_hits = db.advanced_search(query,fn,1)
+	if total_hits > 1 then
+		logger.error("found "..total_hits.." documents for query: "..stringify(query))
+	end
+	return doc
+end
+
+return function()
+	local types = Http.request.parameters.type or error()
+	types = to_list(types)
+	local owner = Http.request.parameters.owner or error()
+	local today = Http.request.parameters.today or error()
+	today = to_number(today) or error(today)
+	today = long(today)
+	db.run_in_transaction( function()
+		for _, type in ipairs(types) do
+			local value = Http.request.parameters[type]
+			local before = Http.request.parameters[type.."_before"]
+			before = before and long(to_number(before))
+			--logger.info(type.." ~ "..owner.." ~ "..value)
+			local keys = {
+				type = type
+				owner = owner
+				value = value
+				day = today
+			}
+			local found = get_document(keys)
+			if found == nil then
+				local doc = copy(keys)
+				doc.count = 1
+				db.save(doc)
+				--logger.info(1)
+			else
+				found.count = found.count + 1
+				db.save(found)
+				--logger.info(found.count)
+			end
+			if before ~= nil then
+				keys.day = before
+				found = get_document(keys)
+				if found == nil then
+					logger.warn("before not found: "..stringify(keys))
+				else
+					local count = found.count
+					if count <= 0 then
+						logger.warn("before count is "..count.." for: "..stringify(keys))
+					else
+						found.count = count - 1
+						db.save(found)
+						--logger.info("before")
+					end
+				end
+			end
+		end_for
+	end )
+end