Skip to content

Commit

Permalink
add ExternalStorageLimitationAnalysis Kentico#224
Browse files Browse the repository at this point in the history
  • Loading branch information
kentico-ericd committed Jun 11, 2024
1 parent 518ef96 commit 26223f4
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/KInspector.Core/Constants/ModuleTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public static class ModuleTags
public const string WebFarms = "Web farms";
public const string Staging = "Content staging";
public const string Emails = "Emails";
public const string Azure = "Azure";
public const string AmazonS3 = "Amazon S3";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
terms:
summaries:
good: No restart events found.
information: '<totalEvents> total <totalEvents|event|events> (<totalStartEvents> STARTAPP, <totalEndEvents> ENDAPP) spanning <earliestTime> - <latestTime>.'
information: <totalEvents> total <totalEvents|event|events> (<totalStartEvents> STARTAPP, <totalEndEvents> ENDAPP) spanning <earliestTime> - <latestTime>.
tableTitles:
applicationRestartEvents: Application restart events
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
details:
name: External storage limitation analysis
shortDescription: Checks whether media libraries are following recommendations for external storage.
longDescription: |
Web sites utilizing external storage (Azure Blob or Amazon S3) should limit media library folder size to a maximum 100 items per folder. If you exceed the limit, alter the structure that allows you to divide the files into multiple folders.
See [Media library limitations when storing files in an external storage](https://docs.kentico.com/13/configuring-xperience/configuring-the-environment-for-content-editors/configuring-media-libraries#media-library-limitations-when-storing-files-in-an-external-storage).
terms:
summaries:
good: No issues found.
error: <totalFolders> <totalFolders|folder|folders> with excessive media files.
tableTitles:
tablesOverLimit: Folders with more than 100 files
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace KInspector.Reports.ExternalStorageLimitationAnalysis.Models
{
public class CmsMediaFolderResult
{
public string? LibraryDisplayName { get; set; }

public string? FolderName { get; set; }

public string? SiteName { get; set; }

public int NumberOfFiles { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using KInspector.Core.Models;

namespace KInspector.Reports.ExternalStorageLimitationAnalysis.Models
{
public class Terms
{
public Summaries? Summaries { get; set; }

public TableTitles? TableTitles { get; set; }
}

public class Summaries
{
public Term? Good { get; set; }

public Term? Error { get; set; }
}

public class TableTitles
{
public Term? TablesOverLimit { get; set; }
}
}
55 changes: 55 additions & 0 deletions src/KInspector.Reports/ExternalStorageLimitationAnalysis/Report.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using KInspector.Core;
using KInspector.Core.Constants;
using KInspector.Core.Helpers;
using KInspector.Core.Models;
using KInspector.Core.Services.Interfaces;
using KInspector.Reports.ExternalStorageLimitationAnalysis.Models;

namespace KInspector.Reports.ExternalStorageLimitationAnalysis
{
public class Report : AbstractReport<Terms>
{
private readonly IDatabaseService databaseService;

public Report(IDatabaseService databaseService, IModuleMetadataService moduleMetadataService) : base(moduleMetadataService)
{
this.databaseService = databaseService;
}

public override IList<Version> CompatibleVersions => VersionHelper.GetVersionList("12", "13");

public override IList<string> Tags => new List<string> {
ModuleTags.Performance,
ModuleTags.Azure,
ModuleTags.AmazonS3
};

public async override Task<ModuleResults> GetResults()
{
var foldersOverLimit = await databaseService.ExecuteSqlFromFile<CmsMediaFolderResult>(Scripts.GetMediaFoldersWithMoreThan100Files);
if (foldersOverLimit.Any())
{
var result = new ModuleResults
{
Status = ResultsStatus.Error,
Type = ResultsType.TableList,
Summary = Metadata.Terms.Summaries?.Error?.With(new { totalFolders = foldersOverLimit.Count() })
};
result.TableResults.Add(new TableResult
{
Name = Metadata.Terms.TableTitles?.TablesOverLimit,
Rows = foldersOverLimit
});

return result;
}

return new ModuleResults
{
Status = ResultsStatus.Good,
Type = ResultsType.NoResults,
Summary = Metadata.Terms.Summaries?.Good
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace KInspector.Reports.ExternalStorageLimitationAnalysis
{
public static class Scripts
{
public static string BaseDirectory => $"{nameof(ExternalStorageLimitationAnalysis)}/Scripts";

public static string GetMediaFoldersWithMoreThan100Files => $"{BaseDirectory}/{nameof(GetMediaFoldersWithMoreThan100Files)}.sql";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- File parent folder path is used to group items
-- This path is calculated from FilePath field by removing the file part
-- Example:
-- /somefolder/subfolder/filename.ext -> /somefolder/subfolder
-- /filename.ext -> '' (empty string)

SELECT LibraryDisplayName,
COALESCE(NULLIF(SUBSTRING('/' + f.FilePath, 0, LEN('/' + f.FilePath) - CHARINDEX('/', REVERSE('/' + f.FilePath)) + 1), ''), '/') AS 'FolderName',
SiteName,
COUNT(*) AS 'NumberOfFiles'
FROM Media_File AS F
INNER JOIN Media_Library AS L ON F.FileLibraryID = L.LibraryID
INNER JOIN CMS_Site AS S ON S.SiteID = L.LibrarySiteID
GROUP BY SUBSTRING('/' + F.FilePath, 0, LEN('/' + F.FilePath) - CHARINDEX('/', REVERSE('/' + F.FilePath)) + 1), LibraryDisplayName, SiteName
HAVING COUNT(*) > 100
ORDER BY LibraryDisplayName
6 changes: 6 additions & 0 deletions src/KInspector.Reports/KInspector.Reports.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<None Update="EventLogErrorSummary\Scripts\GetEventLogErrors.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ExternalStorageLimitationAnalysis\Metadata\en-US.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ExternalStorageLimitationAnalysis\Scripts\GetMediaFoldersWithMoreThan100Files.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="InactiveContactSettingsSummary\Metadata\en-US.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 26223f4

Please sign in to comment.