Created
August 25, 2019 07:45
-
-
Save nivleshc/ca8b0002b7d319a5163c87249c9b784b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#read the contents ot pushpinLabelsTable and populate pushpinLabels_LabelNum and pushpinLabels_Label dict | |
def load_pushpinLabels_from_dynamodb(today): | |
global dict_pushpinLabel_to_desc_map | |
global dict_vehicleId_to_pushpinLabel_map | |
global lastpushpinLabel | |
rdate = today.strftime("%Y-%m-%d") | |
pushpinLabelsTableContents = pushpinLabelsTable.query( | |
IndexName='TripDate-GSI', | |
KeyConditionExpression='TripDate = :tripdate', | |
ExpressionAttributeValues={":tripdate":rdate} | |
) | |
#check to see if there were any items returned from the above query | |
if len(pushpinLabelsTableContents['Items']) > 0: | |
#there were some items returned | |
#the vehicle's label could be same across different vehicles however its id is unique for the day. For this reason | |
#we must store the vehicle id along with the pushpinLabel and pushpinLabelDesc | |
for item in pushpinLabelsTableContents['Items']: | |
pushpinLabel = item['pushpinLabel'] | |
pushpinLabelDesc = item['pushpinLabelDesc'] | |
vehicleId = item['VehicleId'] | |
inDynamodbTable = True #we will mark all entries that are being populated from dynamodb, so that when we update dynamodb | |
#we only write items that are missing, not everything. This saves on write capacity units | |
#the following fields were added progressively to dynamodb. Ensure that missing items doesn't break the code. Just add blanks if missing | |
try: | |
latitude = item['latitude'] | |
except: | |
latitude = "" | |
try: | |
longitude = item['longitude'] | |
except: | |
longitude = "" | |
try: | |
tripId = item['tripId'] | |
except: | |
tripId = "" | |
dict_pushpinLabel_to_desc_map[str(pushpinLabel)] = {"desc": str(pushpinLabelDesc), "vehicleId": str(vehicleId), "inDynamodbTable": inDynamodbTable, "latitude": latitude, "longitude": longitude, "tripId": tripId} | |
dict_vehicleId_to_pushpinLabel_map[str(vehicleId)] = {"label": str(pushpinLabel), "desc": str(pushpinLabelDesc)} | |
#if a higher pushpinLabel was found in pushpinLabels table, then set the lastPushpinLabel to this | |
if int(pushpinLabel) > int(lastpushpinLabel): | |
lastpushpinLabel = int(pushpinLabel) | |
#else there were no results for the above query, which means there were no entries in dynamodb table for today. Let's start fresh | |
print("load_pushpinLabels_from_dynamodb: Loaded ", len(pushpinLabelsTableContents['Items']), "pushpinLabels from dynamodb table. lastpushpinLabel:", lastpushpinLabel) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment