-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
223 lines (186 loc) · 8.21 KB
/
lambda_function.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
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
import os
import pandas as pd
import boto3
import json
from openpyxl import Workbook
from openpyxl.styles import Font
from openpyxl.styles import NamedStyle
from openpyxl.utils import get_column_letter
from io import StringIO
def perform_tangerine_transformation(input_filename, fileContents):
input_dir = "transactions"
output_dir = "transformed"
try:
input_filepath = os.path.join(input_dir, input_filename)
output_filepath = '/tmp/' + input_filename.replace(".csv", "_transformed.xlsx")
stringIO = StringIO(fileContents)
df = pd.read_csv(stringIO)
wb = Workbook()
ws = wb.active
headers = ["", "Item", "Date", "Amount (CAD)", "Split", "Category"]
ws.append(headers)
for cell in ws[1]:
cell.font = Font(bold=True)
rowIndex = 2
for index, row in df.iterrows():
item = row["Name"]
date = pd.to_datetime(row["Transaction date"]).date()
amount_cad = float(row["Amount"])
decided_split = f"=D{rowIndex}/2"
category = "n/a"
rowIndex = rowIndex + 1
ws.append(["", item, date, amount_cad, decided_split, category])
for column_cells in ws.columns:
max_length = max(len(str(cell.value)) for cell in column_cells)
adjusted_width = min((max_length + 2) * 1.2, 35)
column_letter = get_column_letter(column_cells[0].column)
ws.column_dimensions[column_letter].width = adjusted_width
for _ in range(3):
ws.append([])
ws.append(["Total", None, None, "=SUM(D2:D" + str(rowIndex) + ")", "=SUM(E2:E" + str(rowIndex) + ")"])
ws.append([])
wb.save(output_filepath)
print(f"Completed processing {input_filename}. Saved {output_filepath} to storage")
return output_filepath
except Exception as e:
print(f"An error occured during transformation: {str(e)}")
return None
def perform_transformations(input_filename, fileContents):
# Input and output directories
input_dir = "transactions"
output_dir = "transformed"
try:
input_filepath = os.path.join(input_dir, input_filename)
output_filepath = '/tmp/' + input_filename.replace(".csv", "_transformed.xlsx")
# Read the CSV file
stringIO = StringIO(fileContents)
df = pd.read_csv(stringIO)
# Create a new Excel workbook and select the active sheet
wb = Workbook()
ws = wb.active
# Write the transformed headers to the Excel sheet
headers = ["", "Item", "Date", "Amount (CAD)", "Decided Split", "Category"]
ws.append(headers)
for cell in ws[1]:
cell.font = Font(bold=True)
# Perform transformations and write to Excel sheet
rowIndex = 2
for index, row in df.iterrows():
if row["Category"] in [
"Transfer",
"Deposit",
"Credit Card Payment",
"Hide from Budgets & Trends",
"Bank Fee",
"Income",
"Investments",
"Interest Income",
"Mortgage & Rent",
"Parking",
"TFSA Investment",
"Subscriptions"
"Mobile Phone",
"Books",
"Video Games",
"Canada Student Loan",
"Alberta Student Loan"]:
continue
item = row["Description"]
date = pd.to_datetime(row["Date"]).date()
amount_cad = float(row["Amount"])
decided_split = f"=D{rowIndex}/2" # Referencing Amount (CAD) cell
category = row["Category"]
rowIndex = rowIndex + 1
ws.append(["",item, date, amount_cad, decided_split, category])
# Auto-fit column widths
for column_cells in ws.columns:
max_length = max(len(str(cell.value)) for cell in column_cells)
adjusted_width = min((max_length + 2) * 1.2, 35) # Adding some padding and scaling
column_letter = get_column_letter(column_cells[0].column)
ws.column_dimensions[column_letter].width = adjusted_width
# Add three rows of space at the bottom
for _ in range(3):
ws.append([])
# Add "Total" row
ws.append(["Total", None, None, "=SUM(D2:D" + str(rowIndex) + ")", "=SUM(E2:E" + str(rowIndex) + ")"])
# Add blank row
ws.append([])
# Save the Excel workbook
wb.save(output_filepath)
print(f"Completed processing {input_filename}. Saved {output_filepath} to storage")
return output_filepath
except Exception as e:
print(f"An error occurred during transformations: {str(e)}")
return None
def handler (event, context):
print("Cassady new test")
try:
is_dev_env = os.environ.get('IS_DEV', False)
if (is_dev_env):
print("Environment is dev")
# Access AWS credentials from environment variables
aws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID')
aws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
aws_region = os.environ.get('AWS_DEFAULT_REGION')
s3 = boto3.client(
's3',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name='ca-central-1'
)
else:
print("Environment is not dev")
s3 = boto3.client(
's3',
)
s3 = boto3.client('s3')
# Define your input and output bucket names
input_bucket_name = 'mint-transactions'
output_bucket_name = 'mint-transformed'
# List all objects in the input S3 bucket
prefix = 'transactions/'
response = s3.list_objects_v2(Bucket=input_bucket_name, Prefix=prefix)
# print(f"Contents: {response.get('Contents', [])}" )
print(f"Going through object item in bucket: {input_bucket_name}")
for obj in response.get('Contents', []):
object_key = obj['Key']
file_name = os.path.basename(object_key)
if not file_name.lower().endswith('.csv'):
print(f"Skipping this object in bucket: {file_name}")
continue
# Download CSV file from input bucket
# Extract the file name using os.path.basename
if file_name.lower().endswith('.csv'):
response = s3.get_object(Bucket=input_bucket_name, Key=object_key)
print(f"Processing file: {file_name}")
csv_content = response['Body'].read().decode('utf-8')
if 'tangerine' in file_name.lower():
print(f"Processing tangerine file: {file_name}")
transformed_data = perform_tangerine_transformation(file_name, csv_content)
else:
transformed_data = perform_transformations(file_name, csv_content)
newFileName = file_name.replace("_transactions.csv", "_transformed.xlsx")
s3.upload_file(transformed_data, input_bucket_name, f'transformed/{newFileName}')
print(f"Saved to S3")
response = {
"statusCode": 200,
"body": json.dumps({"message": "Done processing all objects successfully!"})
}
try:
print("**********************************************************************")
print("Attempting to email subscribers...")
sns_client = boto3.client('sns')
topic_arn = os.environ.get('SNS_TOPIC')
# Publish to SNS
sns_client.publish(
TopicArn=topic_arn,
Message="Done processing all objects successfully!\n Download the transactions on S3 and apply any changes you'd like manually.",
Subject='mint_to_expenses Execution Notification',
)
print("Subscribers emailed successfully!")
except Exception as error:
print(f"Unable to publish to SNS topic: {error}")
return response
# Rest of your code...
except Exception as e:
print(f"An error occurred during AWS setup: {str(e)}")