forked from iryonetwork/IryoAirdrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
387 lines (352 loc) · 14.4 KB
/
storage.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# pip install azure-storage
import azure
from azure.storage.table import TableService, Entity
from azure.storage.file import FileService, ContentSettings
from datetime import datetime, timedelta, time
import traceback
import logging
TIME_DURATION_LIMIT = 15
table_service = TableService(account_name='accountNameXXXXX', account_key='Big secret:)')
def createDatabases():
try:
if table_service.exists('participants') == False:
table_service.create_table('participants')
if table_service.exists('participantReferrer') == False:
table_service.create_table('participantReferrer')
except Exception as e:
logging.error(traceback.format_exc())
def addNewParticipant(id, address, ownReferralLink, isConfirmed, isCompleted = False, invitedReferral = ''):
try:
task = Entity()
task.PartitionKey = 'participant'
task.RowKey = "participant_" + str(id)
task.userID = id
task.ethereumAddress = address
task.referral = ownReferralLink
task.invitedReferralLink = invitedReferral
task.isConfirmed = isConfirmed
task.isCompleted = isCompleted
task.datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
task.version = 1
table_service.insert_or_replace_entity('participants', task)
except Exception as e:
logging.error(traceback.format_exc())
def updateParticipantComplete(id, referrerCode = ''):
try:
if referrerCode != '':
addReferralToMainUser(id, referrerCode)
else:
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
item.isCompleted = True
item.isConfirmed = True
table_service.insert_or_replace_entity('participants', item)
except Exception as e:
logging.error(traceback.format_exc())
def addReferralToMainUser(id, referralLink):
try:
task = Entity()
task.PartitionKey = 'referral_' + referralLink
task.RowKey = "referral_" + str(id)
task.userID = id
task.referral = referralLink
task.datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
task.version = 1
table_service.insert_or_replace_entity('participantReferrer', task)
#update participant
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
item.isConfirmed = True
item.isCompleted = True
item.invitedReferralLink = referralLink
table_service.insert_or_replace_entity('participants', item)
except Exception as e:
logging.error(traceback.format_exc())
def addEOSToMainUser(id, eosAddress):
try:
#update participant
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
item.eosAddress = eosAddress
table_service.insert_or_replace_entity('participants', item)
except Exception as e:
logging.error(traceback.format_exc())
def isReferralTimeCorrect(id):
try:
now = datetime.now()
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
itemDate = datetime.strptime(item.datetime, "%Y-%m-%d %H:%M:%S")
itemDatePlusLimit = itemDate + timedelta(minutes = TIME_DURATION_LIMIT)
return True if now < itemDatePlusLimit else False
except Exception as e:
logging.error(traceback.format_exc())
def importALotOfRows():
for a in range(1,1100):
task = Entity()
task.PartitionKey = 'participant'
task.RowKey = "participant_" + str(a)
task.userID = a
task.datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
task.version = a
table_service.insert_or_replace_entity('participants', task)
def getall():
i = 0
next_pk = None
next_rk = None
part_k = "PartitionKey eq 'participant'"
counter = 0
while True:
entities = table_service.query_entities('participants', filter=part_k, next_partition_key=next_pk, next_row_key=next_rk, top=1000)
i += 1
for ent in entities:
counter = counter + 1
if hasattr(entities, 'x_ms_continuation'):
x_ms_continuation = getattr(entities, 'x_ms_continuation')
next_pk = x_ms_continuation['nextpartitionkey']
next_rk = x_ms_continuation['nextrowkey']
else:
break;
return counter
def GetData():
keyMarkers = {}
keyMarkers['nextpartitionkey'] = 0
keyMarkers['nextrowkey'] = 0
#b=[]
counter = 0
while True:
#get a batch of data
a = table_service.query_entities(table_name="participants", filter="PartitionKey eq 'participant'" ,num_results=1000 ,marker=keyMarkers)
#copy results to list
for item in a.items:
counter = counter + 1
#check to see if more data is available
if len(a.next_marker) == 0:
del a
break
#if more data available setup current position
keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
keyMarkers['nextrowkey'] = a.next_marker['nextrowkey']
#house keep temp storage
del a
#return final list
return counter
def isReferralExists(referral):
try:
#entities = table_service.query_entities('participants', filter="PartitionKey eq 'participant'")
#for entity in entities:
# if entity.referral == referral:
# return True
#return False
keyMarkers = {}
keyMarkers['nextpartitionkey'] = 0
keyMarkers['nextrowkey'] = 0
while True:
# get a batch of data
a = table_service.query_entities(table_name="participants", filter="PartitionKey eq 'participant'",
num_results=1000, marker=keyMarkers)
# copy results to list
for item in a.items:
try:
if item.referral == referral:
return True
except Exception as e:
print("No referral found")
# check to see if more data is available
if len(a.next_marker) == 0:
del a
break
# if more data available setup current position
keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
keyMarkers['nextrowkey'] = a.next_marker['nextrowkey']
# house keep temp storage
del a
return False
except Exception as e:
print (traceback.format_exc())
return False
def stat():
try:
keyMarkers = {}
keyMarkers['nextpartitionkey'] = 0
keyMarkers['nextrowkey'] = 0
statDict= {}
while True:
# get a batch of data
a = table_service.query_entities(table_name="participants", filter="PartitionKey eq 'participant'",
num_results=1000, marker=keyMarkers)
# copy results to list
for item in a.items:
itemDate = datetime.strptime(item.datetime, "%Y-%m-%d %H:%M:%S")
printDate = itemDate.strftime('%d/%m/%Y')
cameWithReferral = 0
try:
if hasattr(item, 'invitedReferralLink') and item.invitedReferralLink != '':
cameWithReferral = 1
except azure.common.AttributeError:
cameWithReferral = 0
if printDate not in statDict:
statDict[printDate] = (1, cameWithReferral)
else:
statDict[printDate] = (statDict[printDate][0] + 1, statDict[printDate][1] + cameWithReferral)
# check to see if more data is available
if len(a.next_marker) == 0:
del a
break
# if more data available setup current position
keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
keyMarkers['nextrowkey'] = a.next_marker['nextrowkey']
# house keep temp storage
del a
return statDict
except Exception as e:
logging.error(traceback.format_exc())
def isUserExists(id):
try:
try:
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
return True
except azure.common.AzureMissingResourceHttpError:
return False
except Exception as e:
logging.error(traceback.format_exc())
def isEOSAddressExists(id):
try:
try:
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
return item.eosAddress
except azure.common.AzureMissingResourceHttpError:
return ""
except Exception as e:
return ""
def getReferralLink(id):
try:
try:
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
return item.referral
except azure.common.AzureMissingResourceHttpError:
return ""
except Exception as e:
logging.error(traceback.format_exc())
def getMyReferral(id):
try:
try:
item = table_service.get_entity('participants', 'participant', "participant_" + str(id))
return item.invitedReferralLink
except azure.common.AzureMissingResourceHttpError:
return ""
except Exception as e:
logging.error(traceback.format_exc())
def getReferralCount(id):
try:
try:
referralLink = getReferralLink(id)
if referralLink == "":
return 0
entities = table_service.query_entities('participantReferrer', filter="PartitionKey eq 'referral_"+ referralLink + "'")
return len(entities.items)
except azure.common.AzureMissingResourceHttpError:
return ""
except Exception as e:
logging.error(traceback.format_exc())
def getParticipantsCount():
try:
totalSize = 0
keyMarkers = {}
keyMarkers['nextpartitionkey'] = 0
keyMarkers['nextrowkey'] = 0
while True:
# get a batch of data
a = table_service.query_entities(table_name="participants", filter="PartitionKey eq 'participant'",
num_results=1000, marker=keyMarkers)
totalSize += len(a.items)
# check to see if more data is available
if len(a.next_marker) == 0:
del a
break
# if more data available setup current position
keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
keyMarkers['nextrowkey'] = a.next_marker['nextrowkey']
# house keep temp storage
del a
return totalSize
except Exception as e:
return 0
def getParticipantsCountOverLimit():
try:
LIMIT = 22500
totalSize = 0
keyMarkers = {}
keyMarkers['nextpartitionkey'] = 0
keyMarkers['nextrowkey'] = 0
while True:
# get a batch of data
a = table_service.query_entities(table_name="participants", filter="PartitionKey eq 'participant'",
num_results=1000, marker=keyMarkers)
totalSize += len(a.items)
# check to see if more data is available
if len(a.next_marker) == 0:
del a
break
# if more data available setup current position
keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
keyMarkers['nextrowkey'] = a.next_marker['nextrowkey']
# house keep temp storage
del a
return True if totalSize > LIMIT else False
except Exception as e:
return 0
def getParticipantsCountTodayOverLimit():
try:
LIMIT = 2000
totalSize = 0
today = datetime.now().strftime("%Y-%m-%d")
curentTime = datetime.now().time()
restartTime = time(12)
if curentTime < restartTime:
yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")
filter = "PartitionKey eq 'participant' and Timestamp ge datetime'" + yesterday + "T12:00:00' and Timestamp le datetime'" + today + "T11:59:00'"
else:
tomorrow = (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d")
filter= "PartitionKey eq 'participant' and Timestamp ge datetime'" + today + "T12:00:00' and Timestamp le datetime'" + tomorrow + "T11:59:00'"
keyMarkers = {}
keyMarkers['nextpartitionkey'] = 0
keyMarkers['nextrowkey'] = 0
while True:
# get a batch of data
a = table_service.query_entities(table_name="participants",
filter=filter,
num_results=1000, marker=keyMarkers)
totalSize += len(a.items)
# check to see if more data is available
if len(a.next_marker) == 0:
del a
break
# if more data available setup current position
keyMarkers['nextpartitionkey'] = a.next_marker['nextpartitionkey']
keyMarkers['nextrowkey'] = a.next_marker['nextrowkey']
# house keep temp storage
del a
return True if totalSize > LIMIT else False
except Exception as e:
return 0
"""
try:
LIMIT = 999
today = datetime.now().strftime("%Y-%m-%d")
keyMarkers = {}
# get a batch of data
a = table_service.query_entities(table_name="participants", filter="PartitionKey eq 'participant' and Timestamp ge datetime'" + today + "T00:00:00' and Timestamp le datetime'" + today + "T23:59:00'",
num_results=1000, marker=keyMarkers)
return True if len(a.items) > LIMIT else False
except Exception as e:
return 0"""
if __name__ == '__main__':
a = getParticipantsCountTodayOverLimit()
print (a)
#importALotOfRows()
#print(str(GetData()))
#statistic = stat()
#print ("Date | New users (with referrals)")
#for item in statistic:
# print(str(item) + " | " + str(statistic[item][0]) + " (" + str(statistic[item][1]) + ")")
#createDatabases()
#addNewParticipant(1234, '0xgjsdghsdfgsdf', "5hu8d", "7fkdjd")
#addReferralToMainUser(3453, "7fkdjd")
#print ("bula: " + str(getReferralCount(1234)))