예제 #1
0
        public async Task Run([TimerTrigger("0 15 5 * * *")] TimerInfo myTimer, ILogger log)
        {
            try
            {
                log.LogInformation($"{nameof(GetActivitySummaryByDate)} executed at: {DateTime.Now}");
                var dateParameter = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                log.LogInformation($"Attempting to retrieve Daily Activity Summary for {dateParameter}");
                var activityResponse = await _fitbitApiService.GetActivityResponseObject(dateParameter);

                log.LogInformation("Mapping API response to Activity object");
                var activity = new mdl.Activity();
                activity.ActivityDate = dateParameter;
                _mapper.Map(activityResponse, activity);

                log.LogInformation("Sending Activity log to service bus");
                await _serviceBusHelpers.SendMessageToTopic(_configuration["ActivityTopic"], activity);
            }
            catch (Exception ex)
            {
                log.LogError($"Exception thrown in {nameof(GetActivitySummaryByDate)}: {ex.Message}");
                await _serviceBusHelpers.SendMessageToQueue(_configuration["ExceptionQueue"], ex);

                throw ex;
            }
        }
        public async Task ThrowExceptionWhenInputDataIsInvalid()
        {
            // Arrange
            var testActivity = new mdl.Activity();

            byte[]       byteArray    = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(testActivity));
            MemoryStream memoryStream = new MemoryStream(byteArray);

            // Act
            Func <Task> parserAction = async() => await _sut.ParseActivityStream(memoryStream);

            // Assert
            await parserAction.Should().ThrowAsync <Exception>();
        }
        public async Task ParseActivityStream(Stream inputStream)
        {
            try
            {
                inputStream.Seek(0, SeekOrigin.Begin);

                using (var activityStream = new StreamReader(inputStream))
                    using (var csv = new CsvReader(activityStream, CultureInfo.InvariantCulture))
                    {
                        if (csv.Read())
                        {
                            csv.ReadHeader();
                            while (csv.Read())
                            {
                                var activity = new mdl.Activity
                                {
                                    ActivityDate         = ParseActivityDate(csv.GetField("Date")),
                                    CaloriesBurned       = int.Parse(csv.GetField("Calories Burned"), NumberStyles.AllowThousands),
                                    Steps                = int.Parse(csv.GetField("Steps"), NumberStyles.AllowThousands),
                                    Distance             = double.Parse(csv.GetField("Distance")),
                                    Floors               = int.Parse(csv.GetField("Floors"), NumberStyles.AllowThousands),
                                    MinutesSedentary     = int.Parse(csv.GetField("Minutes Sedentary"), NumberStyles.AllowThousands),
                                    MinutesLightlyActive = int.Parse(csv.GetField("Minutes Lightly Active"), NumberStyles.AllowThousands),
                                    MinutesFairlyActive  = int.Parse(csv.GetField("Minutes Fairly Active"), NumberStyles.AllowThousands),
                                    MinutesVeryActive    = int.Parse(csv.GetField("Minutes Very Active"), NumberStyles.AllowThousands),
                                    ActivityCalories     = int.Parse(csv.GetField("Activity Calories"), NumberStyles.AllowThousands)
                                };

                                await _serviceBusHelpers.SendMessageToTopic(_configuration["ActivityTopic"], activity);
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }