< Summary - Jellyfin

Information
Class: Jellyfin.Server.Implementations.Activity.ActivityManager
Assembly: Jellyfin.Server.Implementations
File(s): /srv/git/jellyfin/Jellyfin.Server.Implementations/Activity/ActivityManager.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 103
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ConvertToOldModel(...)100%11100%

File(s)

/srv/git/jellyfin/Jellyfin.Server.Implementations/Activity/ActivityManager.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Threading.Tasks;
 4using Jellyfin.Data.Events;
 5using Jellyfin.Data.Queries;
 6using Jellyfin.Database.Implementations;
 7using Jellyfin.Database.Implementations.Entities;
 8using MediaBrowser.Model.Activity;
 9using MediaBrowser.Model.Querying;
 10using Microsoft.EntityFrameworkCore;
 11
 12namespace Jellyfin.Server.Implementations.Activity
 13{
 14    /// <summary>
 15    /// Manages the storage and retrieval of <see cref="ActivityLog"/> instances.
 16    /// </summary>
 17    public class ActivityManager : IActivityManager
 18    {
 19        private readonly IDbContextFactory<JellyfinDbContext> _provider;
 20
 21        /// <summary>
 22        /// Initializes a new instance of the <see cref="ActivityManager"/> class.
 23        /// </summary>
 24        /// <param name="provider">The Jellyfin database provider.</param>
 25        public ActivityManager(IDbContextFactory<JellyfinDbContext> provider)
 26        {
 2127            _provider = provider;
 2128        }
 29
 30        /// <inheritdoc/>
 31        public event EventHandler<GenericEventArgs<ActivityLogEntry>>? EntryCreated;
 32
 33        /// <inheritdoc/>
 34        public async Task CreateAsync(ActivityLog entry)
 35        {
 36            var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
 37            await using (dbContext.ConfigureAwait(false))
 38            {
 39                dbContext.ActivityLogs.Add(entry);
 40                await dbContext.SaveChangesAsync().ConfigureAwait(false);
 41            }
 42
 43            EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
 44        }
 45
 46        /// <inheritdoc/>
 47        public async Task<QueryResult<ActivityLogEntry>> GetPagedResultAsync(ActivityLogQuery query)
 48        {
 49            var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
 50            await using (dbContext.ConfigureAwait(false))
 51            {
 52                var entries = dbContext.ActivityLogs
 53                    .OrderByDescending(entry => entry.DateCreated)
 54                    .Where(entry => query.MinDate == null || entry.DateCreated >= query.MinDate)
 55                    .Where(entry => !query.HasUserId.HasValue || entry.UserId.Equals(default) != query.HasUserId.Value);
 56
 57                return new QueryResult<ActivityLogEntry>(
 58                    query.Skip,
 59                    await entries.CountAsync().ConfigureAwait(false),
 60                    await entries
 61                        .Skip(query.Skip ?? 0)
 62                        .Take(query.Limit ?? 100)
 63                        .Select(entity => new ActivityLogEntry(entity.Name, entity.Type, entity.UserId)
 64                        {
 65                            Id = entity.Id,
 66                            Overview = entity.Overview,
 67                            ShortOverview = entity.ShortOverview,
 68                            ItemId = entity.ItemId,
 69                            Date = entity.DateCreated,
 70                            Severity = entity.LogSeverity
 71                        })
 72                        .ToListAsync()
 73                        .ConfigureAwait(false));
 74            }
 75        }
 76
 77        /// <inheritdoc />
 78        public async Task CleanAsync(DateTime startDate)
 79        {
 80            var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false);
 81            await using (dbContext.ConfigureAwait(false))
 82            {
 83                await dbContext.ActivityLogs
 84                    .Where(entry => entry.DateCreated <= startDate)
 85                    .ExecuteDeleteAsync()
 86                    .ConfigureAwait(false);
 87            }
 88        }
 89
 90        private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
 91        {
 3492            return new ActivityLogEntry(entry.Name, entry.Type, entry.UserId)
 3493            {
 3494                Id = entry.Id,
 3495                Overview = entry.Overview,
 3496                ShortOverview = entry.ShortOverview,
 3497                ItemId = entry.ItemId,
 3498                Date = entry.DateCreated,
 3499                Severity = entry.LogSeverity
 34100            };
 101        }
 102    }
 103}