示例#1
0
        public async Task <IActionResult> Index()
        {
            var ARMtoken = await MsiHelper.GetToken("https://management.azure.com/");

            var allCosmos = await GetAllCosmosDb(ARMtoken);

            ViewData["AllCosmosNames"] = new List <string>(allCosmos.Select(c => c.Name));
            ViewData["AllCosmosJson"]  = PrettyStringHelper.JsonPrettify(JsonConvert.SerializeObject(allCosmos));
            // replace if in query, otherwise use defaults
            if (HttpContext.Request.Query.ContainsKey("rg"))
            {
                cosmos_rg = HttpContext.Request.Query["rg"];
            }
            if (HttpContext.Request.Query.ContainsKey("name"))
            {
                cosmos_name = HttpContext.Request.Query["name"];
            }
            ViewData["CosmosName"] = cosmos_name;
            var cosmosKeys = await CosmosHelper.GetKeysAsync(ARMtoken, subscription_id, cosmos_rg, cosmos_name);

            ViewData["NumKeys"] = cosmosKeys?.Keys?.Count.ToString() ?? "Failed to get keys for " + cosmos_name + " in rg " + cosmos_rg;
            if (cosmosKeys?.Keys != null)
            {
                ViewData["KeyNamesJson"] = PrettyStringHelper.JsonPrettify(JsonConvert.SerializeObject(cosmosKeys.Keys));
            }
            else
            {
                ViewData["KeyNamesJson"] = "[]";
            }
            return(View());
        }
        private async Task <IActionResult> HandleCloudEvent(string jsonContent)
        {
            var details = JsonConvert.DeserializeObject <CloudEvent <dynamic> >(jsonContent);
            await CosmosHelper.CreateDocumentAsync("eventgriddata", "events", details);

            return(Ok());
        }
        private void  MockEvent(string reference, string uri, string expectedType, bool pass = true)
        {
            ContentEvent mockItem = new ContentEvent();

            mockItem.eventType        = expectedType;
            mockItem.id               = Guid.NewGuid().ToString();
            mockItem.subject          = "/content/sharedcontent/29849536-f716-468d-bf49-41446cb68284";
            mockItem.eventTime        = "";
            mockItem.dataVersion      = "";
            mockItem.metadataVersion  = "";
            mockItem.topic            = "";
            mockItem.data.itemId      = reference;
            mockItem.data.api         = uri;
            mockItem.data.versionId   = Guid.NewGuid().ToString();
            mockItem.data.displayText = "some content";
            mockItem.data.author      = "Fred";

            string response;

            CosmosHelper.Initialise(_scenarioContext.GetEnv().eventStoreEndPoint, _scenarioContext.GetEnv().eventStoreKey);

            CosmosHelper.InsertDocumentFromJson <ContentEvent>("EventStore",
                                                               "events",
                                                               mockItem,
                                                               out response);
        }
示例#4
0
        private static async Task StoreBreakdownJsonInCosmos(VideoBreakdown videoBreakdownJson, TraceWriter log)
        {
            //string CosmosCollectionName = ConfigurationManager.AppSettings["CosmosCollectionName"];
            //if (String.IsNullOrEmpty(CosmosCollectionName))
            //    throw new ApplicationException("CosmosCollectionName app setting not set");
            var cosmosHelper   = new CosmosHelper(log);
            var collectionName = "Breakdowns";
            var client         = cosmosHelper.GetCosmosClient(collectionName);
            var json           = JsonConvert.SerializeObject(videoBreakdownJson);

            cosmosHelper.LogMessage($"saving json: {json}");
            // save the json as a new document
            try
            {
                Document r =
                    await client.UpsertDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(CosmosHelper.CosmosDatabasename, collectionName),
                        videoBreakdownJson);
            }
            catch (Exception e)
            {
                cosmosHelper.LogMessage($"error inserting document in cosmos: {e.Message}");
                // ignore for now, but maybe should replace the document if it already exists..
                // seems to be caused by dev environment where queue items are being reprocesssed
            }
        }
示例#5
0
        public static async Task <IActionResult> GetAllHealthRadarResults([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            log.LogInformation("Return a list of all users health radar results");
            var results = await CosmosHelper.GetAllResultsFromCosmosDb();

            return(new OkObjectResult(results));
        }
示例#6
0
        public void GetDocumentBatch()
        {
            const string hashResult = "4FD33DCDE4707D09696A";
            var          result     = CosmosHelper.DocumentBatch("device", 100, 1);
            var          test       = "";

            //Assert.AreEqual(hashResult, hash);
        }
示例#7
0
        public static async Task RunAsync(
            [QueueTrigger("%EncodingCompleteQueue%", Connection = "AzureWebJobsStorage")] string myQueueItem, TraceWriter log)
        {
            var videoIndexerHelper = new VideoIndexerHelper(log);
            var cosmosHelper       = new CosmosHelper(log);
            var msg = JsonConvert.DeserializeObject <NotificationMessage>(myQueueItem);

            if (msg.EventType != NotificationEventType.TaskStateChange)
            {
                return; // ignore anything but job complete
            }
            var newJobStateStr = msg.Properties.FirstOrDefault(j => j.Key == Constants.NEWSTATE).Value;

            if (newJobStateStr == Enums.StateEnum.Finished.ToString())
            {
                var jobId  = msg.Properties["JobId"];
                var taskId = msg.Properties["TaskId"];

                _context = MediaServicesHelper.Context;

                var job = _context.Jobs.Where(j => j.Id == jobId).FirstOrDefault();
                if (job == null)
                {
                    videoIndexerHelper.LogMessage($"Job for JobId:{jobId} is null");
                    return;
                }
                var task = job.Tasks.Where(l => l.Id == taskId).FirstOrDefault();
                if (task == null)
                {
                    videoIndexerHelper.LogMessage($"Task for taskId:{taskId} is null");
                    return;
                }
                var outputAsset = task.OutputAssets[0];
                var inputAsset  = task.InputAssets[0];


                cosmosHelper.LogMessage("Read policy");
                var readPolicy =
                    _context.AccessPolicies.Create("readPolicy", TimeSpan.FromHours(4), AccessPermissions.Read);
                var outputLocator = _context.Locators.CreateLocator(LocatorType.Sas, outputAsset, readPolicy);
                cosmosHelper.LogMessage("Create cloud blob client");
                var destBlobStorage = BlobHelper.AmsStorageAccount.CreateCloudBlobClient();
                cosmosHelper.LogMessage("get asset container");
                // Get the asset container reference
                var outContainerName = new Uri(outputLocator.Path).Segments[1];
                var outContainer     = destBlobStorage.GetContainerReference(outContainerName);
                cosmosHelper.LogMessage("use largest single mp4 ");
                // use largest single mp4 output (highest bitrate) to send to Video Indexer
                var biggestblob = outContainer.ListBlobs().OfType <CloudBlockBlob>()
                                  .Where(b => b.Name.ToLower().EndsWith(".mp4"))
                                  .OrderBy(u => u.Properties.Length).Last();
                cosmosHelper.LogMessage("GetSasUrl");
                var sas = videoIndexerHelper.GetSasUrl(biggestblob);
                cosmosHelper.LogMessage(" submit to VI ");
                // Submit processing job to Video Indexer
                await videoIndexerHelper.SubmitToVideoIndexerAsync(biggestblob.Name, sas, inputAsset.AlternateId, log);
            }
        }
        private async Task <IActionResult> HandleGridEvents(string jsonContent)
        {
            var events = JArray.Parse(jsonContent);

            foreach (var e in events)
            {
                var details = JsonConvert.DeserializeObject <GridEvent <dynamic> >(e.ToString());
                await CosmosHelper.CreateDocumentAsync("eventgriddata", "events", details);
            }
            return(Ok());
        }
        private List <ContentEvent> GetMatchingDocuments(string id, string eventType = "")
        {
            string additionalClause = eventType.Equals(string.Empty) ? string.Empty : $" and c.eventType = '{eventType.ToLower()}'";
            string query            = $"SELECT * FROM c where  c.subject = '{id}'{additionalClause}";

            CosmosHelper.Initialise(_scenarioContext.GetEnv().eventStoreEndPoint, _scenarioContext.GetEnv().eventStoreKey);
            List <ContentEvent> list = CosmosHelper.SearchForDocuments <ContentEvent>("dfc-eventstore", "events", query);

            Console.WriteLine($"Check Event Store: found {list.Count} with query '{query}'");
            return(list);
        }
示例#10
0
        public ContainerEditorViewModel(CosmosContainer?container, int?throughput, bool databaseHasProvisionedThroughput, bool isServerlessAccount)
        {
            _id                  = container?.Id ?? string.Empty;
            _eTag                = container?.ETag;
            _throughput          = isServerlessAccount ? 0 : throughput ?? 400;
            _provisionThroughput = throughput.HasValue;
            _partitionKeyPath    = container?.PartitionKeyPath ?? string.Empty;
            _largePartitionKey   = container?.LargePartitionKey ?? false;
            IsServerlessAccount  = isServerlessAccount;
            if (container?.DefaultTTL is int defaultTTL)
            {
                _enableTTL     = true;
                _hasDefaultTTL = defaultTTL >= 0;
                _defaultTTL    = Math.Max(defaultTTL, 1);
            }
            else
            {
                _enableTTL     = false;
                _hasDefaultTTL = false;
                _defaultTTL    = 1;
            }

            IsNew = container is null;

            if (isServerlessAccount)
            {
                _provisionThroughput         = false;
                CanChangeProvisionThroughput = false;
            }
            else if (databaseHasProvisionedThroughput)
            {
                CanChangeProvisionThroughput = IsNew;
            }
            else
            {
                _provisionThroughput         = true;
                CanChangeProvisionThroughput = false;
            }

            Title = IsNew
                ? "Add container"
                : "Edit container";

            _saveCommand = new DelegateCommand(Save, CanSave);
            AddOkButton(button => button.Command = _saveCommand);
            AddCancelButton();

            Validator = new ViewModelValidator <ContainerEditorViewModel>(this);
            Validator.AddValidator(vm => vm.Id, id => CosmosHelper.ValidateId(id, "container id"));
            Validator.AddValidator(vm => vm.Throughput, throughput => CosmosHelper.ValidateThroughput(throughput, ProvisionThroughput));
            Validator.AddValidator(vm => vm.DefaultTTL, defaultTTL => CosmosHelper.ValidateDefaultTTL(defaultTTL, EnableTTL && HasDefaultTTL));
            Validator.AddValidator(vm => vm.PartitionKeyPath, CosmosHelper.ValidatePartitionKeyPath);
        }
示例#11
0
        public static async Task <bool> Import([ActivityTrigger] int batch, ILogger log)
        {
            log.LogInformation($"Prepare documents for batch {batch}");
            string partitionKey = Environment.GetEnvironmentVariable("PartitionKey");
            int    docsPerBatch;

            int.TryParse(Environment.GetEnvironmentVariable("DocsPerBatch"), out docsPerBatch);
            List <string> documentsToImportInBatch = CosmosHelper.DocumentBatch(partitionKey, docsPerBatch, batch);
            await BulkImport.BulkImportDocuments(documentsToImportInBatch);

            return(true);
        }
        private async Task <JsonResult> HandleValidation(string jsonContent)
        {
            var gridEvent = JsonConvert.DeserializeObject <List <GridEvent <Dictionary <string, string> > > >(jsonContent)
                            .First();
            await CosmosHelper.CreateDocumentAsync("eventgriddata", "events", gridEvent);

            // Retrieve the validation code and echo back.
            var validationCode = gridEvent.Data["validationCode"];

            return(new JsonResult(new
            {
                validationResponse = validationCode
            }));
        }
示例#13
0
        public NotificationsController(IConfiguration config, ILogger <NotificationsController> logger)
        {
            _logger = logger;
            //logger.LogWarning("NotificationsController called!");
            this.Configuration = config;
            if (cosmos == null)
            {
                cosmos = new CosmosHelper(config);
            }

            TelemetryConfiguration telemetryConfig = TelemetryConfiguration.CreateDefault();

            telemetryConfig.InstrumentationKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
            this.telemetryClient = new TelemetryClient(telemetryConfig);
            //this.telemetryClient.InstrumentationKey = Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
        }
示例#14
0
        public async Task <IActionResult> Demo()
        {
            ViewData["SubscriptionId"] = subscription_id;
            try
            {
                var ARMtoken = await MsiHelper.GetToken("https://management.azure.com/");

                ViewData["ARMTokenStatus"] = string.IsNullOrEmpty(ARMtoken) ? "Failed to get token for management.azure.com/" : "Got an ARM token for management.azure.com/";

                var storageToken = await MsiHelper.GetToken("https://storage.azure.com/");

                ViewData["BlobTokenStatus"] = string.IsNullOrEmpty(storageToken) ?  "Failed to get token for storage.azure.com/" : "Got an ARM token for storage.azure.com/";

                if (string.IsNullOrEmpty(ARMtoken))
                {
                    ViewData["NumKeys"] = "Get ARMtoken was unsuccessful, won't attempt to get COSMOS keys";
                }
                else
                {
                    // let's get this from input
                    //var cosmosKey = CosmosHelper.GetKeys(ARMtoken, "e39a92b5-b9a4-43d1-97a3-c31c819a583a", "istiotest", "msitester-table");
                    var cosmosKey = await CosmosHelper.GetKeysAsync(ARMtoken, subscription_id, cosmos_rg, cosmos_name);

                    ViewData["NumKeys"] = cosmosKey?.Keys?.Count.ToString() ?? "Failed to get keys for " + cosmos_name + " in rg " + cosmos_rg;
                }
                if (string.IsNullOrEmpty(storageToken))
                {
                    ViewData["Containers"] = "Get StorageToken was unsuccessful";
                }
                else
                {
                    ViewData["ContainersXML"] = AzureStorageHelper.GetAllContainerNamesXml(storageToken, storage_account);
                }
                var rgs = await AzureServiceHelper.GetResourceGroups(ARMtoken, subscription_id);

                ViewData["ResourceGroups"] = new List <string> (rgs.Select(r => r.Name).ToList());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ViewData["Exception"]      = ex?.Message;
                ViewData["InnerException"] = ex?.InnerException?.Message;
            }

            return(View());
        }
示例#15
0
        public static async Task <IActionResult> SaveUserHealthRadarResult([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
        {
            log.LogInformation("Save Health Radar Result: " + req.Body);

            string requestBody = String.Empty;

            using (StreamReader streamReader = new StreamReader(req.Body))
            {
                requestBody = await streamReader.ReadToEndAsync();
            }
            log.LogInformation(requestBody);
            dynamic data = JsonConvert.DeserializeObject(requestBody);

            var    result = new HealthRadarResult();
            string tempId = data.id;

            if (!string.IsNullOrEmpty(tempId))
            {
                result.Id = new Guid(tempId);
            }
            result.Area    = data.area;
            result.Role    = data.role;
            result.Answers = data.answers.ToObject <List <int> >();
            result.Posted  = DateTime.Now;

            if (result.Id == Guid.Empty)
            {
                log.LogInformation("Create new record");
                await CosmosHelper.CreateResultInCosmosDb(result);
            }
            else
            {
                try
                {
                    log.LogInformation("Update record: " + result.Id);
                    await CosmosHelper.UpdateResultInCosmosDb(result);
                }catch (Exception e)
                {
                    log.LogInformation("Create new record");
                    await CosmosHelper.CreateResultInCosmosDb(result);
                }
            }

            return(new OkObjectResult(result.Id));
        }
 public Step(
     ScenarioContext scenarioContext,
     FeatureContext featureContext,
     HttpHelper httphelper,
     EnvironmentSettings settings,
     HttpResponseHelper assertHelper,
     SqlHelper sqlhelper,
     CosmosHelper cosmosHelper)
 {
     _scenarioContext = scenarioContext;
     _featureContext  = featureContext;
     _settings        = settings;
     _httpHelper      = httphelper;
     _assertionHelper = assertHelper;
     _sqlHelper       = sqlhelper;
     _cosmosHelper    = cosmosHelper;
     _touchPointId    = _settings.TestEndpoint01;
 }
        public DatabaseEditorViewModel(CosmosDatabase?database, int?throughput, bool isServerlessAccount)
        {
            _id                    = database?.Id ?? string.Empty;
            _eTag                  = database?.ETag;
            _throughput            = isServerlessAccount ? 0 : throughput ?? 400;
            _provisionThroughput   = throughput.HasValue;
            IsNew                  = database is null;
            CanProvisionThroughput = IsNew && !isServerlessAccount;
            IsServerlessAccount    = isServerlessAccount;

            Title = IsNew
                ? "Add database"
                : "Edit database";

            _saveCommand = new DelegateCommand(Save, CanSave);
            AddOkButton(button => button.Command = _saveCommand);
            AddCancelButton();

            Validator = new ViewModelValidator <DatabaseEditorViewModel>(this);
            Validator.AddValidator(vm => vm.Id, id => CosmosHelper.ValidateId(id, "container id"));
            Validator.AddValidator(vm => vm.Throughput, throughput => CosmosHelper.ValidateThroughput(throughput, ProvisionThroughput));
        }
示例#18
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var requestBody         = await new StreamReader(req.Body).ReadToEndAsync();
            var resourceHealthAlert = JsonConvert.DeserializeObject <ResourceHealthAlert>(requestBody);

            var alertObj = CosmosHelper.GetDtoMapping(resourceHealthAlert);

            log.LogInformation("Resource Health Alert Id: " + alertObj.alertId);
            log.LogInformation("Resource Subscription: " + alertObj.subscriptionId);
            log.LogInformation("Resource Id: " + alertObj.resourceId);
            log.LogInformation("Resource Status: " + alertObj.currentHealthStatus);

            var collectionId = GetEnvironmentVariable("CosmosDb_Collection");
            var databaseId   = GetEnvironmentVariable("CosmosDb_Database");

            CosmosClient client = new CosmosClient(GetEnvironmentVariable("CosmosDb_Uri"), GetEnvironmentVariable("CosmosDb_Key"));
            ItemResponse <ResourceHealthDto> response = await client.GetContainer(databaseId, collectionId).CreateItemAsync(alertObj, new PartitionKey(alertObj.resourceId));

            log.LogInformation("Document created in Cosmos: " + response.StatusCode);

            return(new OkObjectResult("Resource Health Alert Processed"));
        }
示例#19
0
        public static async Task Run([QueueTrigger("%InputQueue%", Connection = "AzureWebJobsStorage")] VippyProcessingState manifest,
                                     [Blob("%AmsBlobInputContainer%/{BlobName}", FileAccess.ReadWrite)] CloudBlockBlob videoBlobTriggered,
                                     [Blob("%ExistingAmsBlobInputContainer%/{BlobName}", FileAccess.ReadWrite)] CloudBlockBlob videoBlobExisting,

                                     TraceWriter log)
        {
            //================================================================================
            // Function AMSInputQueueHandler
            // Purpose:
            // This is where the start of the pipeline work begins. It will submit an encoding
            // job to Azure Media Services.  When that job completes asyncronously, a notification
            // webhook will be called by AMS which causes the next stage of the pipeline to
            // continue.
            //================================================================================
            CloudBlockBlob videoBlob = null;

            if (manifest.Origin == Enums.OriginEnum.Existing)
            {
                videoBlob = videoBlobExisting;
            }
            else if (manifest.Origin == Enums.OriginEnum.Trigger)
            {
                videoBlob = videoBlobTriggered;
            }
            if (videoBlob == null)
            {
                log.Error("There is being an error, videoblog not initialized, not marked as existing or trigger or video is null");
                return;
            }
            var context               = MediaServicesHelper.Context;
            var cosmosHelper          = new CosmosHelper(log);
            var toDeleteContainerName = Environment.GetEnvironmentVariable("AmsBlobToDeleteContainer");

            // only set the starttime if it wasn't already set in blob watcher function (that way
            // it works if the job is iniaited by using this queue directly
            if (manifest.StartTime == null)
            {
                manifest.StartTime = DateTime.Now;
            }

            var videofileName = videoBlob.Name;

            // get a new asset from the blob, and use the file name if video title attribute wasn't passed.
            IAsset newAsset;

            try
            {
                newAsset = BlobHelper.CreateAssetFromBlob(videoBlob, videofileName, log)
                           .GetAwaiter().GetResult();
            }
            catch (Exception e)
            {
                throw new ApplicationException($"Error occured creating asset from Blob;/r/n{e.Message}");
            }

            // If an internal_id was passed in the metadata, use it within AMS (AlternateId) and Cosmos(Id - main document id) for correlation.
            // if not, generate a unique id.  If the same id is ever reprocessed, all stored metadata
            // will be overwritten.

            newAsset.AlternateId = manifest.AlternateId;
            newAsset.Update();

            manifest.AmsAssetId = newAsset.Id;

            log.Info($"Deleting  the file  {videoBlob.Name} from the container ");

            //move the video to the to delete folder
            await BlobHelper.Move(videoBlob.Container.Name, toDeleteContainerName, videofileName, log);

            //move the manifest to the to delete folder
            string manifestName = videofileName.Remove(videofileName.IndexOf('.')) + ".json";

            log.Info($"Deleting  the file  {manifestName} from the container ");
            await BlobHelper.Move(videoBlob.Container.Name, toDeleteContainerName, manifestName, log);

            // copy blob into new asset
            // create the encoding job
            var job = context.Jobs.Create("MES encode from input container - ABR streaming");

            // Get a media processor reference, and pass to it the name of the
            // processor to use for the specific task.
            var processor = MediaServicesHelper.GetLatestMediaProcessorByName("Media Encoder Standard");

            var task = job.Tasks.AddNew("encoding task",
                                        processor,
                                        "Content Adaptive Multiple Bitrate MP4",
                                        TaskOptions.None
                                        );

            task.Priority = 100;
            task.InputAssets.Add(newAsset);

            // setup webhook notification
            var keyBytes = new byte[32];

            // Check for existing Notification Endpoint with the name "FunctionWebHook"
            var existingEndpoint = context.NotificationEndPoints.Where(e => e.Name == "FunctionWebHook").FirstOrDefault();
            INotificationEndPoint endpoint;

            //if (existingEndpoint != null)
            //{
            //    endpoint = existingEndpoint;
            //}
            //else
            try
            {
                endpoint = context.NotificationEndPoints.Create("FunctionWebHook",
                                                                NotificationEndPointType.WebHook, WebHookEndpoint, keyBytes);
            }
            catch (Exception)
            {
                throw new ApplicationException(
                          $"The endpoing address specified - '{WebHookEndpoint}' is not valid.");
            }

            task.TaskNotificationSubscriptions.AddNew(NotificationJobState.FinalStatesOnly, endpoint, false);
            cosmosHelper.LogMessage($"Add an output asset to contain the results of the job");
            // Add an output asset to contain the results of the job.
            // This output is specified as AssetCreationOptions.None, which
            // means the output asset is not encrypted.
            task.OutputAssets.AddNew(videofileName, AssetCreationOptions.None);

            // Starts the job in AMS.  AMS will notify the webhook when it completes
            job.Submit();
            cosmosHelper.LogMessage($"Saving on cosmos DB");
            // update processing progress with id and metadata payload
            await cosmosHelper.StoreProcessingStateRecordInCosmosAsync(manifest);

            cosmosHelper.LogMessage($"AMS encoding job submitted for {videofileName}");
        }
示例#20
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log
            //,[Queue("retryresources", Connection = "AzureWebJobsStorage")] ICollector<ResourceHealthDto> retryQueue
            )
        {
            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation("requestBodyis");
            log.LogInformation(requestBody);

            //Process ResourceHealth Alert
            if (requestBody.Contains("Resource Health"))
            {
                var resourceHealthAlert = JsonConvert.DeserializeObject <ResourceHealthAlert>(requestBody);
                log.LogInformation(resourceHealthAlert.schemaId);
                var alertObj        = CosmosHelper.GetResourceHealthDtoMapping(resourceHealthAlert);
                var alertHistoryObj = CosmosHelper.GetDtoHistoryMapping(resourceHealthAlert);

                log.LogInformation("Resource Health Alert Id: " + alertObj.alertId);
                log.LogInformation("Resource Subscription: " + alertObj.subscriptionId);
                log.LogInformation("Resource Id: " + alertObj.resourceId);
                log.LogInformation("Resource Status: " + alertObj.currentHealthStatus);

                try
                {
                    List <string> subs = new List <string>
                    {
                        alertObj.subscriptionId
                    };
                    string token = AuthHelper.GetTokenAsync().Result;
                    log.LogInformation($"Token Received: {token}");

                    /*  Below is to do the quick testing for resolution of access token
                     * string linkId = "subscriptions/a7f5830b-4c53-4a55-a641-bc07ef502ab2/resourcegroups/rp-gmsa-rg/providers/microsoft.compute/virtualmachines/rpwinapp01";
                     * string resourceGraphUri = $"https://management.azure.com/{linkId}?api-version=2016-09-01";
                     * ResourceGraphResponse resourcesGraph = await ResilientRestClient.GetAsync<ResourceGraphResponse>(resourceGraphUri, token);
                     */

                    string resourceGraphUri          = "https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01";
                    string query                     = $"where id =~ '{alertObj.resourceId}' | project location";
                    Dictionary <string, int> options = new Dictionary <string, int>();
                    options["$skip"] = 0;
                    Dictionary <string, object> resourceObj = new Dictionary <string, object>();

                    resourceObj.Add("subscriptions", subs);
                    resourceObj.Add("query", query);
                    resourceObj.Add("options", options);

                    ResourceGraphResponse resourcesGraph = ResilientRestClient.PostAsync <ResourceGraphResponse>(resourceGraphUri, token, resourceObj).Result;
                    alertObj.location        = resourcesGraph.data.rows[0][0].ToString();
                    alertHistoryObj.location = resourcesGraph.data.rows[0][0].ToString();
                }
                catch (System.Exception)
                {
                    alertObj.location        = "NA";
                    alertHistoryObj.location = "NA";
                    log.LogError($"Unable to get location for {alertObj.resourceId}.  Set default for NA location");
                }

                var collectionId        = GetEnvironmentVariable("CosmosDb_Collection");
                var databaseId          = GetEnvironmentVariable("CosmosDb_Database");
                var collectionHistoryId = GetEnvironmentVariable("CosmosDb_HistoryCollection");

                try
                {
                    ItemResponse <ResourceHealthDto> response = await client.GetContainer(databaseId, collectionId).UpsertItemAsync(alertObj, new PartitionKey(alertObj.resourceId));

                    log.LogInformation("Document created in Cosmos: " + response.StatusCode);
                }
                catch (CosmosException ex)
                {
                    log.LogInformation("error created in Cosmos: " + ex.Message);
                    log.LogInformation("Add Object to Retry Queue: " + ex.Message);
                    // retryQueue.Add(alertObj);
                }

                try
                {
                    ItemResponse <ResourceHealthDto> responseHistory = await client.GetContainer(databaseId, collectionHistoryId).CreateItemAsync(alertHistoryObj, new PartitionKey(alertHistoryObj.resourceId));

                    log.LogInformation("Document created in Cosmos History: " + responseHistory.StatusCode);
                }
                catch (CosmosException ex)
                {
                    log.LogInformation("error created in Cosmos: " + ex.Message);
                    //retryQueue.Add(alertHistoryObj);
                }
            }
            else
            {
                var activitylog = JsonConvert.DeserializeObject <ActivityLogAlert>(requestBody);
                log.LogInformation(activitylog.schemaId);

                var alertObj = CosmosHelper.GetActivityLogAlertDtoMapping(activitylog, log);

                try
                {
                    List <string> subs = new List <string>
                    {
                        alertObj.subscriptionId
                    };
                    string token = AuthHelper.GetTokenAsync().Result;
                    log.LogInformation($"Token Received: {token}");
                    string resourceGraphUri = "https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2018-09-01-preview";
                    string query            = $"where id =~ '{alertObj.resourceId}' | project location, properties.hardwareProfile.vmSize, sku.name ";
                    //, properties.hardwareProfile.vmSize,sku.name";
                    Dictionary <string, int> options = new Dictionary <string, int>();
                    options["$skip"] = 0;
                    Dictionary <string, object> resourceObj = new Dictionary <string, object>();

                    resourceObj.Add("subscriptions", subs);
                    resourceObj.Add("query", query);
                    resourceObj.Add("options", options);

                    ResourceGraphResponse resourcesGraph = ResilientRestClient.PostAsync <ResourceGraphResponse>(resourceGraphUri, token, resourceObj).Result;

                    alertObj.location = resourcesGraph.data.rows[0][0].ToString();

                    if (!(String.IsNullOrEmpty(resourcesGraph.data.rows[0][1])))
                    {
                        alertObj.size = resourcesGraph.data.rows[0][1].ToString();
                    }
                    else
                    {
                        alertObj.size = String.IsNullOrEmpty(resourcesGraph.data.rows[0][2])? "" :resourcesGraph.data.rows[0][2].ToString();
                    }
                }
                catch (System.Exception ex)
                {
                    log.LogError($"error {ex.Message}");

                    alertObj.location = "N/A";
                    alertObj.size     = "N/A";
                    log.LogError($"Unable to get location for {alertObj.resourceId}");

                    log.LogInformation($"Unable to get location for {alertObj.resourceId}");
                    log.LogInformation($"error {ex.Message}");
                }
                log.LogInformation("Resource Subscription: " + alertObj.subscriptionId);
                log.LogInformation("Resource Id: " + alertObj.resourceId);
                log.LogInformation("Resource Status: " + alertObj.alertStatus);
                var collectionId = GetEnvironmentVariable("CosmosDb_Activitylogs");
                var databaseId   = GetEnvironmentVariable("CosmosDb_Database");

                try
                {
                    ItemResponse <ActivityLogDto> response = await client.GetContainer(databaseId, collectionId).UpsertItemAsync(alertObj, new PartitionKey(alertObj.id));

                    log.LogInformation("Document created in Cosmos: " + response.StatusCode);
                }
                catch (CosmosException ex)
                {
                    log.LogInformation("error created in Cosmos: " + ex.Message);
                }
            }

            return(new OkObjectResult("Resource Health Alert Processed"));
        }
示例#21
0
        public static async Task RunAsync(
            [QueueTrigger("%VIProcessingCompleteQueue%", Connection = "AzureWebJobsStorage")] CloudQueueMessage myQueueItem,
            TraceWriter log)
        {
            var videoIndexerHelper = new VideoIndexerHelper(log);
            var cosmosHelper       = new CosmosHelper(log);

            var queueContents = myQueueItem.AsString;

            // queue item should be id & state
            var completionData = JsonConvert.DeserializeObject <Dictionary <string, string> >(queueContents);

            // ignore if not proper state
            if (completionData["state"] != "Processed")
            {
                return;
            }

            var videoIndexerVideoId = completionData["id"];

            var apiUrl = videoIndexerHelper.VideoIndexerApiUrl;
            var client = videoIndexerHelper.GetVideoIndexerHttpClient();

            var uri = string.Format(apiUrl + "/{0}", videoIndexerVideoId);

            videoIndexerHelper.LogMessage($" call VI with uri{uri}");
            var response = await client.GetAsync(uri);

            var json = await response.Content.ReadAsStringAsync();

            videoIndexerHelper.LogMessage($"get state with id: {videoIndexerVideoId}");
            var state = cosmosHelper.GetManifestStateRecordByVideoId(videoIndexerVideoId, log);

            videoIndexerHelper.LogMessage("create poco from json ");
            var videoBreakdownPoco = JsonConvert.DeserializeObject <VideoBreakdown>(json);



            var taskList = new List <Task>();


            // these tasks are network io dependant and can happen in parallel
            //TODO:  setup default languages to be pulled from app settings, but
            //TODO: the languages set in config would override
            //TODO: validate languages
            videoIndexerHelper.LogMessage("Process transcripts");
            if (state.Transcripts != null)
            {
                taskList = state.Transcripts
                           .Select(language => GetCaptionsVttAsync(videoIndexerVideoId, videoBreakdownPoco, language, videoIndexerHelper))
                           .ToList();
            }
            videoIndexerHelper.LogMessage("start task extract images");
            taskList.Add(ExtractImages(videoBreakdownPoco, log, videoIndexerHelper));
            //var englishCaptionsTask = GetCaptionsVttAsync(videoIndexerVideoId, videoBreakdownPoco, "English");
            //var japaneseCaptionsTask = GetCaptionsVttAsync(videoIndexerVideoId, videoBreakdownPoco, "Japanese");
            //var imagesTask = ExtractImages(videoBreakdownPoco);
            videoIndexerHelper.LogMessage($"wait for tasks to finish");
            await Task.WhenAll(taskList.ToArray());

            videoIndexerHelper.LogMessage($"store in json breakdowns");
            // we wait to store breakdown json because it's modified in previous tasks
            var storeTask1 = StoreBreakdownJsonInCosmos(videoBreakdownPoco, log);

            videoIndexerHelper.LogMessage($"update processing ");
            var storeTask2 = cosmosHelper.UpdateProcessingStateAsync(completionData["id"]);

            videoIndexerHelper.LogMessage($"wait for  store tasks to finish");
            await Task.WhenAll(storeTask1, storeTask2);
        }
示例#22
0
 public ProcessBuildings()
 {
     _cosmosHelper = new CosmosHelper <CoverageBuilding>();
 }