/// <summary> /// Cancels any non-completed trips and consignments. This is called when the user requests /// to generate new trips. There cannot be more than one active or pending trip associated /// with a vehicle at one time. /// </summary> /// <param name="container"></param> /// <returns></returns> private static async Task CancelNonCompletedTripsAndConsignments(Container container, BulkImporter bulkImporter, CancellationToken cancellationToken) { var trips = new List <Trip>(); var consignments = new List <Consignment>(); WriteLineInColor($"\nCanceling incomplete trips and consignments from Cosmos DB.", ConsoleColor.DarkCyan); var query = new QueryDefinition("SELECT * FROM c WHERE c.entityType = @entityType AND (c.status != @status AND c.status != @status2)") .WithParameter("@entityType", WellKnown.EntityTypes.Trip) .WithParameter("@status", WellKnown.Status.Completed) .WithParameter("@status2", WellKnown.Status.Canceled); var tripResults = container.GetItemQueryIterator <Trip>(query); while (tripResults.HasMoreResults) { foreach (var trip in await tripResults.ReadNextAsync(cancellationToken)) { trip.status = WellKnown.Status.Canceled; trips.Add(trip); } } query = new QueryDefinition("SELECT * FROM c WHERE c.entityType = @entityType AND (c.status != @status AND c.status != @status2)") .WithParameter("@entityType", WellKnown.EntityTypes.Consignment) .WithParameter("@status", WellKnown.Status.Completed) .WithParameter("@status2", WellKnown.Status.Canceled); var consignmentResults = container.GetItemQueryIterator <Consignment>(query); while (consignmentResults.HasMoreResults) { foreach (var consignment in await consignmentResults.ReadNextAsync(cancellationToken)) { consignment.status = WellKnown.Status.Canceled; consignments.Add(consignment); } } // Bulk update trips. await bulkImporter.BulkImport(trips, DatabaseName, MetadataContainerName, cancellationToken, 1); // Bulk update consignments. await bulkImporter.BulkImport(consignments, DatabaseName, MetadataContainerName, cancellationToken, 1); }
/// <summary> /// Seeds the Cosmos DB metadata container. /// </summary> private static async Task SeedDatabase(CosmosDbConnectionString cosmosDbConnectionString, CancellationToken cancellationToken) { // Check if data exists before seeding. var count = 0; var query = new QueryDefinition($"SELECT VALUE COUNT(1) FROM c"); var container = await GetContainerIfExists(MetadataContainerName); var resultSetIterator = container.GetItemQueryIterator <int>(query, requestOptions: new QueryRequestOptions() { MaxItemCount = 1 }); if (resultSetIterator.HasMoreResults) { var result = await resultSetIterator.ReadNextAsync(); if (result.Count > 0) { count = result.FirstOrDefault(); } } if (count == 0) { // Scale up the requested throughput (RU/s) for the metadata container prior to bulk import: WriteLineInColor($"No data currently exists in the {MetadataContainerName} container. Scaling up the container RU/s to 50,000 prior to bulk data insert...", ConsoleColor.Cyan); await ChangeContainerPerformance(container, 50000); WriteLineInColor("Container RU/s adjusted. Generating data to seed database...", ConsoleColor.Cyan); var bulkImporter = new BulkImporter(cosmosDbConnectionString); var vehicles = DataGenerator.GenerateVehicles().ToList(); var consignments = DataGenerator.GenerateConsignments(900).ToList(); var packages = DataGenerator.GeneratePackages(consignments.ToList()).ToList(); var trips = DataGenerator.GenerateTrips(consignments.ToList(), vehicles.ToList()).ToList(); WriteLineInColor("Generated data to seed database. Saving metadata to Cosmos DB...", ConsoleColor.Cyan); // Save vehicles: WriteLineInColor($"Adding {vehicles.Count()} vehicles...", ConsoleColor.Green); await bulkImporter.BulkImport(vehicles, DatabaseName, MetadataContainerName, cancellationToken, 1); // Save consignments: WriteLineInColor($"Adding {consignments.Count()} consignments...", ConsoleColor.Green); await bulkImporter.BulkImport(consignments, DatabaseName, MetadataContainerName, cancellationToken, 1); // Save packages: WriteLineInColor($"Adding {packages.Count()} packages...", ConsoleColor.Green); await bulkImporter.BulkImport(packages, DatabaseName, MetadataContainerName, cancellationToken, 4); // Save trips: WriteLineInColor($"Adding {trips.Count()} trips...", ConsoleColor.Green); await bulkImporter.BulkImport(trips, DatabaseName, MetadataContainerName, cancellationToken, 1); WriteLineInColor("Finished seeding Cosmos DB.", ConsoleColor.Cyan); // Scale down the requested throughput (RU/s) for the metadata container: await ChangeContainerPerformance(container, 15000); } else { WriteLineInColor("\nCosmos DB already contains data. Skipping database seeding step...", ConsoleColor.Yellow); } }