99using Coderr . Server . App . Core . Reports . Config ;
1010using Coderr . Server . ReportAnalyzer . Inbound ;
1111using Coderr . Server . Web . Infrastructure ;
12+ using Coderr . Server . Web . Infrastructure . Misc ;
1213using Coderr . Server . Web . Infrastructure . Results ;
1314using DotNetCqs . Queues ;
1415using Griffin . Data ;
@@ -21,6 +22,8 @@ namespace Coderr.Server.Web.Controllers
2122 public class ReportReceiverController : Controller
2223 {
2324 private const int CompressedReportSizeLimit = 1000000 ;
25+
26+ private static int _currentReportCount ;
2427 private readonly ConfigurationStore _configStore ;
2528 private readonly ILog _logger = LogManager . GetLogger ( typeof ( ReportReceiverController ) ) ;
2629 private readonly IMessageQueue _messageQueue ;
@@ -51,6 +54,8 @@ public async Task<IActionResult> Post(string appKey, string sig)
5154 if ( contentLength == null || contentLength < 1 )
5255 return BadRequest ( "Content required." ) ;
5356
57+ if ( ! IsBelowReportLimit ( ) ) return NoContent ( ) ;
58+
5459 try
5560 {
5661 var buffer = new byte [ contentLength . Value ] ;
@@ -83,7 +88,8 @@ public async Task<IActionResult> Post(string appKey, string sig)
8388 }
8489 catch ( Exception exception )
8590 {
86- _logger . Error ( "Failed to handle request from " + appKey + " / " + Request . HttpContext . Connection . RemoteIpAddress ,
91+ _logger . Error (
92+ "Failed to handle request from " + appKey + " / " + Request . HttpContext . Connection . RemoteIpAddress ,
8793 exception ) ;
8894 return new ContentResult
8995 {
@@ -103,13 +109,37 @@ internal static ClaimsPrincipal CreateReporterPrincipal()
103109 } ) ) ;
104110 return principal ;
105111 }
112+
113+ private bool IsBelowReportLimit ( )
114+ {
115+ if ( ! string . IsNullOrEmpty ( License . LicensedTo ) )
116+ return true ;
117+
118+ var count = License . Count / 5 ;
119+ count = count / 10 ;
120+
121+ if ( _currentReportCount == 0 )
122+ using ( var cmd = _unitOfWork . CreateDbCommand ( ) )
123+ {
124+ var from = new DateTime ( DateTime . Today . Year , DateTime . Today . Month , 1 ) ;
125+ cmd . CommandText =
126+ "SELECT count(*) FROM ErrorReports WHERE CreatedAtUtc >= @from AND CreatedAtUtc <= @to" ;
127+ cmd . AddParameter ( "from" , from ) ;
128+ cmd . AddParameter ( "to" , DateTime . Now ) ;
129+ _currentReportCount = ( int ) cmd . ExecuteScalar ( ) ;
130+ }
131+ else
132+ _currentReportCount ++ ;
133+
134+ return _currentReportCount < count ;
135+ }
136+
106137 private Task < IActionResult > KillLargeReportAsync ( string appKey )
107138 {
108- _logger . Error ( appKey + "Too large report: " + Request . ContentLength + " from " +
139+ _logger . Error ( appKey + "Too large report: " + Request . ContentLength + " from " +
109140 Request . HttpContext . Connection . RemoteIpAddress ) ;
110141 //TODO: notify
111142 return Task . FromResult < IActionResult > ( NoContent ( ) ) ;
112143 }
113-
114144 }
115- }
145+ }
0 commit comments