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) { var sourceRows = await _tableStore.GetRowsAsync(_sourceTableName, CancellationToken.None); var targetRows = new List <DataRow>(); var targetSchema = new RowSchema( new FieldDefinition("ContactId", FieldKind.Key, FieldDataType.Guid), new FieldDefinition("MovieId", FieldKind.Key, FieldDataType.String), new FieldDefinition("Title", FieldKind.Attribute, FieldDataType.String), new FieldDefinition("Overview", FieldKind.Attribute, FieldDataType.String), new FieldDefinition("Image", FieldKind.Attribute, FieldDataType.String), new FieldDefinition("ImdbRating", FieldKind.Attribute, FieldDataType.String) ); while (await sourceRows.MoveNext()) { foreach (var row in sourceRows.Current) { var movieIds = row["LastMoviesId"].ToString().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); //grabbing only unique movie ID's movieIds = movieIds.Distinct().ToArray(); if (!movieIds.Any()) { continue; } var recommendations = _movieRecommendationsProvider.GetRecommendations(movieIds); //clean recommendations to not have any dups recommendations = recommendations.GroupBy(m => m.netflixid).Select(g => g.FirstOrDefault()).ToList(); if (recommendations.Any()) { for (var i = 0; i < _limit; i++) { if (i < recommendations.Count) { var targetRow = new DataRow(targetSchema); targetRow.SetGuid(0, row.GetGuid(0)); targetRow.SetString(1, recommendations[i].netflixid); targetRow.SetString(2, recommendations[i].title); targetRow.SetString(3, recommendations[i].synopsis); targetRow.SetString(4, recommendations[i].image); targetRow.SetString(5, recommendations[i].rating); targetRows.Add(targetRow); } } } } } var tableDefinition = new TableDefinition(_targetTableName, targetSchema); var targetTable = new InMemoryTableData(tableDefinition, targetRows); await _tableStore.PutTableAsync(targetTable, TimeSpan.FromMinutes(30), CancellationToken.None); }
public async Task RunAsync(CancellationToken token) { var sourceRows = await _tableStore.GetRowsAsync(_sourceTableName, CancellationToken.None); var targetRows = new List <DataRow>(); var targetSchema = new RowSchema( new FieldDefinition("ContactId", FieldKind.Key, FieldDataType.Guid), new FieldDefinition("MovieId", FieldKind.Key, FieldDataType.Int64), new FieldDefinition("Title", FieldKind.Attribute, FieldDataType.String), new FieldDefinition("Overview", FieldKind.Attribute, FieldDataType.String), new FieldDefinition("PosterPath", FieldKind.Attribute, FieldDataType.String) ); //ML context MLContext mlContext = new MLContext(); ITransformer mlModel = mlContext.Model.Load(GetAbsolutePath(MODEL_FILEPATH), out DataViewSchema inputSchema); var predEngine = mlContext.Model.CreatePredictionEngine <TacoFlixML.Model.DataModels.ModelInput, ModelOutput>(mlModel); while (await sourceRows.MoveNext()) { foreach (var row in sourceRows.Current) { var movieId = int.Parse(row["LastMovieId"].ToString()); var recommendations = _movieRecommendationsProvider.GetRecommendations(movieId); if (recommendations.Any()) { for (var i = 0; i < _limit; i++) { if (i < recommendations.Count) { //ML ModelInput input = new ModelInput(); input.Movieid = recommendations[i].Id; ModelOutput predictionResult = predEngine.Predict(input); var targetRow = new DataRow(targetSchema); targetRow.SetGuid(0, row.GetGuid(0)); targetRow.SetInt64(1, recommendations[i].Id); targetRow.SetString(2, recommendations[i].Title + "score=" + predictionResult.Score); targetRow.SetString(3, recommendations[i].Overview); targetRow.SetString(4, recommendations[i].PosterPath); targetRows.Add(targetRow); } } } } } var tableDefinition = new TableDefinition(_targetTableName, targetSchema); var targetTable = new InMemoryTableData(tableDefinition, targetRows); await _tableStore.PutTableAsync(targetTable, TimeSpan.FromMinutes(30), 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 async Task RunAsync(CancellationToken token) { // Retrieve the source data from the projection. var sourceRows = await _tableStore.GetRowsAsync(_sourceTableName, CancellationToken.None); // Define the target table shcema we'll be populating into. var targetRows = new List <DataRow>(); var targetSchema = new RowSchema( 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) ); // Iterate the source data. while (await sourceRows.MoveNext()) { foreach (var row in sourceRows.Current) { // Retrieve the IDs of the album artists from the projected data. var artistIds = row["Artists"].ToString().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); // Call the recomender service. var recommendations = await _musicRecommender.GetRecommendationsAsync(artistIds, _limit); // Add a target data row for each recommendation. foreach (var album in recommendations) { var targetRow = new DataRow(targetSchema); targetRow.SetGuid(0, row.GetGuid(0)); targetRow.SetString(1, album.AlbumId); targetRow.SetString(2, album.AlbumName); targetRow.SetString(3, album.ArtistId); targetRow.SetString(4, album.ArtistName); targetRows.Add(targetRow); } } } // Populate the rows into the target table. var tableDefinition = new TableDefinition(_targetTableName, targetSchema); var targetTable = new InMemoryTableData(tableDefinition, targetRows); await _tableStore.PutTableAsync(targetTable, TimeSpan.FromMinutes(30), CancellationToken.None); }
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 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 async Task RunAsync(CancellationToken token) { var sourceRows = await _tableStore.GetRowsAsync(_sourceTableName, CancellationToken.None); var targetRows = new List <DataRow>(); var targetSchema = new RowSchema( new FieldDefinition("ContactId", FieldKind.Key, FieldDataType.Guid), new FieldDefinition("ProductId", FieldKind.Key, FieldDataType.Int64), new FieldDefinition("Score", FieldKind.Attribute, FieldDataType.Double) ); while (await sourceRows.MoveNext()) { foreach (var row in sourceRows.Current) { var purchasedProductId = uint.Parse(row["ProductId"].ToString()); var recommendations = GetRecommendList(purchasedProductId, _limit); if (recommendations.Any()) { for (var i = 0; i < _limit; i++) { var targetRow = new DataRow(targetSchema); targetRow.SetGuid(0, row.GetGuid(0)); targetRow.SetInt64(1, recommendations[i].RecommendedItemId); targetRow.SetDouble(2, recommendations[i].Score); targetRows.Add(targetRow); } } } } var tableDefinition = new TableDefinition(_targetTableName, targetSchema); var targetTable = new InMemoryTableData(tableDefinition, targetRows); await _tableStore.PutTableAsync(targetTable, TimeSpan.FromMinutes(30), CancellationToken.None); }