|
| 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 | + |
| 47 | + |
| 48 | +- Upload the file [demo-training-data.csv](./ai-data-references/demo-training-data.csv) to the bucket. |
| 49 | + |
| 50 | + |
| 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 | + |
| 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 | + |
| 62 | + |
| 63 | +- Click `Create and Train Model` and create a model. Select the `Data Asset` created in the previous step. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +- Provide a name and provide the default FAP and Training ratio and review create the model. |
| 68 | + |
| 69 | + |
| 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 | + |
| 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 | + |
| 86 | + |
| 87 | +- Push the whole contents here to the OCI CODE REPO created. |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +- Create an OCI Build pipeline. |
| 92 | + |
| 93 | + |
| 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 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +- Add a build parameter as `MODEL_OCID` and add the Anomaly detection model's OCID as value. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +- Create a `DevOps trigger` and associate `Code repo` and `build a pipeline |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 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 | + |
| 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 | + |
| 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 | + |
| 183 | + |
| 184 | +- The execution should follow with a success and with no anomalies. |
| 185 | + |
| 186 | + |
| 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 | + |
0 commit comments