Skip to content

Commit

Permalink
Code fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed May 22, 2024
1 parent a1f952c commit e7be38e
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 128 deletions.
7 changes: 7 additions & 0 deletions dev-proxy-abstractions/BaseReportingPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Microsoft.DevProxy.Abstractions;

public abstract class BaseReportingPlugin : BaseProxyPlugin
{
protected BaseReportingPlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
{
}

protected virtual void StoreReport(object report, ProxyEventArgsBase e)
{
if (report is null)
Expand Down
6 changes: 3 additions & 3 deletions dev-proxy-abstractions/MSGraphDbUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,20 @@ private static void FillData(SqliteConnection dbConnection, ILogger logger)

foreach (var path in document.Paths)
{
logger.LogDebug("Endpoint {graphVersion}{key}...", graphVersion, path.Key);
logger.LogTrace("Endpoint {graphVersion}{key}...", graphVersion, path.Key);

// Get the GET operation for this path
var getOperation = path.Value.Operations.FirstOrDefault(o => o.Key == OperationType.Get).Value;
if (getOperation == null)
{
logger.LogDebug("No GET operation found for {graphVersion}{key}", graphVersion, path.Key);
logger.LogTrace("No GET operation found for {graphVersion}{key}", graphVersion, path.Key);
continue;
}

// Check if the GET operation has a $select parameter
var hasSelect = getOperation.Parameters.Any(p => p.Name == "$select");

logger.LogDebug("Inserting endpoint {graphVersion}{key} with hasSelect={hasSelect}...", graphVersion, path.Key, hasSelect);
logger.LogTrace("Inserting endpoint {graphVersion}{key} with hasSelect={hasSelect}...", graphVersion, path.Key, hasSelect);
insertEndpoint.Parameters["@path"].Value = path.Key;
insertEndpoint.Parameters["@graphVersion"].Value = graphVersion;
insertEndpoint.Parameters["@hasSelect"].Value = hasSelect;
Expand Down
25 changes: 13 additions & 12 deletions dev-proxy-plugins/Reporters/BaseReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ namespace Microsoft.DevProxy.Plugins.Reporters;

public abstract class BaseReporter : BaseProxyPlugin
{
protected BaseReporter(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
{
}

public virtual string FileExtension => throw new NotImplementedException();

public override void Register(IPluginEvents pluginEvents,
IProxyContext context,
ISet<UrlToWatch> urlsToWatch,
IConfigurationSection? configSection = null)
public override void Register()
{
base.Register(pluginEvents, context, urlsToWatch, configSection);
base.Register();

pluginEvents.AfterRecordingStop += AfterRecordingStop;
PluginEvents.AfterRecordingStop += AfterRecordingStop;
}

protected abstract string? GetReport(KeyValuePair<string, object> report);
Expand All @@ -29,32 +30,32 @@ protected virtual Task AfterRecordingStop(object sender, RecordingArgs e)
e.GlobalData[ProxyUtils.ReportsKey] is not Dictionary<string, object> reports ||
!reports.Any())
{
_logger?.LogDebug("No reports found");
Logger.LogDebug("No reports found");
return Task.CompletedTask;
}

foreach (var report in reports)
{
_logger?.LogDebug("Transforming report {reportKey}...", report.Key);
Logger.LogDebug("Transforming report {reportKey}...", report.Key);

var reportContents = GetReport(report);

if (string.IsNullOrEmpty(reportContents))
{
_logger?.LogDebug("Report {reportKey} is empty, ignore", report.Key);
Logger.LogDebug("Report {reportKey} is empty, ignore", report.Key);
continue;
}

var fileName = $"{report.Key}_{Name}{FileExtension}";
_logger?.LogDebug("File name for report {report}: {fileName}", report.Key, fileName);
Logger.LogDebug("File name for report {report}: {fileName}", report.Key, fileName);

if (File.Exists(fileName))
{
_logger?.LogDebug("File {fileName} already exists, appending timestamp", fileName);
Logger.LogDebug("File {fileName} already exists, appending timestamp", fileName);
fileName = $"{report.Key}_{Name}_{DateTime.Now:yyyyMMddHHmmss}{FileExtension}";
}

_logger?.LogDebug("Writing report {reportKey} to {fileName}...", report.Key, fileName);
Logger.LogDebug("Writing report {reportKey} to {fileName}...", report.Key, fileName);
File.WriteAllText(fileName, reportContents);
}

Expand Down
17 changes: 11 additions & 6 deletions dev-proxy-plugins/Reporters/JsonReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json;
using Microsoft.DevProxy.Abstractions;
using Microsoft.DevProxy.Plugins.RequestLogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Microsoft.DevProxy.Plugins.Reporters;
Expand All @@ -19,37 +20,41 @@ public class JsonReporter : BaseReporter
{ typeof(ExecutionSummaryPluginReportByMessageType), TransformExecutionSummary },
};

public JsonReporter(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
{
}

protected override string GetReport(KeyValuePair<string, object> report)
{
_logger?.LogDebug("Serializing report {reportKey}...", report.Key);
Logger.LogDebug("Serializing report {reportKey}...", report.Key);

var reportData = report.Value;
var reportType = reportData.GetType();

if (_transformers.TryGetValue(reportType, out var transform))
{
_logger?.LogDebug("Transforming {reportType} using {transform}...", reportType.Name, transform.Method.Name);
Logger.LogDebug("Transforming {reportType} using {transform}...", reportType.Name, transform.Method.Name);
reportData = transform(reportData);
}
else
{
_logger?.LogDebug("No transformer found for {reportType}", reportType.Name);
Logger.LogDebug("No transformer found for {reportType}", reportType.Name);
}

if (reportData is string strVal)
{
_logger?.LogDebug("{reportKey} is a string. Checking if it's JSON...", report.Key);
Logger.LogDebug("{reportKey} is a string. Checking if it's JSON...", report.Key);

try
{
JsonSerializer.Deserialize<object>(strVal);
_logger?.LogDebug("{reportKey} is already JSON, ignore", report.Key);
Logger.LogDebug("{reportKey} is already JSON, ignore", report.Key);
// already JSON, ignore
return strVal;
}
catch
{
_logger?.LogDebug("{reportKey} is not JSON, serializing...", report.Key);
Logger.LogDebug("{reportKey} is not JSON, serializing...", report.Key);
}
}

Expand Down
11 changes: 8 additions & 3 deletions dev-proxy-plugins/Reporters/MarkdownReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Microsoft.DevProxy.Abstractions;
using Microsoft.DevProxy.Plugins.RequestLogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Microsoft.DevProxy.Plugins.Reporters;
Expand All @@ -27,21 +28,25 @@ public class MarkdownReporter : BaseReporter
private const string _requestsInterceptedMessage = "Requests intercepted";
private const string _requestsPassedThroughMessage = "Requests passed through";

public MarkdownReporter(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
{
}

protected override string? GetReport(KeyValuePair<string, object> report)
{
_logger?.LogDebug("Transforming {report}...", report.Key);
Logger.LogDebug("Transforming {report}...", report.Key);

var reportType = report.Value.GetType();

if (_transformers.TryGetValue(reportType, out var transform))
{
_logger?.LogDebug("Transforming {reportType} using {transform}...", reportType.Name, transform.Method.Name);
Logger.LogDebug("Transforming {reportType} using {transform}...", reportType.Name, transform.Method.Name);

return transform(report.Value);
}
else
{
_logger?.LogDebug("No transformer found for {reportType}", reportType.Name);
Logger.LogDebug("No transformer found for {reportType}", reportType.Name);
return null;
}
}
Expand Down
11 changes: 8 additions & 3 deletions dev-proxy-plugins/Reporters/PlainTextReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Microsoft.DevProxy.Abstractions;
using Microsoft.DevProxy.Plugins.RequestLogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Microsoft.DevProxy.Plugins.Reporters;
Expand All @@ -27,21 +28,25 @@ public class PlainTextReporter : BaseReporter
private const string _requestsInterceptedMessage = "Requests intercepted";
private const string _requestsPassedThroughMessage = "Requests passed through";

public PlainTextReporter(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
{
}

protected override string? GetReport(KeyValuePair<string, object> report)
{
_logger?.LogDebug("Transforming {report}...", report.Key);
Logger.LogDebug("Transforming {report}...", report.Key);

var reportType = report.Value.GetType();

if (_transformers.TryGetValue(reportType, out var transform))
{
_logger?.LogDebug("Transforming {reportType} using {transform}...", reportType.Name, transform.Method);
Logger.LogDebug("Transforming {reportType} using {transform}...", reportType.Name, transform.Method);

return transform(report.Value);
}
else
{
_logger?.LogDebug("No transformer found for {reportType}", reportType.Name);
Logger.LogDebug("No transformer found for {reportType}", reportType.Name);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private async Task AfterRecordingStop(object sender, RecordingArgs e)
var methodAndUrl = methodAndUrlString.Split(' ');
if (methodAndUrl[0] == "OPTIONS")
{
_logger?.LogDebug("Skipping OPTIONS request {request}", methodAndUrl[1]);
Logger.LogDebug("Skipping OPTIONS request {request}", methodAndUrl[1]);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@ public class MinimalPermissionsGuidancePlugin : BaseReportingPlugin
{
public override string Name => nameof(MinimalPermissionsGuidancePlugin);

public override void Register(IPluginEvents pluginEvents,
public MinimalPermissionsGuidancePlugin(IPluginEvents pluginEvents, IProxyContext context, ILogger logger, ISet<UrlToWatch> urlsToWatch, IConfigurationSection? configSection = null) : base(pluginEvents, context, logger, urlsToWatch, configSection)
{
}

public override void Register()
{
base.Register();

IProxyContext context,
ISet<UrlToWatch> urlsToWatch,
IConfigurationSection? configSection = null)
{
base.Register(pluginEvents, context, urlsToWatch, configSection);

pluginEvents.AfterRecordingStop += AfterRecordingStop;
PluginEvents.AfterRecordingStop += AfterRecordingStop;
}

private async Task AfterRecordingStop(object? sender, RecordingArgs e)
Expand Down
1 change: 1 addition & 0 deletions dev-proxy-plugins/RequestLogs/MinimalPermissionsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.DevProxy.Plugins.RequestLogs;

Expand Down
Loading

0 comments on commit e7be38e

Please sign in to comment.