Skip to content

Commit

Permalink
Added debug information to query results
Browse files Browse the repository at this point in the history
  • Loading branch information
ustims committed Sep 22, 2023
1 parent 0134e49 commit 74be9b4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ "main" ]

env:
PACKAGE_VERSION: 0.2.0
PACKAGE_VERSION: 0.2.1

jobs:

Expand Down Expand Up @@ -45,4 +45,4 @@ jobs:
# Comma-separated list of files to upload
files: # optional
# Directory to search for coverage reports.
directory: ./artifacts
directory: ./artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ protected override ProjectionRepositoryFactory GetProjectionRepositoryFactory()
{
_projectionRepositoryFactory = new PostgresqlProjectionRepositoryFactory(
NullLoggerFactory.Instance,
TestsConnectionStrings.CONNECTION_STRING
TestsConnectionStrings.CONNECTION_STRING,
includeDebugInformation: true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ protected override ProjectionRepositoryFactory GetProjectionRepositoryFactory()
{
_projectionRepositoryFactory = new PostgresqlProjectionRepositoryFactory(
NullLoggerFactory.Instance,
TestsConnectionStrings.CONNECTION_STRING
TestsConnectionStrings.CONNECTION_STRING,
includeDebugInformation: true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ protected override ProjectionRepositoryFactory GetProjectionRepositoryFactory()
{
_projectionRepositoryFactory = new PostgresqlProjectionRepositoryFactory(
NullLoggerFactory.Instance,
TestsConnectionStrings.CONNECTION_STRING
TestsConnectionStrings.CONNECTION_STRING,
includeDebugInformation: true
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ public class QueryChunk
public class PostgresqlProjectionRepository<TProjectionDocument> : PostgresqlProjectionRepository, IProjectionRepository<TProjectionDocument>
where TProjectionDocument : ProjectionDocument
{
public PostgresqlProjectionRepository(string connectionString, ILoggerFactory loggerFactory)
: base(connectionString, ProjectionDocumentSchemaFactory.FromTypeWithAttributes<TProjectionDocument>(), loggerFactory)
public PostgresqlProjectionRepository(
string connectionString,
ILoggerFactory loggerFactory,
bool includeDebugInformation = false)
: base(connectionString, ProjectionDocumentSchemaFactory.FromTypeWithAttributes<TProjectionDocument>(), loggerFactory, includeDebugInformation)
{
}

Expand Down Expand Up @@ -98,6 +101,7 @@ public Task Upsert(

return new ProjectionQueryResult<TProjectionDocument>
{
DebugInformation = recordsDictionary.DebugInformation,
IndexName = recordsDictionary.IndexName,
TotalRecordsFound = recordsDictionary.TotalRecordsFound,
Records = records
Expand All @@ -107,6 +111,8 @@ public Task Upsert(

public class PostgresqlProjectionRepository : ProjectionRepository
{
private bool _includeDebugInformation = false;

private readonly string _connectionString;

private string? _keyPropertyName;
Expand All @@ -115,10 +121,12 @@ public class PostgresqlProjectionRepository : ProjectionRepository
public PostgresqlProjectionRepository(
string connectionString,
ProjectionDocumentSchema projectionDocumentSchema,
ILoggerFactory loggerFactory
ILoggerFactory loggerFactory,
bool includeDebugInformation = false
) : base(projectionDocumentSchema, loggerFactory.CreateLogger<ProjectionRepository>())
{
_connectionString = connectionString;
_includeDebugInformation = includeDebugInformation;

// for dynamic projection document schemas we need to ensure 'partitionKey' column is always there
if (ProjectionDocumentSchema.Properties.All(p => p.PropertyName != "PartitionKey"))
Expand Down Expand Up @@ -597,16 +605,44 @@ protected override async Task UpsertInternal(

records.Add(document);
}


var debugInformation = "";

if (_includeDebugInformation)
{
debugInformation += cmd.CommandText;

foreach (NpgsqlParameter param in cmd.Parameters)
{
var paramValue = "";

switch (param.NpgsqlDbType)
{
case NpgsqlDbType.Text:
paramValue = $"'{param.NpgsqlValue}'";
break;
default:
paramValue = param.NpgsqlValue.ToString();
break;
}

debugInformation = debugInformation.Replace($"@{param.ParameterName}", paramValue);
}

debugInformation += $"\n\nOriginal command:\n{cmd.CommandText}; " +
$"{string.Join(',', cmd.Parameters.Select(p => $"@{p.ParameterName}:{p.NpgsqlDbType}={p.NpgsqlValue}"))}";
}

await reader.DisposeAsync();
// clear previous command in order to prevent conflicts
cmd.Parameters.Clear();
cmd.Dispose();

return new ProjectionQueryResult<Dictionary<string, object?>>
{
DebugInformation = _includeDebugInformation ? debugInformation : String.Empty,
IndexName = TableName,
TotalRecordsFound = totalCount.Value,
TotalRecordsFound = totalCount,
Records = records.Select(x =>
new QueryResultDocument<Dictionary<string, object?>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace CloudFabric.Projections.Postgresql;
public class PostgresqlProjectionRepositoryFactory: ProjectionRepositoryFactory
{
private readonly string _projectionsConnectionString;
private readonly bool _includeDebugInformation;
private readonly string? _sourceEventStoreConnectionId;

/// <summary>
Expand All @@ -23,11 +24,13 @@ public class PostgresqlProjectionRepositoryFactory: ProjectionRepositoryFactory
public PostgresqlProjectionRepositoryFactory(
ILoggerFactory loggerFactory,
string projectionsConnectionString,
string? sourceEventStoreConnectionId = null
string? sourceEventStoreConnectionId = null,
bool includeDebugInformation = false
): base(loggerFactory)
{
_projectionsConnectionString = projectionsConnectionString;
_sourceEventStoreConnectionId = sourceEventStoreConnectionId;
_includeDebugInformation = includeDebugInformation;
}

public override IProjectionRepository<TProjectionDocument> GetProjectionRepository<TProjectionDocument>()
Expand All @@ -39,7 +42,7 @@ public override IProjectionRepository<TProjectionDocument> GetProjectionReposito
}

var repository = new PostgresqlProjectionRepository<TProjectionDocument>(
_projectionsConnectionString, _loggerFactory
_projectionsConnectionString, _loggerFactory, _includeDebugInformation
);
SetToCache<TProjectionDocument>(repository);
return repository;
Expand All @@ -56,7 +59,8 @@ public override ProjectionRepository GetProjectionRepository(ProjectionDocumentS
var repository = new PostgresqlProjectionRepository(
_projectionsConnectionString,
projectionDocumentSchema,
_loggerFactory
_loggerFactory,
_includeDebugInformation
);
SetToCache(projectionDocumentSchema, repository);
return repository;
Expand Down

0 comments on commit 74be9b4

Please sign in to comment.