public async Task PushBulkCore_IsFasterThanLoop() { using CancellationTokenSource source = new(); const int itemCount = 1000; var docs = Enumerable.Range(1, itemCount).Select(i => Document.Random()).ToImmutableArray(); CosmosConnector c = null; try { c = await CreateTargetAsync(); Stopwatch bulk = Stopwatch.StartNew(); await c.PushBulkCoreAsync(docs, i => i.Properties, source.Token); bulk.Stop(); c = await CreateTargetAsync(); Stopwatch oneAtATime = Stopwatch.StartNew(); foreach (var d in docs) { await c.PushCoreAsync(d, i => i.Properties, source.Token); } oneAtATime.Stop(); var ratio = Convert.ToDouble(bulk.ElapsedMilliseconds) / Convert.ToDouble(oneAtATime.ElapsedMilliseconds); Assert.True(ratio < 0.5); } finally { c?.Dispose(); } }
private static async Task <ReindeerResponse> Låge3(ParticipateResponse participateResponse, SantaResponse data, HttpClient httpClient) { CosmosConnector cosmosConnector = new CosmosConnector(_documentdbEndpointUri, _authKey, _databaseId, _collectionId); List <ReindeerLocation> positions = new List <ReindeerLocation>(); foreach (Zone zone in data.Zones) { Zone normalized = zone.NormalizeRadius(); Console.WriteLine($"Normalized - {normalized.ToString()}"); ReindeerPosition locatedReindeer = await cosmosConnector.GetDocumentByZoneAsync(normalized); positions.Add(new ReindeerLocation() { Name = locatedReindeer.Name, Position = new GeoPoint(locatedReindeer.Location.Position.Latitude, locatedReindeer.Location.Position.Longitude) }); Console.WriteLine("-----------------------------------------------------------"); } HttpResponseMessage apiReindeerResponse = await httpClient.PostAsJsonAsync("/api/reindeerrescue", new { id = participateResponse.Id, locations = positions }); if (!apiReindeerResponse.IsSuccessStatusCode) { throw new ChristmasException($"{apiReindeerResponse.StatusCode}: {(await apiReindeerResponse.Content.ReadAsStringAsync())}"); } Console.WriteLine("##############################################################################"); ReindeerResponse result = await apiReindeerResponse.Content.ReadAsAsync <ReindeerResponse>(); Console.WriteLine($"Result: {result}"); return(result); }
private async Task <List <FullStudent> > GetAllFullStudents() { //CosmosConnector dbConnector = new CosmosConnector("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="); var url = UtilityFunctions.UtilityFunctions.GetValueOfSetting("cosmosUrl"); var accessKey = UtilityFunctions.UtilityFunctions.GetValueOfSetting("cosmosAccessKey"); CosmosConnector dbConnector = new CosmosConnector(url, accessKey); dbConnector.PreviousDatabaseName = "StudentDatabase"; dbConnector.PreviousTableName = "StudentRecords"; var records = await dbConnector.GetStudentRecords(); List <FullStudent> fullStudents = new List <FullStudent>(); foreach (var record in records) { var dict = (IDictionary <string, object>)record; FullStudent fullStudent = StudentMapper.DictionaryObjectToFullStudent(dict); //FullStudent fullStudent = new FullStudent() //{ // FirstName = dict["FirstName"] as string, // MiddleName = dict["MiddleName"] as string, // LastName = dict["LastName"] as string, // DateOfBirth = (DateTime)dict["DateOfBirth"], // Organization = dict["Organization"] as string, // SchoolDivision = dict["SchoolDivision"] as string, // Degree = dict["Degree"] as string, // Awarded = (DateTime)dict["Awarded"], // Major = dict["Major"] as string, // PreviousRecordHash = dict["PreviousRecordHash"] as string, // CurrentNodeHash = dict["CurrentNodeHash"] as string, // Salt = dict["Salt"] as string, // RecordId = Convert.ToInt32(dict["RecordId"]) //}; fullStudents.Add(fullStudent); } return(fullStudents); }
private async Task <List <FullStudent> > GetAllFullStudents() { var url = UtilityFunctions.UtilityFunctions.GetValueOfSetting("cosmosUrl"); var accessKey = UtilityFunctions.UtilityFunctions.GetValueOfSetting("cosmosAccessKey"); IDatabaseConnector dbConnector = new CosmosConnector(url, accessKey); dbConnector.PreviousDatabaseName = "StudentDatabase"; dbConnector.PreviousTableName = "StudentRecords"; var records = await dbConnector.GetStudentRecords(); List <FullStudent> fullStudents = new List <FullStudent>(); foreach (var record in records) { var dict = (IDictionary <string, object>)record; FullStudent fullStudent = StudentMapper.DictionaryObjectToFullStudent(dict); fullStudents.Add(fullStudent); } return(fullStudents); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context) { log.LogInformation($"C# HTTP trigger function processed request #{++count}."); bool isLocal = UtilityFunctions.UtilityFunctions.IsLocalEnvironment(); string response = isLocal ? "Function is running on local environment." : "Function is running on Azure."; Console.WriteLine(response); // load the connection string for the db var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); Student student = JsonConvert.DeserializeObject <Student>(requestBody); student.FirstName = student.FirstName.CapitalizeEveryLetterOnSplit(' '); student.MiddleName = student.MiddleName.CapitalizeEveryLetterOnSplit(' '); student.LastName = student.LastName.CapitalizeEveryLetterOnSplit(' '); // search the cosmosdb for the record and then grab the record dynamic studentCosmosRecord; string uri = config.GetConnectionString("address"); string accessKey = config.GetConnectionString("accessKey"); using (CosmosConnector dbConnector = new CosmosConnector(uri, accessKey)) { dbConnector.UseDataBase("StudentDatabase"); dbConnector.UseTableName("StudentRecords"); studentCosmosRecord = dbConnector.GetFullStudentRecordFromName(student); } dynamic previousRecord = studentCosmosRecord; var dict = (IDictionary <string, object>)previousRecord; BasicStudent basicStudent = new BasicStudent() { FirstName = dict["FirstName"] as string, MiddleName = dict["MiddleName"] as string, LastName = dict["LastName"] as string, DateOfBirth = (DateTime)dict["DateOfBirth"], Organization = dict["Organization"] as string, SchoolDivision = dict["SchoolDivision"] as string, Degree = dict["Degree"] as string, Awarded = (DateTime)dict["Awarded"], Major = dict["Major"] as string }; // send the record data to the pdf template generator and generate pdf Pdf studentPdf = new Pdf(basicStudent, converter); string pdfOutput = studentPdf.SaveToBase64String(); // send pdf back as response object dataToSendBack = new { Pdf = pdfOutput }; var json = JsonConvert.SerializeObject(dataToSendBack, Formatting.Indented); var successMessageToReturn = new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent(json, Encoding.UTF8, "application/json") }; var failureMessageToReturn = new HttpResponseMessage(HttpStatusCode.NotFound); return(student.FirstName != null ? successMessageToReturn : failureMessageToReturn); }