-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbotreports.py
363 lines (338 loc) · 13.3 KB
/
botreports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
import boto3
import boto3.session
import glob
import hashlib
import os
import botledger as ledger
import botutils as utils
import botfiles as files
logger = None
config = None
def getNpubsWithEvents():
return os.listdir(files.userEventsFolder)
def getEventsForNpub(npub):
path = f"{files.userEventsFolder}{npub}"
return os.listdir(path)
def makeAllReports():
# Get configs, only process reports for those with proper names
# This supports disabling a config or report by prefixing the filename
botConfigs = files.listUserConfigs()
validNpubs = []
for botConfigFile in botConfigs:
npub = botConfigFile.split(".")[0]
if not npub.startswith("npub"):
continue
if npub not in validNpubs:
validNpubs.append(npub)
npubs = getNpubsWithEvents()
for npub in npubs:
if npub not in validNpubs:
continue
makeLedgerReport(npub)
events = getEventsForNpub(npub)
for eventId in events:
if os.path.isdir(os.path.join(f"{files.userEventsFolder}{npub}", eventId)):
makeEventReport(npub, eventId)
makeIndex(npub)
def getReportFilename(npub, eventId):
destFolder = f"{files.userReportsFolder}{npub}/"
utils.makeFolderIfNotExists(destFolder)
destFile = f"{destFolder}{eventId}.html"
return destFile
def makeEventReport(npub, eventId):
sourceFolder = f"{files.userEventsFolder}{npub}/{eventId}/"
utils.makeFolderIfNotExists(sourceFolder)
sourcePaidNpubsFile = f"{sourceFolder}paidnpubs.json"
sourcePaidNpubs = files.loadJsonFile(sourcePaidNpubsFile, {})
destFile = getReportFilename(npub, eventId)
logger.debug(f"Making report at {destFile}")
destData = \
buildEventReportHeader(npub, eventId) + \
buildEventReportLines(sourcePaidNpubs) + \
buildEventReportFooter(npub, eventId)
fileChanged = saveIfFileContentDifferent(destFile, destData)
if fileChanged:
uploadFile(npub=npub, srcFile=destFile, destFile=f"{eventId}.html")
return fileChanged
def saveIfFileContentDifferent(filename, data):
different = False
if not os.path.exists(filename):
different = True
else:
with open(filename) as f:
fileContent = f.read()
different = fileContent != data
if different:
with open(filename, "w") as f:
f.write(data)
return different
def isAWSEnabled():
if "aws" not in config: return False
if not all(k in config["aws"] for k in (
"enabled",
"s3Bucket",
"aws_access_key_id",
"aws_secret_access_key",
"baseKey",
"pepper")):
return False
if not config["aws"]["enabled"]: return False
return True
def getS3Folder(npub):
if not isAWSEnabled: return npub
baseKey = config["aws"]["baseKey"]
pepper = config["aws"]["pepper"]
input = npub + pepper
hexresult = hashlib.md5(input.encode()).hexdigest()
result = f"{baseKey}{hexresult}"
return result
def uploadFile(npub, srcFile=None, destFile="index.html"):
if srcFile is None:
logger.error(f"Source file was not provided when calling uploadFile")
return
s3Key = getS3Folder(npub)
s3Key = f"{s3Key}/{destFile}"
uploadToAWS(s3Key, srcFile)
def getReportIndexURL(npub):
if not isAWSEnabled(): return None
s3Key = getS3Folder(npub) + "/index.html"
s3Bucket = config["aws"]["s3Bucket"]
url = f"https://{s3Bucket}.s3.amazonaws.com/{s3Key}"
return url
def uploadToAWS(s3Key, filename):
# Get AWS info
if not isAWSEnabled():
logger.debug(f"File {filename} not uploaded. AWS not enabled")
return
s3Bucket = config["aws"]["s3Bucket"]
aws_access_key_id = config["aws"]["aws_access_key_id"]
aws_secret_access_key = config["aws"]["aws_secret_access_key"]
# URL
url = f"https://{s3Bucket}.s3.amazonaws.com/{s3Key}"
# Upload to bucket
mysession = boto3.session.Session(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key)
s3Client = mysession.client('s3')
s3Client.upload_file(
Filename=filename,
Bucket=s3Bucket,
Key=s3Key,
ExtraArgs={'ContentType':"text/html", "CacheControl": "public,max-age=86400"}
)
logger.debug(f"Updated {url}")
def buildEventReportHeader(npub, eventId):
output = "<html>"
output += "<head><style>\n"
output += "body {font-family:DejaVuSansMono,Consolas,Monospace,Lucida Console;font-size:12pt;}\n"
output += "table {width:100%;border-collapse:collapse;}\n"
output += "thead {background-color:#440044;color:#ffffff;font-weight:700;font-size:12pt;}\n"
output += "tbody {background-color:#880088;color:#ffffff;font-size:10pt;}\n"
output += "tr.d {border-bottom: 2px solid #610061;}\n"
output += "td {font-size:8pt;}\n"
output += "a {color:#ffffff;}\n"
output += "</style></head>"
output += "<body>"
output += f"<h3>BoostZapper report for event: {eventId}</h3>"
output += "<p>Below are the amount of zaps recorded for the event, and the routing fees charged by peer channels</p>"
return output
def buildEventReportLines(sourcedata):
output = "<table>"
i = 0
output += "<thead><tr><td width=50>#</td><td width=150>Date</td><td>Pubkey / Lightning Address</td><td width=150>Sats Rewarded</td><td width=150>Routing Fee</td></tr></thead>"
output += "<tbody>"
for k,v in sourcedata.items():
i = i + 1
dateiso = "unknown"
if "payment_time_human" in v: dateiso = v["payment_time_human"]
if "payment_time_iso" in v: dateiso = v["payment_time_iso"]
lightning = "unknown"
if "lightning_id" in v: lightning = v["lightning_id"]
amount = "unknown"
if "amount_sat" in v: amount = v["amount_sat"]
fee = "unknown"
if "fee_msat" in v: fee = v["fee_msat"]
line1 = f"<tr><td rowspan=\"2\">{i}</td><td rowspan=\"2\">{dateiso}</td><td colspan=\"3\">{k}</td></tr>"
line2 = f"<tr><td>{lightning}</td><td>{amount} sat</td><td>{fee} msat</td>"
line3 = f"<tr class=\"d\"><td colspan=5></td></tr>"
output += line1
output += line2
output += line3
output += "</tbody>"
output += "</table>"
return output
def buildEventReportFooter(npub, eventId):
output = "</body></html>"
return output
def buildIndexHeader(npub):
output = "<html>"
output += "<head><style>\n"
output += "body {font-family:DejaVuSansMono,Consolas,Monospace,Lucida Console;font-size:12pt;}\n"
output += "table {width:100%;border-collapse:collapse;}\n"
output += "thead {background-color:#440044;color:#ffffff;font-weight:700;font-size:12pt;}\n"
output += "tbody {background-color:#880088;color:#ffffff;font-size:10pt;}\n"
output += "tr.d {border-bottom: 2px solid #610061;}\n"
output += "td {font-size:8pt;}\n"
output += "a {color:#ffffff;}\n"
output += "a.ledger {color:#000000;}\n"
output += "</style></head>"
output += "<body>"
output += f"<h3><a class=\"ledger\" href=\"ledger.html\">Click Here For Ledger</a></h3>"
output += f"<h3>List of Events Monitored by BoostZapper for {npub}</h3>"
return output
def buildIndexLines(sourcedata):
output = "<table>"
i = 0
output += "<thead><tr><td width=50>#</td><td width=150>Date</td><td>Note Event</td></tr></thead>"
output += "<tbody>"
for v in sourcedata:
if type(v) is not dict: continue
i = i + 1
if not all(k in v for k in ("date_iso","eventId")): continue
date_iso = v["date_iso"]
eventId = v["eventId"]
output += f"<tr><td>{i}</td><td>{date_iso}</td><td><a href=\"./{eventId}.html\">{eventId}</a></td></tr>"
output += f"<tr class=\"d\"><td colspan=5></td></tr>"
output += "</tbody>"
output += "</table>"
return output
def buildIndexFooter(npub):
output = "</body></html>"
return output
def getIndexFilename(npub):
destFolder = f"{files.userReportsFolder}{npub}/"
utils.makeFolderIfNotExists(destFolder)
destFile = f"{destFolder}index.html"
return destFile
def makeIndex(npub):
sourceFolder = f"{files.userEventsFolder}{npub}/"
utils.makeFolderIfNotExists(sourceFolder)
eventIndexFile = f"{sourceFolder}index.json"
eventIndex = files.loadJsonFile(eventIndexFile, [])
eventIndex.reverse()
destFile = getIndexFilename(npub)
logger.debug(f"Making index at {destFile}")
destData = \
buildIndexHeader(npub) + \
buildIndexLines(eventIndex) + \
buildIndexFooter(npub)
fileChanged = saveIfFileContentDifferent(destFile, destData)
if fileChanged:
uploadFile(npub=npub, srcFile=destFile)
return fileChanged
def getLedgerReportFilename(npub):
destFolder = f"{files.userReportsFolder}{npub}/"
utils.makeFolderIfNotExists(destFolder)
destFile = f"{destFolder}ledger.html"
return destFile
def buildLedgerReportHeader(npub):
output = ""
output += "<html>"
output += "<head><style>\n"
output += "body {font-family:DejaVuSansMono,Consolas,Monospace,Lucida Console;font-size:12pt;}\n"
output += "table {width:100%;border-collapse:collapse;}\n"
output += "thead {background-color:#440044;color:#ffffff;font-weight:700;font-size:12pt;}\n"
output += "tbody {background-color:#880088;color:#ffffff;font-size:10pt;}\n"
output += "tr.d {border-bottom: 2px solid #610061;}\n"
output += "td {font-size:8pt;}\n"
output += "a {color:#ffffff;}\n"
output += "</style></head>"
output += "<body>"
output += f"<h3>BoostZapper ledger for {npub}</h3>"
output += "<table>"
output += "<thead><tr>"
output += "<td width=150>Date</td>"
output += "<td>Description</td>"
output += "<td width=80 align=center>Credits</td>"
output += "<td width=90 align=center>Zap Amount</td>"
output += "<td width=90 align=center>Routing Cost</td>"
output += "<td width=90 align=center>Service Fee</td>"
output += "<td width=90 align=center>Balance</td>"
output += "</tr></thead>"
output += "<tbody>"
return output
def buildLedgerReportLines(sourcedata):
output = ""
linedescription = ""
linecredits = ""
linezap = ""
lineroutingfee = ""
lineservicefee = ""
for dataEntry in sourcedata:
if type(dataEntry) is not dict: continue
# from the entry
linedateiso = dataEntry["created_at_iso"]
description = dataEntry["description"]
entrytype = dataEntry["type"]
credits = dataEntry["credits"]
mcredits = dataEntry["mcredits"]
balance = dataEntry["balance"]
# Ignore entry if carry over
if description == "Carry over from ledger rotation": continue
# line management
writeLine = False
if linedescription == "": linedescription = description
if entrytype == "ZAPS":
linezap = str(credits)
if entrytype == "ROUTING FEES":
if description.startswith("Credit for zap payment"):
linecredits = f"{(mcredits/1000):.3f}"
writeLine = True
else:
lineroutingfee = f"{(mcredits/1000):.3f}"
if entrytype == "SERVICE FEES":
lineservicefee = f"{(mcredits/1000):.3f}"
writeLine = True
if entrytype == "INITIALIZED":
writeLine = True
if entrytype == "CREDITS APPLIED":
linecredits = str(credits)
writeLine = True
if entrytype == "REPLY MESSAGE":
lineservicefee = f"{(mcredits/1000):.3f}"
writeLine = True
# Write the line if ready
if writeLine:
linebalance = f"{balance:.3f}"
output += "<tr>"
output += f"<td>{linedateiso}</td>"
output += f"<td>{linedescription}</td>"
output += f"<td align=right>{linecredits}</td>"
output += f"<td align=right>{linezap}</td>"
output += f"<td align=right>{lineroutingfee}</td>"
output += f"<td align=right>{lineservicefee}</td>"
output += f"<td align=right>{linebalance}</td>"
output += "</tr>"
# and then reset variables
linedescription = ""
linecredits = ""
linezap = ""
lineroutingfee = ""
lineservicefee = ""
return output
def buildLedgerReportFooter(npub):
output = ""
output += "</tbody>"
output += "</table>"
output = "</body></html>"
return output
def makeLedgerReport(npub):
destFile = getLedgerReportFilename(npub)
logger.debug(f"Making ledger report at {destFile}")
destData = buildLedgerReportHeader(npub)
archiveFilePattern = f"{files.userLedgerFolder}archived/{npub}*"
archiveFiles = []
for archiveFilename in glob.glob(archiveFilePattern):
archiveFiles.append(archiveFilename)
archiveFiles.sort()
for archiveFilename in archiveFiles:
sourcedata = files.loadJsonFile(archiveFilename)
destData += buildLedgerReportLines(sourcedata)
sourcedata = files.loadJsonFile(ledger.getUserLedgerFilename(npub))
destData += buildLedgerReportLines(sourcedata)
destData += buildLedgerReportFooter(npub)
fileChanged = saveIfFileContentDifferent(destFile, destData)
if fileChanged:
uploadFile(npub=npub, srcFile=destFile, destFile=f"ledger.html")
return fileChanged