Beispiel #1
0
        public async Task Run()
        {
            HashSet <DateTimeOffset> timestamps = new HashSet <DateTimeOffset>();
            byte           runs = 0;
            DateTimeOffset now  = _dateTimeOffsetProvider.UtcNow;
            bool           continueRunning;

            do
            {
                DateTimeOffset date = now.AddHours(runs * -1);
                int            hour = date.Hour;
                StationRecentlyPlayedResponse response = await GetResponse(date, hour);

                response.Schedule.RemoveAll(x => x.Playing);
                IEnumerable <RawArtistWorkStationOccurrence> rawOccurrences = response.ToRawOccurrences();

                rawOccurrences = rawOccurrences.Where(x => !timestamps.Contains(x.StartTime)).AsList();
                timestamps.UnionWith(rawOccurrences.Select(x => x.StartTime));
                int occurrences = rawOccurrences.Count();

                runs++;
                continueRunning = runs <= _radiocomDataCollectorEngineOptions.HoursBackToRetrive;
                if (continueRunning && occurrences > 0)
                {
                    Thread.Sleep(100);//throttle requests
                    _log.LogInformation($"Will process {occurrences} raw occurrences.");
                    int totalNeededProcessing = await _radiocomRepository.ProcessRawOccurrances(rawOccurrences);

                    continueRunning = totalNeededProcessing == occurrences;
                    _log.LogInformation($"Total occurrences needed processing: {totalNeededProcessing}.");
                }
            } while (continueRunning);
            await _publishCollectorEventCompleted.NotifyCollectorEventCompleted();
        }
Beispiel #2
0
        private async Task <StationRecentlyPlayedResponse> GetResponse(DateTimeOffset date, int hour)
        {
            DateTimeOffset time = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
            StationRecentlyPlayedResponse response = null;
            byte tries = 0;
            bool tryAgain;

            do
            {
                try
                {
                    tries   += 1;
                    response = await _client.StationRecentlyPlayed(KISW_STATION_ID, hour, time.DayOfWeek.ToRadiocomDayOfWeek());

                    tryAgain = false;
                }
                catch (HttpRequestException httpRequestException)
                {
                    _log.LogError(httpRequestException, $"requesting station:{KISW_STATION_ID}, hour:{hour}, day:{time.DayOfWeek}");
                    tryAgain = tries < 3;
                    if (!tryAgain)
                    {
                        throw;
                    }
                }
            } while (tryAgain);
            return(response);
        }
        /// <summary>
        /// Convert to RawArtistWorkStationOccurrence
        /// </summary>
        /// <param name="response">data to convert</param>
        /// <returns>response data converted to RawArtistWorkStationOccurrence </returns>
        public static IEnumerable <RawArtistWorkStationOccurrence> ToRawOccurrences(this StationRecentlyPlayedResponse response)
        {
            List <ScheduleItem> items = response.Schedule;

            return(items.Select(x =>
                                new RawArtistWorkStationOccurrence()
            {
                Artist = x.Artist,
                Title = x.Title,
                StartTime = x.StartTime,
                StationId = response.Station.Id
            }));
        }
 public void ToRawOccurrencesTest()
 {
     #region arrange
     StationRecentlyPlayedResponse response = GetStationRecentlyPlayedResponse();
     #endregion arrange
     #region act
     List <RawArtistWorkStationOccurrence> result = response.ToRawOccurrences().ToList();
     #endregion act
     #region assert
     CollectionAssert.IsNotEmpty(result);
     Assert.AreEqual(response.Schedule[3].Artist, result[3].Artist);
     Assert.AreEqual(response.Schedule[0].Title, result[0].Title);
     Assert.AreEqual(response.Schedule[1].StartTime, result[1].StartTime);
     Assert.AreEqual(response.Station.Id, result[2].StationId);
     #endregion assert
 }