Some performance improvements around the new TV stuff

pull/470/head
tidusjar 8 years ago
parent 4bccb8fcf0
commit 53f806fc71

@ -37,22 +37,21 @@ namespace PlexRequests.Helpers
{ {
public static class LoggingHelper public static class LoggingHelper
{ {
/// <summary>
/// WARNING, This method uses up a LOT of memory and can lead to leaks.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string DumpJson(this object value) public static string DumpJson(this object value)
{ {
var dumpTarget = value; object dumpTarget;
//if this is a string that contains a JSON object, do a round-trip serialization to format it: //if this is a string that contains a JSON object, do a round-trip serialization to format it:
var stringValue = value as string; var stringValue = value as string;
if (stringValue != null) if (stringValue != null)
{ {
if (stringValue.Trim().StartsWith("{", StringComparison.Ordinal)) dumpTarget = stringValue.Trim().StartsWith("{", StringComparison.Ordinal)
{ ? JsonConvert.SerializeObject(JsonConvert.DeserializeObject(stringValue), Formatting.Indented)
var obj = JsonConvert.DeserializeObject(stringValue); : stringValue;
dumpTarget = JsonConvert.SerializeObject(obj, Formatting.Indented);
}
else
{
dumpTarget = stringValue;
}
} }
else else
{ {
@ -73,7 +72,8 @@ namespace PlexRequests.Helpers
CommandType = CommandType.Text, CommandType = CommandType.Text,
ConnectionString = connectionString, ConnectionString = connectionString,
DBProvider = "Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756", DBProvider = "Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756",
Name = "database" Name = "database",
}; };
var messageParam = new DatabaseParameterInfo { Name = "@Message", Layout = "${message}" }; var messageParam = new DatabaseParameterInfo { Name = "@Message", Layout = "${message}" };
@ -94,7 +94,7 @@ namespace PlexRequests.Helpers
config.AddTarget("database", databaseTarget); config.AddTarget("database", databaseTarget);
// Step 4. Define rules // Step 4. Define rules
var rule1 = new LoggingRule("*", LogLevel.Info, databaseTarget); var rule1 = new LoggingRule("*", LogLevel.Debug, databaseTarget);
config.LoggingRules.Add(rule1); config.LoggingRules.Add(rule1);
@ -106,7 +106,7 @@ namespace PlexRequests.Helpers
CreateDirs = true CreateDirs = true
}; };
config.AddTarget(fileTarget); config.AddTarget(fileTarget);
var rule2 = new LoggingRule("*", LogLevel.Info, fileTarget); var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
config.LoggingRules.Add(rule2); config.LoggingRules.Add(rule2);
// Step 5. Activate the configuration // Step 5. Activate the configuration

@ -39,5 +39,16 @@ namespace PlexRequests.Services.Interfaces
List<PlexAlbum> GetPlexAlbums(); List<PlexAlbum> GetPlexAlbums();
bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist); bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist);
bool IsEpisodeAvailable(string theTvDbId, int season, int episode); bool IsEpisodeAvailable(string theTvDbId, int season, int episode);
/// <summary>
/// Gets the episode's stored in the cache.
/// </summary>
/// <returns></returns>
IEnumerable<PlexEpisodeModel> GetEpisodeCache();
/// <summary>
/// Gets the episode's stored in the cache and then filters on the TheTvDBId.
/// </summary>
/// <param name="theTvDbId">The tv database identifier.</param>
/// <returns></returns>
IEnumerable<PlexEpisodeModel> GetEpisodeCache(int theTvDbId);
} }
} }

@ -257,6 +257,38 @@ namespace PlexRequests.Services.Jobs
return false; return false;
} }
/// <summary>
/// Gets the episode's stored in the cache.
/// </summary>
/// <returns></returns>
public IEnumerable<PlexEpisodeModel> GetEpisodeCache()
{
var episodes = Cache.Get<List<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
if (episodes == null)
{
Log.Info("Episode cache info is not available.");
return new List<PlexEpisodeModel>();
}
return episodes;
}
/// <summary>
/// Gets the episode's stored in the cache and then filters on the TheTvDBId.
/// </summary>
/// <param name="theTvDbId">The tv database identifier.</param>
/// <returns></returns>
public IEnumerable<PlexEpisodeModel> GetEpisodeCache(int theTvDbId)
{
var episodes = Cache.Get<List<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
if (episodes == null)
{
Log.Info("Episode cache info is not available.");
return new List<PlexEpisodeModel>();
}
return episodes.Where(x => x.Episodes.ProviderId == theTvDbId.ToString());
}
public List<PlexAlbum> GetPlexAlbums() public List<PlexAlbum> GetPlexAlbums()
{ {
var albums = new List<PlexAlbum>(); var albums = new List<PlexAlbum>();

@ -608,9 +608,10 @@ namespace PlexRequests.UI.Modules
} }
if (episodeRequest) if (episodeRequest)
{ {
var cachedEpisodes = Checker.GetEpisodeCache().ToList();
foreach (var d in difference) foreach (var d in difference)
{ {
if (Checker.IsEpisodeAvailable(providerId, d.SeasonNumber, d.EpisodeNumber)) if (cachedEpisodes.Any(x => x.Episodes.SeasonNumber == d.SeasonNumber && x.Episodes.EpisodeNumber == d.EpisodeNumber && x.Episodes.ProviderId == providerId))
{ {
return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {d.SeasonNumber} - {d.EpisodeNumber} {Resources.UI.Search_AlreadyInPlex}" }); return Response.AsJson(new JsonResponseModel { Result = false, Message = $"{fullShowName} {d.SeasonNumber} - {d.EpisodeNumber} {Resources.UI.Search_AlreadyInPlex}" });
} }
@ -982,13 +983,15 @@ namespace PlexRequests.UI.Modules
sonarrEpisodes = sonarrEp?.ToList() ?? new List<SonarrEpisodes>(); sonarrEpisodes = sonarrEp?.ToList() ?? new List<SonarrEpisodes>();
} }
var plexCache = Checker.GetEpisodeCache(seriesId).ToList();
foreach (var ep in seasons) foreach (var ep in seasons)
{ {
var requested = dbDbShow?.Episodes var requested = dbDbShow?.Episodes
.Any(episodesModel => .Any(episodesModel =>
ep.number == episodesModel.EpisodeNumber && ep.season == episodesModel.SeasonNumber) ?? false; ep.number == episodesModel.EpisodeNumber && ep.season == episodesModel.SeasonNumber) ?? false;
var alreadyInPlex = Checker.IsEpisodeAvailable(seriesId.ToString(), ep.season, ep.number); var alreadyInPlex = plexCache.Any(x => x.Episodes.EpisodeNumber == ep.number && x.Episodes.SeasonNumber == ep.season);
var inSonarr = sonarrEpisodes.Any(x => x.seasonNumber == ep.season && x.episodeNumber == ep.number && x.monitored); var inSonarr = sonarrEpisodes.Any(x => x.seasonNumber == ep.season && x.episodeNumber == ep.number && x.monitored);
model.Add(new EpisodeListViewModel model.Add(new EpisodeListViewModel

Loading…
Cancel
Save