comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:8f4df159f06b
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local ipairs = Luan.ipairs or error()
4 local stringify = Luan.stringify or error()
5 local String = require "luan:String.luan"
6 local to_number = String.to_number or error()
7 local Number = require "luan:Number.luan"
8 local long = Number.long or error()
9 local Table = require "luan:Table.luan"
10 local copy = Table.copy or error()
11 local Io = require "luan:Io.luan"
12 local Http = require "luan:http/Http.luan"
13 local Reporting = require "site:/lib/Reporting.luan"
14 local db = Reporting.db or error()
15 local Utils = require "site:/lib/Utils.luan"
16 local to_list = Utils.to_list or error()
17 local Logging = require "luan:logging/Logging.luan"
18 local logger = Logging.logger "report.js"
19
20
21 -- modified from Lucene.luan
22 local function get_document(query)
23 local doc
24 local function fn(_,doc_fn,_)
25 doc = doc_fn()
26 end
27 local total_hits = db.advanced_search(query,fn,1)
28 if total_hits > 1 then
29 logger.error("found "..total_hits.." documents for query: "..stringify(query))
30 end
31 return doc
32 end
33
34 return function()
35 local types = Http.request.parameters.type or error()
36 types = to_list(types)
37 local owner = Http.request.parameters.owner or error()
38 local today = Http.request.parameters.today or error()
39 today = to_number(today) or error(today)
40 today = long(today)
41 db.run_in_transaction( function()
42 for _, type in ipairs(types) do
43 local value = Http.request.parameters[type]
44 local before = Http.request.parameters[type.."_before"]
45 before = before and long(to_number(before))
46 --logger.info(type.." ~ "..owner.." ~ "..value)
47 local keys = {
48 type = type
49 owner = owner
50 value = value
51 day = today
52 }
53 local found = get_document(keys)
54 if found == nil then
55 local doc = copy(keys)
56 doc.count = 1
57 db.save(doc)
58 --logger.info(1)
59 else
60 found.count = found.count + 1
61 db.save(found)
62 --logger.info(found.count)
63 end
64 if before ~= nil then
65 keys.day = before
66 found = get_document(keys)
67 if found == nil then
68 logger.warn("before not found: "..stringify(keys))
69 else
70 local count = found.count
71 if count <= 0 then
72 logger.warn("before count is "..count.." for: "..stringify(keys))
73 else
74 found.count = count - 1
75 db.save(found)
76 --logger.info("before")
77 end
78 end
79 end
80 end_for
81 end )
82 end