Skip to content

Commit

Permalink
added controller for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
rGunti committed May 25, 2024
1 parent 52f3f5a commit b944440
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 51 deletions.
7 changes: 7 additions & 0 deletions src/FloppyBot.Base.Logging.MongoDb/Entities/LogStats.cs
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
);
106 changes: 60 additions & 46 deletions src/FloppyBot.Base.Logging.MongoDb/LogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / Build & Test Project

Check warning on line 132 in src/FloppyBot.Base.Logging.MongoDb/LogService.cs

View workflow job for this annotation

GitHub Actions / Check Code Style

Static members should appear before non-static members
LogLevel? minLevel,
LogLevel? maxLevel
)
{
return LogLevels.All.Where(l =>
l <= (minLevel ?? LogLevel.Verbose) && l >= (maxLevel ?? LogLevel.Fatal)
Expand Down
12 changes: 7 additions & 5 deletions src/FloppyBot.WebApi.V2/Controllers/Admin/LogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ public LogController(LogService logService)
_logService = logService;
}

[HttpGet]
public IActionResult GetLog([FromQuery] LogRecordSearchParameters searchParams)
[HttpPost]
public ActionResult<LogRecord[]> GetLogEntries(
[FromBody] LogRecordSearchParameters searchParams
)
{
return Ok(_logService.GetLog(searchParams));
}

[HttpPost]
public IActionResult GetLogEntries([FromBody] LogRecordSearchParameters searchParams)
[HttpPost("stats")]
public ActionResult<LogStats> GetStats([FromBody] LogRecordSearchParameters searchParams)
{
return Ok(_logService.GetLog(searchParams));
return _logService.GetLogStats(searchParams);
}
}

0 comments on commit b944440

Please sign in to comment.