From a96d26e0c2a19c995920e30e7272ded4c18015eb Mon Sep 17 00:00:00 2001 From: HenningNormann Date: Tue, 8 Oct 2024 14:50:35 +0200 Subject: [PATCH] - Made calls to npgsql params async (#519) - Div other sonarcloud suggestions Co-authored-by: Henning Normann --- .../Controllers/ContentOnDemandController.cs | 34 ++++++------ src/Storage/Repository/PgA2Repository.cs | 52 +++++++------------ .../Repository/PgApplicationRepository.cs | 6 +-- src/Storage/Repository/PgDataRepository.cs | 6 +-- .../Repository/PgInstanceEventRepository.cs | 4 +- .../Repository/PgInstanceRepository.cs | 22 ++++---- src/Storage/Repository/PgTextRepository.cs | 4 +- 7 files changed, 56 insertions(+), 72 deletions(-) diff --git a/src/Storage/Controllers/ContentOnDemandController.cs b/src/Storage/Controllers/ContentOnDemandController.cs index 683363ba..91b573c3 100644 --- a/src/Storage/Controllers/ContentOnDemandController.cs +++ b/src/Storage/Controllers/ContentOnDemandController.cs @@ -148,28 +148,24 @@ public async Task GetFormdataAsPdf([FromRoute] string org, [FromRoute] s if (xsls.Count > 1 && xsls.Exists(x => x.IsPortrait) && xsls.Exists(x => !x.IsPortrait)) { // Mix of portrait and landscape, we must generate each page and merge them - using (var mergedDoc = new PdfDocument()) + using var mergedDoc = new PdfDocument(); + foreach (var xsl in xsls) { - foreach (var xsl in xsls) + var pdfPages = await _pdfGeneratorClient.GeneratePdf( + $"{Request.Scheme}://{Request.Host}{Request.PathBase}{Request.Path}" + .Replace("formdatapdf", $"formdatahtml/{xsl.PageNumber}"), + xsl.IsPortrait); + using var pageDoc = PdfReader.Open(pdfPages, PdfDocumentOpenMode.Import); + for (var i = 0; i < pageDoc.PageCount; i++) { - var pdfPages = await _pdfGeneratorClient.GeneratePdf( - $"{Request.Scheme}://{Request.Host}{Request.PathBase}{Request.Path}" - .Replace("formdatapdf", $"formdatahtml/{xsl.PageNumber}"), - xsl.IsPortrait); - using (var pageDoc = PdfReader.Open(pdfPages, PdfDocumentOpenMode.Import)) - { - for (var i = 0; i < pageDoc.PageCount; i++) - { - pageDoc.Pages[i].Orientation = xsl.IsPortrait ? PdfSharp.PageOrientation.Portrait : PdfSharp.PageOrientation.Landscape; - mergedDoc.AddPage(pageDoc.Pages[i]); - } - } + pageDoc.Pages[i].Orientation = xsl.IsPortrait ? PdfSharp.PageOrientation.Portrait : PdfSharp.PageOrientation.Landscape; + mergedDoc.AddPage(pageDoc.Pages[i]); } - - MemoryStream mergedPdf = new MemoryStream(); - mergedDoc.Save(mergedPdf); - return mergedPdf; } + + MemoryStream mergedPdf = new(); + mergedDoc.Save(mergedPdf); + return mergedPdf; } else { @@ -287,7 +283,7 @@ public class SignatureView /// Gets or sets whether this signing is done for all the items in the form /// 1=Group signing is set, 2=Group signing not set, 3=Group signing defined at each form level /// - ////public int IsSigningAllRequired { get; set; } + public int IsSigningAllRequired { get; set; } /// /// Gets or sets The authentication level attached with that signature. diff --git a/src/Storage/Repository/PgA2Repository.cs b/src/Storage/Repository/PgA2Repository.cs index 58953e98..a849345b 100644 --- a/src/Storage/Repository/PgA2Repository.cs +++ b/src/Storage/Repository/PgA2Repository.cs @@ -15,7 +15,14 @@ namespace Altinn.Platform.Storage.Repository /// /// Handles a2 repository. /// - public class PgA2Repository : IA2Repository + /// + /// Initializes a new instance of the class. + /// + /// The npgsql data source. + /// Telemetry client + public class PgA2Repository( + NpgsqlDataSource dataSource, + TelemetryClient telemetryClient = null) : IA2Repository { private static readonly string _readXslSql = "select * from storage.reada2xsls (@_org, @_app, @_lformid, @_language, @_xsltype)"; private static readonly string _insertXslSql = "call storage.inserta2xsl (@_org, @_app, @_lformid, @_language, @_pagenumber, @_xsl, @_xsltype, @_isportrait)"; @@ -30,27 +37,8 @@ public class PgA2Repository : IA2Repository private static readonly string _updateMigrationStateCompletedSql = "call storage.updatea2migrationstatecompleted (@_instanceguid)"; private static readonly string _deleteMigrationStateSql = "call storage.deletea2migrationstate (@_instanceguid)"; - private readonly NpgsqlDataSource _dataSource; - private readonly TelemetryClient _telemetryClient; - private readonly ILogger _logger; - - /// - /// Initializes a new instance of the class. - /// - /// the general settings - /// The npgsql data source. - /// Logger - /// Telemetry client - public PgA2Repository( - IOptions generalSettings, - NpgsqlDataSource dataSource, - ILogger logger, - TelemetryClient telemetryClient = null) - { - _dataSource = dataSource; - _telemetryClient = telemetryClient; - _logger = logger; - } + private readonly NpgsqlDataSource _dataSource = dataSource; + private readonly TelemetryClient _telemetryClient = telemetryClient; /// public async Task CreateXsl(string org, string app, int lformId, string language, int pageNumber, string xsl, int xslType, bool isPortrait) @@ -120,7 +108,7 @@ public async Task CreateImage(string name, byte[] image) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); while (await reader.ReadAsync()) { - xsls.Add((reader.GetFieldValue("xsl"), reader.GetFieldValue("isportrait"))); + xsls.Add((await reader.GetFieldValueAsync("xsl"), await reader.GetFieldValueAsync("isportrait"))); } tracker.Track(); @@ -145,7 +133,7 @@ public async Task GetImage(string name) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - image = reader.GetFieldValue("image"); + image = await reader.GetFieldValueAsync("image"); } tracker.Track(); @@ -169,7 +157,7 @@ public async Task GetCodelist(string name, string preferredLanguage) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - codelist = reader.GetFieldValue("codelist"); + codelist = await reader.GetFieldValueAsync("codelist"); } tracker.Track(); @@ -238,7 +226,7 @@ public async Task GetMigrationInstanceId(int a2ArchiveReference) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - instanceId = reader.IsDBNull("instanceguid") ? null : reader.GetFieldValue("instanceguid").ToString(); + instanceId = await reader.IsDBNullAsync("instanceguid") ? null : (await reader.GetFieldValueAsync("instanceguid")).ToString(); } tracker.Track(); @@ -247,13 +235,13 @@ public async Task GetMigrationInstanceId(int a2ArchiveReference) private static List GetOrderedLanguages(string language) { - switch (language) + return language switch { - case "nb": return new List { "nb", "nn", "en" }; - case "nn": return new List { "nn", "nb", "en" }; - case "en": return new List { "en", "nb", "nn" }; - default: return new List { "nb", "nn", "en" }; - } + "nb" => ["nb", "nn", "en"], + "nn" => ["nn", "nb", "en"], + "en" => ["en", "nb", "nn"], + _ => ["nb", "nn", "en"], + }; } } } diff --git a/src/Storage/Repository/PgApplicationRepository.cs b/src/Storage/Repository/PgApplicationRepository.cs index 5d84d4a3..fae5cfe9 100644 --- a/src/Storage/Repository/PgApplicationRepository.cs +++ b/src/Storage/Repository/PgApplicationRepository.cs @@ -70,7 +70,7 @@ public async Task> FindAll() await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); while (await reader.ReadAsync()) { - applications.Add(reader.GetFieldValue("application")); + applications.Add(await reader.GetFieldValueAsync("application")); } tracker.Track(); @@ -88,7 +88,7 @@ public async Task> FindByOrg(string org) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); while (await reader.ReadAsync()) { - applications.Add(reader.GetFieldValue("application")); + applications.Add(await reader.GetFieldValueAsync("application")); } tracker.Track(); @@ -108,7 +108,7 @@ public async Task FindOne(string appId, string org) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - application = reader.GetFieldValue("application"); + application = await reader.GetFieldValueAsync("application"); _memoryCache.Set(cacheKey, application, _cacheEntryOptionsMetadata); } else diff --git a/src/Storage/Repository/PgDataRepository.cs b/src/Storage/Repository/PgDataRepository.cs index 4ac0c5b2..16c50563 100644 --- a/src/Storage/Repository/PgDataRepository.cs +++ b/src/Storage/Repository/PgDataRepository.cs @@ -52,7 +52,7 @@ public async Task Create(DataElement dataElement, long instanceInte await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - dataElement = reader.GetFieldValue("updatedElement"); + dataElement = await reader.GetFieldValueAsync("updatedElement"); } tracker.Track(); @@ -104,7 +104,7 @@ public async Task Read(Guid instanceGuid, Guid dataElementId) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - dataElement = reader.GetFieldValue("element"); + dataElement = await reader.GetFieldValueAsync("element"); } tracker.Track(); @@ -168,7 +168,7 @@ public async Task Update(Guid instanceGuid, Guid dataElementId, Dic await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - element = reader.GetFieldValue("updatedElement"); + element = await reader.GetFieldValueAsync("updatedElement"); } tracker.Track(); diff --git a/src/Storage/Repository/PgInstanceEventRepository.cs b/src/Storage/Repository/PgInstanceEventRepository.cs index 765dc70d..76fc421b 100644 --- a/src/Storage/Repository/PgInstanceEventRepository.cs +++ b/src/Storage/Repository/PgInstanceEventRepository.cs @@ -62,7 +62,7 @@ public async Task GetOneEvent(string instanceId, Guid eventGuid) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - instanceEvent = reader.GetFieldValue("event"); + instanceEvent = await reader.GetFieldValueAsync("event"); } tracker.Track(); @@ -88,7 +88,7 @@ public async Task> ListInstanceEvents( { while (await reader.ReadAsync()) { - events.Add(reader.GetFieldValue("event")); + events.Add(await reader.GetFieldValueAsync("event")); } } diff --git a/src/Storage/Repository/PgInstanceRepository.cs b/src/Storage/Repository/PgInstanceRepository.cs index c4af8314..1cedf7de 100644 --- a/src/Storage/Repository/PgInstanceRepository.cs +++ b/src/Storage/Repository/PgInstanceRepository.cs @@ -127,7 +127,7 @@ public async Task> GetHardDeletedInstances() { while (await reader.ReadAsync()) { - Instance i = reader.GetFieldValue("instance"); + Instance i = await reader.GetFieldValueAsync("instance"); if ((i.CompleteConfirmations != null && i.CompleteConfirmations.Exists(c => c.StakeholderId.ToLower().Equals(i.Org) && c.ConfirmedOn <= DateTime.UtcNow.AddDays(-7))) || !i.Status.IsArchived) { @@ -154,10 +154,10 @@ public async Task> GetHardDeletedDataElements() bool currentInstanceAllowsDelete = false; while (await reader.ReadAsync()) { - id = reader.GetFieldValue("id"); + id = await reader.GetFieldValueAsync("id"); if (id != previousId) { - Instance instance = reader.GetFieldValue("instance"); + Instance instance = await reader.GetFieldValueAsync("instance"); currentInstanceAllowsDelete = instance.CompleteConfirmations != null && instance.CompleteConfirmations.Exists(c => c.StakeholderId.Equals(instance.Org, StringComparison.OrdinalIgnoreCase) && @@ -167,7 +167,7 @@ public async Task> GetHardDeletedDataElements() if (currentInstanceAllowsDelete) { - elements.Add(reader.GetFieldValue("element")); + elements.Add(await reader.GetFieldValueAsync("element")); } } @@ -278,7 +278,7 @@ private async Task GetInstancesInternal( Instance instance = new(); // make sonarcloud happy while (await reader.ReadAsync()) { - id = reader.GetFieldValue("id"); + id = await reader.GetFieldValueAsync("id"); if (id != previousId) { if (previousId != -1) @@ -286,7 +286,7 @@ private async Task GetInstancesInternal( ToExternal(instance); } - instance = reader.GetFieldValue("instance"); + instance = await reader.GetFieldValueAsync("instance"); lastChanged = instance.LastChanged ?? DateTime.MinValue; queryResponse.Instances.Add(instance); instance.Data = []; @@ -295,7 +295,7 @@ private async Task GetInstancesInternal( if (!reader.IsDBNull("element")) { - instance.Data.Add(reader.GetFieldValue("element")); + instance.Data.Add(await reader.GetFieldValueAsync("element")); } } @@ -337,13 +337,13 @@ private async Task GetInstancesInternal( if (!instanceCreated) { instanceCreated = true; - instance = reader.GetFieldValue("instance"); - instanceInternalId = reader.GetFieldValue("id"); + instance = await reader.GetFieldValueAsync("instance"); + instanceInternalId = await reader.GetFieldValueAsync("id"); } if (includeElements && !reader.IsDBNull("element")) { - instanceData.Add(reader.GetFieldValue("element")); + instanceData.Add(await reader.GetFieldValueAsync("element")); } } @@ -387,7 +387,7 @@ public async Task Update(Instance instance, List updatePropert await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - instance = reader.GetFieldValue("updatedInstance"); + instance = await reader.GetFieldValueAsync("updatedInstance"); } instance.Data = dataElements; diff --git a/src/Storage/Repository/PgTextRepository.cs b/src/Storage/Repository/PgTextRepository.cs index ba3ea165..0a062798 100644 --- a/src/Storage/Repository/PgTextRepository.cs +++ b/src/Storage/Repository/PgTextRepository.cs @@ -75,7 +75,7 @@ public async Task Get(string org, string app, string language) await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - textResource = PostProcess(org, app, language, reader.GetFieldValue("textResource")); + textResource = PostProcess(org, app, language, await reader.GetFieldValueAsync("textResource")); } else { @@ -129,7 +129,7 @@ public async Task Create(string org, string app, TextResource text await using NpgsqlDataReader reader = await pgcomReadApp.ExecuteReaderAsync(); if (await reader.ReadAsync()) { - applicationInternalId = reader.GetFieldValue("id"); + applicationInternalId = await reader.GetFieldValueAsync("id"); } else {