From ff74cd1e107902aa5572a4b0eccb1db04c4d6579 Mon Sep 17 00:00:00 2001 From: Andrii Andreiev <129078694+AndriiAndreiev@users.noreply.github.com> Date: Fri, 29 Nov 2024 17:01:27 +0200 Subject: [PATCH] feat(dotnet): buffer-length check & queue --- .../HarJsonTranslationLogics/HarJsonBuilder.cs | 6 ++---- .../ReadmeApiCaller.cs | 8 ++++---- packages/dotnet/ReadMe/Metrics.cs | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/dotnet/ReadMe/HarJsonTranslationLogics/HarJsonBuilder.cs b/packages/dotnet/ReadMe/HarJsonTranslationLogics/HarJsonBuilder.cs index 32a0d105aa..56b701e01c 100644 --- a/packages/dotnet/ReadMe/HarJsonTranslationLogics/HarJsonBuilder.cs +++ b/packages/dotnet/ReadMe/HarJsonTranslationLogics/HarJsonBuilder.cs @@ -5,7 +5,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; -using Newtonsoft.Json; using ReadMe.HarJsonObjectModels; namespace ReadMe.HarJsonTranslationLogics @@ -28,7 +27,7 @@ public HarJsonBuilder(RequestDelegate next, HttpContext context, IConfiguration this.configValues = configValues; } - public async Task BuildHar() + internal async Task BuildHar() { Root harObj = new Root(); harObj._id = Guid.NewGuid().ToString(); @@ -37,8 +36,7 @@ public async Task BuildHar() harObj.clientIPAddress = this.context.Connection.RemoteIpAddress.ToString(); harObj.group = this.BuildGroup(); harObj.request = new RequestMain(await this.BuildLog()); - string harJsonObj = JsonConvert.SerializeObject(new List() { harObj }); - return harJsonObj; + return harObj; } private Group BuildGroup() diff --git a/packages/dotnet/ReadMe/HarJsonTranslationLogics/ReadmeApiCaller.cs b/packages/dotnet/ReadMe/HarJsonTranslationLogics/ReadmeApiCaller.cs index 001e77ee93..e5d3493c3f 100644 --- a/packages/dotnet/ReadMe/HarJsonTranslationLogics/ReadmeApiCaller.cs +++ b/packages/dotnet/ReadMe/HarJsonTranslationLogics/ReadmeApiCaller.cs @@ -7,12 +7,12 @@ namespace ReadMe.HarJsonTranslationLogics { class ReadMeApiCaller { - private readonly string harJsonObject; + private readonly string harJsonObjects; private readonly string apiKey; - public ReadMeApiCaller(string harJsonObject, string apiKey) + public ReadMeApiCaller(string harJsonObjects, string apiKey) { - this.harJsonObject = harJsonObject; + this.harJsonObjects = harJsonObjects; this.apiKey = apiKey; } @@ -25,7 +25,7 @@ public void SendHarObjToReadMeApi() request.AddHeader("Content-Type", "application/json"); string apiKey = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(this.apiKey + ":")); request.AddHeader("Authorization", apiKey); - request.AddParameter("application/json", this.harJsonObject, ParameterType.RequestBody); + request.AddParameter("application/json", this.harJsonObjects, ParameterType.RequestBody); client.ExecuteAsync(request); } catch (Exception) diff --git a/packages/dotnet/ReadMe/Metrics.cs b/packages/dotnet/ReadMe/Metrics.cs index 6bf275b585..1483cf309f 100644 --- a/packages/dotnet/ReadMe/Metrics.cs +++ b/packages/dotnet/ReadMe/Metrics.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; +using Newtonsoft.Json; using ReadMe.HarJsonObjectModels; using ReadMe.HarJsonTranslationLogics; @@ -11,12 +12,14 @@ public class Metrics { private readonly RequestDelegate next; private readonly IConfiguration configuration; + private readonly List harQueue; private Group group; public Metrics(RequestDelegate next, IConfiguration configuration) { this.next = next; this.configuration = configuration; + this.harQueue = new List(); } public async Task InvokeAsync(HttpContext context) @@ -44,9 +47,18 @@ public async Task InvokeAsync(HttpContext context) context.Request.EnableBuffering(); HarJsonBuilder harJsonBuilder = new HarJsonBuilder(this.next, context, this.configuration, configValues); - string harJsonObj = await harJsonBuilder.BuildHar(); - ReadMeApiCaller readmeApiCaller = new ReadMeApiCaller(harJsonObj, configValues.apiKey); - readmeApiCaller.SendHarObjToReadMeApi(); + var harObj = await harJsonBuilder.BuildHar(); + lock (this.harQueue) + { + this.harQueue.Add(harObj); + if (this.harQueue.Count >= configValues.options.bufferLength) + { + string serializaedHars = JsonConvert.SerializeObject(this.harQueue); + ReadMeApiCaller readmeApiCaller = new ReadMeApiCaller(serializaedHars, configValues.apiKey); + readmeApiCaller.SendHarObjToReadMeApi(); + this.harQueue.Clear(); + } + } } else {