-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathLicensingController.cs
More file actions
108 lines (93 loc) · 4.66 KB
/
LicensingController.cs
File metadata and controls
108 lines (93 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
namespace Particular.LicensingComponent.WebApi
{
using System.IO.Compression;
using System.Text;
using System.Text.Json;
using System.Threading;
using Contracts;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using Particular.LicensingComponent.Report;
[ApiController]
[Route("api/licensing")]
public class LicensingController : ControllerBase
{
public LicensingController(IThroughputCollector throughputCollector)
{
this.throughputCollector = throughputCollector;
}
[Route("endpoints")]
[HttpGet]
public async Task<List<EndpointThroughputSummary>> GetEndpointThroughput(CancellationToken cancellationToken)
{
return await throughputCollector.GetThroughputSummary(cancellationToken);
}
[Route("endpoints/update")]
[HttpPost]
public async Task<IActionResult> UpdateUserSelectionOnEndpointThroughput(List<UpdateUserIndicator> updateUserIndicators, CancellationToken cancellationToken)
{
await throughputCollector.UpdateUserIndicatorsOnEndpoints(updateUserIndicators, cancellationToken);
return Ok();
}
[Route("report/available")]
[HttpGet]
public async Task<ReportGenerationState> CanThroughputReportBeGenerated(CancellationToken cancellationToken)
{
return await throughputCollector.GetReportGenerationState(cancellationToken);
}
[Route("report/file")]
[HttpGet]
public async Task GetThroughputReportFile([FromQuery(Name = "spVersion")] string? spVersion, CancellationToken cancellationToken)
{
var reportStatus = await CanThroughputReportBeGenerated(cancellationToken);
if (!reportStatus.ReportCanBeGenerated)
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
HttpContext.Response.ContentType = "text/plain; charset=utf-8";
await HttpContext.Response.WriteAsync($"Report cannot be generated – {reportStatus.Reason}", Encoding.UTF8, cancellationToken);
return;
}
var report = await throughputCollector.GenerateThroughputReport(
spVersion ?? "Unknown",
null,
cancellationToken);
var fileName = $"{report.ReportData.CustomerName}.throughput-report-{report.ReportData.EndTime:yyyyMMdd-HHmmss}";
HttpContext.Response.ContentType = "application/zip";
HttpContext.Response.Headers[HeaderNames.ContentDisposition] = new ContentDispositionHeaderValue("attachment")
{
FileName = $"{fileName}.zip"
}.ToString();
// The zip archive is written directly to the response body stream and has to remain open until the response is fully sent.
// This is done for performance reasons to avoid buffering the entire report in memory before sending it.
// The BodyWriter is used as a stream to avoid into synchronous IO operations that would be prevented by the ASP.NET Core pipeline.
using var archive = new ZipArchive(Response.BodyWriter.AsStream(), ZipArchiveMode.Create, leaveOpen: true);
var entry = archive.CreateEntry($"{fileName}.json");
await using var entryStream = entry.Open();
await JsonSerializer.SerializeAsync(entryStream, report, SerializationOptions.IndentedWithNoEscaping, cancellationToken);
}
[Route("settings/info")]
[HttpGet]
public async Task<ThroughputConnectionSettings> GetThroughputSettingsInformation(CancellationToken cancellationToken)
{
return await throughputCollector.GetThroughputConnectionSettingsInformation(cancellationToken);
}
[Route("settings/test")]
[HttpGet]
public async Task<ConnectionTestResults> TestThroughputConnectionSettings(CancellationToken cancellationToken) => await throughputCollector.TestConnectionSettings(cancellationToken);
[Route("settings/masks")]
[HttpGet]
public async Task<List<string>> GetMasks(CancellationToken cancellationToken)
{
return await throughputCollector.GetReportMasks(cancellationToken);
}
[Route("settings/masks/update")]
[HttpPost]
public async Task<IActionResult> UpdateMasks(List<string> updateMasks, CancellationToken cancellationToken)
{
await throughputCollector.UpdateReportMasks(updateMasks, cancellationToken);
return Ok();
}
readonly IThroughputCollector throughputCollector;
}
}