|
13 | 13 | logger = logging.getLogger(__name__)
|
14 | 14 |
|
15 | 15 |
|
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") |
18 | 18 | def blob_trigger(inbound: func.InputStream, outbound: func.Out[str]):
|
19 | 19 | try:
|
| 20 | + logging.info("Triggered blob function with blob: %s", inbound.name) |
| 21 | + |
20 | 22 | # Read CSV content from the blob
|
21 | 23 | csv_content = inbound.read().decode("utf-8")
|
| 24 | + logging.info("Read CSV content from blob successfully") |
22 | 25 |
|
23 | 26 | # Convert CSV content to a pandas DataFrame
|
24 | 27 | df = pd.read_csv(StringIO(csv_content))
|
| 28 | + logging.info("Converted CSV content to DataFrame") |
25 | 29 |
|
26 | 30 | # Label encode 'Gender' and 'State' columns
|
27 | 31 | label_encoder = LabelEncoder()
|
28 | 32 | df['Gender'] = label_encoder.fit_transform(df['Gender'])
|
29 | 33 | df['State'] = label_encoder.fit_transform(df['State'])
|
| 34 | + logging.info("Label encoded 'Gender' and 'State' columns") |
30 | 35 |
|
31 | 36 | # Calculate correlations
|
32 | 37 | gender_to_income_corr = df[['Gender', 'Income']].corr().iloc[0, 1]
|
33 | 38 | experience_to_income_corr = df[['Experience', 'Income']].corr().iloc[0, 1]
|
34 | 39 | state_to_income_corr = df[['State', 'Income']].corr().iloc[0, 1]
|
| 40 | + logging.info("Calculated correlations") |
35 | 41 |
|
36 | 42 | # Create statistics dictionary
|
37 | 43 | statistics = {
|
38 | 44 | "gender_to_income_corr": gender_to_income_corr,
|
39 | 45 | "experience_to_income_corr": experience_to_income_corr,
|
40 | 46 | "state_to_income_corr": state_to_income_corr
|
41 | 47 | }
|
| 48 | + logging.info("Created statistics dictionary: %s", statistics) |
42 | 49 |
|
43 | 50 | # Convert statistics to JSON format
|
44 | 51 | statistics_json = json.dumps(statistics, indent=2)
|
| 52 | + logging.info("Converted statistics to JSON format") |
45 | 53 |
|
46 | 54 | # Upload statistics JSON file to storage account container blob
|
47 | 55 | outbound.set(statistics_json)
|
48 |
| - logging.info("- - - - - |File 'statistics.json' was uploaded| - - - - - ") |
| 56 | + logging.info("File 'statistics.json' was uploaded") |
49 | 57 |
|
50 | 58 | except Exception as e:
|
51 |
| - logging.error(f"An error occurred: {str(e)}") |
| 59 | + logging.error("An error occurred: %s", str(e)) |
52 | 60 | return f"Error: {str(e)}"
|
53 | 61 |
|
54 | 62 |
|
55 | 63 | @app.route(route="upload_csv", auth_level=func.AuthLevel.ANONYMOUS)
|
56 | 64 | @app.blob_output(arg_name="outbound", path="hvalfangstcontainer/in/input.csv", connection="")
|
57 | 65 | def upload_csv(req: func.HttpRequest, outbound: func.Out[str]) -> str:
|
58 | 66 | try:
|
| 67 | + logging.info("Received HTTP request to upload CSV") |
| 68 | + |
59 | 69 | # Parse raw bytes derived from request body to string
|
60 | 70 | string_body = req.get_body().decode("utf-8")
|
| 71 | + logging.info("Parsed request body to string") |
61 | 72 |
|
62 | 73 | # Upload parsed string body, which conforms to CSV format
|
63 | 74 | outbound.set(string_body)
|
64 |
| - logging.info("- - - - - |Successfully uploaded CSV content| - - - - - ") |
| 75 | + logging.info("Successfully uploaded CSV content") |
65 | 76 | return "Successfully uploaded CSV content"
|
66 | 77 |
|
67 | 78 | except Exception as e:
|
68 |
| - logging.error(f"An error occurred: {str(e)}") |
| 79 | + logging.error("An error occurred: %s", str(e)) |
69 | 80 | return f"Error: {str(e)}"
|
0 commit comments