Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed audience count in first query, and remove subsequent queries #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
using IGraphQLClient = UNRVLD.ODP.VisitorGroups.GraphQL.IGraphQLClient;
using EPiServer.ServiceLocation;
using UNRVLD.ODP.VisitorGroups.GraphQL.Models;
using UNRVLD.ODP.VisitorGroups.GraphQL.Models.AudienceCount;
using EPiServer.Framework.Cache;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace UNRVLD.ODP.VisitorGroups.Criteria.Models
{
Expand All @@ -24,18 +21,12 @@
{
private readonly IGraphQLClient client;
private readonly ISynchronizedObjectInstanceCache cache;
private string cacheKey = "OdpVisitorGroups_AudienceList_";

Check warning on line 24 in src/UNRVLD.ODP.VisitorGroups/Criteria/Models/AudienciesSelectionFactory.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AudienciesSelectionFactory.cacheKey' is assigned but its value is never used

Check warning on line 24 in src/UNRVLD.ODP.VisitorGroups/Criteria/Models/AudienciesSelectionFactory.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AudienciesSelectionFactory.cacheKey' is assigned but its value is never used

Check warning on line 24 in src/UNRVLD.ODP.VisitorGroups/Criteria/Models/AudienciesSelectionFactory.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AudienciesSelectionFactory.cacheKey' is assigned but its value is never used

Check warning on line 24 in src/UNRVLD.ODP.VisitorGroups/Criteria/Models/AudienciesSelectionFactory.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AudienciesSelectionFactory.cacheKey' is assigned but its value is never used

Check warning on line 24 in src/UNRVLD.ODP.VisitorGroups/Criteria/Models/AudienciesSelectionFactory.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AudienciesSelectionFactory.cacheKey' is assigned but its value is never used

Check warning on line 24 in src/UNRVLD.ODP.VisitorGroups/Criteria/Models/AudienciesSelectionFactory.cs

View workflow job for this annotation

GitHub Actions / build

The field 'AudienciesSelectionFactory.cacheKey' is assigned but its value is never used
#if NET5_0_OR_GREATER
private readonly IServiceScopeFactory serviceScopeFactory;
#endif

public AudienciesSelectionFactory()
{
client = ServiceLocator.Current.GetInstance<IGraphQLClient>();
cache = ServiceLocator.Current.GetInstance<ISynchronizedObjectInstanceCache>();
#if NET5_0_OR_GREATER
serviceScopeFactory = ServiceLocator.Current.GetInstance<IServiceScopeFactory>();
#endif
}

public IEnumerable<SelectListItem> GetSelectListItems(Type propertyType)
Expand All @@ -46,96 +37,55 @@
node {
description
name
population_estimate(percent_error: 10) {
estimated_lower_bound
estimated_upper_bound
}
}
}
}
}";

var selectItems = new List<SelectListItem>();

try
{
var cachePopulationRequested = false;
var result = client.Query<AudiencesResponse>(query).Result;
var orderedResult = result.Items.OrderBy(x => x.Description);

selectItems = new List<SelectListItem>();
foreach (var audience in orderedResult)
{
var cacheResult = cache.Get(cacheKey + audience.Name);
if (cacheResult != null)
{
selectItems.Add(new SelectListItem() { Text = audience.Description + GetCountEstimateString((AudienceCount)cacheResult), Value = audience.Name });
}
else
{
selectItems.Add(new SelectListItem() { Text = audience.Description + " (Calculating segment size...)", Value = audience.Name });
if (cachePopulationRequested == false)
{
cachePopulationRequested = true;

#if NET5_0_OR_GREATER
_ = Task.Run(async () =>
{
try
{
using var scope = serviceScopeFactory.CreateScope();
var cachePopulator = scope.ServiceProvider.GetRequiredService<IAudienceSizeCachePopulator>();
await cachePopulator.PopulateEntireCache(false);
}
catch (Exception e)
{
Console.WriteLine(e);
}
});
#elif NET461_OR_GREATER
try
{
var cachePopulator = ServiceLocator.Current.GetInstance<IAudienceSizeCachePopulator>();
HostingEnvironment.QueueBackgroundWorkItem(c => cachePopulator.PopulateEntireCache(false));
}
catch (Exception e)
{
Console.WriteLine(e);
}
#endif
}
}
}

return selectItems;
return result.Items
.OrderBy(x => x.Description)
.Select(audience => new SelectListItem { Text = audience.Description + GetCountEstimateString(audience), Value = audience.Name })
.ToArray();
}
catch
catch
{
return new List<SelectListItem>();
return Enumerable.Empty<SelectListItem>();
}
}

private string GetCountEstimateString(AudienceCount audienceCount)
private string GetCountEstimateString(Audience audience)
{
if (audienceCount == null ||
audienceCount.PopulationEstimate == null)
if (audience?.PopulationEstimate == null)
{
return string.Empty;
}

if (audienceCount.PopulationEstimate.EstimatedLowerBound == 0)
var populationEstimate = audience.PopulationEstimate;
if (populationEstimate.EstimatedLowerBound == 0)
{
return " (close to 0 visitors)";
}

if (audienceCount.PopulationEstimate.EstimatedLowerBound == 1)
if (populationEstimate.EstimatedLowerBound == 1)
{
return " (more than 1 visitor)";
}

if (audienceCount.PopulationEstimate.EstimatedLowerBound < 100)
if (populationEstimate.EstimatedLowerBound < 100)
{
return $" (more than {audienceCount.PopulationEstimate.EstimatedLowerBound} visitors)";
return $" (more than {populationEstimate.EstimatedLowerBound} visitors)";
}

int calc = (audienceCount.PopulationEstimate.EstimatedLowerBound +
audienceCount.PopulationEstimate.EstimatedUpperBound) / 2;
int calc = (populationEstimate.EstimatedLowerBound +
populationEstimate.EstimatedUpperBound) / 2;
return $" (about {calc} visitors)";

}
Expand Down

This file was deleted.

8 changes: 7 additions & 1 deletion src/UNRVLD.ODP.VisitorGroups/GraphQL/Models/Audience.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
namespace UNRVLD.ODP.VisitorGroups.GraphQL.Models
using Newtonsoft.Json;
using UNRVLD.ODP.VisitorGroups.GraphQL.Models.AudienceCount;

namespace UNRVLD.ODP.VisitorGroups.GraphQL.Models
{
public class Audience
{
public string Name { get; set; }
public string Description { get; set; }

[JsonProperty("population_estimate")]
public PopulationEstimate PopulationEstimate { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static void AddODPVisitorGroups(this IServiceCollection services)
services.AddScoped<ICustomerDataRetriever, CustomerDataRetriever>();
services.AddScoped<ICustomerPropertyListRetriever, CustomerPropertyListRetriever>();
services.AddHttpContextOrThreadScoped<IODPUserProfile, ODPUserProfile>();
services.AddTransient<IAudienceSizeCachePopulator, AudienceSizeCachePopulator>();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public void ConfigureContainer(ServiceConfigurationContext context)
services.AddScoped<ICustomerDataRetriever, CustomerDataRetriever>();
services.AddScoped<ICustomerPropertyListRetriever, CustomerPropertyListRetriever>();
services.AddHttpContextOrThreadScoped<IODPUserProfile, ODPUserProfile>();
services.AddTransient<IAudienceSizeCachePopulator, AudienceSizeCachePopulator>();
}

public void Initialize(InitializationEngine context)
Expand Down
Loading