Skip to content

Commit a5d216e

Browse files
KarishmaGhiyajeremymengwitemple-msft
authored
Migrate samples metrics to V2 (Azure#14951)
Co-authored-by: Jeremy Meng <yumeng@microsoft.com> Co-authored-by: Will Temple <witemple@microsoft.com>
1 parent 13e6508 commit a5d216e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2394
-371
lines changed

sdk/metricsadvisor/ai-metrics-advisor/package.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
"build:autorest": "autorest ./swagger/README.md --typescript --version=3.0.6267",
5151
"build:browser": "tsc -p . && cross-env ONLY_BROWSER=true rollup -c 2>&1",
5252
"build:node": "tsc -p . && cross-env ONLY_NODE=true rollup -c 2>&1",
53-
"build:samples": "dev-tool samples prep && cd dist-samples && tsc -p .",
53+
"build:samples": "echo Obsolete.",
5454
"build:test": "tsc -p . && rollup -c 2>&1",
5555
"build": "tsc -p . && rollup -c 2>&1 && api-extractor run --local",
56-
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
56+
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
5757
"clean": "rimraf dist dist-esm dist-browser dist-test dist-test temp types *.tgz *.log",
58-
"execute:samples": "npm run build:samples && dev-tool samples run samples/javascript/ && dev-tool samples run dist-samples/typescript/dist/dist-samples/typescript/src/",
58+
"execute:samples": "dev-tool samples run samples-dev",
5959
"extract-api": "tsc -p . && api-extractor run --local",
60-
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
60+
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
6161
"integration-test:browser": "karma start --single-run",
6262
"integration-test:node": "nyc mocha -r esm --require source-map-support/register --reporter ../../../common/tools/mocha-multi-reporter.js --full-trace -t 300000 \"dist-esm/test/internal/*.spec.js\" \"dist-esm/test/public/*.spec.js\" \"dist-esm/test/public/node/*.spec.js\" \"dist-esm/test/internal/node/*.spec.js\"",
6363
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
@@ -137,6 +137,18 @@
137137
"metricQueries.js",
138138
"quickstart.js",
139139
"seriesData.js"
140-
]
140+
],
141+
"productName": "Azure Metrics Advisor",
142+
"productSlugs": [
143+
"azure",
144+
"azure-cognitive-services",
145+
"azure-metrics-advisor"
146+
],
147+
"requiredResources": {
148+
"Azure Cognitive Services account": "https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account"
149+
},
150+
"skipFolder": false,
151+
"disableDocsMs": false,
152+
"apiRefLink": "https://docs.microsoft.com/javascript/api/@azure/ai-metrics-advisor/"
141153
}
142154
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary This sample demonstrates Alerting Configuration CRUD operations.
6+
* @azsdk-weight 50
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+
AnomalyAlertConfiguration
17+
} from "@azure/ai-metrics-advisor";
18+
19+
main()
20+
.then((_) => {
21+
console.log("Succeeded");
22+
})
23+
.catch((err) => {
24+
console.log("Error occurred:");
25+
console.log(err);
26+
});
27+
28+
export async function main() {
29+
// You will need to set these environment variables or edit the following values
30+
const endpoint = process.env["METRICS_ADVISOR_ENDPOINT"] || "<service endpoint>";
31+
const subscriptionKey = process.env["METRICS_ADVISOR_SUBSCRIPTION_KEY"] || "<subscription key>";
32+
const apiKey = process.env["METRICS_ADVISOR_API_KEY"] || "<api key>";
33+
const detectionConfigId =
34+
process.env["METRICS_ADVISOR_DETECTION_CONFIG_ID"] || "<detection config id>";
35+
const hookId = process.env["METRICS_ADVISOR_HOOK_ID"] || "<existing hook id>";
36+
37+
const credential = new MetricsAdvisorKeyCredential(subscriptionKey, apiKey);
38+
const adminClient = new MetricsAdvisorAdministrationClient(endpoint, credential);
39+
40+
const created = await createAlertConfig(adminClient, detectionConfigId);
41+
42+
await updateAlertConfig(adminClient, created.id!, detectionConfigId, [hookId]);
43+
44+
await listAlertConfig(adminClient, detectionConfigId);
45+
46+
await deleteAlertConfig(adminClient, created.id!);
47+
}
48+
49+
// create a new alerting configuration
50+
async function createAlertConfig(
51+
adminClient: MetricsAdvisorAdministrationClient,
52+
detectionConfigId: string
53+
) {
54+
console.log("Creating a new alerting configuration...");
55+
const alertConfig: Omit<AnomalyAlertConfiguration, "id"> = {
56+
name: "js alerting config name " + new Date().getTime().toString(),
57+
crossMetricsOperator: "AND",
58+
metricAlertConfigurations: [
59+
{
60+
detectionConfigurationId: detectionConfigId,
61+
alertScope: {
62+
scopeType: "All"
63+
}
64+
},
65+
{
66+
detectionConfigurationId: detectionConfigId,
67+
alertScope: {
68+
scopeType: "Dimension",
69+
dimensionAnomalyScope: { city: "Manila", category: "Handmade" }
70+
}
71+
}
72+
],
73+
hookIds: [],
74+
description: "alerting config description"
75+
};
76+
const result = await adminClient.createAlertConfig(alertConfig);
77+
console.log(result);
78+
return result;
79+
}
80+
81+
// updating an alerting configuration
82+
async function updateAlertConfig(
83+
adminClient: MetricsAdvisorAdministrationClient,
84+
alertConfigId: string,
85+
detectionConfigId: string,
86+
hookIds: string[]
87+
) {
88+
const patch: Omit<AnomalyAlertConfiguration, "id"> = {
89+
name: "new Name",
90+
//description: "new description",
91+
hookIds,
92+
crossMetricsOperator: "OR",
93+
metricAlertConfigurations: [
94+
{
95+
detectionConfigurationId: detectionConfigId,
96+
alertScope: {
97+
scopeType: "All"
98+
}
99+
},
100+
{
101+
detectionConfigurationId: detectionConfigId,
102+
alertScope: {
103+
scopeType: "Dimension",
104+
dimensionAnomalyScope: {
105+
city: "Kolkata",
106+
category: "Shoes Handbags & Sunglasses"
107+
}
108+
}
109+
}
110+
]
111+
};
112+
console.log(`Updating alerting configuration ${detectionConfigId}`);
113+
const updated = await adminClient.updateAlertConfig(alertConfigId, patch);
114+
return updated;
115+
}
116+
117+
async function deleteAlertConfig(
118+
adminClient: MetricsAdvisorAdministrationClient,
119+
alertConfigId: string
120+
) {
121+
console.log(`Deleting alerting configuration ${alertConfigId}`);
122+
await adminClient.deleteAlertConfig(alertConfigId);
123+
}
124+
125+
async function listAlertConfig(
126+
adminClient: MetricsAdvisorAdministrationClient,
127+
detectdionConfigId: string
128+
) {
129+
console.log(`Listing alert configurations for detection configuration ${detectdionConfigId}`);
130+
let i = 1;
131+
const iterator = adminClient.listAlertConfigs(detectdionConfigId);
132+
for await (const config of iterator) {
133+
console.log(`Alert configuration ${i++}`);
134+
console.log(config);
135+
}
136+
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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

Comments
 (0)