forked from ssantos21/cln-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
total_fee_income.py
64 lines (46 loc) · 2.05 KB
/
total_fee_income.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
# total-fee-income.py
from collections import defaultdict
from datetime import datetime, timedelta
import json
import requests
import utils
def execute(hours=24):
print("Executing total-fee-income.py")
settings_data = utils.get_settings_data()
rpc_method = "v1/bkpr-listaccountevents"
url = "%s/%s" % (settings_data["nodeRestUrl"], rpc_method)
headers = {
"content-type": "application/json",
"Rune": settings_data["rune"]
}
response = requests.post(url, headers=headers)
return calculate_total_fee_income(response.text, hours)
def calculate_total_fee_income(json_data, hours=24):
"""
Calculate the total fee income grouped by type for the last 'hours' hours.
Parameters:
json_data (str): JSON string containing the event data.
hours (int): The number of hours to look back from the current time. Default is 24.
Returns:
dict: A dictionary with the aggregated sums grouped by type.
"""
# Parse the JSON data
data = json.loads(json_data)
# Get the current time
current_time = datetime.now()
# Dictionary to hold the aggregated sums for the given time range, grouped by type
aggregated_values_by_type = defaultdict(lambda: {'credit_msat': 0, 'debit_msat': 0, 'total': 0})
# Process each event
for event in data['events']:
# Convert the timestamp to a datetime object
event_time = datetime.fromtimestamp(event['timestamp'])
# Check if the event is within the last 'hours' hours
if current_time - timedelta(hours=hours) <= event_time <= current_time:
event_type = event['type']
credit = event.get('credit_msat', 0)
debit = event.get('debit_msat', 0)
aggregated_values_by_type[event_type]['credit_msat'] += credit
aggregated_values_by_type[event_type]['debit_msat'] += debit
aggregated_values_by_type[event_type]['total'] += credit - debit
# Convert defaultdict to a regular dictionary before returning
return dict(aggregated_values_by_type)