-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
executable file
·144 lines (115 loc) · 4.83 KB
/
parser.py
File metadata and controls
executable file
·144 lines (115 loc) · 4.83 KB
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
#!/usr/bin/env python3
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
import json
import sys
import datetime
def openCassandraSessionStagingTransaction():
auth_provider = PlainTextAuthProvider(
username='webhook_rw', password='Private_access_transactions')
cluster = Cluster(auth_provider=auth_provider)
session = cluster.connect('comchain')
return session
def openCassandraSession():
auth_provider = PlainTextAuthProvider(
username='transactions_rw', password='Private_access_transactions')
cluster = Cluster(auth_provider=auth_provider)
session = cluster.connect('comchain')
return session
session = openCassandraSession()
sessioStaging = openCassandraSessionStagingTransaction()
for line in sys.stdin:
if line == "true\n":
break
#print(line)
data = json.loads(line)
transaction = data['args']
transTime = transaction['time']
transInsertTime = transTime
try:
transFrom = transaction['from']
except:
transFrom = "Admin"
transTo = transaction['to']
try:
transRecieved = transaction['recieved']
except:
transRecieved = transaction['value']
try:
transSent = transaction['sent']
except:
transSent = transaction['recieved']
try:
transTax = transaction['tax']
except:
transTax = 0
try:
currency = data['currency']
except:
currency = ''
transEvent = data['event']
transHash = data['transactionHash']
transBlock = str(data['blockNumber'])
print(str(transTime) + " - Added transaction " + transHash + " on currency "+currency+" from block " + transBlock)
# Check if the transaction is in the pending transaction table (webshop_transactions)
cqlcommand = "SELECT hash, store_id, store_ref, wh_status, delegate, message_from, message_to, toTimestamp(now()) AS stamp, receivedat FROM webshop_transactions WHERE hash='{}'".format(transHash)
rows = sessioStaging.execute(cqlcommand)
additional_fields = []
additional_values = []
shop_tx = False
for row in rows:
# message
if hasattr(row, 'message_from') and row.message_from is not None:
additional_fields.append('message_from')
additional_values.append("'{}'".format(row.message_from))
if hasattr(row, 'message_to') and row.message_to is not None:
additional_fields.append('message_to')
additional_values.append("'{}'".format(row.message_to))
# delegate
if hasattr(row, 'delegate') and row.delegate is not None:
additional_fields.append('delegate')
additional_values.append("'{}'".format(row.delegate))
# webshop
if hasattr(row, 'store_id') and row.store_id is not None: # this is a webshop transaction
shop_tx = True
additional_fields.append('store_id')
additional_values.append("'{}'".format(row.store_id))
additional_fields.append('store_ref')
additional_values.append("'{}'".format(row.store_ref))
wh_status = row.wh_status
nb_attempt ='0'
if wh_status>1: # 2 failed attempt / 3 success
nb_attempt ='1'
additional_fields.append('wh_status')
additional_values.append(str(wh_status)) # New shop transction
additional_fields.append('tr_attempt_nb')
additional_values.append(nb_attempt)
additional_fields.append('tr_attempt_date')
diff = row.stamp - datetime.datetime(1970, 1, 1)
timestamp = int(diff.total_seconds())
additional_values.append("'{}'".format(str(timestamp-10800000)))
#insert time
if hasattr(row, 'receivedat') and row.receivedat is not None:
transInsertTime = row.receivedat
if not shop_tx:
additional_fields.append('wh_status')
additional_values.append('0') # Not a shop transction
if (currency!=''):
additional_fields.append('currency')
additional_values.append("'{}'".format(currency))
add_fields = ', '.join(additional_fields)
add_val = ', '.join(additional_values)
cqlcommand = "INSERT INTO testtransactions (add1, add2, status, hash, time, receivedAt, direction, recieved, sent, tax, type, block, {}) VALUES ('{}', '{}', {}, '{}', {}, {}, {}, {}, {}, {}, '{}', '{}', {}) IF NOT EXISTS"
cqlcommand_1 = cqlcommand.format(add_fields, transFrom, transTo, 0, transHash, transInsertTime, transTime, 1, transRecieved, transSent, transTax, transEvent, transBlock, add_val )
cqlcommand_2 = cqlcommand.format(add_fields, transTo, transFrom, 0, transHash, transInsertTime, transTime, 2, transRecieved, transSent, transTax, transEvent, transBlock, add_val )
#print(cqlcommand_1)
try:
session.execute(cqlcommand_1)
#print(cqlcommand_1)
except:
print("Error Executing:" + cqlcommand_1)
try:
session.execute(cqlcommand_2)
#print(cqlcommand_2)
except:
print("Error Executing:" + cqlcommand_2)