Skip to content

Commit

Permalink
Proposal: Refactor publisher (#29)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
shane-agoda committed Jul 4, 2023
1 parent ae46947 commit 80ff768
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Agoda.Builds.Metrics/MeasureBuildTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override bool Execute()
gitContext: gitContext
);

DevFeedbackPublisher.Publish(ApiEndPoint, data);
DevFeedbackPublisher.Publish(ApiEndPoint, data, DevLocalDataType.NUnit);
}
catch (GitContextException ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
71 changes: 37 additions & 34 deletions src/Agoda.DevFeedback.Common/DevFeedbackPublisher.cs
Original file line number Diff line number Diff line change
@@ -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<T>(string apiEndpoint, T data)
/// <summary>
/// Data data type to endpoints
/// </summary>
private static Dictionary<DevLocalDataType, List<string>> _dataTypeEndpoints = new Dictionary<DevLocalDataType, List<string>>()
{
Publish(apiEndpoint,data, DevLocalDataType.Build);
}
{ DevLocalDataType.Build, new List<string>() { "dotnet", "BUILD_METRICS_ES_ENDPOINT" } },
{ DevLocalDataType.NUnit, new List<string>() { "dotnet/nunit", "NUNIT_METRICS_ES_ENDPOINT" } }
};

// Default URL
private const string BASE_URL = "http://compilation-metrics/";

public static void Publish<T>(string apiEndpoint, T data, DevLocalDataType devLocalDataType)
/// <summary>
/// Get the endpoint to use with a specific data source.
/// </summary>
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;
}

/// <summary>
/// Publish the data as JSON to the appropriate endpoint
/// </summary>
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)
/// <summary>
/// Non-async version of publish
/// </summary>
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();
}
}
}

0 comments on commit 80ff768

Please sign in to comment.