|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +/** |
| 5 | + * @summary This sample demonstrates data feed management operations. |
| 6 | + * @azsdk-weight 80 |
| 7 | + */ |
| 8 | + |
| 9 | +// Load the .env file if it exists |
| 10 | +import * as dotenv from "dotenv"; |
| 11 | +dotenv.config(); |
| 12 | + |
| 13 | +import { |
| 14 | + MetricsAdvisorKeyCredential, |
| 15 | + MetricsAdvisorAdministrationClient, |
| 16 | + GetDataFeedResponse, |
| 17 | + DataFeedPatch, |
| 18 | + DataFeedDescriptor |
| 19 | +} from "@azure/ai-metrics-advisor"; |
| 20 | + |
| 21 | +export async function main() { |
| 22 | + // You will need to set these environment variables or edit the following values |
| 23 | + const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>"; |
| 24 | + const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>"; |
| 25 | + const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>"; |
| 26 | + const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey); |
| 27 | + const existingDataFeedId = process.env["METRICS_ADVISOR_DATAFEED_ID"] || "<datafeed id>"; |
| 28 | + |
| 29 | + const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential); |
| 30 | + await listDataFeeds(adminClient); |
| 31 | + await getDataFeed(adminClient, existingDataFeedId); |
| 32 | + const created = await createDataFeed(adminClient); |
| 33 | + await updateDataFeed(adminClient, created.id); |
| 34 | + await deleteDataFeed(adminClient, created.id); |
| 35 | +} |
| 36 | + |
| 37 | +async function listDataFeeds(client: MetricsAdvisorAdministrationClient) { |
| 38 | + console.log("Listing Datafeeds ..."); |
| 39 | + console.log(" using while loop"); |
| 40 | + const iter = client.listDataFeeds({ |
| 41 | + filter: { |
| 42 | + dataFeedName: "js-blob-datafeed" |
| 43 | + } |
| 44 | + }); |
| 45 | + let result = await iter.next(); |
| 46 | + while (!result.done) { |
| 47 | + console.log(`id :${result.value.id}, name: ${result.value.name}`); |
| 48 | + result = await iter.next(); |
| 49 | + } |
| 50 | + |
| 51 | + // second approach |
| 52 | + console.log(" using for-await-of loop"); |
| 53 | + const iterator = client.listDataFeeds(); |
| 54 | + for await (const datatFeed of iterator) { |
| 55 | + console.log(`id :${datatFeed.id}, name: ${datatFeed.name}`); |
| 56 | + } |
| 57 | + |
| 58 | + // by pages |
| 59 | + console.log(" by pages"); |
| 60 | + const pages = client.listDataFeeds().byPage({ maxPageSize: 1 }); |
| 61 | + let page = await pages.next(); |
| 62 | + let i = 1; |
| 63 | + while (!page.done) { |
| 64 | + if (page.value) { |
| 65 | + console.log(`-- page ${i++}`); |
| 66 | + for (const feed of page.value) { |
| 67 | + console.log(` ${feed.id} - ${feed.name}`); |
| 68 | + } |
| 69 | + } |
| 70 | + page = await pages.next(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +async function createDataFeed( |
| 75 | + client: MetricsAdvisorAdministrationClient |
| 76 | +): Promise<GetDataFeedResponse> { |
| 77 | + console.log("Creating Datafeed..."); |
| 78 | + const feed: DataFeedDescriptor = { |
| 79 | + name: "test-datafeed-" + new Date().getTime().toString(), |
| 80 | + source: { |
| 81 | + dataSourceType: "AzureBlob", |
| 82 | + dataSourceParameter: { |
| 83 | + connectionString: |
| 84 | + process.env.METRICS_ADVISOR_AZURE_BLOB_CONNECTION_STRING || |
| 85 | + "<Azure Blob storage connection string>", |
| 86 | + container: |
| 87 | + process.env.METRICS_ADVISOR_AZURE_BLOB_CONTAINER || "<Azure Blob container name>", |
| 88 | + blobTemplate: |
| 89 | + process.env.METRICS_ADVISOR_AZURE_BLOB_TEMPLATE || "<Azure Blob data file name template>" |
| 90 | + } |
| 91 | + }, |
| 92 | + granularity: { |
| 93 | + granularityType: "Daily" |
| 94 | + }, |
| 95 | + schema: { |
| 96 | + metrics: [ |
| 97 | + { |
| 98 | + name: "Metric1", |
| 99 | + displayName: "Metric1 display", |
| 100 | + description: "" |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "Metric2", |
| 104 | + displayName: "Metric2 display", |
| 105 | + description: "" |
| 106 | + } |
| 107 | + ], |
| 108 | + dimensions: [ |
| 109 | + { name: "Dim1", displayName: "Dim1 display" }, |
| 110 | + { name: "Dim2", displayName: "Dim2 display" } |
| 111 | + ], |
| 112 | + timestampColumn: undefined |
| 113 | + }, |
| 114 | + ingestionSettings: { |
| 115 | + ingestionStartTime: new Date(Date.UTC(2020, 8, 21)), |
| 116 | + ingestionStartOffsetInSeconds: 0, |
| 117 | + dataSourceRequestConcurrency: -1, |
| 118 | + ingestionRetryDelayInSeconds: -1, |
| 119 | + stopRetryAfterInSeconds: -1 |
| 120 | + }, |
| 121 | + rollupSettings: { |
| 122 | + rollupType: "AutoRollup", |
| 123 | + rollupMethod: "Sum", |
| 124 | + rollupIdentificationValue: "__CUSTOM_SUM__" |
| 125 | + }, |
| 126 | + missingDataPointFillSettings: { |
| 127 | + fillType: "CustomValue", |
| 128 | + customFillValue: 567 |
| 129 | + }, |
| 130 | + accessMode: "Private" |
| 131 | + }; |
| 132 | + const result = await client.createDataFeed(feed); |
| 133 | + |
| 134 | + console.dir(result); |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +async function getDataFeed(client: MetricsAdvisorAdministrationClient, dataFeedId: string) { |
| 139 | + console.log("Retrieving datafeed by id..."); |
| 140 | + const result = await client.getDataFeed(dataFeedId); |
| 141 | + console.log("datafeed result is as follows - "); |
| 142 | + console.log(` id: ${result.id}`); |
| 143 | + console.log(` data source type: ${result.source.dataSourceType}`); |
| 144 | + console.log(` name: ${result.name}`); |
| 145 | +} |
| 146 | + |
| 147 | +async function updateDataFeed(client: MetricsAdvisorAdministrationClient, dataFeedId: string) { |
| 148 | + const patch: DataFeedPatch = { |
| 149 | + source: { |
| 150 | + dataSourceType: "AzureBlob" |
| 151 | + }, |
| 152 | + name: "new name test-datafeed " + new Date().getTime().toString(), |
| 153 | + ingestionSettings: { |
| 154 | + ingestionStartTime: new Date(Date.UTC(2020, 8, 15)), |
| 155 | + ingestionRetryDelayInSeconds: 3000, |
| 156 | + stopRetryAfterInSeconds: 667777, |
| 157 | + ingestionStartOffsetInSeconds: 4444 |
| 158 | + }, |
| 159 | + description: "New datafeed description", |
| 160 | + missingDataPointFillSettings: { |
| 161 | + fillType: "SmartFilling" |
| 162 | + }, |
| 163 | + status: "Paused" |
| 164 | + }; |
| 165 | + |
| 166 | + try { |
| 167 | + console.log(`Updating datafeed ${dataFeedId}...`); |
| 168 | + const updated = await client.updateDataFeed(dataFeedId, patch); |
| 169 | + console.dir(updated); |
| 170 | + } catch (err) { |
| 171 | + console.log("Error occurred when updating data feed"); |
| 172 | + console.log(err); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +async function deleteDataFeed(client: MetricsAdvisorAdministrationClient, dataFeedId: string) { |
| 177 | + console.log(`Deleting datafeed ${dataFeedId}...`); |
| 178 | + await client.deleteDataFeed(dataFeedId); |
| 179 | +} |
| 180 | + |
| 181 | +main() |
| 182 | + .then((_) => { |
| 183 | + console.log("Succeeded"); |
| 184 | + }) |
| 185 | + .catch((err) => { |
| 186 | + console.log("Error occurred:"); |
| 187 | + console.log(err); |
| 188 | + }); |
0 commit comments