示例#1
0
 internal static Types.BackfillRequest CreateDeviceBackfillRequest(Types.DataGap dataGap, ILogger logger)
 {
     Types.BackfillRequest backfillRequest = new Types.BackfillRequest()
     {
         BatchId     = dataGap.BatchId,
         EndWindow   = dataGap.EndWindow,
         StartWindow = dataGap.StartWindow,
         Created     = DateTime.UtcNow
     };
     return(backfillRequest);
 }
示例#2
0
        public static async Task RunAsync(
            [QueueTrigger("%StorageQueueName%", Connection = "StorageConnectionString")] string backfillRequest,
            [EventHub("%ExecutionEventHubName%", Connection = "EventHubConnectionString")] IAsyncCollector <string> outputEvents,
            ILogger log,
            ExecutionContext context)
        {
            try
            {
                log.LogInformation($"{context.FunctionName}: {backfillRequest}");

                Types.DataGap request = JsonConvert.DeserializeObject <Types.DataGap>(backfillRequest);

                Types.BackfillRequest existingDeviceBackfillRequest = await Helpers.SqlHelpers.GetExistingDeviceBackfillRequest(request, log);

                if (existingDeviceBackfillRequest != null)
                {
                    log.LogInformation($"{context.FunctionName}: Found existing backfill request {JsonConvert.SerializeObject(existingDeviceBackfillRequest)}");
                    return;
                }

                List <Types.DataGap> latestGaps = await Helpers.SqlHelpers.CalculateActualGaps(request, log);

                foreach (Types.DataGap gap in latestGaps)
                {
                    existingDeviceBackfillRequest = await Helpers.SqlHelpers.GetExistingDeviceBackfillRequest(request, log);

                    if (existingDeviceBackfillRequest != null)
                    {
                        log.LogInformation($"{context.FunctionName}: Found existing backfill request {JsonConvert.SerializeObject(existingDeviceBackfillRequest)}");
                        continue;
                    }

                    Types.BackfillRequest requestSection = Helpers.SqlHelpers.CreateDeviceBackfillRequest(gap, log);

                    string requestSectionString = JsonConvert.SerializeObject(requestSection);

                    await outputEvents.AddAsync(requestSectionString);

                    log.LogInformation($"{context.FunctionName}: Created backfill request {requestSectionString}");
                }
            }
            catch (Exception e)
            {
                log.LogError($"Error in {context.FunctionName}: {e.ToString()}");
                throw;
            }
        }
示例#3
0
        internal static async Task <List <Types.DataGap> > CalculateActualGaps(Types.DataGap dataGap, ILogger logger)
        {
            var dataGaps = new List <Types.DataGap>();

            try
            {
                using (SqlConnection connection = new SqlConnection(Environment.GetEnvironmentVariable("SQLConnectionString")))
                {
                    await connection.OpenAsync();

                    using (SqlCommand command = new SqlCommand(SqlHelpers.SQL_GET_GAPS_IN_WINDOW, connection))
                    {
                        command.Parameters.AddWithValue("@batchId", dataGap.BatchId);
                        command.Parameters.AddWithValue("@startWindow", dataGap.StartWindow.ToUniversalTime().ToString("o"));
                        command.Parameters.AddWithValue("@endWindow", dataGap.EndWindow.ToUniversalTime().ToString("o"));

                        using (SqlDataReader reader = await command.ExecuteReaderAsync())
                        {
                            while (await reader.ReadAsync())
                            {
                                dataGaps.Add(new Types.DataGap()
                                {
                                    BatchId      = reader.GetString(0),
                                    StartWindow  = DateTime.Parse(reader.GetString(1)).ToUniversalTime(),
                                    EndWindow    = DateTime.Parse(reader.GetString(2)).ToUniversalTime(),
                                    GapInSeconds = reader.GetInt32(3)
                                });
                            }
                        }
                    }
                }
            }
            catch (SqlException e)
            {
                logger.LogError($"Error when reading sql database: {e.ToString()}");
                throw;
            }
            return(dataGaps);
        }
示例#4
0
        internal static async Task <Types.BackfillRequest> GetExistingDeviceBackfillRequest(Types.DataGap request, ILogger logger)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(Environment.GetEnvironmentVariable("SQLConnectionString")))
                {
                    await connection.OpenAsync();

                    using (SqlCommand command = new SqlCommand(SqlHelpers.SQL_GET_EXISTING_BACKFILL_REQUEST, connection))
                    {
                        command.Parameters.AddWithValue("@startWindow", request.StartWindow.ToUniversalTime().ToString("o"));
                        command.Parameters.AddWithValue("@endWindow", request.StartWindow.ToUniversalTime().ToString("o"));
                        command.Parameters.AddWithValue("@batchId", request.BatchId);
                        command.Parameters.AddWithValue("@timeout", Int32.Parse(Environment.GetEnvironmentVariable("BackfillTimeout")));

                        using (SqlDataReader reader = await command.ExecuteReaderAsync())
                        {
                            if (await reader.ReadAsync())
                            {
                                return(new Types.BackfillRequest()
                                {
                                    StartWindow = DateTime.Parse(reader.GetString(0)).ToUniversalTime(),
                                    EndWindow = DateTime.Parse(reader.GetString(1)).ToUniversalTime(),
                                    BatchId = reader.GetString(2),
                                    Created = DateTime.Parse(reader.GetString(3)).ToUniversalTime()
                                });
                            }
                        }
                    }
                }
            }
            catch (SqlException e)
            {
                logger.LogError($"Error when reading sql database: {e.ToString()}");
                throw;
            }
            return(null);
        }