From 0bdc2d680a9f3b38d61c1d8db0cb9d600aae6982 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Fri, 4 Feb 2022 23:29:46 +0100 Subject: [PATCH] Performance improvements --- .../Api/Common/ReportBuilderBase.cs | 32 ++----- .../Api/Data/ReportExport.cs | 93 ++++++++++++------- Jellyfin.Plugin.Reports/Api/ReportsService.cs | 29 +++--- 3 files changed, 80 insertions(+), 74 deletions(-) diff --git a/Jellyfin.Plugin.Reports/Api/Common/ReportBuilderBase.cs b/Jellyfin.Plugin.Reports/Api/Common/ReportBuilderBase.cs index f653385..a1d99d1 100644 --- a/Jellyfin.Plugin.Reports/Api/Common/ReportBuilderBase.cs +++ b/Jellyfin.Plugin.Reports/Api/Common/ReportBuilderBase.cs @@ -42,15 +42,7 @@ namespace Jellyfin.Plugin.Reports.Api.Common /// Options for controlling the operation. /// The active headers. protected List GetActiveHeaders(List> options, ReportDisplayType displayType) - { - List headers = new List(); - foreach (ReportOptions option in options.Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType))) - { - headers.Add(option.Header); - } - - return headers; - } + => options.Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType)).Select(x => x.Header).ToList(); /// Gets audio stream. /// The item. @@ -73,11 +65,13 @@ namespace Jellyfin.Plugin.Reports.Api.Common /// The episode. protected string GetEpisode(BaseItem item) { - - if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null) + if (string.Equals(item.GetClientTypeName(), ChannelMediaContentType.Episode.ToString(), StringComparison.Ordinal) + && item.ParentIndexNumber != null) + { return "Season " + item.ParentIndexNumber; - else - return item.Name; + } + + return item.Name; } /// Gets a genre. @@ -105,15 +99,7 @@ namespace Jellyfin.Plugin.Reports.Api.Common /// Options for controlling the operation. /// The headers. protected List GetHeaders(List> options) - { - List headers = new List(); - foreach (ReportOptions option in options) - { - headers.Add(option.Header); - } - - return headers; - } + => options.ConvertAll(x => x.Header); /// Gets the headers. /// Generic type parameter. @@ -132,7 +118,7 @@ namespace Jellyfin.Plugin.Reports.Api.Common /// The list as string. protected string GetListAsString(List items) { - return String.Join("; ", items); + return string.Join("; ", items); } /// Gets localized header. diff --git a/Jellyfin.Plugin.Reports/Api/Data/ReportExport.cs b/Jellyfin.Plugin.Reports/Api/Data/ReportExport.cs index 223edbb..4d50039 100644 --- a/Jellyfin.Plugin.Reports/Api/Data/ReportExport.cs +++ b/Jellyfin.Plugin.Reports/Api/Data/ReportExport.cs @@ -1,6 +1,6 @@ #nullable disable -using System.Globalization; +using System.Collections.Generic; using System.Linq; using System.Text; using Jellyfin.Plugin.Reports.Api.Model; @@ -8,30 +8,35 @@ using Jellyfin.Plugin.Reports.Api.Model; namespace Jellyfin.Plugin.Reports.Api.Data { /// A report export. - public class ReportExport + public static class ReportExport { /// Export to CSV. /// The report result. /// A string. - public string ExportToCsv(ReportResult reportResult) + public static string ExportToCsv(ReportResult reportResult) { - StringBuilder returnValue = new StringBuilder(); + static void AppendRows(StringBuilder builder, List rows) + { + foreach (ReportRow row in rows) + { + builder.AppendJoin(';', row.Columns.Select(s => s.Name.Replace(',', ' '))).AppendLine(); + } + } - returnValue.AppendLine(string.Join(';', reportResult.Headers.Select(s => s.Name.Replace(',', ' ')))); + StringBuilder returnValue = new StringBuilder(); + returnValue.AppendJoin(';', reportResult.Headers.Select(s => s.Name.Replace(',', ' '))).AppendLine(); if (reportResult.IsGrouped) + { foreach (ReportGroup group in reportResult.Groups) { - foreach (ReportRow row in reportResult.Rows) - { - returnValue.AppendLine(string.Join(';', row.Columns.Select(s => s.Name.Replace(',', ' ')))); - } + AppendRows(returnValue, group.Rows); } + } else - foreach (ReportRow row in reportResult.Rows) - { - returnValue.AppendLine(string.Join(';', row.Columns.Select(s => s.Name.Replace(',', ' ')))); - } + { + AppendRows(returnValue, reportResult.Rows); + } return returnValue.ToString(); } @@ -40,10 +45,9 @@ namespace Jellyfin.Plugin.Reports.Api.Data /// Export to excel. /// The report result. /// A string. - public string ExportToExcel(ReportResult reportResult) + public static string ExportToExcel(ReportResult reportResult) { - - string style = @"