Friday, January 21, 2022

#898 - OCI Anomaly Detection Service & OIC - Part I


Oracle Cloud Infrastructure (OCI) Anomaly Detection is an AI service that enables developers to more easily build business-specific anomaly detection models that flag critical incidents, resulting in faster time to detection and resolution. Specialized APIs and automated model selection simplify training and deploying anomaly detection models to applications and operations—all without data science expertise.

Read more about OCI Anomaly Detection here

My starting point is the workshop, which can be found here

The workshop is very easy to follow - essentially you have the following artifacts -

  • Projects - for organizing your work
  • Data Assets - links to the data sources which will be used as the basis for anomaly detection.
  • Models - ML models trained to detect anomalies. Models leverage Data Assets for the base data.
  • Detection - you can upload a file and test it against the model, or else you can leverage the Anomaly Detection api for this. We will be using the latter, in respect of OIC.
So now to the simple workshop example - the starting point is a .csv file containing sensor readings - 

We have a timestamp column and then 5 temperature and 5 pressure readings columns.
You get the drift about which anomalies we'll be looking for here. Please note that the Anomaly Detection Service currently only supports time series based data sets, hence the timestamp column.

Ok, I upload this .csv file to object storage and then create a Data Asset in Anomaly Detection.

The menu is very simple -


Now to the Model - 

It's that easy! No take some quality time out while waiting for the model to be trained. 

Let's do some detection - the lab provides the following test file - 

Sehr gut! as we say in Franconia. Next let's look at the Anomaly Detection API.

Now I use OIC to invoke a couple of these - first to creating the connection...

Create an OIC connection to Anomaly Detection Service

Configuring the AD REST adapter in an Integration 

Here is the configuration of the REST invoke - 

Note how I define the response as {}, as I do not have an example for the response. I use this to execute the invoke the first time and then replace with the actual payload returned.


I add a loop and then log the model names -

Here is the output - 

I have output the model name and it's id (ocid). Currently there is no public REST api at this level to invoke the Detect Anomalies operation. However, the lab does provide a python client for doing this. I will look at invoking this client in the next post in this series.My idea is to create the relevant python function in OCI Functions and invoke it from OIC - 


Monday, January 17, 2022

#897 OIC invoking OCI Functions (python) - Part 2 - Creating the OIC Integration

So now we have the python function, let's leverage it from OIC - 

Create Connection for OCI Functions 

So what will we need here?
1. base url
2. tenancy ocid
3. user ocid
4. private key
5. fingerprint

Check out my previous post on leveraging functions from OIC here, if you need details on where to retrieve these values from.

Create the Integration

I create a app driven integration with a REST trigger - 

Response is as follows - 

Set the relative uri to the following - 

The function endpoint has the following format -

The relative uri is set to /20181201/functions/ocid/actions/invoke


Request / Response configured as follows - 

Request Mapping - 

Map Integration response - 

Test the Integration

#896 OIC invoking OCI Functions (python) - Part 1 Creating the python function

OIC is part of the OCI family, a family of rich services that can be leveraged from your integrations and processes. This post shows how easy it is to create a python function in OCI Functions. 

The next post will cover leveraging this python function from OIC.

So to the simple use case - product discounts - the python function will apply these for me. I know it is a banal example but aren't we all experts at extrapolating?

Starting point is a simple python function I have created.  

So how do we expose this logic via OCI Functions?

Step 1 - Create a new Application in OCI Functions

Step 2- Click on Cloud Shell (Getting Started) and create the function

Now I will create a python based function within this application - first step - list my current apps - 
fn list apps - there's my app - NiallCPythonApp -

Then I create a new default python function - 

fn init --runtime python niallCPythonDemo

Check out what's been generated - 

Let's look at the generated function code - 

This is a simple helloWorld example, I will modify to include the discount functionality I mentioned at the outset - 
the business logic is simple - If the product is iBike and the quantity ordered is > 30 then 
apply a 10% discount; all other others get a 5% discount.

Here is the revised code - first I make a copy of the default function -

then I edit as follows - 

I delete and then edit the yaml file to refer to the new function  - 

Step 3 - Deploy and Test

fn -v deploy --app NiallCPythonApp -

Function has been deployed - 

Now to testing this - method is as follows, when argument passing is required - 

so my cmd is as follows - 

echo -n '{"product":"iBike", "unitsOrdered":40}' | fn invoke NiallCPythonApp niallcpythondemo

Simple and succinct!