Skip to content

Commit

Permalink
Merge pull request #698 from SMI/fix/693-cata-request-logging
Browse files Browse the repository at this point in the history
Fixes #693 Add query log on exceptions
  • Loading branch information
rkm authored Apr 4, 2021
2 parents 93dda79 + 39e9477 commit 9281b58
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions news/698-bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CohortExtractor database queries that crash during execution are now logged
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using ReusableLibraryCode.DataAccess;
using FAnsi.Discovery;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using MapsDirectlyToDatabaseTable;
using MySqlConnector;
using NLog;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.Curation.Data.Spontaneous;
using Rdmp.Core.Repositories;
using Rdmp.Core.QueryBuilding;
using Rdmp.Core.Repositories;
using ReusableLibraryCode.DataAccess;
using System;
using System.Collections.Generic;
using System.Data.Common;

namespace Microservices.CohortExtractor.Execution.RequestFulfillers
{
public class QueryToExecute
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();

protected QueryToExecuteColumnSet Columns { get; }

/// <summary>
/// The column to search for in the WHERE logic
/// </summary>
Expand Down Expand Up @@ -125,33 +126,45 @@ public IEnumerable<QueryToExecuteResult> Execute(string valueToLookup, List<IRej
using (DbConnection con = Server.GetConnection())
{
con.Open();
DbDataReader r = Server.GetCommand(GetSqlForKeyValue(valueToLookup), con).ExecuteReader();

while (r.Read())
string sqlString = GetSqlForKeyValue(valueToLookup);

DbDataReader reader;
try
{
reader = Server.GetCommand(sqlString, con).ExecuteReader();
}
catch (MySqlException)
{
_logger.Error($"The following query resulted in an exception: {sqlString}");
throw;
}

while (reader.Read())
{
object imagePath = r[path];
object imagePath = reader[path];

if (imagePath == DBNull.Value)
continue;

bool reject = false;
string rejectReason = null;

//Ask the rejectors how good this record is
foreach (IRejector rejector in rejectors)
{
if (rejector.Reject(r, out rejectReason))
if (rejector.Reject(reader, out rejectReason))
{
reject = true;
break;
}
}
yield return
new QueryToExecuteResult((string) imagePath,
study == null ? null : (string) r[study],
series == null ? null : (string) (string) r[series],
instance == null ? null : (string) (string) r[instance],

yield return
new QueryToExecuteResult((string)imagePath,
study == null ? null : (string)reader[study],
series == null ? null : (string)(string)reader[series],
instance == null ? null : (string)(string)reader[instance],
reject,
rejectReason);
}
Expand Down

0 comments on commit 9281b58

Please sign in to comment.