public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "v1/users")] HttpRequest req,
            [Blob("user", Connection = "StorageConnectionString")] CloudBlobContainer container,
            [Table("user", Connection = "StorageConnectionString")] CloudTable userTable,
            ILogger log)
        {
            try
            {
                //await userTable.CreateIfNotExistsAsync(); // create before execution, to avoid overhead proccess

                PureCloudClient purecloudClient = new PureCloudClient();
                await purecloudClient.GetAccessToken();

                List <User> users = await purecloudClient.GetAvailableUsers();

                if (!users.Count.Equals(0))
                {
                    log.LogInformation($"Total users to save: {users.Count}");

                    foreach (var user in users)
                    {
                        TableQuery <Domain.Models.User> query = new TableQuery <Domain.Models.User>()
                                                                .Where(TableQuery.GenerateFilterCondition(
                                                                           "Id", QueryComparisons.Equal, user.Id));

                        var results = await userTable.ExecuteQuerySegmentedAsync(query, null);

                        Domain.Models.User userDb = results.Results.FirstOrDefault();
                        if (userDb == null)
                        {
                            TableOperation operation = TableOperation.Insert(new Domain.Models.User()
                            {
                                Id    = userDb.Id,
                                Email = userDb.Email
                            });
                            operation.Entity.PartitionKey = "";
                            operation.Entity.RowKey       = Guid.NewGuid().ToString();

                            await userTable.ExecuteAsync(operation);

                            var cloudBlockBlob = container.GetBlockBlobReference($"{user.Id}.json");
                            await cloudBlockBlob.UploadTextAsync(JsonConvert.SerializeObject(user));
                        }
                    }
                }

                return((ActionResult) new OkResult());
            }
            catch (Exception ex)
            {
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                telemetry.TrackException(ex);

                return((ActionResult) new StatusCodeResult(500));
            }
        }
Exemple #2
0
        public static async Task RunAsync(
            [ServiceBusTrigger("jobqueue", Connection = "ServiceBusConnectionString")] string jobId,
            [ServiceBus("jobqueue", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] IAsyncCollector <string> jobQueue,
            [Blob("conversation", Connection = "StorageConnectionString")] CloudBlobContainer container,
            ILogger log)
        {
            try
            {
                //await container.CreateIfNotExistsAsync(); // create before execution, to avoid overhead proccess

                // TODO: read from "jobQueue"
                PureCloudClient purecloudClient = new PureCloudClient();
                await purecloudClient.GetAccessToken();

                BatchDownloadJobStatusResult batch = await purecloudClient.GetJobRecordingDownloadResultByConversation(jobId);

                log.LogInformation($"Result forjob: ExpectedResultCound({batch.ExpectedResultCount}) and ResultCount({batch.ResultCount})");

                // TODO: end when resultcount == resultaudios
                if (!batch.ExpectedResultCount.Equals(0) && batch.ExpectedResultCount.Equals(batch.ResultCount))
                {
                    if (batch.Results != null)
                    {
                        //List<Task> taskList = new List<Task>(); // to mutch performatic kkkk :P
                        foreach (var item in batch.Results)
                        {
                            if (!string.IsNullOrEmpty(item.ResultUrl))
                            {
                                if (string.IsNullOrEmpty(item.ErrorMsg))
                                {
                                    var extension = string.Empty;
                                    switch (item.ContentType)
                                    {
                                    case "audio/ogg":
                                        extension = "ogg";
                                        break;

                                    case "application/zip":
                                        extension = "zip";
                                        break;

                                    default:
                                        extension = "none";
                                        break;
                                    }

                                    CloudBlockBlob convesrationBlob = container.GetBlockBlobReference($"{item.ConversationId}-{item.RecordingId}.{extension}");
                                    await convesrationBlob.StartCopyAsync(new Uri(item.ResultUrl));
                                }
                                else
                                {
                                    CloudBlockBlob convesrationBlob = container.GetBlockBlobReference($"{item.ConversationId}.error");
                                    await convesrationBlob.UploadTextAsync(JsonConvert.SerializeObject(item));
                                }
                            }
                        }
                        //await Task.WhenAll(taskList.ToArray()); // to mutch performatic kkkk :P
                    }
                }
                else
                {
                    // TODO: else, return item to jobQueue for next tentative
                    await jobQueue.AddAsync(jobId);

                    log.LogInformation($"JobId: {jobId} re added to jobQueue");
                }
            }
            catch (Exception ex)
            {
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                telemetry.TrackException(ex);

                log.LogInformation($"Exception during execution: {ex.Message}");

                do
                {
                    try
                    {
                        await Task.Delay(Convert.ToInt32(Environment.GetEnvironmentVariable("deplaytime")));

                        await jobQueue.AddAsync(jobId);

                        log.LogInformation($"Readded jobId: {jobId} to jobQueue");

                        break;
                    }
                    catch (Exception exEx) {
                        telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                        telemetry.TrackException(exEx);

                        log.LogInformation($"Exception during execution: {exEx.Message}");
                    }
                } while (true);
            }
        }
        public static async Task RunAsync(
            [ServiceBusTrigger("pagequeue", Connection = "ServiceBusConnectionString")] string pageJson,
            [EventHub("conversationhub", Connection = "EventhubConnectionString")] IAsyncCollector <string> conversationhub,
            [ServiceBus("pagequeue", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] IAsyncCollector <string> pageQueue,
            [Blob("conversation", Connection = "StorageConnectionString")] CloudBlobContainer container,
            ILogger log)
        {
            try
            {
                //await container.CreateIfNotExistsAsync(); // create before execution, to avoid overhead proccess

                // TODO: read from "pageQueue"
                DatePage datePage = new DatePage(pageJson);

                // TODO: get conversations from PureCloudClient
                PureCloudClient pureCloudClient = new PureCloudClient();
                await pureCloudClient.GetAccessToken();

                List <AnalyticsConversation> conversations = await pureCloudClient.GetConversationsByInterval(
                    datePage.Date, datePage.Date, datePage.Page);

                log.LogInformation($"Processing date: {datePage.Date}, at page: {datePage.Page}, with {conversations.Count} conversations");

                // TODO: add on "conversationQueue"
                //List<Task> taskList = new List<Task>(); // to mutch performatic kkkk :P
                foreach (var item in conversations)
                {
                    string conversationJson = JsonConvert.SerializeObject(item);
                    await conversationhub.AddAsync(item.ConversationId);

                    // add conversationJson to blob storage
                    CloudBlockBlob convesrationBlob = container.GetBlockBlobReference($"{item.ConversationId}-conversation.json");
                    await convesrationBlob.UploadTextAsync(conversationJson);
                }
                //await Task.WhenAll(taskList.ToArray()); // to mutch performatic kkkk :P

                // TODO: add new page on same date
                if (conversations.Count.Equals(100))
                {
                    await pageQueue.AddAsync(new DatePage()
                    {
                        Date = datePage.Date,
                        Page = datePage.Page + 1
                    }.ToJson());
                }
            }
            catch (Exception ex)
            {
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                telemetry.TrackException(ex);

                log.LogInformation($"Exception during execution: {ex.Message}");

                do
                {
                    try
                    {
                        await Task.Delay(Convert.ToInt32(Environment.GetEnvironmentVariable("deplaytime")));

                        await pageQueue.AddAsync(pageJson);

                        log.LogInformation($"Readded pageJson: {pageJson} to pageQueue");

                        break;
                    }
                    catch (Exception exEx)
                    {
                        telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                        telemetry.TrackException(exEx);

                        log.LogInformation($"Exception during execution: {exEx.Message}");
                    }
                } while (true);
            }
        }
Exemple #4
0
        public static async void Run(
            [EventHubTrigger("conversationhub", Connection = "EventhubConnectionString")] EventData[] events,
            [EventHub("conversationhub", Connection = "EventhubConnectionString")] IAsyncCollector <string> conversationhub,
            [ServiceBus("jobqueue", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] IAsyncCollector <string> jobQueue,
            ILogger log)
        {
            try
            {
                List <string> conversations = new List <string>();

                // TODO: get list of conversations from "eventHub"
                foreach (EventData eventData in events)
                {
                    string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                    conversations.Add(messageBody);
                    //await Task.Yield();
                }

                log.LogInformation($"Total conversations: {conversations.Count}");

                // TODO: batch job donwload
                PureCloudClient purecloudClient = new PureCloudClient();
                await purecloudClient.GetAccessToken();

                BatchDownloadJobSubmissionResult job = await purecloudClient.BatchRecordingDownloadByConversation(conversations);

                // TODO: add in "jobQueue"
                await jobQueue.AddAsync(job.Id);
            }
            catch (Exception ex)
            {
                TelemetryClient telemetry = new TelemetryClient();
                telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                telemetry.TrackException(ex);

                log.LogInformation($"Exception during execution: {ex.Message}");

                do
                {
                    try
                    {
                        await Task.Delay(Convert.ToInt32(Environment.GetEnvironmentVariable("deplaytime")));

                        //List<Task> taskList = new List<Task>(); // to mutch performatic kkkk :P
                        foreach (EventData eventData in events)
                        {
                            string messageBody = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
                            await conversationhub.AddAsync(messageBody);

                            log.LogInformation($"Readded conversation: {messageBody} to conversationhub");
                        }
                        //await Task.WhenAll(taskList); // to mutch performatic kkkk :P

                        break;
                    }
                    catch (Exception exEx)
                    {
                        telemetry.InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                        telemetry.TrackException(exEx);

                        log.LogInformation($"Exception during execution: {exEx.Message}");
                    }
                } while (true);
            }
        }