/// <summary> /// Initializes a new instance of the <see cref="QueryContext"/> class. /// </summary> /// <param name="filters">query filters</param> /// <param name="client">the client</param> /// <param name="otherClients">other clients</param> /// <param name="sort">deas sort</param> /// <param name="userLocation">user location</param> /// <param name="userData">user data</param> /// <param name="inputFlights">input flights</param> /// <param name="parentEventId">parent event id</param> public QueryContext(QueryFilters filters = null, Client client = null, IEnumerable <Client> otherClients = null, DealsSort?sort = null, UserLocation userLocation = null, UserData userData = null, IEnumerable <string> inputFlights = null, Guid?parentEventId = null) { Filters = filters ?? new QueryFilters(); Sort = sort ?? DealsSort.Relevance; UserLocation = userLocation; UserData = userData ?? new UserData(); Client = client ?? new Client(Client.IdUnknown, Client.AppUnknown); OtherClients = otherClients ?? Enumerable.Empty <Client>(); InputFlights = inputFlights; AssignFlightKey(); Ranking = new QueryRanking(Client, Filters, FlightKey); EventId = Guid.NewGuid(); ParentEventId = parentEventId == null ? EventId : parentEventId.Value; }
/// <summary> /// Initializes a new instance of the <see cref="QueryRanking" /> class. /// </summary> /// <param name="client">the client</param> /// <param name="filters">query filters</param> /// <param name="flightKey">query flight key</param> public QueryRanking(Client client, QueryFilters filters, string flightKey) { var defaultRankingGroupSequence = RankingGroup.DefaultSequence; var pcfps = GetPlacementCfps(client, flightKey, true); var featureRankingGroupSequences = new List <int>(); var featureRankingGroupCounts = new List <byte>(); var sequenceToPlacementIndex = new Dictionary <int, string>(); var isFiltered = (filters.QueryKeywords != null && filters.QueryKeywords.Any()) || (filters.Categories != null && filters.Categories.Any()); foreach (var pcfp in pcfps.OrderBy(_ => _.Value.PlacementSequence)) { var cfpInt = pcfp.Value; RankingGroup rankingGroup; if (!RankingGroup.List.TryGetValue(cfpInt.RankingGroupKey, out rankingGroup)) { Log.Warn("QueryRanking: Ranking group for {0} key is not found.", cfpInt.RankingGroupKey); rankingGroup = RankingGroup.List.Values.FirstOrDefault(_ => _.Id == RankingGroup.DefaultId); if (rankingGroup == null) { Log.Warn("QueryRanking: RankingGroup.List.Count()={0}", RankingGroup.List.Count()); throw new Exception("QueryRanking: Default ranking group is not found."); } } // switch to unfiltered ranking group if query has explicit filters RankingGroup notFilteredRankingGroup; if (isFiltered && rankingGroup.NotFilteredKey != null && RankingGroup.List.TryGetValue(rankingGroup.NotFilteredKey, out notFilteredRankingGroup)) { rankingGroup = notFilteredRankingGroup; } var sequence = rankingGroup.QueryRankArraySequence != null ? rankingGroup.QueryRankArraySequence.Value : RankingGroup.DefaultSequence; if (cfpInt.Placement == "Default") { UseRandomDealSelection = rankingGroup.UseRandomDealSelection; defaultRankingGroupSequence = sequence; DealsCountForRandomization = cfpInt.DealsCountForRandomization; FallbackToOnlineDeals = cfpInt.FallbackToOnlineDeals; // There are cases when the same ranking group is used for default and other placements. // For these cases it better to have mapping to default if (sequenceToPlacementIndex.ContainsKey(sequence)) { sequenceToPlacementIndex.Remove(sequence); } sequenceToPlacementIndex.Add(sequence, cfpInt.Placement); } else { featureRankingGroupSequences.Add(sequence); featureRankingGroupCounts.Add(cfpInt.DefaultDealsCount); if (!sequenceToPlacementIndex.ContainsKey(sequence)) { sequenceToPlacementIndex.Add(sequence, cfpInt.Placement); } } } DefaultRankingGroupSequence = defaultRankingGroupSequence; FeatureRankingGroupSequences = featureRankingGroupSequences.ToList(); FeatureRankingGroupCounts = featureRankingGroupCounts.ToList(); SequenceToPlacementIndex = sequenceToPlacementIndex; RankArraySlot = RankingGroup.List.Values.FirstOrDefault().QueryRankArraySlot.Value; PublishingVersion = Query.PublishingVersion.List.FirstOrDefault().Key; }