-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.py
214 lines (183 loc) · 8.44 KB
/
report.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
from tranGen import transaction_generator
from statistics import mean
import glob
import json
class In_Memory_Report():
""" Report class for querying the ledger in memory """
def __init__(self, name):
self._name = name
@classmethod
def total_transactions(cls, ledger):
""" Print the total transactions for a specified ledger """
print(f'Total transactions for {ledger.name}: {ledger.total_transactions()}')
@classmethod
def total_cancelled_transactions(cls, ledger):
""" Print the total cancelled transactions for a specified ledger """
print(f'Total cancelled transactions for {ledger.name}: {ledger.total_cancelled_transactions()}')
@classmethod
def total_transactions_per_user(cls, ledger):
""" Print the total cancelled transactions per user a specified ledger """
transaction_data = ledger.transactions_by_user
for user in ledger.transactions_by_user:
sent_transactions = transaction_data[user]['sent_transactions']
received_transactions = transaction_data[user]['received_transactions']
total_transactions = sent_transactions + received_transactions
print(f'Total transactions for {user} : {total_transactions}')
@classmethod
def total_credits_per_user(cls, ledger):
""" Print the total credits per user a specified ledger """
transaction_data = ledger.transactions_by_user
for user in ledger.transactions_by_user:
received_totals = transaction_data[user]['received_totals']
print(f'Total credits for {user} : {received_totals}')
@classmethod
def total_debits_per_user(cls, ledger):
""" Print the total debits per user a specified ledger """
transaction_data = ledger.transactions_by_user
for user in ledger.transactions_by_user:
sent_totals = transaction_data[user]['sent_totals']
print(f'Total debits for {user} : {sent_totals}')
@classmethod
def highest_balance_per_user(cls, ledger):
""" Print the highest balance per user a specified ledger """
transaction_data = ledger.transactions_by_user
for user in ledger.transactions_by_user:
highest_balance = transaction_data[user]['highest_balance']
print(f'Highest balance for {user} : {highest_balance}')
@classmethod
def lowest_balance_per_user(cls, ledger):
""" Print the lowest balance per user a specified ledger """
transaction_data = ledger.transactions_by_user
for user in ledger.transactions_by_user:
lowest_balance = transaction_data[user]['lowest_balance']
print(f'Lowest balance for {user} : {lowest_balance}')
@classmethod
def total_money_exchanged(cls, ledger):
""" Print the amount of money exchanged on a specified ledger """
all_money = sum(ledger.transaction_values)
print(f'Total money exchanged on {ledger.name} : {all_money}')
@classmethod
def average_transaction_value(cls, ledger):
""" Print the average transaction value on a specified ledger """
average_transaction = mean(ledger.transaction_values)
print(f'Average transaction value on {ledger.name} : {average_transaction}')
@classmethod
def transactions(cls, ledger):
""" Print the recorded transaction amounts on a specified ledger """
transactions = ledger.transaction_values
print(f'Transactions amount on {ledger.name} : {transactions}')
@classmethod
def transactions_with_cancels(cls, ledger):
""" Print the transaction amounts on a specified ledger with cancelled transactions """
transactions = ledger.transaction_values_with_cancels
print(f'Transactions (with cancels) amount on {ledger.name} : {transactions}')
@classmethod
def all_transactions(cls, ledger):
""" Print the transaction amounts on a specified ledger with cancelled transactions """
transactions = ledger.all_transactions
print(f'All transactions on the following ledger {ledger.name} : {transactions}')
class Report():
""" Report class for reading .json files """
def __init__(self, name):
self._name = name
@classmethod
def load_all_transaction_files(cls):
all_transactions = []
for name in glob.glob('block_*.json'):
with open(name, 'r') as f:
datastore = json.load(f)
all_transactions.append(datastore)
return all_transactions
@classmethod
def load_all_data_per_user(cls):
all_transactions_per_user = []
for name in glob.glob('transactions_*.json'):
with open(name, 'r') as f:
datastore = json.load(f)
all_transactions_per_user.append(datastore)
return all_transactions_per_user
@classmethod
def total_transactions(cls, transactions):
""" Print the total transactions for a specified ledger """
count = 0
for transaction in transactions:
count += len(transaction['transactions'])
print(f'Total transactions: {count}')
@classmethod
def total_cancelled_transactions(cls, transactions):
""" Print the total cancelled transactions for a specified ledger """
count = 0
for transaction in transactions:
for i in transaction['transactions']:
if 'cancelled_tid' in i:
count += 1
print(f'Total cancelled transactions for {count}')
@classmethod
def total_transactions_per_user(cls, data):
""" Print the total cancelled transactions per user a specified ledger """
data = data[0]
for user in data:
sent_transactions = data[user]['sent_transactions']
received_transactions = data[user]['received_transactions']
total_transactions = sent_transactions + received_transactions
print(f'Total transactions for {user} : {total_transactions}')
@classmethod
def total_credits_per_user(cls, data):
""" Print the total credits per user a specified ledger """
data = data[0]
for user in data:
received_totals = data[user]['received_totals']
print(f'Total credits for {user} : {received_totals}')
@classmethod
def total_debits_per_user(cls, data):
""" Print the total debits per user a specified ledger """
data = data[0]
for user in data:
sent_totals = data[user]['sent_totals']
print(f'Total debits for {user} : {sent_totals}')
@classmethod
def highest_balance_per_user(cls, data):
""" Print the highest balance per user a specified ledger """
data = data[0]
for user in data:
highest_balance = data[user]['highest_balance']
print(f'Highest balance for {user} : {highest_balance}')
@classmethod
def lowest_balance_per_user(cls, data):
""" Print the lowest balance per user a specified ledger """
data = data[0]
for user in data:
lowest_balance = data[user]['lowest_balance']
print(f'Lowest balance for {user} : {lowest_balance}')
@classmethod
def total_money_exchanged(cls, data):
""" Print the amount of money exchanged on a specified ledger """
total = 0
data = data[0]
for user in data:
sent_totals = data[user]['sent_totals']
total += sent_totals
print(f'Total money exchanged: {total}')
@classmethod
def average_transaction_value(cls, transactions):
""" Print the average transaction value on a specified ledger """
all_transaction_amounts = []
for transaction in transactions:
for i in transaction['transactions']:
if 'amount' in i:
all_transaction_amounts.append(i['amount'])
print(f'Average transaction value : {mean(all_transaction_amounts)}')
def createReport():
report_2 = Report('Report from JSON')
monies = report_2.load_all_transaction_files()
data = report_2.load_all_data_per_user()
report_2.total_transactions(monies)
report_2.total_cancelled_transactions(monies)
report_2.total_transactions_per_user(data)
report_2.total_credits_per_user(data)
report_2.total_debits_per_user(data)
report_2.highest_balance_per_user(data)
report_2.lowest_balance_per_user(data)
report_2.total_money_exchanged(data)
report_2.average_transaction_value(monies)
createReport()