(Created by GPT)
This repository contains Python code for an anomaly detection process that can be integrated with Azure Logic Apps. The process involves parsing a CSV file from any source supported by your Logic App (such as Storage Accounts, SharePoint, or One Drive), processing the data using a Function App, and returning the aggregated data.
The Python code in this repository is optimized for use with Azure Functions, allowing for quick and easy deployment and scaling of the anomaly detection process. The code is modular and reusable, making it easy to integrate with other components of a data processing pipeline.
The anomaly detection process is designed to help businesses identify unusual patterns or trends in their data that may be indicative of potential issues or opportunities. By detecting anomalies in their data, businesses can take proactive measures to mitigate risks or capitalize on opportunities.
Before using this code, you'll need the following:
- A GitHub Account
- A Microsoft Azure Account, a subscription and contributor rights to create resources
Here's how to get started with using this code.
The Azure Function is our connector between the Logic App and the Anomaly Detection Cognitive Service. Azure Functions is a serverless computing service that allows us to run code without managing any infrastructure. We only pay for the executions of our function, not for idle time. The Azure Function supports different languages, such as C#, Java, Python, and JavaScript.
- Create a Cognitive Services Anomaly Detector resource.
- Create an Azure Function App with Python (3.9+) and serverless hosting. You don't need CI/CD, we will do this manually. You can choose region, monitoring, storage, and networking as you like, following the best practices for Azure Functions.
- Go to your Function App
Configuration
>Application settings
in the function app and add:ANOMALYENDPOINT
: The endpoint URL for your Cognitive Services Anomaly Detector instance from step 1.OCP_APIM_SUB
: The subscription key for your Cognitive Services Anomaly Detector instance from step 1.AzureWebJobsFeatureFlags
: with the keyEnableWorkerIndexing
to enable V2 Python models.
GitHub Actions is a powerful automation tool that allows you to automate tasks, such as building, testing and deploying code. We will use GitHub Actions to deploy your latest version of you Microsoft Function App to Azure.
- Fork this repository.
- (Optional): Adjust
function_app.py
to your needs. You might have a different schema or anomaly detection configuration. - Edit the GitHub Workflow
github\workflows\main_logic-app-anomaly-detection-connector.yml
- set
FUNCTION_APP_NAME
to your function app name. - We will need permissions to deploy the App to Azure. Therefore follow these steps to set your Function Publish profile as a GitHub secret and then adjust the
publish profile
in the 'Deploy to Azure Functions' step.
- set
- Run the GitHub Workflow. After your Function App is deployed, go to the Function App to 'Functions' and select the 'AnomalyDetector' (the name you gave it in
function_app.py
) function. Under 'Function Keys,' copy the default Function Key.
For more information about deploying with GitHub Actions, see Deploying with GitHub Actions .
- Create a Logic App in Azure that can parse a CSV file. You can use the suiting connector (e.g. OneDrive, SharePoint) for this purpose. You could also check the example
logic_app_example.json
. - Add the 'HTTP Request' action.
- Set the 'Method' field to 'POST' and enter the URL of your function app in the 'URI' field. You can find the URL of your function app in the Azure portal.
- In the Header section, add a key named
x-functions-key
and set its value to your Function Key. You can find your Function Key in the Azure portal as well. - In the 'Body' field, paste the CSV data that you want to analyze. The comma serapated CSV data should have minimum two columns: "month" and "value". You can adjust the schema under
function_app.py
. - Run your Logic App and check the output of the 'HTTP Request' action. It should return a JSON object with the anomaly detection results
enriched_data
, theanomaly_months
when anomalies were detected, and theanomaly_detected
boolean if anomalies were detected.
To fully secure your function endpoints in production, you should consider implementing one of the following function app-level security options. When using one of these function app-level security methods, you should set the HTTP-triggered function authorization level to anonymous
.
If you only call the Function from a Logic App, go to the Function App Networking
> Inbound Traffic
> Access restriction
and add a Allow rule with Type Service Tag
and Service Tag Logic Apps
.
- Set
ANOMALYENDPOINT
andOCP_APIM_SUB
(endpoint and key of your Anomaly Detector) as repository secrets in your freshly forked repository. - Edit the GitHub Workflow
github\workflows\main_logic-app-anomaly-detection-connector.yml
and underjobs.build
uncomment the stepsinstall pytest
,Set PYTHONPATH
andRun pytest
- Add more tests to
tests\test_function_app.py
.
If you want to avoid adding secrets manually, you can create an Azure Service Principal and add AZURE_CRENDENTIALS as a repository secret.
You can then in github\workflows\main_logic-app-anomaly-detection-connector.yml
uncomment the Login to Azure
and Set environment variables
steps.
Contributions are welcome! If you have changes to suggest, please create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.