You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
4.6 KiB
C#
126 lines
4.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Haoliang.Models.DataCollection;
|
|
using Haoliang.Data.Repositories;
|
|
|
|
namespace Haoliang.Data.Repositories
|
|
{
|
|
public interface ICollectionLogRepository : IRepository<CollectionLog>
|
|
{
|
|
Task<IEnumerable<CollectionLog>> GetByDeviceAsync(int deviceId);
|
|
Task<IEnumerable<CollectionLog>> GetByLogLevelAsync(LogLevel logLevel);
|
|
Task<int> GetErrorCountAsync(int deviceId);
|
|
Task ArchiveLogsAsync(int daysToKeep = 30);
|
|
Task ClearLogsAsync();
|
|
Task<IEnumerable<CollectionLog>> GetRecentLogsAsync(int count = 100);
|
|
Task<CollectionLogStatistics> GetLogStatisticsAsync(DateTime date);
|
|
Task<IEnumerable<CollectionLog>> GetLogsByCategoryAsync(string category);
|
|
Task<bool> LogExistsAsync(int logId);
|
|
}
|
|
|
|
public class CollectionLogRepository : Repository<CollectionLog>, ICollectionLogRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public CollectionLogRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<CollectionLog>> GetByDeviceAsync(int deviceId)
|
|
{
|
|
return await _context.CollectionLogs
|
|
.Where(l => l.DeviceId == deviceId)
|
|
.OrderByDescending(l => l.LogTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<CollectionLog>> GetByLogLevelAsync(LogLevel logLevel)
|
|
{
|
|
return await _context.CollectionLogs
|
|
.Where(l => l.LogLevel == logLevel)
|
|
.OrderByDescending(l => l.LogTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> GetErrorCountAsync(int deviceId)
|
|
{
|
|
return await _context.CollectionLogs
|
|
.CountAsync(l => l.DeviceId == deviceId &&
|
|
(l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical));
|
|
}
|
|
|
|
public async Task ArchiveLogsAsync(int daysToKeep = 30)
|
|
{
|
|
var cutoffDate = DateTime.Now.AddDays(-daysToKeep);
|
|
|
|
var logsToArchive = await _context.CollectionLogs
|
|
.Where(l => l.LogTime < cutoffDate)
|
|
.ToListAsync();
|
|
|
|
if (logsToArchive.Any())
|
|
{
|
|
// In a real implementation, you would move these to an archive table
|
|
_context.CollectionLogs.RemoveRange(logsToArchive);
|
|
await SaveAsync();
|
|
}
|
|
}
|
|
|
|
public async Task ClearLogsAsync()
|
|
{
|
|
_context.CollectionLogs.RemoveRange(_context.CollectionLogs);
|
|
await SaveAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<CollectionLog>> GetRecentLogsAsync(int count = 100)
|
|
{
|
|
return await _context.CollectionLogs
|
|
.OrderByDescending(l => l.LogTime)
|
|
.Take(count)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<CollectionLogStatistics> GetLogStatisticsAsync(DateTime date)
|
|
{
|
|
var startOfDay = date.Date;
|
|
var endOfDay = startOfDay.AddDays(1);
|
|
|
|
var logs = await _context.CollectionLogs
|
|
.Where(l => l.LogTime >= startOfDay && l.LogTime <= endOfDay)
|
|
.ToListAsync();
|
|
|
|
var stats = new CollectionLogStatistics
|
|
{
|
|
Date = date,
|
|
TotalLogs = logs.Count,
|
|
ErrorLogs = logs.Count(l => l.LogLevel == LogLevel.Error || l.LogLevel == LogLevel.Critical),
|
|
WarningLogs = logs.Count(l => l.LogLevel == LogLevel.Warning),
|
|
InfoLogs = logs.Count(l => l.LogLevel == LogLevel.Information),
|
|
DebugLogs = logs.Count(l => l.LogLevel == LogLevel.Debug),
|
|
DeviceLogs = logs.GroupBy(l => l.DeviceId)
|
|
.ToDictionary(g => g.Key, g => g.Count()),
|
|
LogCategories = logs.GroupBy(l => l.LogCategory)
|
|
.ToDictionary(g => g.Key, g => g.Count())
|
|
};
|
|
|
|
return stats;
|
|
}
|
|
|
|
public async Task<IEnumerable<CollectionLog>> GetLogsByCategoryAsync(string category)
|
|
{
|
|
return await _context.CollectionLogs
|
|
.Where(l => l.LogCategory == category)
|
|
.OrderByDescending(l => l.LogTime)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> LogExistsAsync(int logId)
|
|
{
|
|
return await _context.CollectionLogs
|
|
.AnyAsync(l => l.LogId == logId);
|
|
}
|
|
}
|
|
} |