-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReport.cs
More file actions
100 lines (93 loc) · 4.22 KB
/
Report.cs
File metadata and controls
100 lines (93 loc) · 4.22 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
namespace hasheous_server.Classes.Report
{
/// <summary>
/// Provides a shared report model for the host service and related components.
/// </summary>
public class Report
{
/// <summary>
/// Initializes a new instance of the Report class.
/// </summary>
/// <param name="reportingServerUrl">The URL of the reporting server. Should only contain the base address (scheme, host and port). Example: "https://example.com:443"</param>
/// <param name="processId">The process identifier.</param>
/// <param name="correlationId">The correlation identifier for tracking.</param>
public Report(string reportingServerUrl, string processId, string correlationId)
{
this.processId = processId;
this.correlationId = correlationId;
this.reportingServerUrl = reportingServerUrl;
}
private static readonly HttpClient httpClient = new HttpClient(new SocketsHttpHandler
{
AllowAutoRedirect = false,
PooledConnectionLifetime = TimeSpan.FromSeconds(300),
})
{
Timeout = TimeSpan.FromSeconds(30)
};
private static readonly SemaphoreSlim sendSemaphore = new SemaphoreSlim(1, 1);
private string processId;
private string correlationId;
private string reportingServerUrl;
/// <summary>
/// Shared instance of the report model used to aggregate reporting data across the host process.
/// </summary>
private hasheous_server.Models.ReportModel _reportModel = new hasheous_server.Models.ReportModel();
/// <summary>
/// Sends a progress update to the shared report model.
/// </summary>
/// <param name="progressItemKey">The unique key identifying the progress item.</param>
/// <param name="count">The current progress count.</param>
/// <param name="total">The total count to complete.</param>
/// <param name="description">A description of the progress item.</param>
/// <param name="performETACalculation">If true, performs ETA calculation for the progress item.</param>
public async System.Threading.Tasks.Task SendAsync(string progressItemKey, int? count, int? total, string description, bool performETACalculation = false)
{
if (_reportModel.Progress.ContainsKey(progressItemKey))
{
// Update existing progress item
var item = _reportModel.Progress[progressItemKey];
item.count = count;
item.total = total;
item.description = description;
item.enableETACalculation = performETACalculation;
}
else
{
// Add new progress item
_reportModel.Progress[progressItemKey] = new hasheous_server.Models.ReportModel.ProgressItem
{
count = count,
total = total,
description = description,
enableETACalculation = performETACalculation
};
}
// send to reporting server if configured
if (!string.IsNullOrEmpty(this.reportingServerUrl))
{
// Acquire semaphore to serialize requests
await sendSemaphore.WaitAsync();
try
{
var jsonContent = System.Text.Json.JsonSerializer.Serialize(_reportModel);
var content = new StringContent(jsonContent, System.Text.Encoding.UTF8, "application/json");
try
{
string url = $"{this.reportingServerUrl}/api/v1.0/BackgroundTasks/{this.processId}/{this.correlationId}/report";
var response = await httpClient.PostAsync(url, content);
response.EnsureSuccessStatusCode();
}
catch
{
// swallow the error to prevent reporting issues to the console
}
}
finally
{
sendSemaphore.Release();
}
}
}
}
}