public async Task ProcessExport()

in src/ApiForFhirMigrationTool.Function/ExportOrchestrator.cs [96:253]


    public async Task<ResponseModel> ProcessExport([ActivityTrigger] string name, FunctionContext executionContext)
    {
       // ILogger logger = executionContext.GetLogger("ProcessExport");
        _logger.LogInformation($"SearchParameterMigration Started");
        _logger?.LogInformation($"Export process Started");
        ResponseModel exportResponse = new ResponseModel();
        try
        {
            HttpMethod method = HttpMethod.Get;
            _logger?.LogInformation($"Getting Query for export operation");
            string query = GetQueryStringForExport(_logger!);
            _logger?.LogInformation("Query for export operation retrieved successfully.");

            if (!string.IsNullOrEmpty(query))
            {
                string sinceValue = string.Empty;
                string tillValue = string.Empty;
                string resourceTypeValue = string.Empty;
                _logger?.LogInformation("Initiating the export process.");
                exportResponse = await _exportProcessor.CallProcess(method, string.Empty, _options.SourceUri, query, _options.SourceHttpClient);
                _logger?.LogInformation("Export process completed successfully.");

                _logger?.LogInformation("Creating table clients");
                TableClient chunktableClient = _azureTableClientFactory.Create(_options.ChunkTableName);
                TableClient exportTableClient = _azureTableClientFactory.Create(_options.ExportTableName);
                _logger?.LogInformation("Table clients created successfully.");

                var statusUrl = string.Empty;

                string pattern = @"_since=(.*?)&_till=(.*?)($|&)";
                Match match = Regex.Match(query, pattern);
                if (match.Success)
                {
                    sinceValue = match.Groups[1].Value;
                    tillValue = match.Groups[2].Value;
                }
                if (!_options.IsParallel)
                {
                    string patternResourceType = @"[?&]_type=([^&]+)";
                    // Match the pattern against the URL
                    Match matchResourceType = Regex.Match(query, patternResourceType);

                    // Check if a match is found
                    if (matchResourceType.Success)
                    {
                        resourceTypeValue = matchResourceType.Groups[1].Value;
                    }
                }
                if (exportResponse.Status == ResponseStatus.Accepted)
                {
                    _logger?.LogInformation("Export operation status: Accepted.");

                    statusUrl = exportResponse.Content;

                    TableEntity qEntity = _azureTableMetadataStore.GetEntity(chunktableClient, _options.PartitionKey, _options.RowKey);
                    if (qEntity["JobId"] != null)
                    {
                        int jobId = (int)qEntity["JobId"];
                        string rowKey = _options.RowKey + jobId++;

                        var tableEntity = new TableEntity(_options.PartitionKey, rowKey)
                            {
                                { "exportContentLocation", statusUrl },
                                { "importContentLocation", string.Empty },
                                { "IsExportComplete", false },
                                { "IsExportRunning", "Started" },
                                { "IsImportComplete", false },
                                { "IsImportRunning", "Not Started" },
                                { "ImportRequest", string.Empty },
                                { "Since", sinceValue },
                                { "Till", tillValue },
                                { "StartTime", DateTime.UtcNow },
                                {"resourceTypeValue",resourceTypeValue }
                            };
                        _logger?.LogInformation("Starting update of the export table.");
                        _azureTableMetadataStore.AddEntity(exportTableClient, tableEntity);
                        _logger?.LogInformation("Completed update of the export table.");
                        
                        TableEntity qEntitynew = _azureTableMetadataStore.GetEntity(chunktableClient, _options.PartitionKey, _options.RowKey);

                        qEntitynew["JobId"] = jobId++;
                        
                        _logger?.LogInformation("Starting update of the chunk table.");
                        _azureTableMetadataStore.UpdateEntity(chunktableClient, qEntitynew);
                        _logger?.LogInformation("Completed update of the chunk table.");

                        _logger?.LogInformation("Updating logs in Application Insights.");
                        _telemetryClient.TrackEvent(
                        "Export",
                        new Dictionary<string, string>()
                        {
                            { "ExportId", _orchestrationHelper.GetProcessId(statusUrl) },
                            { "StatusUrl", statusUrl },
                            { "ExportStatus", "Started" },
                            { "Since", sinceValue },
                            { "Till", tillValue },
                        });
                        _logger?.LogInformation("Logs updated successfully in Application Insights.");
                    }
                }
                else
                {
                    _logger?.LogInformation("Export operation status: Failed.");
                    TableEntity qEntity = _azureTableMetadataStore.GetEntity(chunktableClient, _options.PartitionKey, _options.RowKey);
                    if (qEntity["JobId"] != null)
                    {
                        int jobId = (int)qEntity["JobId"];
                        string rowKey = _options.RowKey + jobId++;
                        string diagnosticsValue = JObject.Parse(exportResponse.Content)?["issue"]?[0]?["diagnostics"]?.ToString() ?? "For more information check Content location.";
                       _logger?.LogInformation($"Export check returned: Unsuccessful. Reason : {diagnosticsValue}");
                        var tableEntity = new TableEntity(_options.PartitionKey, rowKey)
                            {
                                { "exportContentLocation", statusUrl },
                                { "importContentLocation", string.Empty },
                                { "IsExportComplete", false },
                                { "IsExportRunning", "Failed" },
                                { "IsImportComplete", false },
                                { "IsImportRunning", "Failed" },
                                { "ImportRequest", string.Empty },
                                { "FailureReason",diagnosticsValue }
                            };
                        _logger?.LogInformation("Starting update of the export table.");
                        _azureTableMetadataStore.AddEntity(exportTableClient, tableEntity);
                        _logger?.LogInformation("Completed update of the export table.");
                        
                        TableEntity qEntitynew = _azureTableMetadataStore.GetEntity(chunktableClient, _options.PartitionKey, _options.RowKey);
                        qEntitynew["JobId"] = jobId++;

                        _logger?.LogInformation("Starting update of the chunk table.");
                        _azureTableMetadataStore.UpdateEntity(chunktableClient, qEntitynew);
                        _logger?.LogInformation("Completed update of the chunk table.");

                        _logger?.LogInformation("Updating logs in Application Insights.");
                        _telemetryClient.TrackEvent(
                       "Export",
                       new Dictionary<string, string>()
                       {
                            { "ExportId", _orchestrationHelper.GetProcessId(statusUrl) },
                            { "StatusUrl", statusUrl },
                            { "ExportStatus", "Failed" },
                            { "Since", sinceValue },
                            { "Till", tillValue },
                           { "FailureReason", diagnosticsValue }
                       });
                        _logger?.LogInformation("Logs updated successfully in Application Insights.");

                        throw new HttpFailureException($"Status: {exportResponse.Status} Response: {exportResponse.Content} ");
                    }
                }
            }
        }
        catch
        {
            throw;
        }
        _logger?.LogInformation($"Export process Finished");
        return exportResponse;
    }