public static async Task <IReadOnlyCollection <ChannelSheet> > Channels(SheetsCfg sheetsCfg, ILogger log) { var service = GetService(sheetsCfg); var userChannelSheets = await sheetsCfg.UserSheets .Select((v, i) => new { v.SheetId, v.Email, Weight = 1 }) // I used to weight some users higher (that's why there is that logic). Now we weight them all the same (1) .BlockFunc(async s => new { Channels = await SheetValues <UserChannelSheet>(service, s.SheetId, "Channels", log) .WithWrappedException($"error reading user sheet {s.SheetId}", log), s.SheetId, s.Weight, s.Email }, sheetsCfg.Parallel); var userChannelsById = userChannelSheets .SelectMany(u => u.Channels.Select(c => new { u.SheetId, u.Weight, Channel = c, u.Email })) .ToMultiValueDictionary(c => c.Channel.Id); var mainChannels = await MainChannels(sheetsCfg, service, log); var channels = mainChannels .Select(mc => { var ucs = userChannelsById[mc.Id].NotNull() .Where(uc => uc.Channel.Complete == "TRUE").ToList(); var classifications = ucs.Select(uc => new UserChannelStore2 { Email = uc.Email, ChannelId = uc.Channel.Id, SoftTags = SoftTags(uc.Channel), Relevance = uc.Channel.Relevance, LR = uc.Channel.LR, SheetId = uc.SheetId, Notes = uc.Channel.Notes, Weight = uc.Weight, MainChannelId = mc.MainChannelId }).ToList(); var totalWeight = classifications.Sum(c => c.Weight); var distinctTags = classifications.SelectMany(t => t.SoftTags).Distinct(); var softTags = distinctTags .Select(t => classifications.Sum(s => s.SoftTags.Contains(t) ? s.Weight : 0) > totalWeight / 2d ? t : null).NotNull().ToList(); var res = new ChannelSheet { Id = mc.Id, Title = mc.Title, LR = AverageLR(ucs.Select(c => c.Channel.LR).ToList()), MainChannelId = mc.MainChannelId, HardTags = new[] { mc.HardTag1, mc.HardTag2, mc.HardTag3 }.Where(t => t.HasValue()).OrderBy(t => t).ToList(), SoftTags = softTags, Relevance = ucs.Any() ? ucs.Average(s => s.Channel.Relevance) / 10d : 1, UserChannels = classifications }; return(res); } ).ToList(); return(channels); }
ChannelStored2 ChannelStored(ChannelSheet sheet, IKeyedCollection <string, ChannelStored2> channelPev) { var stored = channelPev[sheet.Id] ?? new ChannelStored2 { ChannelId = sheet.Id, ChannelTitle = sheet.Title, HardTags = sheet.HardTags, SoftTags = sheet.SoftTags, Relevance = sheet.Relevance, LR = sheet.LR, MainChannelId = sheet.MainChannelId, UserChannels = sheet.UserChannels, ReviewStatus = ManualAccepted }; return(stored); }