-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_fn_trunkstats.py
86 lines (67 loc) · 3.01 KB
/
lambda_fn_trunkstats.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
from __future__ import print_function
import boto3
import re
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
# update the trunk call counts
def update_trunk_call_counter(trunk, location, calltype, event_datetime, event_count=1, dynamodb = boto3.resource(service_name='dynamodb', region_name='eu-west-1')):
table = dynamodb.Table('call_stats')
# insert the item
response = table.update_item(
Key={
'entityid': trunk,
'date_sort': event_datetime
},
ExpressionAttributeValues={":value":event_count},
UpdateExpression="ADD AA_TOTALS_COUNT :value, %s :value , %s :value" % (location, calltype)
)
def lambda_handler(event, context):
week_account_call_count = defaultdict(int)
month_account_call_count = defaultdict(int)
year_account_call_count = defaultdict(int)
hour_account_call_count = defaultdict(int)
day_account_call_count = defaultdict(int)
# set the date dorts
hour_event_time= calendar.timegm(datetime.strptime(datetime.now().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.now().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.now().strftime("%Y-%m"),"%Y-%m").utctimetuple()) #datetime.now().strftime("%Y-%m")
year_event_time= calendar.timegm(datetime.strptime(datetime.now().strftime("%Y"),"%Y").utctimetuple())#datetime.now().strftime("%Y")
timenow = datetime.now()
print("number of records in this lamda call: ", len(event['Records']))
for record in event['Records']:
# get the variables from the event image
# account
try:
trunk = record['dynamodb']["NewImage"]["trunkid"]["S"]
except:
trunk = 'NULL'
# # location
try:
location = record['dynamodb']["NewImage"]["location"]["S"]
except:
location = 'UNSPECIFIED'
# calltype
try:
calltype = record['dynamodb']["NewImage"]["calltype"]["S"]
except:
calltype = 'NoCallType'
# hour_event_time=datetime.now().strftime("%Y-%m-%dT%H")
# day_event_time=datetime.now().strftime("%Y-%m-%d")
# week_event_time=datetime.now().strftime("%YW%W")
# month_event_time=datetime.now().strftime("%Y-%m")
# year_event_time=datetime.now().strftime("%Y")
update_trunk_call_counter( trunk, location,calltype,hour_event_time, 1)
update_trunk_call_counter( trunk, location,calltype,day_event_time, 1)
# update_trunk_call_counter( trunk, location,calltype,week_event_time, 1)
update_trunk_call_counter( trunk, location,calltype,month_event_time, 1)
update_trunk_call_counter( trunk, location,calltype,year_event_time, 1)
donetime = datetime.now()
print("setting dict: " , donetime - timenow)
return True