-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extra2.py
171 lines (155 loc) · 5.44 KB
/
Extra2.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
# *********************************************************************************************
# Program to update dynamodb with latest data from mta feed. It also cleans up stale entried from db
# Usage python dynamodata.py
# *********************************************************************************************
import json,time,sys
from collections import OrderedDict
import threading
import time
import boto3
from boto3.dynamodb.conditions import Key,Attr
from decimal import Decimal
sys.path.append('../utils')
import tripupdate,vehicle,alert,mtaUpdates,aws
### YOUR CODE HERE ####
dynamodb = aws.getResource('dynamodb','us-east-1')
#endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
#dynamodb = aws.getResource('dynamodb','us-east-1')
# Create the DynamoDB table.
try:
mta = dynamodb.create_table(
TableName='mta',
KeySchema=[
{
'AttributeName': 'tripId',
'KeyType': 'HASH'
},
{
'AttributeName': 'routeId',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{ 'AttributeName':'tripId',
'AttributeType':'S'
},
{
'AttributeName': 'routeId',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 15,
'WriteCapacityUnits': 15
}
)
mta.meta.client.get_waiter('table_exists').wait(TableName='mta')
except:
print "Table already exists!!!"
mta = dynamodb.Table('mta')
with open('/home/ec2-user/Lab3/key.txt','rb') as keyfile:
APIKEY = keyfile.read().rstrip('\n')
keyfile.close()
mutex = threading.Lock()
def add():
try:
while True:
print 'Start to add!'
#global APIKEY
#time.sleep(30)
t = {}
t['routeId'] = 'N/A'
t['startDate'] = 'N/A'
t['direction'] = 'N/A'
t['futureStopData'] = 'N/A'
t['currentStopId'] = 'N/A'
t['currentStopStatus'] = 'N/A'
t['vehicleTimeStamp'] = 'N/A'
t['timeStamp'] = time.time()
if mutex.acquire():
m = mtaUpdates.mtaUpdates(APIKEY)
tmp = m.getTripUpdates()
trips = tmp[0]
#print trips
global mta
#with mta.batch_writer() as batch:
for key in trips:
#print key
for k in trips[key]:
if k == 'routeId':
t['routeId'] = trips[key]['routeId']
if k == 'startDate':
t['startDate'] = trips[key]['startDate']
if k == 'direction':
t['direction'] = trips[key]['direction']
if k == 'futureStopData':
t['futureStopData'] = trips[key]['futureStopData']
if k == 'currentStopId':
t['currentStopId'] = trips[key]['currentStopId']
if k == 'currentStopStatus':
t['currentStopStatus'] = trips[key]['currentStopStatus']
if k == 'vehicleTimeStamp':
t['vehicleTimeStamp'] = trips[key]['vehicleTimeStamp']
if k == 'timeStamp':
t['timeStamp'] = trips[key]['timeStamp']
#with mta.batch_writer() as batch:
mta.put_item(
Item={
'tripId': str(key),
'routeId': str(t['routeId']),
'startDate': t['startDate'],
'direction': t['direction'],
'futureStopData': t['futureStopData'],
'currentStopId': t['currentStopId'],
'vehicleTimeStamp': t['vehicleTimeStamp'],
'currentStopStatus': t['currentStopStatus'],
'timeStamp': Decimal(t['timeStamp'])
})
print " Add Done!"
mutex.release()
time.sleep(30)
except KeyboardInterrupt:
print 'Stop'
except:
print 'Wrong add'
def clean():
try:
while True:
print 'Start to clean!!'
time.sleep(60)
if mutex.acquire():
t = Decimal(time.time() - 120)
response = mta.scan(
FilterExpression=Attr('timeStamp').lt(t)
)
items = response['Items']
for item in items:
hkey = item['tripId']
rkey = item['routeId']
mta.delete_item(
Key = {
'tripId':hkey,
'routeId':rkey,
}
)
print 'Delete done!'
mutex.release()
time.sleep(60)
except KeyboardInterrupt:
print 'Stop '
except:
print 'Wrong Clean!!!'
try:
flag=True
while True:
if flag==True:
c = threading.Thread(target=add)
c.setDaemon(True)
c.start()
cc = threading.Thread(target=clean)
cc.setDaemon(True)
cc.start()
flag=False
except KeyboardInterrupt:
exit
print 'Main exit!!!'