-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerywithfilter.py
111 lines (78 loc) · 4.36 KB
/
querywithfilter.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
from __future__ import print_function
import boto3
import re
from boto3.dynamodb.conditions import Key, Attr
import time
import datetime
from collections import defaultdict
from collections import Counter
from datetime import datetime
import decimal
from decimal import Decimal
import calendar
dynamodb=boto3.resource(service_name='dynamodb', region_name='eu-west-1')
table = dynamodb.Table("callstab3")
# query a index
# query a index
print ('Query an account calls start time :', time.strftime("%H:%M:%S"))
month_account_call_count = defaultdict(int)
year_account_call_count = defaultdict(int)
hour_account_call_count = defaultdict(int)
day_account_call_count = defaultdict(int)
response = table.query(
IndexName='accountid-calldate-index',
KeyConditionExpression=Key('accountid').eq("ACC-1230") , #& Key('calldate').between(1481205344, 1481275607)
# FilterExpression=Attr('calltype').eq("mobile")
)
rowcount = response['Count']
for record in response["Items"]:
try:
account = record["accountid"]
except:
account = 'NULL'
# location
try:
location = record["location"]
except:
location = 'UNSPECIFIED'
hour_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H"),"%Y-%m-%dT%H").utctimetuple()) # datetime.now().strftime("%Y-%m-%dT%H")
day_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d"),"%Y-%m-%d").utctimetuple()) #datetime.now().strftime("%Y-%m-%d")# week_event_time= calendar.timegm(datetime.strptime(datetime.now().strftime("%YW%W"),"%YW%W").utctimetuple()) #datetime.now().strftime("%YW%W")
month_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m"),"%Y-%m").utctimetuple()) #datetime.now().strftime("%Y-%m")
year_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y"),"%Y").utctimetuple())#datetime.now().strftime("%Y")
hour_account_call_count[(account, location,hour_event_time)] += 1
day_account_call_count[(account, location,day_event_time)] += 1
month_account_call_count[(account,location, month_event_time)] += 1
year_account_call_count[(account, location,year_event_time)] += 1
while 'LastEvaluatedKey' in response:
response = table.query(
IndexName='accountid-calldate-index',
KeyConditionExpression=Key('accountid').eq("ACC-1230") , #& Key('calldate').between(1481205344, 1481275607)
FilterExpression=Attr('calltype').eq("mobile")
)
rowcount = rowcount + response['Count']
for record in response["Items"]:
try:
account = record["accountid"]
except:
account = 'NULL'
# location
try:
location = record["location"]
except:
location = 'UNSPECIFIED'
hour_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%dT%H"),"%Y-%m-%dT%H").utctimetuple()) # datetime.now().strftime("%Y-%m-%dT%H")
day_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m-%d"),"%Y-%m-%d").utctimetuple()) #datetime.now().strftime("%Y-%m-%d")# week_event_time= calendar.timegm(datetime.strptime(datetime.now().strftime("%YW%W"),"%YW%W").utctimetuple()) #datetime.now().strftime("%YW%W")
month_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y-%m"),"%Y-%m").utctimetuple()) #datetime.now().strftime("%Y-%m")
year_event_time= calendar.timegm(datetime.strptime(datetime.fromtimestamp(int(record["calldate"])).strftime("%Y"),"%Y").utctimetuple())#datetime.now().strftime("%Y")
hour_account_call_count[(account, location,hour_event_time)] += 1
day_account_call_count[(account, location,day_event_time)] += 1
month_account_call_count[(account,location, month_event_time)] += 1
year_account_call_count[(account, location,year_event_time)] += 1
print ('Query an account calls end time :', time.strftime("%H:%M:%S"))
print('total record count: ' , rowcount)
print('hour record count: ' , len(hour_account_call_count))
print('day record count: ' , len(day_account_call_count))
print('month record count: ' , len(month_account_call_count))
print('year record count: ' , len(year_account_call_count))
# for key,val in hour_account_call_count.iteritems():
# print("%s, %s , %s= %s" % (key[0], key[1], key[2],val))