Skip to content

Commit c3566c9

Browse files
committed
Will now store one new report per day once the report limit per incident is reached. In that way we get fresh data per incident without putting preasure on the system
1 parent 6a40e04 commit c3566c9

File tree

13 files changed

+62
-59
lines changed

13 files changed

+62
-59
lines changed

src/Server/Coderr.Server.App/Core/Reports/Config/ReportConfig.cs renamed to src/Server/Coderr.Server.Abstractions/Reports/ReportConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Collections.Generic;
22
using Coderr.Server.Abstractions.Config;
33

4-
namespace Coderr.Server.App.Core.Reports.Config
4+
namespace Coderr.Server.Abstractions.Reports
55
{
66
/// <summary>
77
/// Configuration settings for reports.

src/Server/Coderr.Server.App/Coderr.Server.App.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
<ItemGroup>
4141
<Folder Include="Core\Environments\" />
4242
<Folder Include="Core\Incidents\Events\" />
43+
<Folder Include="Core\Reports\Config\" />
4344
</ItemGroup>
4445
</Project>

src/Server/Coderr.Server.App/Core/Incidents/Jobs/DeleteEmptyIncidents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Threading.Tasks;
55
using Coderr.Server.Abstractions.Boot;
66
using Coderr.Server.Abstractions.Config;
7-
using Coderr.Server.App.Core.Reports.Config;
7+
using Coderr.Server.Abstractions.Reports;
88
using Griffin.ApplicationServices;
99
using Griffin.Data;
1010
using Griffin.Data.Mapper;

src/Server/Coderr.Server.App/Core/Reports/Jobs/DeleteReportsBelowReportLimit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Data;
22
using Coderr.Server.Abstractions.Boot;
33
using Coderr.Server.Abstractions.Config;
4-
using Coderr.Server.App.Core.Reports.Config;
4+
using Coderr.Server.Abstractions.Reports;
55
using Griffin.ApplicationServices;
66
using Griffin.Data;
77
using log4net;

src/Server/Coderr.Server.ReportAnalyzer/Inbound/Handlers/Reports/ReportAnalyzer.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using Coderr.Server.Abstractions.Boot;
6+
using Coderr.Server.Abstractions.Config;
7+
using Coderr.Server.Abstractions.Reports;
68
using Coderr.Server.Domain.Core.ErrorReports;
79
using Coderr.Server.Domain.Core.Incidents.Events;
810
using Coderr.Server.ReportAnalyzer.Abstractions;
@@ -27,7 +29,8 @@ public class ReportAnalyzer : IReportAnalyzer
2729
private readonly IHashCodeGenerator _hashCodeGenerator;
2830
private readonly ILog _logger = LogManager.GetLogger(typeof(ReportAnalyzer));
2931
private readonly IAnalyticsRepository _repository;
30-
private IDomainQueue _domainQueue;
32+
private readonly IDomainQueue _domainQueue;
33+
3134
/// <summary>
3235
/// Creates a new instance of <see cref="ReportAnalyzer" />.
3336
/// </summary>
@@ -77,7 +80,7 @@ public async Task Analyze(IMessageContext context, ErrorReportEntity report)
7780
var reportJson = JsonConvert.SerializeObject(report);
7881
if (reportJson.Length > 1000000)
7982
reportJson = reportJson.Substring(0, 100000) + "[....]";
80-
_logger.Fatal("Failed to init report " + reportJson, ex);
83+
_logger.Fatal($"Failed to init report {reportJson}", ex);
8184
return;
8285
}
8386

@@ -133,20 +136,28 @@ public async Task Analyze(IMessageContext context, ErrorReportEntity report)
133136
}
134137

135138
incident.AddReport(report);
136-
_repository.UpdateIncident(incident);
137-
if (incident.ReportCount > 25)
139+
140+
// Let's continue to receive reports once a day when
141+
// limit is reached (to get more fresh data, and still not load the system unnecessary).
142+
var timesSinceLastReport = DateTime.UtcNow.Subtract(incident.LastStoredReportUtc);
143+
if (incident.ReportCount > 25
144+
&& timesSinceLastReport < TimeSpan.FromDays(1))
138145
{
139-
_logger.Debug("Report count is more than 25. Storing only report stats for incident " + incident.Id);
146+
_repository.UpdateIncident(incident);
147+
_logger.Debug($"Report count is more than 25. Ignoring report for incident {incident.Id}.");
140148
return;
141149
}
150+
151+
incident.LastStoredReportUtc = DateTime.UtcNow;
152+
_repository.UpdateIncident(incident);
142153
}
143154

144155
if (!string.IsNullOrWhiteSpace(report.EnvironmentName))
145156
_repository.SaveEnvironmentName(incident.Id, report.EnvironmentName);
146157

147158
report.IncidentId = incident.Id;
148159
_repository.CreateReport(report);
149-
_logger.Debug("saving report " + report.Id + " for incident " + incident.Id);
160+
_logger.Debug($"saving report {report.Id} for incident {incident.Id}");
150161

151162
await _repository.StoreReportStats(new ReportMapping()
152163
{
@@ -177,7 +188,7 @@ await _repository.StoreReportStats(new ReportMapping()
177188
await context.SendAsync(new ProcessInboundContextCollections());
178189

179190
if (sw.ElapsedMilliseconds > 200)
180-
_logger.Debug("PublishAsync took " + sw.ElapsedMilliseconds);
191+
_logger.Debug($"PublishAsync took {sw.ElapsedMilliseconds}");
181192
sw.Stop();
182193
}
183194

src/Server/Coderr.Server.ReportAnalyzer/Incidents/IncidentBeingAnalyzed.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,16 @@ public string Description
138138
/// </summary>
139139
public bool IsReOpened { get; set; }
140140

141+
/// <summary>
142+
/// When we received a report (just to keep track of how fresh this incident is).
143+
/// </summary>
141144
public DateTime LastReportAtUtc { get; set; }
142145

146+
/// <summary>
147+
/// When we received a report that we stored.
148+
/// </summary>
149+
public DateTime LastStoredReportUtc { get; set; }
150+
143151
/// <summary>
144152
/// Set if incident was closed and a solution was written
145153
/// </summary>

src/Server/Coderr.Server.ReportAnalyzer/ReportSpikes/Handlers/CheckForReportPeak.cs renamed to src/Server/Coderr.Server.ReportAnalyzer/ReportSpikes/Handlers/CheckForReportSpike.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ namespace Coderr.Server.ReportAnalyzer.ReportSpikes.Handlers
2222
{
2323
/// <summary>
2424
/// </summary>
25-
public class CheckForReportPeak : IMessageHandler<ReportAddedToIncident>
25+
public class CheckForReportSpike : IMessageHandler<ReportAddedToIncident>
2626
{
2727
private readonly IUserNotificationsRepository _repository;
2828
private readonly IReportSpikeRepository _spikeRepository;
2929
private readonly BaseConfiguration _baseConfiguration;
3030
private readonly INotificationService _notificationService;
3131

3232
/// <summary>
33-
/// Creates a new instance of <see cref="CheckForReportPeak" />.
33+
/// Creates a new instance of <see cref="CheckForReportSpike" />.
3434
/// </summary>
3535
/// <param name="repository">To check if spikes should be analyzed</param>
3636
/// <param name="spikeRepository">store/fetch information of current spikes.</param>
3737
/// <param name="baseConfiguration"></param>
38-
public CheckForReportPeak(IUserNotificationsRepository repository, IReportSpikeRepository spikeRepository, IConfiguration<BaseConfiguration> baseConfiguration, INotificationService notificationService)
38+
public CheckForReportSpike(IUserNotificationsRepository repository, IReportSpikeRepository spikeRepository, IConfiguration<BaseConfiguration> baseConfiguration, INotificationService notificationService)
3939
{
4040
_repository = repository;
4141
_spikeRepository = spikeRepository;
@@ -138,7 +138,7 @@ private EmailMessage BuildEmail(ReportAddedToIncident e, UserNotificationSetting
138138
{
139139
Subject = $"Spike detected for {e.Incident.ApplicationName} ({countToday} reports)",
140140
HtmlBody =
141-
$"We've detected a spike in incoming reports for application <a href=\"{_baseConfiguration.BaseUrl}/discover/{e.Incident.ApplicationId}/\">{e.Incident.ApplicationName}</a>\r\n" +
141+
$"We've detected a spike in incoming reports for application <a href=\"{_baseConfiguration.BaseUrl}discover/{e.Incident.ApplicationId}/\">{e.Incident.ApplicationName}</a>\r\n" +
142142
"\r\n" +
143143
$"We've received {countToday.SpikeCount} reports so far. Day average is {countToday.DayAverage}\r\n" +
144144
"\r\n" + "No further spike emails will be sent today for this application."

src/Server/Coderr.Server.SqlServer/Coderr.Server.SqlServer.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,4 @@
2727
<EmbeddedResource Include="Schema\*.sql" />
2828
</ItemGroup>
2929

30-
<ItemGroup>
31-
<None Remove="Schema\Coderr.v21.sql" />
32-
<None Remove="Schema\Coderr.v22.sql" />
33-
</ItemGroup>
34-
3530
</Project>

src/Server/Coderr.Server.SqlServer/Migrations/MigrationScripts.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,22 @@ public void Execute(IDbConnection connection, int version)
7575

7676
private static void ExecuteSql(IDbConnection connection, string sql)
7777
{
78-
using (var transaction = connection.BeginTransaction())
78+
var parts = sql.Split(new[] {"\r\ngo\r\n", "\r\nGO\r\n", "\r\ngo;\r\n"},
79+
StringSplitOptions.RemoveEmptyEntries);
80+
foreach (var part in parts)
7981
{
80-
using (var cmd = connection.CreateCommand())
82+
using (var transaction = connection.BeginTransaction())
8183
{
82-
cmd.Transaction = transaction;
83-
cmd.CommandText = sql;
84-
cmd.ExecuteNonQuery();
84+
using (var cmd = connection.CreateCommand())
85+
{
86+
cmd.Transaction = transaction;
87+
cmd.CommandText = part;
88+
cmd.ExecuteNonQuery();
89+
}
90+
91+
transaction.Commit();
8592
}
86-
87-
transaction.Commit();
8893
}
89-
9094
}
9195

9296
public int GetHighestVersion()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE Incidents ADD LastStoredReportUtc datetime;
2+
go
3+
UPDATE Incidents SET LastStoredReportUtc = LastReportAtUtc;

0 commit comments

Comments
 (0)