Skip to content

Commit 975aa6e

Browse files
committed
Modified blob-triggered azure function
1 parent 5f6de4a commit 975aa6e

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

hvalfangst_function/function_app.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,68 @@
1313
logger = logging.getLogger(__name__)
1414

1515

16-
@app.blob_trigger(arg_name="inbound", path="hvalfangstcontainer/in/input.csv", connection="")
17-
@app.blob_output(arg_name="outbound", path="hvalfangstcontainer/out/statistics.json", connection="")
16+
@app.blob_trigger(arg_name="inbound", path="hvalfangstcontainer/in/input.csv", connection="AzureWebJobsStorage")
17+
@app.blob_output(arg_name="outbound", path="hvalfangstcontainer/out/statistics.json", connection="AzureWebJobsStorage")
1818
def blob_trigger(inbound: func.InputStream, outbound: func.Out[str]):
1919
try:
20+
logging.info("Triggered blob function with blob: %s", inbound.name)
21+
2022
# Read CSV content from the blob
2123
csv_content = inbound.read().decode("utf-8")
24+
logging.info("Read CSV content from blob successfully")
2225

2326
# Convert CSV content to a pandas DataFrame
2427
df = pd.read_csv(StringIO(csv_content))
28+
logging.info("Converted CSV content to DataFrame")
2529

2630
# Label encode 'Gender' and 'State' columns
2731
label_encoder = LabelEncoder()
2832
df['Gender'] = label_encoder.fit_transform(df['Gender'])
2933
df['State'] = label_encoder.fit_transform(df['State'])
34+
logging.info("Label encoded 'Gender' and 'State' columns")
3035

3136
# Calculate correlations
3237
gender_to_income_corr = df[['Gender', 'Income']].corr().iloc[0, 1]
3338
experience_to_income_corr = df[['Experience', 'Income']].corr().iloc[0, 1]
3439
state_to_income_corr = df[['State', 'Income']].corr().iloc[0, 1]
40+
logging.info("Calculated correlations")
3541

3642
# Create statistics dictionary
3743
statistics = {
3844
"gender_to_income_corr": gender_to_income_corr,
3945
"experience_to_income_corr": experience_to_income_corr,
4046
"state_to_income_corr": state_to_income_corr
4147
}
48+
logging.info("Created statistics dictionary: %s", statistics)
4249

4350
# Convert statistics to JSON format
4451
statistics_json = json.dumps(statistics, indent=2)
52+
logging.info("Converted statistics to JSON format")
4553

4654
# Upload statistics JSON file to storage account container blob
4755
outbound.set(statistics_json)
48-
logging.info("- - - - - |File 'statistics.json' was uploaded| - - - - - ")
56+
logging.info("File 'statistics.json' was uploaded")
4957

5058
except Exception as e:
51-
logging.error(f"An error occurred: {str(e)}")
59+
logging.error("An error occurred: %s", str(e))
5260
return f"Error: {str(e)}"
5361

5462

5563
@app.route(route="upload_csv", auth_level=func.AuthLevel.ANONYMOUS)
5664
@app.blob_output(arg_name="outbound", path="hvalfangstcontainer/in/input.csv", connection="")
5765
def upload_csv(req: func.HttpRequest, outbound: func.Out[str]) -> str:
5866
try:
67+
logging.info("Received HTTP request to upload CSV")
68+
5969
# Parse raw bytes derived from request body to string
6070
string_body = req.get_body().decode("utf-8")
71+
logging.info("Parsed request body to string")
6172

6273
# Upload parsed string body, which conforms to CSV format
6374
outbound.set(string_body)
64-
logging.info("- - - - - |Successfully uploaded CSV content| - - - - - ")
75+
logging.info("Successfully uploaded CSV content")
6576
return "Successfully uploaded CSV content"
6677

6778
except Exception as e:
68-
logging.error(f"An error occurred: {str(e)}")
79+
logging.error("An error occurred: %s", str(e))
6980
return f"Error: {str(e)}"

0 commit comments

Comments
 (0)