diff --git a/src/Server/Coderr.Server.SqlServer/Core/Environments/ResetEnvironmentHandler.cs b/src/Server/Coderr.Server.SqlServer/Core/Environments/ResetEnvironmentHandler.cs index ebb9010a..23a55623 100644 --- a/src/Server/Coderr.Server.SqlServer/Core/Environments/ResetEnvironmentHandler.cs +++ b/src/Server/Coderr.Server.SqlServer/Core/Environments/ResetEnvironmentHandler.cs @@ -18,7 +18,9 @@ public ResetEnvironmentHandler(IAdoNetUnitOfWork unitOfWork) public Task HandleAsync(IMessageContext context, ResetEnvironment message) { + _loggr.Info("Resetting environmentId " + message.EnvironmentId + " for app " + message.ApplicationId); + // Start by deleting incidents that are in just our environment. var sql = @"WITH JustOurIncidents (IncidentId) AS ( select ie.IncidentId @@ -29,12 +31,29 @@ join Environments e ON (ie.EnvironmentId = e.Id) group by ie.IncidentId having count(e.Id) = 1 ) + DELETE Incidents + FROM IncidentEnvironments + JOIN JustOurIncidents ON (JustOurIncidents.IncidentId = IncidentEnvironments.IncidentId) + WHERE IncidentEnvironments.EnvironmentId = @environmentId"; + + _unitOfWork.ExecuteNonQuery(sql, new { message.ApplicationId, message.EnvironmentId }); + + // Next delete all environment mappings that are for the given environment. + sql = @"WITH JustOurIncidents (IncidentId) AS + ( + select ie.IncidentId + from IncidentEnvironments ie + join Incidents i ON (i.Id = ie.IncidentId) + join Environments e ON (ie.EnvironmentId = e.Id) + where i.ApplicationId = @applicationId + group by ie.IncidentId + ) DELETE IncidentEnvironments FROM IncidentEnvironments JOIN JustOurIncidents ON (JustOurIncidents.IncidentId = IncidentEnvironments.IncidentId) WHERE IncidentEnvironments.EnvironmentId = @environmentId"; - _unitOfWork.ExecuteNonQuery(sql, new {message.ApplicationId, message.EnvironmentId}); + _unitOfWork.ExecuteNonQuery(sql, new { message.ApplicationId, message.EnvironmentId }); _loggr.Info("Resetting environmentId " + message.EnvironmentId + " for app " + message.ApplicationId); return Task.CompletedTask; } diff --git a/src/Server/Coderr.Server.SqlServer/Core/Incidents/Queries/FindIncidentsHandler.cs b/src/Server/Coderr.Server.SqlServer/Core/Incidents/Queries/FindIncidentsHandler.cs index b3a7ed07..3fb58a10 100644 --- a/src/Server/Coderr.Server.SqlServer/Core/Incidents/Queries/FindIncidentsHandler.cs +++ b/src/Server/Coderr.Server.SqlServer/Core/Incidents/Queries/FindIncidentsHandler.cs @@ -17,12 +17,27 @@ namespace Coderr.Server.SqlServer.Core.Incidents.Queries public class FindIncidentsHandler : IQueryHandler { private readonly IAdoNetUnitOfWork _uow; + private string _where = ""; + private string _joins = ""; public FindIncidentsHandler(IAdoNetUnitOfWork uow) { _uow = uow; } + private void AppendWhere(string constraint) + { + if (_where == "") + _where = " WHERE " + constraint + "\r\n"; + else + _where += " AND " + constraint + "\r\n"; + } + + private void AppendJoin(string clause) + { + _joins += clause + "\r\n"; + } + public async Task HandleAsync(IMessageContext context, FindIncidents query) { using (var cmd = (DbCommand)_uow.CreateCommand()) @@ -31,17 +46,15 @@ public async Task HandleAsync(IMessageContext context, Find FROM Incidents JOIN Applications ON (Applications.Id = Incidents.ApplicationId)"; - var startWord = " WHERE "; if (!string.IsNullOrEmpty(query.Version)) { var versionId = _uow.ExecuteScalar("SELECT Id FROM ApplicationVersions WHERE Version = @version", new { version = query.Version }); - sqlQuery += " JOIN IncidentVersions ON (Incidents.Id = IncidentVersions.IncidentId)" + - " WHERE IncidentVersions.VersionId = @versionId"; + AppendJoin("JOIN IncidentVersions ON (Incidents.Id = IncidentVersions.IncidentId)"); + AppendWhere("IncidentVersions.VersionId = @versionId"); cmd.AddParameter("versionId", versionId); - startWord = " AND "; } if (query.Tags != null && query.Tags.Length > 0) { @@ -52,8 +65,7 @@ WHERE TagName IN ({0}) AND IncidentTags.IncidentId=Incidents.Id GROUP BY IncidentId HAVING Count(IncidentTags.Id) = {1} - )) -"; + ))"; var ps = ""; for (int i = 0; i < query.Tags.Length; i++) { @@ -61,14 +73,14 @@ HAVING Count(IncidentTags.Id) = {1} cmd.AddParameter($"@tag{i}", query.Tags[i]); } - sqlQuery += string.Format(ourSql, ps.Remove(ps.Length - 2, 2), query.Tags.Length); + var sql = string.Format(ourSql, ps.Remove(ps.Length - 2, 2), query.Tags.Length); + AppendJoin(sql); } if (query.EnvironmentIds != null && query.EnvironmentIds.Length > 0) { - sqlQuery += " JOIN IncidentEnvironments ON (Incidents.Id = IncidentEnvironments.IncidentId)" + - $" WHERE IncidentEnvironments.EnvironmentId IN ({string.Join(", ", query.EnvironmentIds)})"; - startWord = " AND "; + AppendJoin("JOIN IncidentEnvironments ON (Incidents.Id = IncidentEnvironments.IncidentId)"); + AppendWhere($"IncidentEnvironments.EnvironmentId IN ({string.Join(", ", query.EnvironmentIds)})"); } if (!string.IsNullOrEmpty(query.ContextCollectionPropertyValue) @@ -79,7 +91,7 @@ HAVING Count(IncidentTags.Id) = {1} where += AddContextProperty(cmd, where, "PropertyName", "ContextPropertyName", query.ContextCollectionPropertyName); where += AddContextProperty(cmd, where, "Value", "ContextPropertyValue", query.ContextCollectionPropertyValue); if (where.EndsWith(" AND ")) - where = where.Remove(where.Length - 5, 5); + where = where.Substring(0, where.Length - 5); var ourSql = $@"with ContextSearch (IncidentId) as ( @@ -89,7 +101,8 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection WHERE {where} ) "; - sqlQuery = ourSql + sqlQuery + " join ContextSearch ON (Incidents.Id = ContextSearch.IncidentId)\r\n"; + sqlQuery = ourSql + sqlQuery; + AppendJoin("join ContextSearch ON (Incidents.Id = ContextSearch.IncidentId)"); } if (query.ApplicationIds != null && query.ApplicationIds.Length > 0) @@ -104,7 +117,7 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection } var ids = string.Join(",", query.ApplicationIds); - sqlQuery += $" {startWord} Applications.Id IN ({ids})"; + AppendWhere($"Applications.Id IN ({ids})"); } else if (!context.Principal.IsSysAdmin()) { @@ -117,12 +130,12 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection return new FindIncidentsResult { Items = new FindIncidentsResultItem[0] }; } - sqlQuery += $" {startWord} Applications.Id IN({string.Join(",", appIds)})"; + AppendWhere($"Applications.Id IN({string.Join(",", appIds)})"); } if (!string.IsNullOrWhiteSpace(query.FreeText)) { - sqlQuery += @" AND ( + AppendWhere(@"( Incidents.Id IN ( SELECT Distinct IncidentId @@ -131,50 +144,51 @@ WHERE StackTrace LIKE @FreeText OR ErrorReports.Title LIKE @FreeText OR ErrorReports.ErrorId LIKE @FreeText OR Incidents.Description LIKE @FreeText) - )"; + )"); cmd.AddParameter("FreeText", $"%{query.FreeText}%"); } - sqlQuery += " AND ("; + _where += " AND ("; if (query.IsIgnored) - sqlQuery += $"State = {(int)IncidentState.Ignored} OR "; + _where += $"State = {(int)IncidentState.Ignored} OR "; if (query.IsNew) - sqlQuery += $"State = {(int)IncidentState.New} OR "; + _where += $"State = {(int)IncidentState.New} OR "; if (query.IsClosed) - sqlQuery += $"State = {(int)IncidentState.Closed} OR "; + _where += $"State = {(int)IncidentState.Closed} OR "; if (query.IsAssigned) - sqlQuery += $"State = {(int)IncidentState.Active} OR "; + _where += $"State = {(int)IncidentState.Active} OR "; if (query.ReOpened) - sqlQuery += "IsReOpened = 1 OR "; + _where += "IsReOpened = 1 OR "; - if (sqlQuery.EndsWith("OR ")) - sqlQuery = sqlQuery.Remove(sqlQuery.Length - 4) + ") "; + if (_where.EndsWith("OR ")) + _where = _where.Remove(_where.Length - 4) + ") "; else - sqlQuery = sqlQuery.Remove(sqlQuery.Length - 5); + _where = _where.Remove(_where.Length - 5); if (query.MinDate > DateTime.MinValue) { - sqlQuery += " AND Incidents.LastReportAtUtc >= @minDate"; + AppendWhere("Incidents.LastReportAtUtc >= @minDate"); cmd.AddParameter("minDate", query.MinDate); } if (query.MaxDate < DateTime.MaxValue) { - sqlQuery += " AND Incidents.LastReportAtUtc <= @maxDate"; + AppendWhere("Incidents.LastReportAtUtc <= @maxDate"); cmd.AddParameter("maxDate", query.MaxDate); } if (query.AssignedToId > 0) { - sqlQuery += "AND AssignedToId = @assignedTo"; + AppendWhere("AssignedToId = @assignedTo"); cmd.AddParameter("assignedTo", query.AssignedToId); } //count first; + sqlQuery += _joins + _where; cmd.CommandText = string.Format(sqlQuery, "count(Incidents.Id)"); var count = await cmd.ExecuteScalarAsync(); diff --git a/src/Server/Coderr.Server.Web/Coderr.Server.Web.csproj b/src/Server/Coderr.Server.Web/Coderr.Server.Web.csproj index 14c48bc4..e738e8b8 100644 --- a/src/Server/Coderr.Server.Web/Coderr.Server.Web.csproj +++ b/src/Server/Coderr.Server.Web/Coderr.Server.Web.csproj @@ -6,7 +6,7 @@ Latest false true - 2.1.1 + 2.2 18bfcacd-1b5d-435b-bc30-febb568a29b2 diff --git a/src/Server/Coderr.Server.Web/Startup.cs b/src/Server/Coderr.Server.Web/Startup.cs index 8e28e839..cb464196 100644 --- a/src/Server/Coderr.Server.Web/Startup.cs +++ b/src/Server/Coderr.Server.Web/Startup.cs @@ -148,7 +148,7 @@ private void UpgradeDatabaseSchema() public void ConfigureServices(IServiceCollection services) { - if (Configuration["EnableCors"].Equals("true", StringComparison.OrdinalIgnoreCase)) + if (Configuration["EnableCors"]?.Equals("true", StringComparison.OrdinalIgnoreCase) == true) { services.AddCors(o => o.AddPolicy("CorsPolicy", builder => { diff --git a/src/Server/Coderr.Server.Web/appsettings.Publish.json b/src/Server/Coderr.Server.Web/appsettings.Publish.json index f7101d00..4bf785f9 100644 --- a/src/Server/Coderr.Server.Web/appsettings.Publish.json +++ b/src/Server/Coderr.Server.Web/appsettings.Publish.json @@ -1,8 +1,9 @@ { "Installation": { - "IsConfigured": false, - "Password": "changeThis" - } , + "IsConfigured": false, + "Password": "changeThis" + }, + "EnableCors": true, "ConnectionStrings": { "Db": "Data Source=.;Initial Catalog=Coderr;Integrated Security=True;Connect Timeout=15;" }, diff --git a/src/Server/Coderr.Server.Web/appsettings.json b/src/Server/Coderr.Server.Web/appsettings.json index 468b7483..006fbb3d 100644 --- a/src/Server/Coderr.Server.Web/appsettings.json +++ b/src/Server/Coderr.Server.Web/appsettings.json @@ -3,7 +3,7 @@ "IsConfigured": true, "Password": "changeThis" } , - "EnableCors": true, + "EnableCors": true, "ConnectionStrings": { "Db": "Data Source=.;Initial Catalog=Coderr99;Integrated Security=True;Connect Timeout=15;" }, diff --git a/src/Server/Coderr.Server.Web/npm-shrinkwrap.json b/src/Server/Coderr.Server.Web/npm-shrinkwrap.json index 3bab0359..9c62032d 100644 --- a/src/Server/Coderr.Server.Web/npm-shrinkwrap.json +++ b/src/Server/Coderr.Server.Web/npm-shrinkwrap.json @@ -5867,7 +5867,8 @@ }, "lodash": { "version": "4.17.11", - "resolved": "", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==", "dev": true } }