Skip to content

Commit 8b7fc7f

Browse files
committed
gh-65 fix warnings reported by sonarqube
Signed-off-by: Victor Chang <[email protected]>
1 parent 9e1cc0f commit 8b7fc7f

File tree

15 files changed

+233
-140
lines changed

15 files changed

+233
-140
lines changed

.editorconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
root = true
55

66
# Copyright File Header
7-
file_header_template = Copyright [year file created] - [last year file modified], MONAI Consortium\nSPDX-License-Identifier: Apache License 2.0
7+
file_header_template = SPDX-FileCopyrightText: © [year file created] - [last year file modified], MONAI Consortium\nSPDX-License-Identifier: Apache License 2.0
88
dotnet_diagnostic.IDE0073.severity = error
99

10-
1110
# Default settings:
1211
# A newline ending every file
1312
# Use 4 spaces as indentation

src/Api/MessageBroker/JsonMessage.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace Monai.Deploy.InformaticsGateway.Api.MessageBroker
99
{
1010
public sealed class JsonMessage<T> : MessageBase
1111
{
12-
public static readonly string JsonApplicationType = "application/json";
13-
1412
/// <summary>
1513
/// Body of the message.
1614
/// </summary>
@@ -65,7 +63,7 @@ public JsonMessage(T body,
6563
MessageDescription = bodyDescription;
6664
MessageId = messageId;
6765
ApplicationId = applicationId;
68-
ContentType = JsonApplicationType;
66+
ContentType = MessageContentTypes.JsonApplicationType;
6967
CorrelationId = correlationId;
7068
CreationDateTime = creationDateTime;
7169
DeliveryTag = deliveryTag;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: © 2021-2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
namespace Monai.Deploy.InformaticsGateway.Api.MessageBroker
5+
{
6+
public static class MessageContentTypes
7+
{
8+
public static readonly string JsonApplicationType = "application/json";
9+
}
10+
}

src/Api/Rest/InferenceRequest.cs

Lines changed: 93 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,23 @@ private bool Validate(out string details)
215215
errors.Add("'transactionId' is required.");
216216
}
217217

218-
if (InputResources.IsNullOrEmpty() ||
219-
!InputResources.Any(predicate => predicate.Interface != InputInterfaceType.Algorithm))
220-
{
221-
errors.Add("No 'inputResources' specified.");
222-
}
223-
224218
if (Application is null)
225219
{
226220
errors.Add("No algorithm defined or more than one algorithms defined in 'inputResources'. 'inputResources' must include one algorithm/pipeline for the inference request.");
227221
}
228222

223+
ValidateInputResources(errors);
224+
ValidateInputMetadata(errors);
225+
ValidateOUtputResources(errors);
226+
227+
details = string.Join(" ", errors);
228+
return errors.Count == 0;
229+
}
230+
231+
private void ValidateOUtputResources(List<string> errors)
232+
{
233+
Guard.Against.Null(errors, nameof(errors));
234+
229235
if (InputMetadata.Inputs.IsNullOrEmpty())
230236
{
231237
errors.Add("Request has no `inputMetadata` defined. At least one `inputs` or `inputMetadata` required.");
@@ -237,18 +243,11 @@ private bool Validate(out string details)
237243
CheckInputMetadataDetails(inputDetails, errors);
238244
}
239245
}
246+
}
240247

241-
foreach (var input in InputResources)
242-
{
243-
if (input.Interface == InputInterfaceType.DicomWeb)
244-
{
245-
CheckDicomWebConnectionDetails("inputResources", errors, input.ConnectionDetails);
246-
}
247-
else if (input.Interface == InputInterfaceType.Fhir)
248-
{
249-
CheckFhirConnectionDetails("inputResources", errors, input.ConnectionDetails);
250-
}
251-
}
248+
private void ValidateInputMetadata(List<string> errors)
249+
{
250+
Guard.Against.Null(errors, nameof(errors));
252251

253252
foreach (var output in OutputResources)
254253
{
@@ -261,51 +260,37 @@ private bool Validate(out string details)
261260
CheckFhirConnectionDetails("outputResources", errors, output.ConnectionDetails);
262261
}
263262
}
263+
}
264264

265-
details = string.Join(" ", errors);
266-
return errors.Count == 0;
265+
private void ValidateInputResources(List<string> errors)
266+
{
267+
Guard.Against.Null(errors, nameof(errors));
268+
269+
if (InputResources.IsNullOrEmpty() ||
270+
!InputResources.Any(predicate => predicate.Interface != InputInterfaceType.Algorithm))
271+
{
272+
errors.Add("No 'inputResources' specified.");
273+
}
274+
275+
foreach (var input in InputResources)
276+
{
277+
if (input.Interface == InputInterfaceType.DicomWeb)
278+
{
279+
CheckDicomWebConnectionDetails("inputResources", errors, input.ConnectionDetails);
280+
}
281+
else if (input.Interface == InputInterfaceType.Fhir)
282+
{
283+
CheckFhirConnectionDetails("inputResources", errors, input.ConnectionDetails);
284+
}
285+
}
267286
}
268287

269288
private void CheckInputMetadataDetails(InferenceRequestDetails details, List<string> errors)
270289
{
271290
switch (details.Type)
272291
{
273292
case InferenceRequestType.DicomUid:
274-
if (details.Studies.IsNullOrEmpty())
275-
{
276-
errors.Add("Request type is set to `DICOM_UID` but no `studies` defined.");
277-
}
278-
else
279-
{
280-
foreach (var study in details.Studies)
281-
{
282-
if (string.IsNullOrWhiteSpace(study.StudyInstanceUid))
283-
{
284-
errors.Add("`StudyInstanceUID` cannot be empty.");
285-
}
286-
287-
if (study.Series is null) continue;
288-
289-
foreach (var series in study.Series)
290-
{
291-
if (string.IsNullOrWhiteSpace(series.SeriesInstanceUid))
292-
{
293-
errors.Add("`SeriesInstanceUID` cannot be empty.");
294-
}
295-
296-
if (series.Instances is null) continue;
297-
298-
foreach (var instance in series.Instances)
299-
{
300-
if (instance.SopInstanceUid.IsNullOrEmpty() ||
301-
instance.SopInstanceUid.Any(p => string.IsNullOrWhiteSpace(p)))
302-
{
303-
errors.Add("`SOPInstanceUID` cannot be empty.");
304-
}
305-
}
306-
}
307-
}
308-
}
293+
CheckInputMetadataWithTypDeicomUid(details, errors);
309294
break;
310295

311296
case InferenceRequestType.DicomPatientId:
@@ -323,25 +308,66 @@ private void CheckInputMetadataDetails(InferenceRequestDetails details, List<str
323308
break;
324309

325310
case InferenceRequestType.FhireResource:
326-
if (details.Resources.IsNullOrEmpty())
311+
CheckInputMetadataWithTypeFhirResource(details, errors);
312+
break;
313+
314+
default:
315+
errors.Add($"'inputMetadata' does not yet support type '{details.Type}'.");
316+
break;
317+
}
318+
}
319+
320+
private static void CheckInputMetadataWithTypeFhirResource(InferenceRequestDetails details, List<string> errors)
321+
{
322+
Guard.Against.Null(details, nameof(details));
323+
Guard.Against.Null(errors, nameof(errors));
324+
325+
if (details.Resources.IsNullOrEmpty())
326+
{
327+
errors.Add("Request type is set to `FHIR_RESOURCE` but no FHIR `resources` defined.");
328+
}
329+
else
330+
{
331+
errors.AddRange(details.Resources.Where(resource => string.IsNullOrWhiteSpace(resource.Type)).Select(resource => "A FHIR resource type cannot be empty."));
332+
}
333+
}
334+
335+
private static void CheckInputMetadataWithTypDeicomUid(InferenceRequestDetails details, List<string> errors)
336+
{
337+
Guard.Against.Null(details, nameof(details));
338+
Guard.Against.Null(errors, nameof(errors));
339+
340+
if (details.Studies.IsNullOrEmpty())
341+
{
342+
errors.Add("Request type is set to `DICOM_UID` but no `studies` defined.");
343+
}
344+
else
345+
{
346+
foreach (var study in details.Studies)
347+
{
348+
if (string.IsNullOrWhiteSpace(study.StudyInstanceUid))
327349
{
328-
errors.Add("Request type is set to `FHIR_RESOURCE` but no FHIR `resources` defined.");
350+
errors.Add("`StudyInstanceUID` cannot be empty.");
329351
}
330-
else
352+
353+
if (study.Series is null) continue;
354+
355+
foreach (var series in study.Series)
331356
{
332-
foreach (var resource in details.Resources)
357+
if (string.IsNullOrWhiteSpace(series.SeriesInstanceUid))
333358
{
334-
if (string.IsNullOrWhiteSpace(resource.Type))
335-
{
336-
errors.Add("A FHIR resource type cannot be empty.");
337-
}
359+
errors.Add("`SeriesInstanceUID` cannot be empty.");
338360
}
339-
}
340-
break;
341361

342-
default:
343-
errors.Add($"'inputMetadata' does not yet support type '{details.Type}'.");
344-
break;
362+
if (series.Instances is null) continue;
363+
errors.AddRange(
364+
series.Instances
365+
.Where(
366+
instance => instance.SopInstanceUid.IsNullOrEmpty() ||
367+
instance.SopInstanceUid.Any(p => string.IsNullOrWhiteSpace(p)))
368+
.Select(instance => "`SOPInstanceUID` cannot be empty."));
369+
}
370+
}
345371
}
346372
}
347373

src/CLI/Commands/ConfigCommand.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void AddCommandRunner()
3232

3333
endpointCommand.AddArgument(new Argument<Runner>("runner"));
3434
endpointCommand.Handler = CommandHandler.Create<Runner, IHost, bool>((Runner runner, IHost host, bool verbose) =>
35-
ConfigUpdateHandler(runner, host, verbose, (IConfigurationService options) =>
35+
ConfigUpdateHandler(host, (IConfigurationService options) =>
3636
{
3737
options.Configurations.Runner = runner;
3838
})
@@ -46,10 +46,10 @@ private void AddCommandEndpoint()
4646

4747
endpointCommand.AddArgument(new Argument<string>("uri"));
4848
endpointCommand.Handler = CommandHandler.Create<string, IHost, bool>((string uri, IHost host, bool verbose) =>
49-
ConfigUpdateHandler(uri, host, verbose, (IConfigurationService options) =>
50-
{
51-
options.Configurations.InformaticsGatewayServerEndpoint = uri;
52-
})
49+
ConfigUpdateHandler(host, (IConfigurationService options) =>
50+
{
51+
options.Configurations.InformaticsGatewayServerEndpoint = uri;
52+
})
5353
);
5454
}
5555

@@ -102,7 +102,7 @@ private int ShowConfigurationHandler(IHost host, bool verbose, CancellationToken
102102
return ExitCodes.Success;
103103
}
104104

105-
private static int ConfigUpdateHandler<T>(T argument, IHost host, bool verbose, Action<IConfigurationService> updater)
105+
private static int ConfigUpdateHandler(IHost host, Action<IConfigurationService> updater)
106106
{
107107
Guard.Against.Null(host, nameof(host));
108108
Guard.Against.Null(updater, nameof(updater));
@@ -140,13 +140,10 @@ private async Task<int> InitHandlerAsync(IHost host, bool verbose, bool yes, Can
140140
Guard.Against.Null(configService, nameof(configService), "Configuration service is unavailable.");
141141
Guard.Against.Null(confirmation, nameof(confirmation), "Confirmation prompt is unavailable.");
142142

143-
if (!yes)
143+
if (!yes && configService.IsConfigExists && !confirmation.ShowConfirmationPrompt($"Existing application configuration file already exists. Do you want to overwrite it?"))
144144
{
145-
if (configService.IsConfigExists && !confirmation.ShowConfirmationPrompt($"Existing application configuration file already exists. Do you want to overwrite it?"))
146-
{
147-
logger.Log(LogLevel.Warning, "Action cancelled.");
148-
return ExitCodes.Stop_Cancelled;
149-
}
145+
logger.Log(LogLevel.Warning, "Action cancelled.");
146+
return ExitCodes.Stop_Cancelled;
150147
}
151148

152149
try

src/CLI/Commands/RestartCommand.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ private async Task<int> RestartCommandHandler(IHost host, bool yes, bool verbose
3333
Guard.Against.Null(service, nameof(service), "Control service is unavailable.");
3434
Guard.Against.Null(logger, nameof(logger), "Logger is unavailable.");
3535

36-
if (!yes)
36+
if (!yes && !confirmation.ShowConfirmationPrompt($"Do you want to restart {Strings.ApplicationName}?"))
3737
{
38-
if (!confirmation.ShowConfirmationPrompt($"Do you want to restart {Strings.ApplicationName}?"))
39-
{
40-
logger.Log(LogLevel.Warning, "Action cancelled.");
41-
return ExitCodes.Restart_Cancelled;
42-
}
38+
logger.Log(LogLevel.Warning, "Action cancelled.");
39+
return ExitCodes.Restart_Cancelled;
4340
}
4441

4542
try

src/CLI/Commands/StopCommand.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ private async Task<int> StopCommandHandler(IHost host, bool yes, bool verbose, C
3333
Guard.Against.Null(confirmation, nameof(confirmation), "Confirmation prompt is unavailable.");
3434
Guard.Against.Null(logger, nameof(logger), "Logger is unavailable.");
3535

36-
if (!yes)
36+
if (!yes && !confirmation.ShowConfirmationPrompt($"Do you want to stop {Strings.ApplicationName}?"))
3737
{
38-
if (!confirmation.ShowConfirmationPrompt($"Do you want to stop {Strings.ApplicationName}?"))
39-
{
40-
logger.Log(LogLevel.Warning, "Action cancelled.");
41-
return ExitCodes.Stop_Cancelled;
42-
}
38+
logger.Log(LogLevel.Warning, "Action cancelled.");
39+
return ExitCodes.Stop_Cancelled;
4340
}
4441

4542
try

src/InformaticsGateway/Common/Unsubscriber.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ protected virtual void Dispose(bool disposing)
2727
{
2828
if (!_disposedValue)
2929
{
30-
if (disposing)
30+
if (disposing && _observers.Contains(_observer))
3131
{
32-
if (_observers.Contains(_observer))
33-
{
34-
_observers.Remove(_observer);
35-
}
32+
_observers.Remove(_observer);
3633
}
3734
_disposedValue = true;
3835
}

0 commit comments

Comments
 (0)