Skip to content

Commit

Permalink
Merge pull request #2 from tblanarik/azurefunc
Browse files Browse the repository at this point in the history
Convert to Azure Function
  • Loading branch information
tblanarik authored May 11, 2023
2 parents a7431fd + 32b4c70 commit d066ba1
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 117 deletions.
66 changes: 0 additions & 66 deletions .github/workflows/azure-functions-app-python.yml

This file was deleted.

23 changes: 0 additions & 23 deletions .github/workflows/get-traffic.yml

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local.settings.json

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
23 changes: 23 additions & 0 deletions commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Deploy

```bash
func azure functionapp publish daily-traffic-function
```

# Init function

```bash
func init . --python -m V2
```

# Install az cli

```bash
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
```

# Install azurite

```bash
npm install -g azurite
```
74 changes: 74 additions & 0 deletions function_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import azure.functions as func
import logging
import os
from message_manager import MessageManager
from wsdot_trafficflow import WSDOTTrafficFlow

app = func.FunctionApp()

@app.function_name(name="ManualRun")
@app.route(route="hello") # HTTP Trigger
def manual_run(req: func.HttpRequest) -> func.HttpResponse:
"""
This function is used to manually run the traffic report
"""
traffic_report(2530, debug=True)
traffic_report(2532, debug=True)
return func.HttpResponse("Finished checking traffic")

@app.function_name(name="timertrigger730")
@app.schedule(schedule="30 14 * * 1-5",
arg_name="timertrigger730",
run_on_startup=False)
def timer_trigger_730(timertrigger730: func.TimerRequest) -> None:
traffic_report(2530)
traffic_report(2532)


@app.function_name(name="timertrigger745")
@app.schedule(schedule="45 14 * * 1-5",
arg_name="timertrigger745",
run_on_startup=False)
def timer_trigger_745(timertrigger745: func.TimerRequest) -> None:
traffic_report(2530)
traffic_report(2532)


@app.function_name(name="timertrigger430")
@app.schedule(schedule="30 23 * * 1-5",
arg_name="timertrigger430",
run_on_startup=False)
def timer_trigger_430(timertrigger430: func.TimerRequest) -> None:
traffic_report(2530)
traffic_report(2532)


@app.function_name(name="timertrigger445")
@app.schedule(schedule="45 23 * * 1-5",
arg_name="timertrigger445",
run_on_startup=False)
def timer_trigger_445(timertrigger445: func.TimerRequest) -> None:
traffic_report(2530)
traffic_report(2532)


def traffic_report(flow_data_id, debug=False):
"""
This function gets a traffic report and sends an email if there is slow traffic
"""
access_code = os.environ["WSDOT_ACCESS_CODE"]
post_url = os.environ["POST_URL"]
recipients = os.environ["TEXT_RECIPIENTS"]

traffic_flow = WSDOTTrafficFlow(access_code, flow_data_id)
message_manager = MessageManager(post_url)

flow_data = traffic_flow.flow_data()

if flow_data.flow_reading_value > 1 or debug:
logging.info("Slow traffic detected: , {flow_data.flow_station_location.description}")
message_manager.send_message(
f"{flow_data.flow_station_location.description} Slow Traffic = ({flow_data.flow_reading_value})!",
recipients)
else:
logging.info("No slow traffic: {flow_data.flow_station_location.description}")
15 changes: 15 additions & 0 deletions host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.15.0, 4.0.0)"
}
}
28 changes: 0 additions & 28 deletions main.py

This file was deleted.

0 comments on commit d066ba1

Please sign in to comment.