From 80ff76862204428caba8def94dbfb8efc0d30869 Mon Sep 17 00:00:00 2001 From: shane-agoda <86223018+shane-agoda@users.noreply.github.com> Date: Tue, 4 Jul 2023 16:36:55 +0700 Subject: [PATCH] Proposal: Refactor publisher (#29) * Refactoring publisher to reduce logic and support async operation. * Forgot to fill in environment variable names in lookup table. * Fixed failed conflict resolution. * Updated to use new publish methods. * Use Wait instead of RunSynchronously --- src/Agoda.Builds.Metrics/MeasureBuildTime.cs | 2 +- .../TimedStartupPublisher.cs | 2 +- .../DevFeedbackPublisher.cs | 71 ++++++++++--------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/Agoda.Builds.Metrics/MeasureBuildTime.cs b/src/Agoda.Builds.Metrics/MeasureBuildTime.cs index 695ddd2..dbdf8e6 100644 --- a/src/Agoda.Builds.Metrics/MeasureBuildTime.cs +++ b/src/Agoda.Builds.Metrics/MeasureBuildTime.cs @@ -50,7 +50,7 @@ public override bool Execute() gitContext: gitContext ); - DevFeedbackPublisher.Publish(ApiEndPoint, data); + DevFeedbackPublisher.Publish(ApiEndPoint, data, DevLocalDataType.NUnit); } catch (GitContextException ex) { diff --git a/src/Agoda.DevFeedback.AspNetStartup/TimedStartupPublisher.cs b/src/Agoda.DevFeedback.AspNetStartup/TimedStartupPublisher.cs index eec415e..afa951b 100644 --- a/src/Agoda.DevFeedback.AspNetStartup/TimedStartupPublisher.cs +++ b/src/Agoda.DevFeedback.AspNetStartup/TimedStartupPublisher.cs @@ -18,7 +18,7 @@ public static void Publish(string type, TimeSpan timeTaken) gitContext: gitContext ); - DevFeedbackPublisher.Publish(apiEndpoint: null, result); + DevFeedbackPublisher.Publish(apiEndpoint: null, result, DevLocalDataType.Build); } } } diff --git a/src/Agoda.DevFeedback.Common/DevFeedbackPublisher.cs b/src/Agoda.DevFeedback.Common/DevFeedbackPublisher.cs index bbd8321..07c5b4a 100644 --- a/src/Agoda.DevFeedback.Common/DevFeedbackPublisher.cs +++ b/src/Agoda.DevFeedback.Common/DevFeedbackPublisher.cs @@ -1,60 +1,63 @@ using System; +using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Text.Json; +using System.Threading.Tasks; namespace Agoda.DevFeedback.Common { public static class DevFeedbackPublisher { - public static void Publish(string apiEndpoint, T data) + /// + /// Data data type to endpoints + /// + private static Dictionary> _dataTypeEndpoints = new Dictionary>() { - Publish(apiEndpoint,data, DevLocalDataType.Build); - } + { DevLocalDataType.Build, new List() { "dotnet", "BUILD_METRICS_ES_ENDPOINT" } }, + { DevLocalDataType.NUnit, new List() { "dotnet/nunit", "NUNIT_METRICS_ES_ENDPOINT" } } + }; + + // Default URL + private const string BASE_URL = "http://compilation-metrics/"; - public static void Publish(string apiEndpoint, T data, DevLocalDataType devLocalDataType) + /// + /// Get the endpoint to use with a specific data source. + /// + private static string GetApiEndpoint(DevLocalDataType dataType, string apiEndpoint) { - var targetEndpoint = string.Empty; - switch (devLocalDataType) + _dataTypeEndpoints.TryGetValue(dataType, out var endpointInfo); + if (endpointInfo == null) + throw new ArgumentOutOfRangeException(nameof(dataType), dataType, null); + // Fetch from environment if not manually provided + if (string.IsNullOrEmpty(apiEndpoint)) { - case DevLocalDataType.Build: - targetEndpoint = GetApiEndpoint(apiEndpoint); - break; - case DevLocalDataType.NUnit: - targetEndpoint = GetNunitApiEndpoint(apiEndpoint); - break; - default: - throw new ArgumentOutOfRangeException(nameof(devLocalDataType), devLocalDataType, null); + apiEndpoint = Environment.GetEnvironmentVariable(endpointInfo[1]); } + return string.IsNullOrEmpty(apiEndpoint) ? $"{BASE_URL}{endpointInfo[0]}" : apiEndpoint; + } + + /// + /// Publish the data as JSON to the appropriate endpoint + /// + public static async Task PublishAsync(string apiEndpoint, object data, DevLocalDataType devLocalDataType) + { + var targetEndpoint = GetApiEndpoint(devLocalDataType, apiEndpoint); using (var httpClient = new HttpClient()) { httpClient.Timeout = TimeSpan.FromSeconds(2); var content = new StringContent(JsonSerializer.Serialize(data), Encoding.UTF8, "application/json"); - var response = httpClient.PostAsync(targetEndpoint, content).Result; + var response = await httpClient.PostAsync(targetEndpoint, content); response.EnsureSuccessStatusCode(); } } - private const string BASE_URL = "http://compilation-metrics/"; - //private const string BASE_URL = "http://localhost:5000/"; - static string GetApiEndpoint(string apiEndpoint) + /// + /// Non-async version of publish + /// + public static void Publish(string apiEndpoint, object data, DevLocalDataType devLocalDataType) { - if (string.IsNullOrEmpty(apiEndpoint)) - { - apiEndpoint = Environment.GetEnvironmentVariable("BUILD_METRICS_ES_ENDPOINT"); - } - - return string.IsNullOrEmpty(apiEndpoint) ? $"{BASE_URL}dotnet" : apiEndpoint; - } - - static string GetNunitApiEndpoint(string apiEndpoint) - { - if (string.IsNullOrEmpty(apiEndpoint)) - { - apiEndpoint = Environment.GetEnvironmentVariable("NUNIT_METRICS_ES_ENDPOINT"); - } - - return string.IsNullOrEmpty(apiEndpoint) ? $"{BASE_URL}dotnet/nunit" : apiEndpoint; + PublishAsync(apiEndpoint, data, devLocalDataType).Wait(); } } }