Exemplo n.º 1
0
async def sample_create_data_feed_async():
    # [START create_data_feed_async]
    from azure.ai.metricsadvisor import MetricsAdvisorKeyCredential
    from azure.ai.metricsadvisor.aio import MetricsAdvisorAdministrationClient
    from azure.ai.metricsadvisor.models import (
        SQLServerDataFeed,
        DataFeedSchema,
        Metric,
        Dimension,
        DataFeedOptions,
        DataFeedRollupSettings,
        DataFeedMissingDataPointFillSettings
    )

    service_endpoint = os.getenv("METRICS_ADVISOR_ENDPOINT")
    subscription_key = os.getenv("METRICS_ADVISOR_SUBSCRIPTION_KEY")
    api_key = os.getenv("METRICS_ADVISOR_API_KEY")
    sql_server_connection_string = os.getenv("METRICS_ADVISOR_SQL_SERVER_CONNECTION_STRING")
    query = os.getenv("METRICS_ADVISOR_SQL_SERVER_QUERY")

    client = MetricsAdvisorAdministrationClient(service_endpoint,
                                  MetricsAdvisorKeyCredential(subscription_key, api_key))

    async with client:
        data_feed = await client.create_data_feed(
            name="My data feed",
            source=SQLServerDataFeed(
                connection_string=sql_server_connection_string,
                query=query,
            ),
            granularity="Daily",
            schema=DataFeedSchema(
                metrics=[
                    Metric(name="cost", display_name="Cost"),
                    Metric(name="revenue", display_name="Revenue")
                ],
                dimensions=[
                    Dimension(name="category", display_name="Category"),
                    Dimension(name="city", display_name="City")
                ],
                timestamp_column="Timestamp"
            ),
            ingestion_settings=datetime.datetime(2019, 10, 1),
            options=DataFeedOptions(
                data_feed_description="cost/revenue data feed",
                rollup_settings=DataFeedRollupSettings(
                    rollup_type="AutoRollup",
                    rollup_method="Sum",
                    rollup_identification_value="__CUSTOM_SUM__"
                ),
                missing_data_point_fill_settings=DataFeedMissingDataPointFillSettings(
                    fill_type="SmartFilling"
                ),
                access_mode="Private"
            )
        )

        return data_feed
Exemplo n.º 2
0
 def _create_data_feed_for_update(self, name):
     data_feed_name = create_random_name(name)
     return self.admin_client.create_data_feed(
         DataFeed(
             name=data_feed_name,
             source=SQLServerDataFeed(
                 connection_string=self.sql_server_connection_string,
                 query='select * from adsample2 where Timestamp = @StartTime'
             ),
             granularity=DataFeedGranularity(
                 granularity_type="Daily",
             ),
             schema=DataFeedSchema(
                 metrics=[
                     DataFeedMetric(name="cost", display_name="display cost", description="the cost"),
                     DataFeedMetric(name="revenue", display_name="display revenue", description="the revenue")
                 ],
                 dimensions=[
                     DataFeedDimension(name="category", display_name="display category"),
                     DataFeedDimension(name="city", display_name="display city")
                 ],
                 timestamp_column="Timestamp"
             ),
             ingestion_settings=DataFeedIngestionSettings(
                 ingestion_begin_time=datetime.datetime(2019, 10, 1),
                 data_source_request_concurrency=0,
                 ingestion_retry_delay=-1,
                 ingestion_start_offset=-1,
                 stop_retry_after=-1,
             ),
             options=DataFeedOptions(
                 admin_emails=["*****@*****.**"],
                 data_feed_description="my first data feed",
                 missing_data_point_fill_settings=DataFeedMissingDataPointFillSettings(
                     fill_type="SmartFilling"
                 ),
                 rollup_settings=DataFeedRollupSettings(
                     rollup_type="NoRollup",
                     rollup_method="None",
                 ),
                 viewer_emails=["viewers"],
                 access_mode="Private",
                 action_link_template="action link template"
             )
         )
     )
    def test_create_data_feed_from_sql_server(self):

        data_feed_name = self.create_random_name("testfeed")
        try:
            data_feed = self.admin_client.create_data_feed(
                name=data_feed_name,
                source=SQLServerDataFeed(
                    connection_string=self.sql_server_connection_string,
                    query=u"select * from adsample2 where Timestamp = @StartTime"
                ),
                granularity=DataFeedGranularity(
                    granularity_type="Daily",
                ),
                schema=DataFeedSchema(
                    metrics=[
                        Metric(name="cost", display_name="display cost", description="the cost"),
                        Metric(name="revenue", display_name="display revenue", description="the revenue")
                    ],
                    dimensions=[
                        Dimension(name="category", display_name="display category"),
                        Dimension(name="city", display_name="display city")
                    ],
                    timestamp_column="Timestamp"
                ),
                ingestion_settings=DataFeedIngestionSettings(
                    ingestion_begin_time=datetime.datetime(2019, 10, 1),
                    data_source_request_concurrency=0,
                    ingestion_retry_delay=-1,
                    ingestion_start_offset=-1,
                    stop_retry_after=-1,
                ),
                options=DataFeedOptions(
                    admins=["*****@*****.**"],
                    data_feed_description="my first data feed",
                    missing_data_point_fill_settings=DataFeedMissingDataPointFillSettings(
                        fill_type="SmartFilling"
                    ),
                    rollup_settings=DataFeedRollupSettings(
                        rollup_type="NoRollup",
                        rollup_method="None",
                    ),
                    viewers=["viewers"],
                    access_mode="Private",
                    action_link_template="action link template"
                )

            )
            self.assertIsNotNone(data_feed.id)
            self.assertIsNotNone(data_feed.created_time)
            self.assertIsNotNone(data_feed.name)
            self.assertEqual(data_feed.source.data_source_type, "SqlServer")
            self.assertIsNotNone(data_feed.source.connection_string)
            self.assertIsNotNone(data_feed.source.query)
            self.assertEqual(data_feed.granularity.granularity_type, "Daily")
            self.assertEqual(data_feed.granularity.custom_granularity_value, None)
            self.assertEqual(data_feed.schema.metrics[0].name, "cost")
            self.assertEqual(data_feed.schema.metrics[1].name, "revenue")
            self.assertEqual(data_feed.schema.metrics[0].display_name, "display cost")
            self.assertEqual(data_feed.schema.metrics[1].display_name, "display revenue")
            self.assertEqual(data_feed.schema.metrics[0].description, "the cost")
            self.assertEqual(data_feed.schema.metrics[1].description, "the revenue")
            self.assertEqual(data_feed.schema.dimensions[0].name, "category")
            self.assertEqual(data_feed.schema.dimensions[1].name, "city")
            self.assertEqual(data_feed.schema.dimensions[0].display_name, "display category")
            self.assertEqual(data_feed.schema.dimensions[1].display_name, "display city")
            self.assertEqual(data_feed.ingestion_settings.ingestion_begin_time,
                             datetime.datetime(2019, 10, 1, tzinfo=tzutc()))
            self.assertEqual(data_feed.ingestion_settings.data_source_request_concurrency, 0)
            self.assertEqual(data_feed.ingestion_settings.ingestion_retry_delay, -1)
            self.assertEqual(data_feed.ingestion_settings.ingestion_start_offset, -1)
            self.assertEqual(data_feed.ingestion_settings.stop_retry_after, -1)
            self.assertIn("*****@*****.**", data_feed.options.admins)
            self.assertEqual(data_feed.options.data_feed_description, "my first data feed")
            self.assertEqual(data_feed.options.missing_data_point_fill_settings.fill_type, "SmartFilling")
            self.assertEqual(data_feed.options.rollup_settings.rollup_type, "NoRollup")
            self.assertEqual(data_feed.options.rollup_settings.rollup_method, "None")
            self.assertEqual(data_feed.options.viewers, ["viewers"])
            self.assertEqual(data_feed.options.access_mode, "Private")
            self.assertEqual(data_feed.options.action_link_template, "action link template")
            self.assertEqual(data_feed.status, "Active")
            self.assertTrue(data_feed.is_admin)
            self.assertIsNotNone(data_feed.metric_ids)

        finally:
            self.admin_client.delete_data_feed(data_feed.id)

            with self.assertRaises(ResourceNotFoundError):
                self.admin_client.get_data_feed(data_feed.id)