Skip to content

Commit b10481f

Browse files
authored
Merge pull request #46 from oracle-devrel/ai
anomaly detection sample
2 parents 27d6e97 + ed154e4 commit b10481f

28 files changed

+10309
-0
lines changed

AIO.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## All in One reference for samples - In Alphabetical (A to Z) order.
22

3+
- A sample to run OCI AI Anomaly detection from OCI Build-pipeline - https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-devops-anomaly-detection
34
- A way (workaround) to execute commands on to a Windows machine using OCI Run command agent from OCI Build pipeline. - https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-devops-connect-to-windows-from-buildmachines
45
- Access resource with Private IP from OCI Build runner - https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-devops-pa-with-private-oke
56
- Build Caching - https://github.com/oracle-devrel/oci-devops-examples/tree/main/oci-build-examples/oci-build-caching
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
# A sample to run OCI AI Anomaly detection from OCI Build-pipeline.
2+
3+
4+
Objective
5+
---
6+
- Set up an anomaly detection project and pre-trained model.
7+
- Run an anomaly detection from `OCI DevOps build pipeline.
8+
- Make an executive decision based on the anomaly results.
9+
10+
```
11+
$ git init oci-DevOps-anomaly-detection
12+
$ cd oci-devops-anomaly-detection
13+
$ git remote add origin <url to this git repo>
14+
$ git config core. sparse checkout true
15+
$ echo "oci-build-examples/ooci-devops-anomaly-detection/*">>.git/info/sparse-checkout
16+
$ git pull --depth=1 origin main
17+
18+
```
19+
20+
Audience: OCI Advanced or Intermediate Devops / AI users.
21+
22+
Procedure to use the illustration
23+
-------
24+
25+
### OCI Identity policies
26+
27+
- Create an OCI Dynamic group with the below rules.
28+
29+
```java
30+
ALL {resource.type = 'devopsbuildpipeline', resource.compartment.id = 'OCID OF YOUR COMPARTMENT'}
31+
```
32+
33+
- Create an OCI Policy with the below rules.
34+
35+
```java
36+
allow any-user to manage ai-service-anomaly-detection-family in compartment <NAME OF YOUR COMPARTMENT>
37+
allow any user to manage object-family in compartment <NAME OF YOUR COMPARTMENT>
38+
Allow dynamic-group <DYNAMIC GROUP NAME> to manage objects in compartment <NAME OF YOUR COMPARTMENT>
39+
Allow dynamic-group <DYNAMIC GROUP NAME> to manage ai-service-anomaly-detection-family in tenancy
40+
```
41+
42+
### OCI Object storage setup
43+
44+
- Create an object storage bucket.
45+
46+
![](images/oci-object-bucket.png)
47+
48+
- Upload the file [demo-training-data.csv](./ai-data-references/demo-training-data.csv) to the bucket.
49+
50+
![](images/oci-bucket-content.png)
51+
52+
53+
### OCI Anomaly detection setup
54+
55+
- Create a project under OCI Anomaly detection. - https://docs.oracle.com/en-us/iaas/Content/anomaly/using/create-project.htm
56+
57+
![](images/oci-anomaly-project.png)
58+
59+
- Under the `project` create a `Data Assets`.Select the `type` as `Oracle Object storage` and select the bucket and the filename.
60+
61+
![](images/oci-ai-datasset.png)
62+
63+
- Click `Create and Train Model` and create a model. Select the `Data Asset` created in the previous step.
64+
65+
![](images/oci-create-model.png)
66+
67+
- Provide a name and provide the default FAP and Training ratio and review create the model.
68+
69+
![](images/oci-train-model-create.png)
70+
71+
- The details about the various parameters can be found here and it's from one of our Live labs from `Oracle Live Labs` - [Get started with Oracle Cloud Infrastructure Anomaly Detection](https://apexapps.oracle.com/pls/apex/r/dbpm/livelabs/run-workshop?p210_wid=819&p210_wec=&session=107963899615339)
72+
73+
- The creation of the model will take around 10 to 20 minutes.
74+
75+
### OCI DevOps setup
76+
77+
- Create a notification topic - https://docs.oracle.com/en-us/iaas/Content/Notification/Tasks/create-topic.htm#top
78+
79+
![](images/oci-notification-topic.png)
80+
81+
- Create a DevOps project - https://docs.oracle.com/en-us/iaas/Content/devops/using/create_project.htm#create_a_project
82+
- Ensure enable logs for the DevOps project.
83+
- Create an `OCI Code repo`
84+
85+
![](images/oci-code-repo.png)
86+
87+
- Push the whole contents here to the OCI CODE REPO created.
88+
89+
![](images/oci-code-files.png)
90+
91+
- Create an OCI Build pipeline.
92+
93+
![](images/oci-buildpipeline.png)
94+
95+
- Use `+` and add a new build stage of type `manage build` .Select the `OCI Code repo` created as the `primary buildsoruce`.
96+
97+
![](images/oci-build-stage-1.png)
98+
99+
![](images/oci-build-stage-2.png)
100+
101+
![](images/oci-build-stage-3.png)
102+
103+
- Add a build parameter as `MODEL_OCID` and add the Anomaly detection model's OCID as value.
104+
105+
![](images/oci-build-params.png)
106+
107+
- Create a `DevOps trigger` and associate `Code repo` and `build a pipeline
108+
109+
![](images/oci-trigger-1.png)
110+
111+
![](images/oci-trigger-2.png)
112+
113+
114+
### Execution
115+
116+
- The below build stage step is checking anomaly against new data and once an anomaly is detected it will exit. It's a sample build spec with one step, however, this can be extended and altered according to the use cases.
117+
118+
```java
119+
steps:
120+
- type: Command
121+
timeoutInSeconds: 6000
122+
name: "Run Anomaly Detection"
123+
command: |
124+
cd ${OCI_PRIMARY_SOURCE_DIR}
125+
cd ai-data-references
126+
oci anomaly-detection model detect-anomalies-inline --model-id ${MODEL_OCID} --data file://data.json --signal-names file://signal-names.json >output.json
127+
counter=`cat output.json|grep -w anomalies`
128+
if [[ ${counter} != 0 ]]
129+
then
130+
echo "Found Anomalies"
131+
cat output.json
132+
exit
133+
fi
134+
echo "No anomalies"
135+
136+
onFailure:
137+
```
138+
139+
- The detection is running against the file [data. json](./ai-data-references/data.json)
140+
141+
```java
142+
[
143+
{
144+
"timestamp": "2020-07-13T20:44:46Z",
145+
"values": [ 9.4713, 1, 0.5479, 1.291, 0.8059, 1.393, 0.0293, 0.1541, 0.2611,0.4098]
146+
},
147+
{
148+
"timestamp": "2020-07-13T20:45:46Z",
149+
"values": [ 0.4713, 1, 0.5479, 1.291, 0.8059, 1.393, 0.0293, 0.1541, 0.2611,0.4098]
150+
}
151+
]
152+
```
153+
154+
- Refer to the OCI Labs mentioned getting more information about the JSON and methodologies used here.
155+
156+
- Click `Start manual run` with the build pipeline and wait for all the steps to complete.
157+
158+
![](images/oci-build-manual-run.png)
159+
160+
- Since one of the values `9.4713` in the first data set is exceeding the limit, the check will detect an anomaly and stop proceeding further.
161+
162+
![](images/oci-buildrun-with-anomalies.png)
163+
164+
165+
- For the next iteration, update the value of [data. json](./ai-data-references/data.json) to an acceptable limit.
166+
167+
```java
168+
[
169+
{
170+
"timestamp": "2020-07-13T20:44:46Z",
171+
"values": [ 0.4713, 1, 0.5479, 1.291, 0.8059, 1.393, 0.0293, 0.1541, 0.2611,0.4098]
172+
},
173+
{
174+
"timestamp": "2020-07-13T20:45:46Z",
175+
"values": [ 0.4713, 1, 0.5479, 1.291, 0.8059, 1.393, 0.0293, 0.1541, 0.2611,0.4098]
176+
}
177+
]
178+
```
179+
- Push the changes back to the code repo. This time the build run will get triggered automatically as we set up the DevOps trigger in the previous DevOps section.
180+
181+
182+
![](images/oci-devops-trigger.png)
183+
184+
- The execution should follow with a success and with no anomalies.
185+
186+
![](images/oci-build-with-no-anomaly.png)
187+
188+
189+
### Clean up resources (Verify it before and ensure they are not shared with any other resources before the deletion).
190+
191+
- Remove devops elements using `Devops Project Cascade deletion`
192+
193+
- Delete the bucket content and Bucket.
194+
195+
- Delete Anomaly detection model , data assets and finally project.
196+
197+
- Delete Devops Logs.
198+
199+
- Delete Notification topics.
200+
201+
Read more
202+
---
203+
204+
- OCI DevOps service - https://docs.oracle.com/en-us/iaas/Content/devops/using/home.htm
205+
- Details about OCI Anomaly detection and workshop -.
206+
207+
- There are reference samples available within the directory [ai-data-references](ai-data-references) to set up the anomaly detection project /model and run execution using [OCI CLI](https://apexapps.oracle.com/pls/apex/r/dbpm/livelabs/run-workshop?p210_wid=819&p210_wec=&session=107963899615339).
208+
209+
Contributors
210+
===========
211+
212+
- Author: [Rahul M R](https://github.com/RahulMR42).
213+
- Collaborators:
214+
- Last release: January 2023
215+
216+
### Back to examples.
217+
----
218+
219+
- 🍿 [Back to OCI Devops Build sample](./../README.md)
220+
- 🏝️ [Back to OCI Devops sample](./../../README.md)
221+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dataSourceType" : "ORACLE_OBJECT_STORAGE",
3+
"bucketName" : "BUCKET NAME",
4+
"namespace" : "NAME SPACE",
5+
"objectName" : "demo-training-data.csv"
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targetFap" : 0.01,
3+
"trainingFraction" : 0.7,
4+
"dataAssetIds" :
5+
[
6+
"OCI of DATA SET"
7+
]
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"timestamp": "2020-07-13T20:44:46Z",
4+
"values": [ 9.4713, 1, 0.5479, 1.291, 0.8059, 1.393, 0.0293, 0.1541, 0.2611,0.4098]
5+
},
6+
{
7+
"timestamp": "2020-07-13T20:45:46Z",
8+
"values": [ 0.4713, 1, 0.5479, 1.291, 0.8059, 1.393, 0.0293, 0.1541, 0.2611,0.4098]
9+
}
10+
]

0 commit comments

Comments
 (0)