-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace FloppyBot.Base.Logging.MongoDb.Entities; | ||
|
||
public record LogStats( | ||
int TotalCount, | ||
DateTimeOffset? OldestLogEntry, | ||
DateTimeOffset? NewestLogEntry | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,105 +20,119 @@ public LogService(IMongoDatabase database, TimeProvider timeProvider) | |
_collection = database.GetCollection<InternalLogRecord>("_Logs"); | ||
} | ||
|
||
public IEnumerable<LogRecord> GetLog() | ||
private static FilterDefinitionBuilder<InternalLogRecord> Filter => | ||
Builders<InternalLogRecord>.Filter; | ||
|
||
private static ProjectionDefinitionBuilder<InternalLogRecord> Projection => | ||
Builders<InternalLogRecord>.Projection; | ||
|
||
public IEnumerable<LogRecord> GetLog(LogRecordSearchParameters searchParams) | ||
{ | ||
return _collection | ||
.AsQueryable() | ||
.OrderByDescending(l => l.Timestamp) | ||
.Take(10_000) | ||
.FindSync(Filter.And(GetFilter(searchParams)), GetFindOptions(searchParams)) | ||
.ToList() | ||
.Select(l => l.ToLogRecord()); | ||
.Select(l => l.ToLogRecord(searchParams.IncludeProperties)); | ||
} | ||
|
||
public IEnumerable<LogRecord> GetLog(LogRecordSearchParameters searchParams) | ||
public LogStats GetLogStats(LogRecordSearchParameters searchParameters) | ||
{ | ||
var (minTime, maxTime) = GetTimeFilter(searchParams); | ||
var filters = GetFilter(searchParameters).ToList(); | ||
var aggregation = _collection.Aggregate(); | ||
if (filters.Count != 0) | ||
{ | ||
aggregation = aggregation.Match(Filter.And(filters)); | ||
} | ||
|
||
var filterBuilder = Builders<InternalLogRecord>.Filter; | ||
List<FilterDefinition<InternalLogRecord>> filters = | ||
[ | ||
filterBuilder.Gte(l => l.Timestamp, minTime), | ||
filterBuilder.Lte(l => l.Timestamp, maxTime) | ||
]; | ||
var data = aggregation | ||
.Group( | ||
l => true, | ||
g => new | ||
{ | ||
Count = g.Count(), | ||
Oldest = g.Min(l => l.Timestamp), | ||
Newest = g.Max(l => l.Timestamp), | ||
} | ||
) | ||
.ToList(); | ||
return data.Select(d => new LogStats(d.Count, d.Oldest, d.Newest)) | ||
.FirstOrDefault(new LogStats(0, null, null)); | ||
} | ||
|
||
private IEnumerable<FilterDefinition<InternalLogRecord>> GetFilter( | ||
LogRecordSearchParameters searchParams | ||
) | ||
{ | ||
var (minTime, maxTime) = GetTimeFilter(searchParams); | ||
yield return Filter.Gte(l => l.Timestamp, minTime); | ||
yield return Filter.Lte(l => l.Timestamp, maxTime); | ||
|
||
if (searchParams.MinLevel is not null || searchParams.MaxLevel is not null) | ||
{ | ||
filters.Add( | ||
filterBuilder.In( | ||
l => l.Level, | ||
GetFilters(searchParams.MinLevel, searchParams.MaxLevel) | ||
) | ||
yield return Filter.In( | ||
l => l.Level, | ||
GetLogLevelsToFilterFor(searchParams.MinLevel, searchParams.MaxLevel) | ||
); | ||
} | ||
|
||
if (searchParams.HasException.HasValue) | ||
{ | ||
filters.Add(filterBuilder.Exists(l => l.Exception, searchParams.HasException.Value)); | ||
yield return Filter.Exists(l => l.Exception, searchParams.HasException.Value); | ||
} | ||
|
||
if (searchParams.Context is { Length: > 0 }) | ||
{ | ||
filters.Add(filterBuilder.In(l => l.Properties["SourceContext"], searchParams.Context)); | ||
yield return Filter.In(l => l.Properties["SourceContext"], searchParams.Context); | ||
} | ||
|
||
if (searchParams.ExcludeContext is { Length: > 0 }) | ||
{ | ||
filters.Add( | ||
filterBuilder.Nin(l => l.Properties["SourceContext"], searchParams.ExcludeContext) | ||
yield return Filter.Nin( | ||
l => l.Properties["SourceContext"], | ||
searchParams.ExcludeContext | ||
); | ||
} | ||
|
||
if (searchParams.Service is { Length: > 0 }) | ||
{ | ||
filters.Add(filterBuilder.In(l => l.Properties["AssemblyName"], searchParams.Service)); | ||
yield return Filter.In(l => l.Properties["AssemblyName"], searchParams.Service); | ||
} | ||
|
||
if (searchParams.ExcludeService is { Length: > 0 }) | ||
{ | ||
filters.Add( | ||
filterBuilder.Nin(l => l.Properties["AssemblyName"], searchParams.ExcludeService) | ||
); | ||
yield return Filter.Nin(l => l.Properties["AssemblyName"], searchParams.ExcludeService); | ||
} | ||
|
||
if (searchParams.InstanceName is { Length: > 0 }) | ||
{ | ||
filters.Add( | ||
filterBuilder.In( | ||
l => l.Properties["FloppyBotInstanceName"], | ||
searchParams.InstanceName | ||
) | ||
yield return Filter.In( | ||
l => l.Properties["FloppyBotInstanceName"], | ||
searchParams.InstanceName | ||
); | ||
} | ||
|
||
if (searchParams.ExcludeInstanceName is { Length: > 0 }) | ||
{ | ||
filters.Add( | ||
filterBuilder.Nin( | ||
l => l.Properties["FloppyBotInstanceName"], | ||
searchParams.ExcludeInstanceName | ||
) | ||
yield return Filter.Nin( | ||
l => l.Properties["FloppyBotInstanceName"], | ||
searchParams.ExcludeInstanceName | ||
); | ||
} | ||
|
||
if (searchParams.MessageTemplate is { Length: > 0 }) | ||
{ | ||
filters.Add(filterBuilder.In(l => l.MessageTemplate, searchParams.MessageTemplate)); | ||
yield return Filter.In(l => l.MessageTemplate, searchParams.MessageTemplate); | ||
} | ||
|
||
if (searchParams.ExcludeMessageTemplate is { Length: > 0 }) | ||
{ | ||
filters.Add( | ||
filterBuilder.Nin(l => l.MessageTemplate, searchParams.ExcludeMessageTemplate) | ||
); | ||
yield return Filter.Nin(l => l.MessageTemplate, searchParams.ExcludeMessageTemplate); | ||
} | ||
|
||
return _collection | ||
.FindSync(filterBuilder.And(filters), GetFindOptions(searchParams)) | ||
.ToList() | ||
.Select(l => l.ToLogRecord(searchParams.IncludeProperties)); | ||
} | ||
|
||
private static IEnumerable<LogLevel> GetFilters(LogLevel? minLevel, LogLevel? maxLevel) | ||
private static IEnumerable<LogLevel> GetLogLevelsToFilterFor( | ||
Check warning on line 132 in src/FloppyBot.Base.Logging.MongoDb/LogService.cs GitHub Actions / Build & Test Project
|
||
LogLevel? minLevel, | ||
LogLevel? maxLevel | ||
) | ||
{ | ||
return LogLevels.All.Where(l => | ||
l <= (minLevel ?? LogLevel.Verbose) && l >= (maxLevel ?? LogLevel.Fatal) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters