public async Task RunAsync(CancellationToken token) { var rows = await _tableStore.GetRowsAsync(_tableName, CancellationToken.None); while (await rows.MoveNext()) { foreach (var row in rows.Current) { var contactId = row.GetGuid(0); var productId = row.GetInt64(1); var score = row.GetDouble(2); var contact = await _xdbContext.GetContactAsync(contactId, new ContactExpandOptions(ProductRecommendationFacet.DefaultFacetKey)); var facet = contact.GetFacet <ProductRecommendationFacet>(ProductRecommendationFacet.DefaultFacetKey) ?? new ProductRecommendationFacet(); if (facet.ProductRecommendations.All(x => x.ProductId != productId)) { facet.ProductRecommendations.Add(new ProductRecommend { ProductId = productId, Score = score }); _xdbContext.SetFacet(contact, ProductRecommendationFacet.DefaultFacetKey, facet); await _xdbContext.SubmitAsync(CancellationToken.None); } } } await _tableStore.RemoveAsync(_tableName, CancellationToken.None); }
public Task ProcessBatchAsync(IReadOnlyList <Contact> batch, CancellationToken token) { foreach (var contact in batch) { _xdbContext.DeleteContact(contact); } return(_xdbContext.SubmitAsync(token)); }
public async Task RunAsync(CancellationToken token) { // Retrieve the recommendation data from the table. var rows = await _tableStore.GetRowsAsync(_tableName, CancellationToken.None); // Iterate the recommendation data. while (await rows.MoveNext()) { foreach (var row in rows.Current) { // Row schema // new FieldDefinition("ContactID", FieldKind.Key, FieldDataType.Guid), // new FieldDefinition("AlbumID", FieldKind.Key, FieldDataType.String), // new FieldDefinition("AlbumName", FieldKind.Attribute, FieldDataType.String), // new FieldDefinition("ArtistID", FieldKind.Attribute, FieldDataType.String), // new FieldDefinition("ArtistName", FieldKind.Attribute, FieldDataType.String) var contactId = row.GetGuid(0); var albumId = row.GetString(1); var albumName = row.GetString(2); var artistId = row.GetString(3); var artistName = row.GetString(4); // Load the contact with their album recommendation facet. var contact = await _xdbContext.GetContactAsync(contactId, new ContactExpandOptions(AlbumRecommendationFacet.DefaultFacetName)); var facet = contact.GetFacet <AlbumRecommendationFacet>(AlbumRecommendationFacet.DefaultFacetName) ?? new AlbumRecommendationFacet(); // Add the album to the facet if it doesn't already exist if (facet.AlbumRecommendations.All(x => x.AlbumId != albumId)) { facet.AlbumRecommendations.Add(new AlbumDescriptor { AlbumId = albumId, AlbumName = albumName, ArtistId = artistId, ArtistName = artistName }); // Update the facet if it's been changed. _xdbContext.SetFacet(contact, AlbumRecommendationFacet.DefaultFacetName, facet); await _xdbContext.SubmitAsync(CancellationToken.None); } } } // Delete the table now that we're done with it. await _tableStore.RemoveAsync(_tableName, CancellationToken.None); }
public async Task <ActionResult> GenerateUsers() { using (IXdbContext xdbContext = SitecoreXConnectClientConfiguration.GetClient()) { var ovais = new Contact(new ContactIdentifier("letsplay", "*****@*****.**", ContactIdentifierType.Known)); var sumith = new Contact(new ContactIdentifier("letsplay", "*****@*****.**", ContactIdentifierType.Known)); xdbContext.AddContact(ovais); xdbContext.AddContact(sumith); await xdbContext.SubmitAsync(); } return(new HttpStatusCodeResult(HttpStatusCode.OK)); }
public async Task <ActionResult> GenerateData(int amountOfContacts = 10, int amountOfInteractions = 10) { using (IXdbContext xdbContext = SitecoreXConnectClientConfiguration.GetClient()) { for (var c = 0; c < amountOfContacts; c++) { var contact = new Contact(new ContactIdentifier("sitecore.demo", Guid.NewGuid().ToString(), ContactIdentifierType.Known)); var currentDate = DateTime.UtcNow; xdbContext.AddContact(contact); for (var i = 1; i <= amountOfInteractions; i++) { // Even are morning runners. 10AM for morning runner and 6PM for evening one. : var startTime = 10 + c % 2 * 8; var startDate = currentDate.Date.AddDays(-i).AddHours(startTime); var endDate = startDate.AddHours(1); var interaction = new Interaction(contact, InteractionInitiator.Contact, /*TODO: Channel ID*/ Guid.NewGuid(), "Some Agent") { Events = { new RunStarted(/*TODO: Definition ID*/ Guid.NewGuid(), startDate) { Time = startDate }, new RunEnded(/*TODO: Definition ID*/ Guid.NewGuid(), endDate) { Time = endDate } } }; xdbContext.AddInteraction(interaction); } } await xdbContext.SubmitAsync(); } return(new HttpStatusCodeResult(HttpStatusCode.OK)); }
public async Task RunAsync(CancellationToken token) { var rows = await _tableStore.GetRowsAsync(_tableName, CancellationToken.None); while (await rows.MoveNext()) { foreach (var row in rows.Current) { var contactId = row.GetGuid(0); var movieId = row.GetString(1); var title = row.GetString(2); var overview = row.GetString(3); var image = row.GetString(4); var rating = row.GetString(5); var contact = await _xdbContext.GetContactAsync(contactId, new ContactExpandOptions(MovieRecommendationFacet.DefaultFacetKey)); var facet = contact.GetFacet <MovieRecommendationFacet>(MovieRecommendationFacet.DefaultFacetKey) ?? new MovieRecommendationFacet(); if (facet.MovieRecommendations.All(x => x.netflixid != movieId)) { facet.MovieRecommendations.Add(new Movie { netflixid = movieId, title = title, synopsis = overview, image = image, rating = rating }); } _xdbContext.SetFacet(contact, MovieRecommendationFacet.DefaultFacetKey, facet); await _xdbContext.SubmitAsync(CancellationToken.None); } } await _tableStore.RemoveAsync(_tableName, CancellationToken.None); System.Console.WriteLine("Finished Storing Recommendations for Contacts"); }
public Task ProcessBatchAsync(IReadOnlyList <Contact> batch, CancellationToken token) { foreach (var contact in batch) { var runEnded = contact.Interactions.SelectMany(x => x.Events).OfType <RunEnded>() .ToArray(); var totalRuns = runEnded.Length; if (totalRuns == 0) { continue; } var totalRunsReciprocal = 1 / (double)totalRuns; var morningRunner = runEnded.Count(x => x.Time.Hour < _morningEndHours) * totalRunsReciprocal; var eveningRunner = runEnded.Count(x => x.Time.Hour > _eveningStartHours) * totalRunsReciprocal; var runnerFacet = contact.GetFacet <RunnerFacet>(); var averageMultiplier = 0.5; if (runnerFacet == null) { runnerFacet = new RunnerFacet(); averageMultiplier = 1; } runnerFacet.IsMorningRunner = (runnerFacet.IsMorningRunner + morningRunner) * averageMultiplier; runnerFacet.IsEveningRunner = (runnerFacet.IsEveningRunner + eveningRunner) * averageMultiplier; _xdbContext.SetFacet(contact, runnerFacet); } return(_xdbContext.SubmitAsync(token)); }
public async Task RunAsync(CancellationToken token) { var rows = await _tableStore.GetRowsAsync(_tableName, CancellationToken.None); while (await rows.MoveNext()) { foreach (var row in rows.Current) { var contactId = row.GetGuid(0); var movieId = row.GetInt64(1); var title = row.GetString(2); var overview = row.GetString(3); var posterPath = row.GetString(4); var contact = await _xdbContext.GetContactAsync(contactId, new ContactExpandOptions(MovieRecommendationFacet.DefaultFacetName)); var facet = contact.GetFacet <MovieRecommendationFacet>(MovieRecommendationFacet.DefaultFacetName) ?? new MovieRecommendationFacet(); if (facet.MovieRecommendations.All(x => x.Id != movieId)) { facet.MovieRecommendations.Add(new Movie { Id = movieId, Title = title, Overview = overview, PosterPath = posterPath }); _xdbContext.SetFacet(contact, MovieRecommendationFacet.DefaultFacetName, facet); await _xdbContext.SubmitAsync(CancellationToken.None); } } } await _tableStore.RemoveAsync(_tableName, CancellationToken.None); }