-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLFMakeMatch.py
More file actions
119 lines (103 loc) · 3.47 KB
/
LFMakeMatch.py
File metadata and controls
119 lines (103 loc) · 3.47 KB
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
# This file is designed for the lambda function to make matches
# in the project CUThen, the final project for COMSE6998_010_2023_3,
# Topics in Computer Science: Cloud Computing and Big Data.
import os
import json
import string
import boto3
from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
from botocore.exceptions import ClientError
REGION = 'us-east-1'
HOST = 'search-cuthen-temp-5fyo5fvs7x7t2myle4ztwa7swa.us-east-1.es.amazonaws.com'
INDEX1 = 'user_to_group'
INDEX2 = 'user_to_inv'
INDEX3 = 'group_to_user'
def get_awsauth(region, service):
cred = boto3.Session().get_credentials()
return AWS4Auth(cred.access_key,
cred.secret_key,
region,
service,
session_token=cred.token)
def lookup_data(key, db=None, table='user_table'):
if not db:
db = boto3.resource('dynamodb')
table = db.Table(table)
try:
response = table.get_item(Key=key)
except ClientError as e:
print('Error', e.response['Error']['Message'])
return False
else:
ret = response['Item']
ret['user_id'] = int(ret['user_id'])
return ret
def scan_data(db=None, table='user_table'):
if not db:
db = boto3.resource('dynamodb')
table = db.Table(table)
try:
response = table.scan()
except ClientError as e:
print('Error', e.response['Error']['Message'])
return False
else:
ret = response['Items']
return ret
def query(index, field, term):
q = {"query": {
"bool": {
"must": {
"match": {
field: term
}
}
}
}
}
client = OpenSearch(hosts=[{
'host': HOST,
'port': 443
}],
http_auth=get_awsauth(REGION, 'es'),
use_ssl=True,
verify_certs=True,
connection_class=RequestsHttpConnection)
res = client.search(index=index, body=q)
print(res)
hits = res['hits']['hits']
return hits[0]['_source']
def get_user(userId):
user = {}
userInfo = lookup_data(key = userId, table='user_table')
user['userId'] = userInfo['user_id']
user['userName'] = userInfo['first_name'] + ' ' + userInfo['last_name']
user['userFeatures'] = [{k: v} for k, v in userInfo.items()]
return user
def get_group(groupId):
group = {}
gobj = query(index=INDEX3, field='group_id', term=str(groupId))
gleader = get_user({"user_id": gobj['leader_id']})
gmember = []
for mid in gobj['user_id']:
gmember.append(get_user({"user_id": mid}))
group['groupId'] = int(groupId)
group['groupLeader'] = gleader
group['groupMembers'] = gmember
return group
def lambda_handler(event, context):
print(event)
currentUser = get_user({"user_id": int(event['userId'])})
all_users = scan_data(table='user_table')
print(f"All users: {all_users}")
print(f"Current user: {currentUser}")
other_users = [user for user in all_users if int(user['user_id']) != int(currentUser['userId'])]
print(f"Other users: {other_users}")
for user in other_users:
user['user_id'] = str(user['user_id']) # Decimals are not JSON serializable so convert to string
return {
'statusCode': 200,
"headers": {"Access-Control-Allow-Origin": "*"},
'body': json.dumps(other_users)
}