Example #1
0
        public async Task <bool> UpdatePrediction(ChannelPublishPrediction channelPublishPrediction)
        {
            var result = await _col.ReplaceOneAsync(
                c => c.Id == channelPublishPrediction.Id,
                channelPublishPrediction, new ReplaceOptions
            {
                IsUpsert = true
            });

            var updateSuccessful = result.ModifiedCount == 1;

            return(updateSuccessful);
        }
Example #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // read all channel ids
            //var channels = await _channelRepository.GetAllChannelIdAndTitle();
            var channels = new List <Channel> {
                new Channel() with {
                    Id = "UCBo9TLJiZ5HI5CXFsCxOhmA"
                }
            };

            _logger.LogInformation(
                $"{channels.Count} channels will be inspected for publish schedules"
                );

            foreach (var channel in channels)
            {
                try
                {
                    _logger.LogInformation(channel.Id);

                    // aggregate the channel video views of this channel per
                    // timeframes (day of week, hour of day)
                    var result = await _aggregateVideoPublishTimesUseCase.Handle(
                        new AggregateVideoPublishTimesRequest(
                            channel.Id
                            )
                        );

                    var items = new List <VideoPublishAggregationItem>();

                    foreach (var daysOfWeek in result.Aggregation)
                    {
                        foreach (var hourOfDay in daysOfWeek.Value)
                        {
                            var item = new VideoPublishAggregationItem()
                            {
                                DayOfTheWeek    = (int)daysOfWeek.Key,
                                HourOfTheDay    = hourOfDay.Key,
                                PublishedVideos = hourOfDay.Value
                            };

                            items.Add(item);
                        }
                    }

                    if (items.Count == 0)
                    {
                        _logger.LogInformation($"No uploads for channel {channel.Id} within the last 4 month");
                        continue;
                    }

                    var average = items.Sum(i => i.PublishedVideos) / items.Count;

                    var sortedByDeviation = items
                                            .Select(item => new PublishSchedulePrediction(
                                                        item,
                                                        item.PublishedVideos / average))
                                            .OrderByDescending(item => item.DeviationFromAverage)
                                            .Where(i => i.DeviationFromAverage > average)
                                            .Take(5)
                                            .ToList();

                    var mergedNeighbors = MergeNeighbors(sortedByDeviation);

                    var gradient = 1 - average / mergedNeighbors.First().DeviationFromAverage;

                    var channelResult = new ChannelPublishPrediction(
                        channel.Id,
                        channel.Title,
                        average,
                        mergedNeighbors,
                        gradient);

                    var updatePredictionSuccessful =
                        await _channelPublishPredictionRepository.UpdatePrediction(channelResult);

                    if (!updatePredictionSuccessful)
                    {
                        _logger.LogWarning($"Updating publish prediction for channel {channel.Id} was not successful");
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e.ToString(), e.InnerException);
                }
            }
        }