|
| 1 | +/** |
| 2 | + * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @param args Arguments to provide to the example. The following arguments are expected: |
| 8 | + * <ul> |
| 9 | + * <li>The first argument is the OCID of the compartment.</li> |
| 10 | + * <li>The second argument is the namespace.</li> |
| 11 | + * <li>The third argument is name of the bucket.</li> |
| 12 | + * <li>The fourth argument is object prefix.</li> |
| 13 | + * <li>The fifth argument is object name.</li> |
| 14 | + * </ul> |
| 15 | + */ |
| 16 | + |
| 17 | +const common = require("oci-common"); |
| 18 | +const cp = require("oci-datalabelingservice"); |
| 19 | +const dp = require("oci-datalabelingservicedataplane"); |
| 20 | + |
| 21 | +const args = process.argv.slice(2); |
| 22 | +console.log(args); |
| 23 | +if (args.length !== 5) { |
| 24 | + console.error( |
| 25 | + "Unexpected number of arguments received. Consult the script header comments for expected arguments" |
| 26 | + ); |
| 27 | + process.exit(-1); |
| 28 | +} |
| 29 | + |
| 30 | +const compartmentId = args[0]; |
| 31 | +const namespace = args[1]; |
| 32 | +const bucket = args[2]; |
| 33 | +const prefix = args[3]; |
| 34 | +const objectName = args[4]; |
| 35 | +let datasetId = ""; |
| 36 | + |
| 37 | +const provider = new common.ConfigFileAuthenticationDetailsProvider(); |
| 38 | + |
| 39 | +const dlsCPClient = new cp.DataLabelingManagementClient({ |
| 40 | + authenticationDetailsProvider: provider |
| 41 | +}); |
| 42 | + |
| 43 | +const dlsDPClient = new dp.DataLabelingClient({ |
| 44 | + authenticationDetailsProvider: provider |
| 45 | +}); |
| 46 | + |
| 47 | +async function listDataset() { |
| 48 | + const datasetList = await dlsCPClient.listDatasets({ |
| 49 | + compartmentId |
| 50 | + }); |
| 51 | + return datasetList.datasetCollection.items; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @description Polls and fetches workrequest details for a particular workrequestId. |
| 56 | + */ |
| 57 | +async function checkWorkRequestForSucceed(workRequestId) { |
| 58 | + return new Promise(async (resolve, reject) => { |
| 59 | + let timer = 0; |
| 60 | + (async function refresh() { |
| 61 | + clearTimeout(timer); |
| 62 | + try { |
| 63 | + const { workRequest } = await dlsCPClient |
| 64 | + .getWorkRequest({ |
| 65 | + workRequestId |
| 66 | + }) |
| 67 | + .catch(error => { |
| 68 | + console.error(error); |
| 69 | + throw new Error(`get Work request : ${error}`); |
| 70 | + }); |
| 71 | + const { operationType, status, timeAccepted, percentComplete } = workRequest; |
| 72 | + console.table([ |
| 73 | + { |
| 74 | + workRequestId, |
| 75 | + operationType, |
| 76 | + status, |
| 77 | + timeAccepted, |
| 78 | + percentComplete |
| 79 | + } |
| 80 | + ]); |
| 81 | + if (workRequest.status === "SUCCEEDED") { |
| 82 | + resolve(workRequest.status); |
| 83 | + return; |
| 84 | + } |
| 85 | + if (workRequest.status === "FAILED") { |
| 86 | + console.error("workRequest failed", workRequest); |
| 87 | + reject(workRequest.status); |
| 88 | + return; |
| 89 | + } |
| 90 | + } catch (error) { |
| 91 | + reject(error); |
| 92 | + return; |
| 93 | + } |
| 94 | + timer = setTimeout(refresh, 3000); |
| 95 | + })(); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +async function createDataset() { |
| 100 | + return dlsCPClient.createDataset({ |
| 101 | + createDatasetDetails: { |
| 102 | + compartmentId, |
| 103 | + displayName: `typescript-sdk-example`, |
| 104 | + datasetSourceDetails: { |
| 105 | + sourceType: "OBJECT_STORAGE", |
| 106 | + namespace, |
| 107 | + bucket, |
| 108 | + prefix |
| 109 | + }, |
| 110 | + datasetFormatDetails: { |
| 111 | + formatType: "IMAGE" |
| 112 | + }, |
| 113 | + annotationFormat: "MULTI_LABEL", |
| 114 | + labelSet: { |
| 115 | + items: [ |
| 116 | + { |
| 117 | + name: "label1" |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "label2" |
| 121 | + } |
| 122 | + ] |
| 123 | + }, |
| 124 | + definedTags: {}, |
| 125 | + freeformTags: {} |
| 126 | + } |
| 127 | + }); |
| 128 | +} |
| 129 | + |
| 130 | +async function addLabels() { |
| 131 | + return dlsCPClient.addDatasetLabels({ |
| 132 | + datasetId, |
| 133 | + addDatasetLabelsDetails: { |
| 134 | + labelSet: { |
| 135 | + items: [{ name: "label3" }] |
| 136 | + } |
| 137 | + } |
| 138 | + }); |
| 139 | +} |
| 140 | + |
| 141 | +async function deleteDataset() { |
| 142 | + return dlsCPClient.deleteDataset({ |
| 143 | + datasetId |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +async function getDataset() { |
| 148 | + const { dataset } = await dlsDPClient.getDataset({ datasetId }); |
| 149 | + return dataset; |
| 150 | +} |
| 151 | + |
| 152 | +async function listRecords() { |
| 153 | + const { recordCollection: { items = [] } = {} } = await dlsDPClient.listRecords({ |
| 154 | + compartmentId, |
| 155 | + datasetId |
| 156 | + }); |
| 157 | + return items; |
| 158 | +} |
| 159 | + |
| 160 | +async function getRecord(recordId) { |
| 161 | + const { record } = await dlsDPClient.getRecord({ |
| 162 | + recordId |
| 163 | + }); |
| 164 | + return record; |
| 165 | +} |
| 166 | + |
| 167 | +async function createRecord() { |
| 168 | + const { datasetSourceDetails: { sourceType = "" } = {} } = await getDataset(); |
| 169 | + return dlsDPClient.createRecord({ |
| 170 | + createRecordDetails: { |
| 171 | + compartmentId, |
| 172 | + datasetId, |
| 173 | + name: objectName, |
| 174 | + sourceDetails: { |
| 175 | + relativePath: objectName, |
| 176 | + sourceType |
| 177 | + } |
| 178 | + } |
| 179 | + }); |
| 180 | +} |
| 181 | + |
| 182 | +const getEntities = ({ label }) => { |
| 183 | + return [ |
| 184 | + { |
| 185 | + labels: [ |
| 186 | + { |
| 187 | + label |
| 188 | + } |
| 189 | + ], |
| 190 | + entityType: "GENERIC" |
| 191 | + } |
| 192 | + ]; |
| 193 | +}; |
| 194 | + |
| 195 | +async function createAnnotation(recordId) { |
| 196 | + const { |
| 197 | + labelSet: { items = [] } |
| 198 | + } = await getDataset(); |
| 199 | + const label = items[0].name || ""; |
| 200 | + return dlsDPClient.createAnnotation({ |
| 201 | + createAnnotationDetails: { |
| 202 | + compartmentId, |
| 203 | + recordId, |
| 204 | + entities: getEntities({ label }) |
| 205 | + } |
| 206 | + }); |
| 207 | +} |
| 208 | + |
| 209 | +async function listAnnotation(id) { |
| 210 | + return dlsDPClient.listAnnotations({ |
| 211 | + compartmentId, |
| 212 | + datasetId, |
| 213 | + id |
| 214 | + }); |
| 215 | +} |
| 216 | + |
| 217 | +(async () => { |
| 218 | + try { |
| 219 | + await listDataset(); |
| 220 | + let { dataset, opcWorkRequestId } = await createDataset(); |
| 221 | + datasetId = dataset.id; |
| 222 | + console.log("Dataset Created"); |
| 223 | + await checkWorkRequestForSucceed(opcWorkRequestId); |
| 224 | + ({ opcWorkRequestId } = await addLabels()); |
| 225 | + await checkWorkRequestForSucceed(opcWorkRequestId); |
| 226 | + console.log("Labels Added"); |
| 227 | + const { record } = await createRecord(); |
| 228 | + console.log("Record created"); |
| 229 | + await getRecord(record.id); |
| 230 | + const { annotation } = await createAnnotation(record.id); |
| 231 | + console.log("Annotation created"); |
| 232 | + await listRecords(); |
| 233 | + await listAnnotation(annotation.id); |
| 234 | + } catch (error) { |
| 235 | + console.log("Error executing example" + error); |
| 236 | + } finally { |
| 237 | + await deleteDataset(); |
| 238 | + } |
| 239 | + console.debug("DONE"); |
| 240 | +})(); |
0 commit comments