public static async Task <string> AdxExportStatusCheck(
            [ActivityTrigger] string operationId, ILogger log)
        {
            using (var client = KustoClientFactory.CreateCslAdminProvider(await GetKustoConnectionStringBuilder()))
            {
                var operationQuery = CslCommandGenerator.GenerateOperationsShowCommand(Guid.Parse(operationId));
                var resultReader   = new ObjectReader <OperationsShowCommandResult>(client.ExecuteControlCommand(adxDatabaseName, operationQuery));

                var res   = resultReader?.FirstOrDefault();
                var state = res?.State;
                if (state == "Completed")
                {
                    // When the state is completed, we can query the export details which contains the path to the file on blob storage
                    var operationDetailsQuery = CslCommandGenerator.GenerateOperationDetailsShowCommand(Guid.Parse(operationId));
                    var resultReader2         = new ObjectReader <DataExportToBlobCommandResult>(client.ExecuteControlCommand(adxDatabaseName, operationDetailsQuery));

                    var res2 = resultReader2?.FirstOrDefault();
                    var path = res2?.Path;
                    return(path);
                }
                else if (state == "Cancelled")
                {
                    return("Error");
                }
                else
                {
                    return(null);
                }
            }
        }
        public static async Task <IActionResult> AdxExportFunctionHttpTriggered(
            [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req,
            ILogger log,
            [DurableClient] IDurableOrchestrationClient starter)
        {
            log.LogInformation("AdxExportFunction processed a request.");

            var requestBody   = await new StreamReader(req.Body).ReadToEndAsync();
            var exportRequest = JsonConvert.DeserializeObject <AdxExportRequest>(requestBody);

            if (exportRequest == null || string.IsNullOrEmpty(exportRequest.AdxQuery) || string.IsNullOrEmpty(exportRequest.UserEmailAddress))
            {
                return(new BadRequestObjectResult("Invalid ADX export request. Check your parameters"));
            }

            using (var client = KustoClientFactory.CreateCslAdminProvider(await GetKustoConnectionStringBuilder()))
            {
                // TODO: Write actual query from user input
                var exportQuery  = CslCommandGenerator.GenerateExportCommand(new[] { storageConnectionString }, "datatable1 | take 100", true, true);
                var resultReader = new ObjectReader <DataExportToBlobCommandResult>(client.ExecuteControlCommand(adxDatabaseName, exportQuery));

                var res = resultReader?.FirstOrDefault();
                var adxExportOperationId = res?.Path;

                if (!string.IsNullOrEmpty(adxExportOperationId))
                {
                    // Start durable orchestrator for the status checking
                    var durableInstanceId = await starter.StartNewAsync(nameof(AdxExportOrchestrator), new Tuple <string, string>(adxExportOperationId, exportRequest.UserEmailAddress));

                    return(new OkObjectResult("Request Accepted. Durable Instance=" + durableInstanceId));
                }
                else
                {
                    log.LogError("Path is empty in result");
                    log.LogError(JsonConvert.SerializeObject(res));
                }
            }
            return(new BadRequestObjectResult("Error on starting ADX export"));
        }