Create metric for item counts

This commit is contained in:
Ahmed Rafiq Ullah 2021-11-20 15:03:32 +06:00
parent 718e4b8665
commit b220f1e127
1 changed files with 46 additions and 0 deletions

View File

@ -30,6 +30,8 @@ using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
@ -50,6 +52,7 @@ using MediaBrowser.Model.Tasks;
using MediaBrowser.Providers.MediaInfo;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Prometheus;
using Episode = MediaBrowser.Controller.Entities.TV.Episode;
using EpisodeInfo = Emby.Naming.TV.EpisodeInfo;
using Genre = MediaBrowser.Controller.Entities.Genre;
@ -97,6 +100,16 @@ namespace Emby.Server.Implementations.Library
private bool _wizardCompleted;
private static readonly Gauge _itemCountGauge = Metrics.CreateGauge("jellyfin_library_items_total", "The number of items in the library", new[] { "item_type" });
private static readonly string[] _metricTypes = new string[]
{
nameof(Movie),
nameof(Series), nameof(Season), nameof(Episode),
nameof(MusicArtist), nameof(MusicAlbum), nameof(MusicVideo), nameof(Audio),
nameof(Book),
nameof(PhotoAlbum), nameof(Photo)
};
/// <summary>
/// Initializes a new instance of the <see cref="LibraryManager" /> class.
/// </summary>
@ -448,6 +461,18 @@ namespace Emby.Server.Implementations.Library
_itemRepository.DeleteItem(child.Id);
}
if (_metricTypes.Contains(item.GetType().Name))
{
_itemCountGauge.WithLabels(item.GetType().Name).Dec();
}
foreach (var child in children)
{
if (_metricTypes.Contains(child.GetType().Name))
{
_itemCountGauge.WithLabels(child.GetType().Name).Dec();
}
}
_memoryCache.Remove(item.Id);
ReportItemRemoved(item, parent);
@ -786,6 +811,8 @@ namespace Emby.Server.Implementations.Library
RegisterItem(folder);
UpdateItemCountGauge();
return rootFolder;
}
@ -1113,6 +1140,8 @@ namespace Emby.Server.Implementations.Library
await RunPostScanTasks(innerProgress, cancellationToken).ConfigureAwait(false);
UpdateItemCountGauge();
progress.Report(100);
}
@ -1836,6 +1865,10 @@ namespace Emby.Server.Implementations.Library
foreach (var item in items)
{
RegisterItem(item);
if (_metricTypes.Contains(item.GetType().Name))
{
_itemCountGauge.WithLabels(item.GetType().Name).Inc();
}
}
if (ItemAdded != null)
@ -3262,5 +3295,18 @@ namespace Emby.Server.Implementations.Library
CollectionFolder.SaveLibraryOptions(virtualFolderPath, libraryOptions);
}
private void UpdateItemCountGauge()
{
foreach (var type in _metricTypes)
{
var query = new InternalItemsQuery
{
IncludeItemTypes = new[] { type }
};
int count = GetCount(query);
_itemCountGauge.WithLabels(type).Set(count);
}
}
}
}