public override Dictionary <MetricType, TimeSeries> ComputePrimitiveTimeSeries(MetricType[] metricNames, string type, DateTime start, DateTime end, Tag[] filters, ArchiveMode archive) { using (var context = new ApplicationDbContext()) { var videoMetricPersonas = MetricsByPersona(start, end, context, filters, archive); var allSamples = (from vmp in videoMetricPersonas select vmp).ToList(); // metricNames.Select(metricName => )).ToList(); var dateSet = DateUtilities.GetDatesBetween(start, end); return(metricNames.Select((v, ix) => { var timeSeriesGroup = from personaMetricRow in allSamples.Select(vmp => new Sample <string>() { Date = vmp.Metric.EventDate, Value = ProjectMetric(vmp.Metric, v), Group = vmp.Persona }) group personaMetricRow by personaMetricRow.Group into personaMetric select new TimeSeriesDataGroup() { GroupName = personaMetric.Key, Values = (from date in dateSet join m in personaMetric on date equals m.Date into dateMetrics select dateMetrics.Select(x => x.Value).DefaultIfEmpty(0).Sum()).ToArray() }; return (v, ReduceTimeSeries(dateSet, timeSeriesGroup.AsEnumerable())); }).ToDictionary(x => x.Item1, x => x.Item2)); } }
public override Dictionary <MetricType, TimeSeries> ComputePrimitiveTimeSeries(MetricType[] metrics, string type, DateTime start, DateTime end, Tag[] filters, ArchiveMode archive) { var store = new Dictionary <MetricType, TimeSeries>(); var dates = DateUtilities.GetDatesBetween(start, end); foreach (var metric in metrics) { var Values = new List <TimeSeriesDataGroup>(); IEnumerable <string> groups; // Marketing ignores type if (type == null) { groups = PersonaList(); } else { groups = ApplyFilters(Videos, filters, archive) .Select(x => x.Tags.Where(y => y.Type.ToLower() == type.ToLower()).Select(y => y.Value)) .Where(v => v.Any()) .Aggregate(new List <string>().AsEnumerable(), (x, y) => x.Concat(y)); } foreach (var group in groups.Distinct()) { var values = dates.Select(x => (rnd.Next(1, 300)) * 1.0).ToArray(); Values.Add(new TimeSeriesDataGroup { GroupName = group, Values = values }); } store.Add(metric, ReduceTimeSeries(dates, Values)); } return(store); }
// returns map of video id -> date -> metric name -> double public Dictionary <int, Dictionary <string, Dictionary <string, double> > > VideoMetricByDay(IEnumerable <int> apVideoIds, DateTime start, DateTime end) { using (var context = new ApplicationDbContext()) { var datesInRange = DateUtilities.GetDatesBetween(start, end); var filteredIds = apVideoIds.ToList(); var sourceDaily = from v in context.ApplicationVideos.Where(video => filteredIds.Contains(video.Id)) join avsv in context.ApplicationVideoSourceVideos on v.Id equals avsv.ApplicationVideoId join sv in context.SourceVideos on avsv.SourceVideoId equals sv.Id join daily in context.SourceVideoMetrics.AsNoTracking().Where(m => m.EventDate >= start && m.EventDate <= end) on sv.Id equals daily.VideoId group daily by new { v.Id, daily.EventDate } into x select new { Id = x.Key.Id, EventDate = x.Key.EventDate.Date, Views = x.Sum(m => m.ViewCount ?? 0), Reactions = x.Sum(m => m.ReactionCount ?? 0), }; return(sourceDaily .ToList() .GroupBy(x => x.Id) .ToDictionary( x => x.Key, x => datesInRange.Select( d => new { Date = DateUtilities.ToRestApiDateFormat(d), Metric = new List <Metric>() { new Metric() { Type = "Views", Value = x.SingleOrDefault(dm => d == dm.EventDate)?.Views ?? 0 }, new Metric() { Type = "Reactions", Value = x.SingleOrDefault(dm => d == dm.EventDate)?.Reactions ?? 0 } } }).ToDictionary( z => z.Date, z => z.Metric.ToDictionary(j => j.Type.ToLower(), j => j.Value) ) )); } }
private TimeSeries GenerateMockedChartData(DateTime startDate, DateTime endDate, IEnumerable <string> groups) { IEnumerable <DateTime> dates = DateUtilities.GetDatesBetween(startDate, endDate); var Values = new List <TimeSeriesDataGroup>(); Random rnd = new Random(); foreach (var groupName in groups) { var values = dates.Select(x => (rnd.Next(1, 300)) * 1.0).ToArray(); Values.Add(new TimeSeriesDataGroup { GroupName = groupName, Values = values }); } return(AbstractDataBackend.ReduceTimeSeries(dates, Values)); }
public override Dictionary <MetricType, TimeSeries> ComputePrimitiveTimeSeries(MetricType[] metrics, string type, DateTime start, DateTime end, Tag[] filters, ArchiveMode archive) { using (var context = new ApplicationDbContext()) { var dateInRange = DateUtilities.GetDatesBetween(start, end); //To prevent null pointer exception when selected metaTagType is "generic", set metaTagTypeId 0 var filteredApplicationVideos = ApplyFilters(context, context.ApplicationVideos, filters, archive).Select(x => x.Id).ToList(); var filteredSourceVideos = ApplySourceFilters(context, filters).Select(x => x.Id).ToList(); var store = new ConcurrentDictionary <MetricType, TimeSeries>(); var sourceVideoMetrics = metrics.Select(metric => Array.Find(SourceVideoMetrics, x => x == metric)).Distinct(); switch (type.ToLower()) { case "generic": TimeSeriesGroupedByGenericTag(metrics, start, end, dateInRange, filteredApplicationVideos, filteredSourceVideos, sourceVideoMetrics, context, store); break; default: TimeSeriesGroupedByMetaTag(metrics, start, end, type, dateInRange, filteredApplicationVideos, filteredSourceVideos, sourceVideoMetrics, context, store); break; } return(store.ToDictionary(x => x.Key, x => x.Value)); } }