Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOSTSD-240 Keep removed servers #126

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public IActionResult GetForId(string id)
[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)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 1200)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disable caching for now.

public IActionResult FindHistory()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public OperatingSystemItemController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OperatingSystemItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Operating System Item" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -113,7 +113,7 @@ public IActionResult Find()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OperatingSystemItemListModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Operating System Item" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult FindList()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down
4 changes: 2 additions & 2 deletions src/api/Areas/Dashboard/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public OrganizationController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OrganizationModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Organization" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -110,7 +110,7 @@ public IActionResult Find()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OrganizationListModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Organization" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult FindList()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down
6 changes: 3 additions & 3 deletions src/api/Areas/Dashboard/Controllers/ServerItemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ServerItemController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ServerItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -117,7 +117,7 @@ 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)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult FindList()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -190,7 +190,7 @@ public IActionResult GetForId(string serviceNowKey, bool includeFileSystemItems
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ServerItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 1200)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 1200)]
public IActionResult FindHistory()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down
4 changes: 2 additions & 2 deletions src/api/Areas/Dashboard/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public TenantController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<TenantModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Tenant" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down Expand Up @@ -109,7 +109,7 @@ public IActionResult Find()
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<TenantListModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Tenant" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
// [ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult FindList()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down
30 changes: 20 additions & 10 deletions src/data-service/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,14 @@ private async Task ProcessConfigurationItemsAsync(Models.DataSyncModel option)
// Need to update with the latest status.
if (_serverItems.TryGetValue(serviceNowKey, out Hsb.ServerItemModel? serverItemHSB))
{
await this.HsbApi.DeleteServerItemAsync(serverItemHSB);
_serverItems.Remove(serverItemHSB.ServiceNowKey);
serverItemHSB.InstallStatus = Int32.Parse(serverItemSN.Data.InstallStatus ?? "0");
serverItemHSB = await this.HsbApi.UpdateServerItemAsync(serverItemHSB);
if (serverItemHSB == null)
{
this.Logger.LogError("Server Item was not returned from HSB: {id}", serviceNowKey);
throw new InvalidOperationException($"Server Item was not returned from HSB: {serviceNowKey}");
}
_serverItems[serverItemHSB.ServiceNowKey] = serverItemHSB;
}

return null;
Expand Down Expand Up @@ -443,8 +449,8 @@ private async Task ProcessConfigurationItemsAsync(Models.DataSyncModel option)
}
else
{
// Delete the file system as it is no longer installed.
await this.HsbApi.DeleteFileSystemItemAsync(fileSystemItem);
fileSystemItem.InstallStatus = Int32.Parse(fileSystemItemSN.Data.InstallStatus ?? "0");
await this.HsbApi.UpdateFileSystemItemAsync(fileSystemItem);
return null;
}
}
Expand All @@ -458,8 +464,8 @@ private async Task ProcessConfigurationItemsAsync(Models.DataSyncModel option)
}
else
{
// Delete the file system as it is no longer required.
await this.HsbApi.DeleteFileSystemItemAsync(fileSystemItem);
fileSystemItem.InstallStatus = 0;
await this.HsbApi.UpdateFileSystemItemAsync(fileSystemItem);
return null;
}
}
Expand Down Expand Up @@ -512,8 +518,9 @@ private async Task ProcessConfigurationItemsAsync(Models.DataSyncModel option)
// Service Now has changed the primary key for some reason.
this.Logger.LogDebug("Replacing File System Item: '{old}:{new}'", fileSystemItem.ServiceNowKey, configurationItemSN.Data.Id);

// Delete the current one and replace it with the new one.
await this.HsbApi.DeleteFileSystemItemAsync(fileSystemItem);
// Update the current one and replace it with the new one.
fileSystemItem.InstallStatus = 0;
await this.HsbApi.UpdateFileSystemItemAsync(fileSystemItem);
fileSystemItem = await this.HsbApi.AddFileSystemItemAsync(new Hsb.FileSystemItemModel(serverItem.ServiceNowKey, fileSystemItemSN, configurationItemSN));
}
else if (fileSystemItem.UpdatedOn.AddHours(this.Options.AllowUpdateAfterXHours).ToUniversalTime() <= DateTime.UtcNow)
Expand Down Expand Up @@ -862,7 +869,9 @@ private async Task ServerItemCleanupProcessAsync()
var serverItemSN = await this.ServiceNowApi.GetTableItemAsync<ServiceNow.BaseItemModel>(serverItem.ClassName, serverItem.ServiceNowKey);
if (serverItemSN?.Data == null)
{
await this.HsbApi.DeleteServerItemAsync(serverItem);
// Service Now did not return the server, we will change the install status to 0.
serverItem.InstallStatus = 0;
await this.HsbApi.UpdateServerItemAsync(serverItem);
}
else
{
Expand All @@ -888,7 +897,8 @@ private async Task ServerItemCleanupProcessAsync()
{
if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await this.HsbApi.DeleteServerItemAsync(serverItem);
serverItem.InstallStatus = 0;
await this.HsbApi.UpdateServerItemAsync(serverItem);
}
this.Logger.LogError(ex, "Failed to fetch server item: {key} - {data}", serverItem.ServiceNowKey, ex.Data["Body"]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 $$
BEGIN
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") 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" >= $1
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;$$
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
CREATE OR REPLACE FUNCTION "FindFileSystemHistoryItemsByMonthForUser"(
"userId" INT
, "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 $$
BEGIN
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") 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" >= $2
AND ($3 IS NULL OR fshi."CreatedOn" <= $3)
AND ($4 IS NULL OR si."TenantId" = $4)
AND ($5 IS NULL OR si."OrganizationId" = $5)
AND ($6 IS NULL OR si."OperatingSystemItemId" = $6)
AND ($7 IS NULL OR fshi."ServerItemServiceNowKey" = $7)
AND (si."TenantId" IN (SELECT "TenantId" FROM public."UserTenant" WHERE "UserId" = $1)
OR si."OrganizationId" IN (SELECT "OrganizationId" FROM public."UserOrganization" WHERE "UserId" = $1))
) AS "sub"
WHERE "rn" = 1
ORDER BY "ServiceNowKey", "CreatedOn";
END;$$
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
CREATE OR REPLACE FUNCTION "FindServerHistoryItemsByMonth"(
"startDate" TIMESTAMPTZ
, "endDate" TIMESTAMPTZ DEFAULT NULL
, "tenantId" INT DEFAULT NULL
, "organizationId" INT DEFAULT NULL
, "operatingSystemItemId" INT DEFAULT NULL
, "serviceNowKey" VARCHAR(200) DEFAULT NULL
)
RETURNS SETOF public."ServerHistoryItem"
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT DISTINCT
"Id"
, "TenantId"
, "OrganizationId"
, "OperatingSystemItemId"
, "ServiceNowKey"
, "HistoryKey"
, "RawData"
, "RawDataCI"
, "ClassName"
, "Name"
, "Category"
, "Subcategory"
, "DnsDomain"
, "Platform"
, "IPAddress"
, "FQDN"
, "DiskSpace"
, "Capacity"
, "AvailableSpace"
, "CreatedOn"
, "CreatedBy"
, "UpdatedOn"
, "UpdatedBy"
, "Version"
, "InstallStatus"
FROM (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY "ServiceNowKey", EXTRACT(YEAR FROM "CreatedOn"), EXTRACT(MONTH FROM "CreatedOn") ORDER BY "CreatedOn") AS "rn"
FROM public."ServerHistoryItem"
WHERE "InstallStatus" = 1
AND "CreatedOn" >= $1
AND ($2 IS NULL OR "CreatedOn" <= $2)
AND ($3 IS NULL OR "TenantId" = $3)
AND ($4 IS NULL OR "OrganizationId" = $4)
AND ($5 IS NULL OR "OperatingSystemItemId" = $5)
AND ($6 IS NULL OR "ServiceNowKey" = $6)
) AS "sub"
WHERE "rn" = 1
ORDER BY "ServiceNowKey", "CreatedOn";
END;$$

-- Use by calling
-- select * from public."FindServerHistoryItemsByMonth"('2023-12-01');
Loading
Loading