Skip to content

Commit 5dfd5d1

Browse files
authored
refactor: add feature management & new featureFlag (#924)
* Add FeatureManagement & fix typos in flags * use features in ApprovalController.cs * Make DemoMode into feature flag * seed data if demo mode is on * Use FeatureFlag instead of Settings for DemoAllInOne
1 parent 3efcb43 commit 5dfd5d1

File tree

9 files changed

+251
-297
lines changed

9 files changed

+251
-297
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace TRE_API.Constants;
2+
3+
public static class FeatureFlags
4+
{
5+
public const string GenerateAccounts = nameof(GenerateAccounts);
6+
7+
public const string SqlAndNotGraphQl = nameof(SqlAndNotGraphQl);
8+
9+
public const string DemoAllInOne = nameof(DemoAllInOne);
10+
}

src/TRE-API/Controllers/ApprovalController.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
using BL.Models.Enums;
66
using Serilog;
77
using BL.Services;
8+
using Microsoft.FeatureManagement;
9+
using TRE_API.Constants;
810
using TRE_API.Repositories.DbContexts;
911
using TRE_API.Services;
10-
using TRE_API.Models;
1112

1213
namespace TRE_API.Controllers
1314
{
@@ -18,21 +19,20 @@ public class ApprovalController : Controller
1819
private readonly ApplicationDbContext _DbContext;
1920
protected readonly IHttpContextAccessor _httpContextAccessor;
2021
private readonly IKeyCloakService _IKeyCloakService;
21-
private readonly Features _Features;
2222
private readonly IEncDecHelper _encDecHelper;
23-
2423
public IDareSyncHelper _dareSyncHelper { get; set; }
24+
private readonly IFeatureManager _features;
2525

2626
public ApprovalController(IDareSyncHelper dareSyncHelper, ApplicationDbContext applicationDbContext,
27-
IHttpContextAccessor httpContextAccessor, IKeyCloakService IKeyCloakService, Features Features,
28-
IEncDecHelper encDecHelper)
27+
IHttpContextAccessor httpContextAccessor, IKeyCloakService IKeyCloakService,
28+
IEncDecHelper encDecHelper, IFeatureManager features)
2929
{
3030
_dareSyncHelper = dareSyncHelper;
3131
_DbContext = applicationDbContext;
3232
_httpContextAccessor = httpContextAccessor;
3333
_IKeyCloakService = IKeyCloakService;
34-
_Features = Features;
3534
_encDecHelper = encDecHelper;
35+
_features = features;
3636
}
3737

3838
[Authorize(Roles = "dare-tre-admin")]
@@ -219,7 +219,7 @@ public async Task<List<TreProject>> UpdateProjects(List<TreProject> projects)
219219
await _DbContext.SaveChangesAsync();
220220
await ControllerHelpers.AddTreAuditLog(dbproj, null, treProject.Decision == Decision.Approved,
221221
_DbContext, _httpContextAccessor, User);
222-
if (_Features.GenerateAcounts)
222+
if (await _features.IsEnabledAsync(FeatureFlags.GenerateAccounts))
223223
{
224224
var acc = _DbContext.ProjectAcount.FirstOrDefault(x => x.Name == dbproj.SubmissionProjectName);
225225

@@ -267,7 +267,7 @@ public async Task<List<TreMembershipDecision>> UpdateMembershipDecisions(
267267
var dbMembership = _DbContext.MembershipDecisions.First(x => x.Id == membershipDecision.Id);
268268
if (membershipDecision.Decision != dbMembership.Decision)
269269
{
270-
if (_Features.GenerateAcounts)
270+
if (await _features.IsEnabledAsync(FeatureFlags.GenerateAccounts))
271271
{
272272
var name = dbMembership.Project.SubmissionProjectName + dbMembership.User.Username;
273273

src/TRE-API/Controllers/SubmissionController.cs

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,59 @@
1-

2-
using BL.Models.ViewModels;
1+
using BL.Models.ViewModels;
32
using BL.Models;
43
using BL.Models.Enums;
54
using BL.Services;
65
using Microsoft.AspNetCore.Mvc;
7-
using Microsoft.EntityFrameworkCore;
86
using Swashbuckle.AspNetCore.Annotations;
97
using TRE_API.Attributes;
108
using TRE_API.Services;
11-
using TRE_API.Services.SignalR;
129
using Microsoft.AspNetCore.Authorization;
13-
using System.Data;
1410
using BL.Models.APISimpleTypeReturns;
15-
using TRE_API.Repositories.DbContexts;
16-
using EasyNetQ.Management.Client.Model;
1711
using BL.Rabbit;
1812
using EasyNetQ;
19-
using Newtonsoft.Json;
20-
using System;
21-
using Amazon.Runtime.Internal.Transform;
13+
using Microsoft.FeatureManagement;
2214
using Serilog;
15+
using TRE_API.Constants;
2316
using TRE_API.Models;
24-
using Minio;
2517

2618
namespace TRE_API.Controllers
2719
{
28-
29-
3020
[ApiController]
3121
[Route("api/[controller]")]
3222
public class SubmissionController : Controller
3323
{
34-
private readonly ISignalRService _signalRService;
35-
private readonly IDareClientWithoutTokenHelper _dareHelper;
36-
private readonly IDataEgressClientWithoutTokenHelper _dataEgressHelper;
3724
private readonly IHutchClientHelper _hutchHelper;
38-
private readonly ApplicationDbContext _dbContext;
3925
private readonly IBus _rabbit;
4026
private readonly ISubmissionHelper _subHelper;
4127
private readonly IMinioSubHelper _minioSubHelper;
4228
private readonly IMinioTreHelper _minioTreHelper;
4329
private readonly MinioTRESettings _minioTreSettings;
44-
private readonly string _treName;
4530
private readonly AgentSettings _agentSettings;
31+
private readonly IFeatureManager _features;
4632

47-
public SubmissionController(ISignalRService signalRService, IDareClientWithoutTokenHelper helper,
48-
ApplicationDbContext dbContext,
49-
IBus rabbit,
33+
public SubmissionController(
34+
IHutchClientHelper hutchHelper,
35+
IBus rabbit,
5036
ISubmissionHelper subHelper,
51-
IDataEgressClientWithoutTokenHelper egressHelper,
52-
IHutchClientHelper hutchClientHelper,
5337
IMinioSubHelper minioSubHelper,
5438
IMinioTreHelper minioTreHelper,
55-
MinioTRESettings minioTreSettings,
56-
IConfiguration config,
57-
AgentSettings agentSettings)
39+
MinioTRESettings minioTreSettings,
40+
AgentSettings agentSettings, IFeatureManager features)
5841
{
59-
_signalRService = signalRService;
60-
_dareHelper = helper;
61-
_dataEgressHelper = egressHelper;
62-
_hutchHelper = hutchClientHelper;
63-
_dbContext = dbContext;
42+
_hutchHelper = hutchHelper;
6443
_rabbit = rabbit;
6544
_subHelper = subHelper;
6645
_minioTreHelper = minioTreHelper;
6746
_minioSubHelper = minioSubHelper;
6847
_minioTreSettings = minioTreSettings;
69-
_treName = config["TreName"];
7048
_agentSettings = agentSettings;
49+
_features = features;
7150
}
7251

7352

74-
75-
7653
[Authorize(Roles = "dare-tre-admin")]
7754
[HttpGet("IsUserApprovedOnProject")]
7855
public BoolReturn IsUserApprovedOnProject(int projectId, int userId)
7956
{
80-
8157
return new BoolReturn()
8258
{
8359
Result = _subHelper.IsUserApprovedOnProject(projectId, userId)
@@ -136,10 +112,11 @@ public IActionResult UpdateStatusForTre(
136112
{
137113
_subHelper.CloseSubmissionForTre(subDetails.SubId, subDetails.StatusType, "", "");
138114
}
115+
139116
return StatusCode(200, result);
140117
}
141-
catch (Exception ex) {
142-
118+
catch (Exception ex)
119+
{
143120
Log.Error(ex, "{Function} Crash", "UpdateStatusForTre");
144121
throw;
145122
}
@@ -156,7 +133,7 @@ public IActionResult GetOutputBucketInfo(string subId)
156133
try
157134
{
158135
var outputInfo = _subHelper.GetOutputBucketGuts(subId, true, true);
159-
136+
160137

161138
return StatusCode(200, outputInfo);
162139
}
@@ -176,7 +153,6 @@ public class OutputBucketInfo
176153
public bool Secure { get; set; }
177154
}
178155

179-
180156

181157
[Authorize(Roles = "dare-hutch-admin,dare-tre-admin")]
182158
[HttpPost]
@@ -208,7 +184,7 @@ public IActionResult FilesReadyForReview([FromBody] ReviewFiles review)
208184
public async Task<IActionResult> EgressResults([FromBody] EgressReview review)
209185
{
210186
try
211-
{
187+
{
212188
Dictionary<string, bool> hutchRes = new Dictionary<string, bool>();
213189
ApprovalType approvalStatus;
214190
var approvedCount = review.FileResults.Count(x => x.Approved);
@@ -230,22 +206,23 @@ public async Task<IActionResult> EgressResults([FromBody] EgressReview review)
230206
foreach (var i in review.FileResults)
231207
{
232208
hutchRes.Add(i.FileName, i.Approved);
233-
234209
}
235210

236211
if (approvalStatus != ApprovalType.FullyApproved)
237212
{
238213
_subHelper.UpdateStatusForTre(review.SubId.ToString(), StatusType.DataOutApprovalRejected, "");
239-
240214

241-
var StatusResult = _subHelper.CloseSubmissionForTre(review.SubId, StatusType.Failed, "", "");
242-
215+
216+
var statusResult = _subHelper.CloseSubmissionForTre(review.SubId, StatusType.Failed, "", "");
243217
}
244218
else
245219
{
246220
_subHelper.UpdateStatusForTre(review.SubId.ToString(), StatusType.DataOutApproved, "");
247221
}
248-
var realurl = string.IsNullOrWhiteSpace(_minioTreSettings.HutchURLOverride) ? _minioTreSettings.Url : _minioTreSettings.HutchURLOverride;
222+
223+
var realurl = string.IsNullOrWhiteSpace(_minioTreSettings.HutchURLOverride)
224+
? _minioTreSettings.Url
225+
: _minioTreSettings.HutchURLOverride;
249226
bool secure = !realurl.ToLower().StartsWith("http://");
250227
var bucket = _subHelper.GetOutputBucketGutsSub(review.SubId, true);
251228
ApprovalResult hutchPayload = new ApprovalResult()
@@ -263,43 +240,46 @@ public async Task<IActionResult> EgressResults([FromBody] EgressReview review)
263240
_subHelper.UpdateStatusForTre(review.SubId, StatusType.RequestingHutchDoesFinalPackaging, "");
264241
}
265242

266-
if (_agentSettings.SimulateResults)
243+
if (await _features.IsEnabledAsync(FeatureFlags.DemoAllInOne))
267244
{
268245
var exch = _rabbit.Advanced.ExchangeDeclare(ExchangeConstants.Tre, "topic");
269246
var outcome = new FinalOutcome()
270247
{
271248
File = review.FileResults.First().FileName,
272249
SubId = review.SubId
273250
};
274-
_rabbit.Advanced.Publish(exch, RoutingConstants.ProcessFinalOutput, false, new Message<FinalOutcome>(outcome));
251+
_rabbit.Advanced.Publish(exch, RoutingConstants.ProcessFinalOutput, false,
252+
new Message<FinalOutcome>(outcome));
275253
}
276254
else if (_agentSettings.UseTESK == false)
277255
{
278-
Log.Information("{Function} Minio url sent {Url} bucket {Bucket}, path {path}", "EgressReview", hutchPayload.Host, hutchPayload.Bucket, hutchPayload.Path);
256+
Log.Information("{Function} Minio url sent {Url} bucket {Bucket}, path {path}", "EgressReview",
257+
hutchPayload.Host, hutchPayload.Bucket, hutchPayload.Path);
279258
//Not sure what the return type is
280259
var HUTCHres =
281260
await _hutchHelper.CallAPI<ApprovalResult, APIReturn>($"/api/jobs/{review.SubId}/approval",
282261
hutchPayload);
283262
}
284263
else
285264
{
286-
Log.Information($"EgressResults with review.OutputBucket > {review.OutputBucket} bucket.Bucket > {bucket.Bucket} ");
287-
foreach (var File in review.FileResults)
265+
Log.Information(
266+
$"EgressResults with review.OutputBucket > {review.OutputBucket} bucket.Bucket > {bucket.Bucket} ");
267+
foreach (var file in review.FileResults)
288268
{
289-
Log.Information($"EgressResults with File.Approved > {File.Approved} File.FileName > {File.FileName} ");
290-
if (File.Approved)
269+
Log.Information(
270+
$"EgressResults with File.Approved > {file.Approved} File.FileName > {file.FileName} ");
271+
if (file.Approved)
291272
{
292-
var source = await _minioTreHelper.GetCopyObject(review.OutputBucket, File.FileName);
293-
var resultcopy = await _minioSubHelper.CopyObjectToDestination(bucket.Bucket, File.FileName, source);
273+
var source = await _minioTreHelper.GetCopyObject(review.OutputBucket, file.FileName);
274+
var resultcopy =
275+
await _minioSubHelper.CopyObjectToDestination(bucket.Bucket, file.FileName, source);
294276
}
295277
}
296278

297279
_subHelper.UpdateStatusForTre(review.SubId, StatusType.Completed, "");
298280
}
299281

300282

301-
302-
303283
return StatusCode(200, new BoolReturn() { Result = true });
304284
}
305285
catch (Exception ex)
@@ -310,8 +290,6 @@ await _hutchHelper.CallAPI<ApprovalResult, APIReturn>($"/api/jobs/{review.SubId}
310290
}
311291

312292

313-
314-
315293
[Authorize(Roles = "dare-hutch-admin,dare-tre-admin")]
316294
[HttpPost]
317295
[Route("FinalOutcome")]
@@ -324,7 +302,8 @@ public IActionResult FinalOutcome([FromBody] FinalOutcome outcome)
324302
{
325303
var exch = _rabbit.Advanced.ExchangeDeclare(ExchangeConstants.Tre, "topic");
326304

327-
_rabbit.Advanced.Publish(exch, RoutingConstants.ProcessFinalOutput, false, new Message<FinalOutcome>(outcome));
305+
_rabbit.Advanced.Publish(exch, RoutingConstants.ProcessFinalOutput, false,
306+
new Message<FinalOutcome>(outcome));
328307

329308
var boolresult = new BoolReturn()
330309
{
@@ -340,7 +319,6 @@ public IActionResult FinalOutcome([FromBody] FinalOutcome outcome)
340319
}
341320

342321

343-
344322
[HttpPost("SendSubmissionToHUTCH")]
345323
public IActionResult SendSubmissionToHUTCH(Submission sub)
346324
{
@@ -374,7 +352,5 @@ public IActionResult SimulateSubmissionProcessing(Submission sub)
374352
throw;
375353
}
376354
}
377-
378-
379355
}
380-
}
356+
}

0 commit comments

Comments
 (0)