Skip to content

Commit fc97f72

Browse files
authored
Develop (#74)
Bugfixes (part of 2.2)
1 parent 1f531c9 commit fc97f72

File tree

7 files changed

+71
-36
lines changed

7 files changed

+71
-36
lines changed

src/Server/Coderr.Server.SqlServer/Core/Environments/ResetEnvironmentHandler.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public ResetEnvironmentHandler(IAdoNetUnitOfWork unitOfWork)
1818

1919
public Task HandleAsync(IMessageContext context, ResetEnvironment message)
2020
{
21+
_loggr.Info("Resetting environmentId " + message.EnvironmentId + " for app " + message.ApplicationId);
2122

23+
// Start by deleting incidents that are in just our environment.
2224
var sql = @"WITH JustOurIncidents (IncidentId) AS
2325
(
2426
select ie.IncidentId
@@ -29,12 +31,29 @@ join Environments e ON (ie.EnvironmentId = e.Id)
2931
group by ie.IncidentId
3032
having count(e.Id) = 1
3133
)
34+
DELETE Incidents
35+
FROM IncidentEnvironments
36+
JOIN JustOurIncidents ON (JustOurIncidents.IncidentId = IncidentEnvironments.IncidentId)
37+
WHERE IncidentEnvironments.EnvironmentId = @environmentId";
38+
39+
_unitOfWork.ExecuteNonQuery(sql, new { message.ApplicationId, message.EnvironmentId });
40+
41+
// Next delete all environment mappings that are for the given environment.
42+
sql = @"WITH JustOurIncidents (IncidentId) AS
43+
(
44+
select ie.IncidentId
45+
from IncidentEnvironments ie
46+
join Incidents i ON (i.Id = ie.IncidentId)
47+
join Environments e ON (ie.EnvironmentId = e.Id)
48+
where i.ApplicationId = @applicationId
49+
group by ie.IncidentId
50+
)
3251
DELETE IncidentEnvironments
3352
FROM IncidentEnvironments
3453
JOIN JustOurIncidents ON (JustOurIncidents.IncidentId = IncidentEnvironments.IncidentId)
3554
WHERE IncidentEnvironments.EnvironmentId = @environmentId";
3655

37-
_unitOfWork.ExecuteNonQuery(sql, new {message.ApplicationId, message.EnvironmentId});
56+
_unitOfWork.ExecuteNonQuery(sql, new { message.ApplicationId, message.EnvironmentId });
3857
_loggr.Info("Resetting environmentId " + message.EnvironmentId + " for app " + message.ApplicationId);
3958
return Task.CompletedTask;
4059
}

src/Server/Coderr.Server.SqlServer/Core/Incidents/Queries/FindIncidentsHandler.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,27 @@ namespace Coderr.Server.SqlServer.Core.Incidents.Queries
1717
public class FindIncidentsHandler : IQueryHandler<FindIncidents, FindIncidentsResult>
1818
{
1919
private readonly IAdoNetUnitOfWork _uow;
20+
private string _where = "";
21+
private string _joins = "";
2022

2123
public FindIncidentsHandler(IAdoNetUnitOfWork uow)
2224
{
2325
_uow = uow;
2426
}
2527

28+
private void AppendWhere(string constraint)
29+
{
30+
if (_where == "")
31+
_where = " WHERE " + constraint + "\r\n";
32+
else
33+
_where += " AND " + constraint + "\r\n";
34+
}
35+
36+
private void AppendJoin(string clause)
37+
{
38+
_joins += clause + "\r\n";
39+
}
40+
2641
public async Task<FindIncidentsResult> HandleAsync(IMessageContext context, FindIncidents query)
2742
{
2843
using (var cmd = (DbCommand)_uow.CreateCommand())
@@ -31,17 +46,15 @@ public async Task<FindIncidentsResult> HandleAsync(IMessageContext context, Find
3146
FROM Incidents
3247
JOIN Applications ON (Applications.Id = Incidents.ApplicationId)";
3348

34-
var startWord = " WHERE ";
3549
if (!string.IsNullOrEmpty(query.Version))
3650
{
3751
var versionId =
3852
_uow.ExecuteScalar("SELECT Id FROM ApplicationVersions WHERE Version = @version",
3953
new { version = query.Version });
4054

41-
sqlQuery += " JOIN IncidentVersions ON (Incidents.Id = IncidentVersions.IncidentId)" +
42-
" WHERE IncidentVersions.VersionId = @versionId";
55+
AppendJoin("JOIN IncidentVersions ON (Incidents.Id = IncidentVersions.IncidentId)");
56+
AppendWhere("IncidentVersions.VersionId = @versionId");
4357
cmd.AddParameter("versionId", versionId);
44-
startWord = " AND ";
4558
}
4659
if (query.Tags != null && query.Tags.Length > 0)
4760
{
@@ -52,23 +65,22 @@ WHERE TagName IN ({0})
5265
AND IncidentTags.IncidentId=Incidents.Id
5366
GROUP BY IncidentId
5467
HAVING Count(IncidentTags.Id) = {1}
55-
))
56-
";
68+
))";
5769
var ps = "";
5870
for (int i = 0; i < query.Tags.Length; i++)
5971
{
6072
ps += $"@tag{i}, ";
6173
cmd.AddParameter($"@tag{i}", query.Tags[i]);
6274
}
6375

64-
sqlQuery += string.Format(ourSql, ps.Remove(ps.Length - 2, 2), query.Tags.Length);
76+
var sql = string.Format(ourSql, ps.Remove(ps.Length - 2, 2), query.Tags.Length);
77+
AppendJoin(sql);
6578
}
6679

6780
if (query.EnvironmentIds != null && query.EnvironmentIds.Length > 0)
6881
{
69-
sqlQuery += " JOIN IncidentEnvironments ON (Incidents.Id = IncidentEnvironments.IncidentId)" +
70-
$" WHERE IncidentEnvironments.EnvironmentId IN ({string.Join(", ", query.EnvironmentIds)})";
71-
startWord = " AND ";
82+
AppendJoin("JOIN IncidentEnvironments ON (Incidents.Id = IncidentEnvironments.IncidentId)");
83+
AppendWhere($"IncidentEnvironments.EnvironmentId IN ({string.Join(", ", query.EnvironmentIds)})");
7284
}
7385

7486
if (!string.IsNullOrEmpty(query.ContextCollectionPropertyValue)
@@ -79,7 +91,7 @@ HAVING Count(IncidentTags.Id) = {1}
7991
where += AddContextProperty(cmd, where, "PropertyName", "ContextPropertyName", query.ContextCollectionPropertyName);
8092
where += AddContextProperty(cmd, where, "Value", "ContextPropertyValue", query.ContextCollectionPropertyValue);
8193
if (where.EndsWith(" AND "))
82-
where = where.Remove(where.Length - 5, 5);
94+
where = where.Substring(0, where.Length - 5);
8395
var ourSql =
8496
$@"with ContextSearch (IncidentId)
8597
as (
@@ -89,7 +101,8 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection
89101
WHERE {where}
90102
)
91103
";
92-
sqlQuery = ourSql + sqlQuery + " join ContextSearch ON (Incidents.Id = ContextSearch.IncidentId)\r\n";
104+
sqlQuery = ourSql + sqlQuery;
105+
AppendJoin("join ContextSearch ON (Incidents.Id = ContextSearch.IncidentId)");
93106
}
94107

95108
if (query.ApplicationIds != null && query.ApplicationIds.Length > 0)
@@ -104,7 +117,7 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection
104117
}
105118

106119
var ids = string.Join(",", query.ApplicationIds);
107-
sqlQuery += $" {startWord} Applications.Id IN ({ids})";
120+
AppendWhere($"Applications.Id IN ({ids})");
108121
}
109122
else if (!context.Principal.IsSysAdmin())
110123
{
@@ -117,12 +130,12 @@ join ErrorReportCollectionProperties ON (ErrorReports.Id = ErrorReportCollection
117130
return new FindIncidentsResult { Items = new FindIncidentsResultItem[0] };
118131
}
119132

120-
sqlQuery += $" {startWord} Applications.Id IN({string.Join(",", appIds)})";
133+
AppendWhere($"Applications.Id IN({string.Join(",", appIds)})");
121134
}
122135

123136
if (!string.IsNullOrWhiteSpace(query.FreeText))
124137
{
125-
sqlQuery += @" AND (
138+
AppendWhere(@"(
126139
Incidents.Id IN
127140
(
128141
SELECT Distinct IncidentId
@@ -131,50 +144,51 @@ WHERE StackTrace LIKE @FreeText
131144
OR ErrorReports.Title LIKE @FreeText
132145
OR ErrorReports.ErrorId LIKE @FreeText
133146
OR Incidents.Description LIKE @FreeText)
134-
)";
147+
)");
135148
cmd.AddParameter("FreeText", $"%{query.FreeText}%");
136149
}
137150

138151

139152

140-
sqlQuery += " AND (";
153+
_where += " AND (";
141154
if (query.IsIgnored)
142-
sqlQuery += $"State = {(int)IncidentState.Ignored} OR ";
155+
_where += $"State = {(int)IncidentState.Ignored} OR ";
143156
if (query.IsNew)
144-
sqlQuery += $"State = {(int)IncidentState.New} OR ";
157+
_where += $"State = {(int)IncidentState.New} OR ";
145158
if (query.IsClosed)
146-
sqlQuery += $"State = {(int)IncidentState.Closed} OR ";
159+
_where += $"State = {(int)IncidentState.Closed} OR ";
147160
if (query.IsAssigned)
148-
sqlQuery += $"State = {(int)IncidentState.Active} OR ";
161+
_where += $"State = {(int)IncidentState.Active} OR ";
149162
if (query.ReOpened)
150-
sqlQuery += "IsReOpened = 1 OR ";
163+
_where += "IsReOpened = 1 OR ";
151164

152165

153-
if (sqlQuery.EndsWith("OR "))
154-
sqlQuery = sqlQuery.Remove(sqlQuery.Length - 4) + ") ";
166+
if (_where.EndsWith("OR "))
167+
_where = _where.Remove(_where.Length - 4) + ") ";
155168
else
156-
sqlQuery = sqlQuery.Remove(sqlQuery.Length - 5);
169+
_where = _where.Remove(_where.Length - 5);
157170

158171
if (query.MinDate > DateTime.MinValue)
159172
{
160-
sqlQuery += " AND Incidents.LastReportAtUtc >= @minDate";
173+
AppendWhere("Incidents.LastReportAtUtc >= @minDate");
161174
cmd.AddParameter("minDate", query.MinDate);
162175
}
163176
if (query.MaxDate < DateTime.MaxValue)
164177
{
165-
sqlQuery += " AND Incidents.LastReportAtUtc <= @maxDate";
178+
AppendWhere("Incidents.LastReportAtUtc <= @maxDate");
166179
cmd.AddParameter("maxDate", query.MaxDate);
167180
}
168181

169182
if (query.AssignedToId > 0)
170183
{
171-
sqlQuery += "AND AssignedToId = @assignedTo";
184+
AppendWhere("AssignedToId = @assignedTo");
172185
cmd.AddParameter("assignedTo", query.AssignedToId);
173186
}
174187

175188

176189

177190
//count first;
191+
sqlQuery += _joins + _where;
178192
cmd.CommandText = string.Format(sqlQuery, "count(Incidents.Id)");
179193
var count = await cmd.ExecuteScalarAsync();
180194

src/Server/Coderr.Server.Web/Coderr.Server.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
77
<IsPackable>false</IsPackable>
88
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
9-
<Version>2.1.1</Version>
9+
<Version>2.2</Version>
1010
<UserSecretsId>18bfcacd-1b5d-435b-bc30-febb568a29b2</UserSecretsId>
1111
</PropertyGroup>
1212

src/Server/Coderr.Server.Web/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private void UpgradeDatabaseSchema()
148148

149149
public void ConfigureServices(IServiceCollection services)
150150
{
151-
if (Configuration["EnableCors"].Equals("true", StringComparison.OrdinalIgnoreCase))
151+
if (Configuration["EnableCors"]?.Equals("true", StringComparison.OrdinalIgnoreCase) == true)
152152
{
153153
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
154154
{

src/Server/Coderr.Server.Web/appsettings.Publish.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"Installation": {
3-
"IsConfigured": false,
4-
"Password": "changeThis"
5-
} ,
3+
"IsConfigured": false,
4+
"Password": "changeThis"
5+
},
6+
"EnableCors": true,
67
"ConnectionStrings": {
78
"Db": "Data Source=.;Initial Catalog=Coderr;Integrated Security=True;Connect Timeout=15;"
89
},

src/Server/Coderr.Server.Web/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"IsConfigured": true,
44
"Password": "changeThis"
55
} ,
6-
"EnableCors": true,
6+
"EnableCors": true,
77
"ConnectionStrings": {
88
"Db": "Data Source=.;Initial Catalog=Coderr99;Integrated Security=True;Connect Timeout=15;"
99
},

src/Server/Coderr.Server.Web/npm-shrinkwrap.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)