public bool SetFacets(UserProfile profile, Contact contact, IXdbContext client) { var email = profile.Email; if (string.IsNullOrEmpty(email)) { return(false); } var emails = contact.GetFacet <EmailAddressList>(EmailAddressList.DefaultFacetKey); if (emails == null) { emails = new EmailAddressList(new EmailAddress(email, false), null); } else { if (emails.PreferredEmail?.SmtpAddress == email) { return(false); } emails.PreferredEmail = new EmailAddress(email, false); } client.SetFacet(contact, EmailAddressList.DefaultFacetKey, emails); return(true); }
public bool SetFacets(UserProfile profile, Contact contact, IXdbContext client) { var phoneNumber = profile[Accounts.Constants.UserProfile.Fields.PhoneNumber]; if (string.IsNullOrEmpty(phoneNumber)) { return(false); } var phoneNumbers = contact.GetFacet <PhoneNumberList>(PhoneNumberList.DefaultFacetKey); if (phoneNumbers == null) { phoneNumbers = new PhoneNumberList(new PhoneNumber(null, phoneNumber), null); } else { if (phoneNumbers.PreferredPhoneNumber?.Number == phoneNumber) { return(false); } phoneNumbers.PreferredPhoneNumber = new PhoneNumber(null, phoneNumber); } client.SetFacet(contact, PhoneNumberList.DefaultFacetKey, phoneNumbers); return(true); }
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 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 bool SetFacets(UserProfile profile, Contact contact, IXdbContext client) { var changed = false; var personalInfo = contact.GetFacet <PersonalInformation>(PersonalInformation.DefaultFacetKey) ?? new PersonalInformation(); changed |= this.SetBirthdate(profile, personalInfo); changed |= this.SetName(profile, personalInfo); changed |= this.SetGender(profile, personalInfo); changed |= this.SetLanguage(profile, personalInfo); if (!changed) { return(false); } client.SetFacet(contact, PersonalInformation.DefaultFacetKey, personalInfo); return(true); }
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); }
public bool SetFacets(UserProfile profile, Contact contact, IXdbContext client) { var url = profile[Accounts.Constants.UserProfile.Fields.PictureUrl]; var mimeType = profile[Accounts.Constants.UserProfile.Fields.PictureMimeType]; if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(mimeType)) { return(false); } try { var pictureData = this.webClient.DownloadData(url); var pictureMimeType = mimeType; var avatar = contact.GetFacet <Avatar>(Avatar.DefaultFacetKey); if (avatar == null) { avatar = new Avatar(pictureMimeType, pictureData); } else if (avatar.Picture == pictureData && avatar.MimeType == mimeType) { return(false); } else { avatar.MimeType = pictureMimeType; avatar.Picture = pictureData; } client.SetFacet(contact, Avatar.DefaultFacetKey, avatar); return(true); } catch (Exception exception) { Log.Warn($"Could not download profile picture {url}", exception, this); return(false); } }