-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
246 lines (222 loc) · 9.91 KB
/
Copy pathlambda_function.py
File metadata and controls
246 lines (222 loc) · 9.91 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import json
import logging
import urllib.parse
import boto3
import requests
from typing import Dict, Any, List
logger = logging.getLogger()
logger.setLevel(logging.INFO)
secretsmanager = boto3.client('secretsmanager')
TINYBIRD_SECRET_NAME = 'plain-customer-card-tb-token'
TINYBIRD_ENDPOINT = 'https://api.europe-west2.gcp.tinybird.co/v0/pipes/plain_customer_card_lookup.json'
def get_tinybird_token() -> str:
try:
logger.info(f"Fetching secret: {TINYBIRD_SECRET_NAME}")
response = secretsmanager.get_secret_value(SecretId=TINYBIRD_SECRET_NAME)
secret = json.loads(response['SecretString'])
token = secret[TINYBIRD_SECRET_NAME]
logger.info("Successfully retrieved Tinybird token")
return token
except Exception as e:
logger.error(f"Failed to retrieve Tinybird token: {str(e)}")
raise Exception(f"Failed to retrieve Tinybird token: {str(e)}")
def fetch_customer_data(email: str, token: str) -> Dict[str, Any]:
try:
logger.info(f"Fetching customer data for email: {email}")
params = {
'user_email': email,
'token': token
}
response = requests.get(TINYBIRD_ENDPOINT, params=params, timeout=10)
response.raise_for_status()
data = response.json()
logger.info(f"Tinybird response - rows: {data.get('rows', 0)}, data items: {len(data.get('data', []))}")
logger.debug(f"Full Tinybird response: {json.dumps(data)}")
return data
except requests.RequestException as e:
logger.error(f"Failed to fetch data from Tinybird: {str(e)}")
raise Exception(f"Failed to fetch data from Tinybird: {str(e)}")
def build_plain_response(tinybird_data: Dict[str, Any]) -> Dict[str, Any]:
if not tinybird_data.get('data') or len(tinybird_data['data']) == 0:
logger.warning("No customer data found in Tinybird response")
return {
"cards": [{
"key": "customer-info",
"timeToLiveSeconds": 86400,
"components": [{
"componentText": {
"text": "No customer data found",
"textColor": "MUTED"
}
}]
}]
}
customer = tinybird_data['data'][0]
logger.info(f"Building Plain response for customer: {customer.get('organization_name', 'N/A')}")
is_dedicated = customer.get('dedicated_clusters_url', '').lower() == 'yes'
return {
"cards": [{
"key": "customer-info",
"timeToLiveSeconds": 86400,
"components": [{
"componentContainer": {
"containerContent": [
{
"componentRow": {
"rowMainContent": [
{
"componentText": {
"text": customer.get('organization_name', 'N/A')
}
},
{
"componentText": {
"text": customer.get('plan_name', 'N/A'),
"textColor": "MUTED"
}
}
],
"rowAsideContent": [
{
"componentLinkButton": {
"linkButtonLabel": "Organization ↗",
"linkButtonUrl": customer.get('organization_url', '#')
}
}
]
}
},
{
"componentDivider": {
"dividerSpacingSize": "M"
}
},
{
"componentRow": {
"rowMainContent": [
{
"componentText": {
"text": "Region",
"textSize": "S",
"textColor": "MUTED"
}
},
{
"componentText": {
"text": customer.get('region', 'N/A')
}
}
],
"rowAsideContent": [
{
"componentBadge": {
"badgeLabel": f"Dedicated cluster: {'YES' if is_dedicated else 'NO'}",
"badgeColor": "GREEN" if is_dedicated else "RED"
}
}
]
}
},
{
"componentSpacer": {
"spacerSize": "M"
}
},
{
"componentRow": {
"rowMainContent": [
{
"componentText": {
"text": "Customer created at",
"textSize": "S",
"textColor": "MUTED"
}
},
{
"componentText": {
"text": customer.get('customer_created_at', 'N/A')
}
}
],
"rowAsideContent": [
{
"componentText": {
"text": "Contract duration (months)",
"textSize": "S",
"textColor": "MUTED"
}
},
{
"componentText": {
"text": customer.get('current_contract_duration_in_months', 'N/A')
}
}
]
}
},
{
"componentSpacer": {
"spacerSize": "M"
}
},
{
"componentRow": {
"rowMainContent": [
{
"componentText": {
"text": "Active orgs",
"textSize": "S",
"textColor": "MUTED"
}
},
{
"componentText": {
"text": customer.get('active_orgs', 'N/A') or 'N/A'
}
}
],
"rowAsideContent": []
}
}
]
}
}]
}]
}
def lambda_handler(event, context):
try:
logger.info("=== Lambda invocation started ===")
logger.info(f"Raw event: {json.dumps(event)}")
if 'body' in event:
logger.info("Event has 'body' key, parsing as Function URL request")
body = json.loads(event['body'])
else:
logger.info("Event has no 'body' key, using event directly")
body = event
logger.info(f"Parsed body: {json.dumps(body)}")
customer_email = body.get('customer', {}).get('email')
if not customer_email:
logger.error(f"No email found. Customer object: {body.get('customer', {})}")
return {
'statusCode': 400,
'body': json.dumps({'error': 'Customer email not found in payload'})
}
logger.info(f"Extracted customer email: {customer_email}")
token = get_tinybird_token()
tinybird_data = fetch_customer_data(customer_email, token)
plain_response = build_plain_response(tinybird_data)
logger.info("Successfully generated Plain response")
logger.debug(f"Plain response: {json.dumps(plain_response)}")
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps(plain_response)
}
except Exception as e:
logger.error(f"Error in lambda_handler: {str(e)}", exc_info=True)
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}