-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallcounter.py
344 lines (278 loc) · 12.6 KB
/
callcounter.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
import boto3
from boto3.dynamodb.conditions import Key
import time
import datetime
from collections import defaultdict
from collections import Counter
from datetime import datetime
import decimal
from decimal import Decimal
def trunkcalls():
dynamodb=boto3.resource(service_name='dynamodb', region_name='eu-west-1')
table = dynamodb.Table("callstab")
# query a index
print ('Collect a trunk call count start time :', time.strftime("%H:%M:%S"))
dd = datetime.fromtimestamp(int(Decimal('1480431567'))).strftime("%Y-%m-%dT%H")
response = table.scan(
IndexName='trunkid-calldate-index',
# KeyConditionExpression=Key('trunkid').eq(trunkid),
Limit=100000
)
hour_trunk_call_count = defaultdict(int)
day_trunk_call_count = defaultdict(int)
week_trunk_call_count = defaultdict(int)
month_trunk_call_count = defaultdict(int)
year_trunk_call_count = defaultdict(int)
for record in response["Items"]:
trunk = record["trunkid"]
# date and hour
hour_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H")
day_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d")
week_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%YW%W")
month_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m")
year_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y")
hour_trunk_call_count[(trunk, hour_event_time)] += 1
day_trunk_call_count[(trunk, day_event_time)] += 1
week_trunk_call_count[(trunk, week_event_time)] += 1
month_trunk_call_count[(trunk, month_event_time)] += 1
year_trunk_call_count[(trunk, year_event_time)] += 1
while 'LastEvaluatedKey' in response:
response = table.scan(
IndexName='trunkid-calldate-index',
ExclusiveStartKey=response['LastEvaluatedKey']
)
for record in response["Items"]:
trunk = record["trunkid"]
# date and hour
hour_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H")
day_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d")
week_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%YW%W")
month_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m")
year_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y")
hour_trunk_call_count[(trunk, hour_event_time)] += 1
day_trunk_call_count[(trunk, day_event_time)] += 1
week_trunk_call_count[(trunk, week_event_time)] += 1
month_trunk_call_count[(trunk, month_event_time)] += 1
year_trunk_call_count[(trunk, year_event_time)] += 1
#
# insert/update trunk call counts hourly
for key,val in hour_trunk_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_trunk_call_counter( key[0], key[1], int(val))
for key,val in day_trunk_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_trunk_call_counter(key[0], key[1], int(val))
for key,val in week_trunk_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_trunk_call_counter( key[0], key[1], int(val))
for key,val in month_trunk_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_trunk_call_counter( key[0], key[1], int(val))
for key,val in year_trunk_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_trunk_call_counter( key[0], key[1], int(val))
print ('Collect a trunk call count end time :', time.strftime("%H:%M:%S"))
def accountcalls():
dynamodb=boto3.resource(service_name='dynamodb', region_name='eu-west-1')
table = dynamodb.Table("callstab")
# query a index
print ('Collect an account call count start time :', time.strftime("%H:%M:%S"))
dd = datetime.fromtimestamp(int(Decimal('1480431567'))).strftime("%Y-%m-%dT%H")
response = table.scan(
IndexName='accountid-calldate-index',
Limit=100000
)
hour_account_call_count = defaultdict(int)
day_account_call_count = defaultdict(int)
week_account_call_count = defaultdict(int)
month_account_call_count = defaultdict(int)
year_account_call_count = defaultdict(int)
for record in response["Items"]:
account = record["accountid"]
# date and hour
hour_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H")
day_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d")
week_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%YW%W")
month_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m")
year_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y")
hour_account_call_count[(account, hour_event_time)] += 1
day_account_call_count[(account, day_event_time)] += 1
week_account_call_count[(account, week_event_time)] += 1
month_account_call_count[(account, month_event_time)] += 1
year_account_call_count[(account, year_event_time)] += 1
while 'LastEvaluatedKey' in response:
response = table.scan(
IndexName='accountid-calldate-index',
ExclusiveStartKey=response['LastEvaluatedKey']
)
for record in response["Items"]:
account = record["accountid"]
# date and hour
hour_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H")
day_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d")
week_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%YW%W")
month_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m")
year_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y")
hour_account_call_count[(account, hour_event_time)] += 1
day_account_call_count[(account, day_event_time)] += 1
week_account_call_count[(account, week_event_time)] += 1
month_account_call_count[(account, month_event_time)] += 1
year_account_call_count[(account, year_event_time)] += 1
#
# insert/update account call counts hourly
for key,val in hour_account_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_account_call_counter(key[0], key[1], int(val))
for key,val in day_account_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_account_call_counter(key[0], key[1], int(val))
for key,val in week_account_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_account_call_counter(key[0], key[1], int(val))
for key,val in month_account_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_account_call_counter(key[0], key[1], int(val))
for key,val in year_account_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_account_call_counter(key[0], key[1], int(val))
print ('Collect an account call count end time :', time.strftime("%H:%M:%S"))
def locationcalls():
dynamodb=boto3.resource(service_name='dynamodb', region_name='eu-west-1')
table = dynamodb.Table("callstab")
# query a index
print ('Collect a location call count start time :', time.strftime("%H:%M:%S"))
dd = datetime.fromtimestamp(int(Decimal('1480431567'))).strftime("%Y-%m-%dT%H")
response = table.scan(
IndexName='location-calldate-index',
Limit=100000
)
hour_location_call_count = defaultdict(int)
day_location_call_count = defaultdict(int)
week_location_call_count = defaultdict(int)
month_location_call_count = defaultdict(int)
year_location_call_count = defaultdict(int)
for record in response["Items"]:
location = record["location"]
# date and hour
hour_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H")
day_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d")
week_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%YW%W")
month_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m")
year_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y")
hour_location_call_count[(location, hour_event_time)] += 1
day_location_call_count[(location, day_event_time)] += 1
week_location_call_count[(location, week_event_time)] += 1
month_location_call_count[(location, month_event_time)] += 1
year_location_call_count[(location, year_event_time)] += 1
while 'LastEvaluatedKey' in response:
response = table.scan(
IndexName='location-calldate-index',
ExclusiveStartKey=response['LastEvaluatedKey']
)
for record in response["Items"]:
location = record["location"]
# date and hour
hour_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H")
day_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d")
week_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%YW%W")
month_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m")
year_event_time=datetime.fromtimestamp(int(record["calldate"])).strftime("%Y")
hour_location_call_count[(location, hour_event_time)] += 1
day_location_call_count[(location, day_event_time)] += 1
week_location_call_count[(location, week_event_time)] += 1
month_location_call_count[(location, month_event_time)] += 1
year_location_call_count[(location, year_event_time)] += 1
#
# # insert/update account call counts hourly
for key,val in hour_location_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_location_call_counter(key[0], key[1], int(val))
for key,val in day_location_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_location_call_counter(key[0], key[1], int(val))
for key,val in week_location_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_location_call_counter(key[0], key[1], int(val))
for key,val in month_location_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_location_call_counter(key[0], key[1], int(val))
for key,val in year_location_call_count.iteritems():
# print("%s, %s = %s" % (str(key[0]), str(key[1]), str(val)))
update_location_call_counter(key[0], key[1], int(val))
print ('Collect a location call count end time :', time.strftime("%H:%M:%S"))
# update the trunk hourly call counts
def update_trunk_call_counter(trunk, event_datetime, event_count=1, dynamodb = boto3.resource(service_name='dynamodb', region_name='eu-west-1')):
table = dynamodb.Table('trunk_call_count')
# delete the item
response = table.delete_item(
Key={
'trunkid': trunk,
'date_sort': event_datetime
}
)
# insert the item
response = table.update_item(
Key={
'trunkid': trunk,
'date_sort': event_datetime
},
ExpressionAttributeValues={":value":event_count},
UpdateExpression="ADD callcount :value")
# update the account hourly call counts
def update_account_call_counter(account, event_datetime, event_count=1, dynamodb = boto3.resource(service_name='dynamodb', region_name='eu-west-1')):
table = dynamodb.Table('account_call_count')
# delete the item
response = table.delete_item(
Key={
'accountid': account,
'date_sort': event_datetime
}
)
# insert the item
response = table.update_item(
Key={
'accountid': account,
'date_sort': event_datetime
},
ExpressionAttributeValues={":value":event_count},
UpdateExpression="ADD callcount :value")
# update the account hourly call counts
def update_location_call_counter(location, event_datetime, event_count=1, dynamodb = boto3.resource(service_name='dynamodb', region_name='eu-west-1')):
table = dynamodb.Table('location_call_count')
# delete the item
response = table.delete_item(
Key={
'location': location,
'date_sort': event_datetime
}
)
# insert the item
response = table.update_item(
Key={
'location': location,
'date_sort': event_datetime
},
ExpressionAttributeValues={":value":event_count},
UpdateExpression="ADD callcount :value")
if __name__ == '__main__':
var = ""
while not var == "x":
print ("")
print ("")
print ("Please select a query:")
print ("")
print ("------------------------------------------")
print("(a) collect trunk call hour")
print("(b) collect account call hour")
# print("(c) collect account call hour")
print("(x) to exit")
print ("------------------------------------------")
print ("")
var = raw_input("")
# try:
if var == "a":
trunkcalls()
if var == "b":
accountcalls()
# if var == "c":
# locationcalls()