示例#1
0
        public static void FetchViewerPercentageMetrics(YouTubeAnalyticsService analyticsService, string channelId, YTD.Video video, Logger logger)
        {
            using (var dbContext = new DataLakeYouTubeAnalyticsContext()) {
                var now = DateTime.UtcNow;

                var lastFetchedDate = dbContext.ViewerPercentageLastDates.SingleOrDefault(x => x.VideoId == video.VideoId);

                var initialDate = video.PublishedAt;

                if (lastFetchedDate == null)
                {
                    // maybe the video simply doesn't support this report, check for early termination
                    if (!RunViewerPercentageReport(analyticsService, channelId, video.PublishedAt, now, video).Any())
                    {
                        logger.Debug("Report not available for video {VideoId}", video.VideoId);
                        return;
                    }
                    lastFetchedDate = new YTA.ViewerPercentageLastDate()
                    {
                        VideoId = video.VideoId, Date = initialDate
                    };
                    dbContext.Add(lastFetchedDate);
                }
                else
                {
                    initialDate = DateHelper.Max(video.PublishedAt, DateHelper.Min(lastFetchedDate.Date, now - TimeMargin));
                }

                int replicatedDays = 0;
                foreach (var date in DateHelper.DaysInRange(initialDate.Date, now.Date))
                {
                    var viewerPercentages = RunViewerPercentageReport(analyticsService, channelId, video.PublishedAt.Date, date, video);
                    lastFetchedDate.Date = date;
                    if (!viewerPercentages.Any())
                    {
                        continue;
                    }

                    DbWriter.Write(
                        video.VideoId,
                        date,
                        viewerPercentages.Select(x => Api2DbObjectConverter.ConvertViewerPercentageRow(x)),
                        now
                        );

                    dbContext.SaveChanges();
                    replicatedDays++;
                    if (replicatedDays % MaxDaysToReplicateInIteration == 0)
                    {
                        break;
                    }
                }
                logger.Debug("Replicated {Days} days for video {VideoId}", replicatedDays, video.VideoId);
            }
        }
示例#2
0
 public static IEnumerable <YTA.VideoDailyMetric> FetchDailyMetrics(YouTubeAnalyticsService analyticsService, string channelId, YTD.Video video, Logger logger, bool reprocess = false)
 {
     using (var dbContext = new DataLakeYouTubeAnalyticsContext()) {
         var now = DateTime.UtcNow;
         var mostRecentRecord = dbContext.VideoDailyMetrics
                                .Where(x => x.VideoId == video.VideoId && x.ValidityStart <= now && now < x.ValidityEnd)
                                .OrderByDescending(x => x.Date)
                                .FirstOrDefault();
         return(FetchVideoDailyMetrics(mostRecentRecord, channelId, video, now, analyticsService, logger, reprocess)
                .Select(x => Api2DbObjectConverter.ConvertDailyMetricRow(video.VideoId, x)));
     }
 }