Skip to content

Commit

Permalink
Improve performance (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fosol authored Aug 1, 2024
1 parent d9b559d commit 59c54ce
Show file tree
Hide file tree
Showing 31 changed files with 2,678 additions and 208 deletions.
8 changes: 4 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/api/bin/Debug/net7.0/HSB.API.dll",
"program": "${workspaceFolder}/src/api/bin/Debug/net8.0/HSB.API.dll",
"args": [],
"cwd": "${workspaceFolder}/src/api",
"stopAtEntry": false,
Expand All @@ -35,7 +35,7 @@
"request": "launch",
"preLaunchTask": "build-api",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/api/bin/Debug/net7.0/HSB.API.dll",
"program": "${workspaceFolder}/src/api/bin/Debug/net8.0/HSB.API.dll",
"args": [],
"cwd": "${workspaceFolder}/src/api",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand All @@ -52,7 +52,7 @@
"request": "launch",
"preLaunchTask": "build-api-css",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/api-css/bin/Debug/net7.0/HSB.CSS.API.dll",
"program": "${workspaceFolder}/src/api-css/bin/Debug/net8.0/HSB.CSS.API.dll",
"args": [],
"cwd": "${workspaceFolder}/src/api-css",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand All @@ -69,7 +69,7 @@
"request": "launch",
"preLaunchTask": "build-data-service",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/src/data-service/bin/Debug/net7.0/HSB.DataService.dll",
"program": "${workspaceFolder}/src/data-service/bin/Debug/net8.0/HSB.DataService.dll",
"args": [],
"cwd": "${workspaceFolder}/src/data-service",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public FileSystemItemController(
[HttpGet(Name = "GetFileSystemItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<FileSystemItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "File System Item" })]
[SwaggerOperation(Tags = ["File System Item"])]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -99,7 +99,7 @@ public IActionResult Find()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(FileSystemItemModel), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
[SwaggerOperation(Tags = new[] { "File System Item" })]
[SwaggerOperation(Tags = ["File System Item"])]
public IActionResult GetForId(string id)
{
var isHSB = this.User.HasClientRole(ClientRole.HSB);
Expand Down Expand Up @@ -128,8 +128,7 @@ public IActionResult GetForId(string id)
[HttpGet("history", Name = "GetFileSystemHistoryItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<FileSystemHistoryItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "File System Item" })]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 1200)]
[SwaggerOperation(Tags = ["File System Item"])]
public IActionResult FindHistory()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -166,7 +165,7 @@ public IActionResult FindHistory()
[Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "File System Item" })]
[SwaggerOperation(Tags = ["File System Item"])]
public IActionResult Export(string format, string name = "service-now")
{
if (format == "excel")
Expand Down
13 changes: 5 additions & 8 deletions src/api/Areas/Dashboard/Controllers/ServerItemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public ServerItemController(
[HttpGet(Name = "GetServerItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ServerItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
[SwaggerOperation(Tags = ["Server Item"])]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -117,7 +116,6 @@ public IActionResult Find()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ServerItemListModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult FindList()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -189,8 +187,7 @@ public IActionResult GetForId(string serviceNowKey, bool includeFileSystemItems
[HttpGet("history", Name = "GetServerHistoryItems-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ServerItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 1200)]
[SwaggerOperation(Tags = ["Server Item"])]
public IActionResult FindHistory()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -225,7 +222,7 @@ public IActionResult FindHistory()
[Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
[SwaggerOperation(Tags = ["Server Item"])]
public IActionResult Export(string format = "excel", string name = "service-now")
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -270,7 +267,7 @@ public IActionResult Export(string format = "excel", string name = "service-now"
[Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(ErrorResponseModel), (int)HttpStatusCode.BadRequest)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
[SwaggerOperation(Tags = ["Server Item"])]
public IActionResult ExportHistory(string format = "excel", string name = "service-now")
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand All @@ -279,7 +276,7 @@ public IActionResult ExportHistory(string format = "excel", string name = "servi

if (format == "excel")
{
IEnumerable<Entities.ServerHistoryItem> items;
IEnumerable<Entities.ServerHistoryItemSmall> items;
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/Helpers/IXlsExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public interface IXlsExporter
/// <param name="sheetName"></param>
/// <param name="items"></param>
/// <returns></returns>
XSSFWorkbook GenerateExcel(string sheetName, IEnumerable<Entities.ServerHistoryItem> items);
XSSFWorkbook GenerateExcel(string sheetName, IEnumerable<Entities.ServerHistoryItemSmall> items);
#endregion
}
37 changes: 36 additions & 1 deletion src/api/Helpers/XlsExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public XSSFWorkbook GenerateExcel(string sheetName, IEnumerable<Entities.ServerI
/// <param name="items"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public XSSFWorkbook GenerateExcel(string sheetName, IEnumerable<Entities.ServerHistoryItem> items)
public XSSFWorkbook GenerateExcel(string sheetName, IEnumerable<Entities.ServerHistoryItemSmall> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));

Expand Down Expand Up @@ -192,6 +192,41 @@ private static void AddContent(IWorkbook workbook, IRow row, Entities.ServerHist
cell8.CellStyle = numberStyle;
cell8.SetCellValue(item.AvailableSpace ?? 0);
}

private static void AddContent(IWorkbook workbook, IRow row, Entities.ServerHistoryItemSmall item)
{
var numberStyle = workbook.CreateCellStyle();
var numberFormat = workbook.CreateDataFormat().GetFormat("#,#0");
numberStyle.DataFormat = numberFormat;

var dateStyle = workbook.CreateCellStyle();
var dateFormat = workbook.CreateDataFormat().GetFormat("yyyy/MM/dd");
dateStyle.DataFormat = dateFormat;

var cell0 = row.CreateCell(0);
cell0.SetCellValue(item.CreatedOn.Date);
cell0.CellStyle = dateStyle;
var cell1 = row.CreateCell(1);
cell1.SetCellValue(item.Tenant?.Code);
var cell2 = row.CreateCell(2);
cell2.SetCellValue(item.Organization?.Code);
var cell3 = row.CreateCell(3);
cell3.SetCellValue(item.Organization?.Name);
var cell4 = row.CreateCell(4);
cell4.SetCellValue(item.ServiceNowKey);
var cell5 = row.CreateCell(5);
cell5.SetCellValue(item.Name);
var cell6 = row.CreateCell(6);
cell6.SetCellValue(item.OperatingSystemItem?.Name);
var cell7 = row.CreateCell(7);
cell7.SetCellType(CellType.Numeric);
cell7.CellStyle = numberStyle;
cell7.SetCellValue(item.Capacity ?? 0);
var cell8 = row.CreateCell(8);
cell8.SetCellType(CellType.Numeric);
cell8.CellStyle = numberStyle;
cell8.SetCellValue(item.AvailableSpace ?? 0);
}
#endregion

#region Helpers
Expand Down
4 changes: 2 additions & 2 deletions src/libs/ches/HSB.Ches.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.6.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/libs/css/HSB.CSS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/libs/dal/Extensions/ModelBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static ModelBuilder ApplyAllConfigurations(this ModelBuilder modelBuilder
var entityConfig = includeContext ? Activator.CreateInstance(config, context) : Activator.CreateInstance(config);
var entityType = config.GetInterfaces().First().GetGenericArguments()[0];
var applyConfigurationMethod = method.MakeGenericMethod(entityType);
applyConfigurationMethod.Invoke(modelBuilder, new[] { entityConfig });
applyConfigurationMethod.Invoke(modelBuilder, [entityConfig]);
}
}

Expand Down
44 changes: 33 additions & 11 deletions src/libs/dal/HSBContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,48 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.HasDbFunction(typeof(HSBContext)
.GetMethod(
nameof(FindServerHistoryItemsByMonth),
new[] { typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string) })!
).HasName("FindServerHistoryItemsByMonth");

[typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string)])!
).HasName(nameof(FindServerHistoryItemsByMonth));
modelBuilder.HasDbFunction(typeof(HSBContext)
.GetMethod(
nameof(FindServerHistoryItemsByMonthForUser),
new[] { typeof(int), typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string) })!
).HasName("FindServerHistoryItemsByMonthForUser");

[typeof(int), typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string)])!
).HasName(nameof(FindServerHistoryItemsByMonthForUser));
modelBuilder.HasDbFunction(typeof(HSBContext)
.GetMethod(
nameof(FindFileSystemHistoryItemsByMonth),
new[] { typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string) })!)
.HasName("FindFileSystemHistoryItemsByMonth");

[typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string)])!
).HasName(nameof(FindFileSystemHistoryItemsByMonth));
modelBuilder.HasDbFunction(typeof(HSBContext)
.GetMethod(
nameof(FindFileSystemHistoryItemsByMonthForUser),
new[] { typeof(int), typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string) })!)
.HasName("FindFileSystemHistoryItemsByMonthForUser");
[typeof(int), typeof(DateTime), typeof(DateTime?), typeof(int?), typeof(int?), typeof(int?), typeof(string)])!
).HasName(nameof(FindFileSystemHistoryItemsByMonthForUser));

// modelBuilder.Entity<ServerHistoryItemsByMonth>().ToTable((string?)null);
// modelBuilder.Entity<ServerHistoryItemsByMonthForUser>().ToTable((string?)null);
// modelBuilder.Entity<FileSystemHistoryItemsByMonth>().ToTable((string?)null);
// modelBuilder.Entity<FileSystemHistoryItemsByMonthForUser>().ToTable((string?)null);

// modelBuilder.Ignore<ServerHistoryItemsByMonth>();
// modelBuilder.Ignore<ServerHistoryItemsByMonthForUser>();
// modelBuilder.Ignore<FileSystemHistoryItemsByMonth>();
// modelBuilder.Ignore<FileSystemHistoryItemsByMonthForUser>();

// modelBuilder.Entity<ServerHistoryItemSmall>().ToView("vServerHistoryItem").HasKey(m => m.Id);
// modelBuilder.Entity<ServerHistoryItemsByMonthForUser>().ToView("vServerHistoryItem").HasKey(m => m.Id);
// modelBuilder.Entity<FileSystemHistoryItemsByMonth>().ToView("vFileSystemHistoryItem").HasKey(m => m.Id);
// modelBuilder.Entity<FileSystemHistoryItemSmall>().ToView("vFileSystemHistoryItem").HasKey(m => m.Id);

// modelBuilder.Entity<ServerHistoryItemsByMonth>().HasNoKey().ToSqlQuery("SELECT * FROM public.vServerHistoryItem");
// modelBuilder.Entity<ServerHistoryItemsByMonthForUser>().HasNoKey().ToSqlQuery("SELECT * FROM public.vServerHistoryItem");
// modelBuilder.Entity<FileSystemHistoryItemsByMonth>().HasNoKey().ToSqlQuery("SELECT * FROM public.vFileSystemHistoryItem");
// modelBuilder.Entity<FileSystemHistoryItemsByMonthForUser>().HasNoKey().ToSqlQuery("SELECT * FROM public.vFileSystemHistoryItem");

// modelBuilder.Entity<ServerHistoryItemsByMonth>().ToFunction(nameof(FindServerHistoryItemsByMonth));
// modelBuilder.Entity<ServerHistoryItemsByMonthForUser>().ToFunction(nameof(FindServerHistoryItemsByMonthForUser));
// modelBuilder.Entity<FileSystemHistoryItemsByMonth>().ToFunction(nameof(FindFileSystemHistoryItemsByMonth));
// modelBuilder.Entity<FileSystemHistoryItemsByMonthForUser>().ToFunction(nameof(FindFileSystemHistoryItemsByMonthForUser));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
DROP FUNCTION "FindFileSystemHistoryItemsByMonth";

CREATE OR REPLACE FUNCTION "FindFileSystemHistoryItemsByMonth"(
"startDate" TIMESTAMPTZ
, "endDate" TIMESTAMPTZ DEFAULT NULL
, "tenantId" INT DEFAULT NULL
, "organizationId" INT DEFAULT NULL
, "operatingSystemItemId" INT DEFAULT NULL
, "serverServiceNowKey" VARCHAR(200) DEFAULT NULL
)
RETURNS SETOF public."FileSystemHistoryItem"
LANGUAGE plpgsql
AS $$
DECLARE
end_date_default TIMESTAMPTZ;
last_month_start TIMESTAMPTZ;
BEGIN
end_date_default := (SELECT COALESCE($2, NOW()));
last_month_start := (SELECT (DATE_TRUNC('month', end_date_default))::TIMESTAMPTZ);
RETURN QUERY
SELECT DISTINCT
"Id"
, "ServiceNowKey"
, "RawData"
, "RawDataCI"
, "Name"
, "Label"
, "Category"
, "Subcategory"
, "StorageType"
, "MediaType"
, "VolumeId"
, "ClassName"
, "Capacity"
, "DiskSpace"
, "Size"
, "SizeBytes"
, "UsedSizeBytes"
, "AvailableSpace"
, "FreeSpace"
, "FreeSpaceBytes"
, "CreatedOn"
, "CreatedBy"
, "UpdatedOn"
, "UpdatedBy"
, "Version"
, "ServerItemServiceNowKey"
, "InstallStatus"
FROM (
SELECT fshi.*
, ROW_NUMBER() OVER (PARTITION BY fshi."ServiceNowKey", EXTRACT(YEAR FROM fshi."CreatedOn"), EXTRACT(MONTH FROM fshi."CreatedOn") ORDER BY fshi."CreatedOn" DESC) AS "rn"
FROM public."FileSystemHistoryItem" AS fshi
JOIN public."FileSystemItem" AS fsi ON fshi."ServiceNowKey" = fsi."ServiceNowKey"
JOIN public."ServerItem" AS si ON fsi."ServerItemServiceNowKey" = si."ServiceNowKey"
WHERE fshi."CreatedOn" >= $1
AND fshi."CreatedOn" < last_month_start
AND ($3 IS NULL OR si."TenantId" = $3)
AND ($4 IS NULL OR si."OrganizationId" = $4)
AND ($5 IS NULL OR si."OperatingSystemItemId" = $5)
AND ($6 IS NULL OR fshi."ServerItemServiceNowKey" = $6)
UNION
SELECT fshi.*
, ROW_NUMBER() OVER (PARTITION BY fshi."ServiceNowKey", EXTRACT(YEAR FROM fshi."CreatedOn"), EXTRACT(MONTH FROM fshi."CreatedOn") ORDER BY fshi."CreatedOn" DESC) AS "rn"
FROM public."FileSystemHistoryItem" AS fshi
JOIN public."FileSystemItem" AS fsi ON fshi."ServiceNowKey" = fsi."ServiceNowKey"
JOIN public."ServerItem" AS si ON fsi."ServerItemServiceNowKey" = si."ServiceNowKey"
WHERE fshi."InstallStatus" = 1
AND fshi."CreatedOn" >= last_month_start
AND ($2 IS NULL OR fshi."CreatedOn" <= $2)
AND ($3 IS NULL OR si."TenantId" = $3)
AND ($4 IS NULL OR si."OrganizationId" = $4)
AND ($5 IS NULL OR si."OperatingSystemItemId" = $5)
AND ($6 IS NULL OR fshi."ServerItemServiceNowKey" = $6)
) AS "sub"
WHERE "rn" = 1
ORDER BY "ServiceNowKey", "CreatedOn";
END;$$
Loading

0 comments on commit 59c54ce

Please sign in to comment.