Skip to content

Commit e132b84

Browse files
author
Bas Visscher
committed
Merge branch 'SHANA-61-Output-api-info'
2 parents 2abf126 + f4174d3 commit e132b84

File tree

7 files changed

+78
-17
lines changed

7 files changed

+78
-17
lines changed

Config/Gateway/Manager.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ profiles:
3636
2: Cooling
3737
3: Heating
3838
4: Off
39+
5: VacantAutoHeat
3940
includeLogs:
4041
SoftwareId: SmarthomeGateway
4142
LogCode: HeatmanagerStateChanged

LogViewer/Config/Loaders/RecursiveConfigLoader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using LogViewer.Config.Cache;
55
using LogViewer.Config.Helpers;
66
using LogViewer.Serializers.Yaml;
7+
using System.Diagnostics;
78

89
namespace LogViewer.Config.Loaders
910
{
@@ -40,7 +41,10 @@ private async Task LoadRecursiveAsync(string path, LogViewerConfig target, HashS
4041
: path;
4142

4243
if (!File.Exists(filePath))
44+
{
45+
Debug.WriteLine($"File not found: {filePath}");
4346
return;
47+
}
4448

4549
if (!YamlSerializer.LoadYaml(new FileInfo(filePath), out LogViewerConfig config))
4650
throw new Exception($"Failed to load configuration from: {filePath}");

LogViewer/Controls/ApiSourceControl.Designer.cs

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LogViewer/Controls/ApiSourceControl.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using LogViewer.Controls.Helpers;
66
using LogViewer.Providers.API;
77
using Microsoft.Extensions.Caching.Hybrid;
8+
using System.Security.Cryptography;
89

910
namespace LogViewer.Controls
1011
{
@@ -141,8 +142,8 @@ private async void LoadObjectItemsForResort(Resort? resort, string? searchField)
141142
await RunWithDisabledControlsAsync(async token =>
142143
{
143144
var builder = new ApiObjectItemProviderBuilder(apiClient)
144-
.ForResort(resort.Id.Value)
145-
.WithSortByName();
145+
.ForResort(resort.Id.Value)
146+
.WithSortByName();
146147

147148
if (checkBoxRequireGateways.Checked)
148149
builder.WithRequireGateway();
@@ -195,6 +196,13 @@ await RunWithDisabledControlsAsync(async token =>
195196
{
196197
var logProvider = new ApiGatewayLogProvider(apiClient, gateway.Id.Value, dateTimePickerFrom.Value, dateTimePickerUntill.Value);
197198

199+
200+
infoViewManager.ClearApiStatsInfo();
201+
logProvider.OnResponseTimeReported += (s, responseTime) =>
202+
{
203+
infoViewManager.ReportApiCall(responseTime);
204+
};
205+
198206
DataSource.ScopeViewContext.StartDate = dateTimePickerFrom.Value;
199207
DataSource.ScopeViewContext.EndDate = dateTimePickerUntill.Value;
200208

@@ -223,7 +231,7 @@ private async Task RunWithDisabledControlsAsync(Func<CancellationToken, Task> ta
223231
{
224232
progressBarManager.Reset();
225233
controlStateManager.EnableAll();
226-
cancellationTokenSource.Dispose();
234+
cancellationTokenSource?.Dispose();
227235
cancellationTokenSource = null;
228236
}
229237
}

LogViewer/Controls/Helpers/InfoViewManager.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public class InfoViewManager
1515
private string dEVID = "";
1616
private string apiUrl = "";
1717

18+
private int completedRequests = 0;
19+
private TimeSpan minResponseTime = TimeSpan.MaxValue;
20+
private TimeSpan maxResponseTime = TimeSpan.Zero;
21+
private TimeSpan totalResponseTime = TimeSpan.Zero;
22+
private TimeSpan avgResponseTime = TimeSpan.Zero;
23+
1824
public InfoViewManager(RichTextBox textBox)
1925
{
2026
this.textBox = textBox;
@@ -43,6 +49,32 @@ public void Update(Gateway? gateway)
4349
Print();
4450
}
4551

52+
public void ReportApiCall(TimeSpan elapsed)
53+
{
54+
completedRequests++;
55+
totalResponseTime += elapsed;
56+
57+
if (elapsed < minResponseTime)
58+
minResponseTime = elapsed;
59+
60+
if (elapsed > maxResponseTime)
61+
maxResponseTime = elapsed;
62+
63+
avgResponseTime = totalResponseTime / completedRequests;
64+
65+
Print();
66+
}
67+
68+
public void ClearApiStatsInfo()
69+
{
70+
completedRequests = 0;
71+
minResponseTime = TimeSpan.MaxValue;
72+
maxResponseTime = TimeSpan.Zero;
73+
totalResponseTime = TimeSpan.Zero;
74+
avgResponseTime = TimeSpan.Zero;
75+
Print();
76+
}
77+
4678
public void ClearOrganisationInfo()
4779
{
4880
apiUrl = "";
@@ -71,11 +103,19 @@ public void ClearGatewayInfo()
71103

72104
private void Print()
73105
{
106+
string compl = this.completedRequests.ToString();
107+
string min = minResponseTime != TimeSpan.MaxValue ? $"{minResponseTime.TotalMilliseconds:F0} ms" : "-";
108+
string max = maxResponseTime != TimeSpan.Zero ? $"{maxResponseTime.TotalMilliseconds:F0} ms" : "-";
109+
string avg = completedRequests > 0 ? $"{avgResponseTime.TotalMilliseconds:F0} ms" : "-";
110+
74111
textBox.Text = @$"Api {apiUrl}
75112
Host {host}
76113
IST COM TRG SID DEVID
77114
{iST.PadRight(7)} {cOM.PadRight(7)} {tRG.PadRight(7)} {sID.PadRight(7)} {dEVID.PadRight(7)}
115+
Calls Min Max Avg
116+
{compl.PadRight(7)} {min.PadRight(7)} {max.PadRight(7)} {avg.PadRight(7)}
78117
";
118+
79119
}
80120
}
81121
}

LogViewer/Providers/Api/ApiGatewayLogProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class ApiGatewayLogProvider
1515
private readonly DateTime from;
1616
private readonly DateTime until;
1717

18+
public event EventHandler<TimeSpan>? OnResponseTimeReported;
19+
1820
public ApiGatewayLogProvider(InternalApiClient client, int gatewayId, DateTime from, DateTime until)
1921
{
2022
this.client = client;
@@ -50,8 +52,7 @@ public async Task<LogCollection> GetData(CancellationToken token = default, IPro
5052
{
5153
var apiResult = await client.GatewayLogApi.GetAllAsync(page, 25, filters, null, token);
5254
stopwatch.Stop();
53-
Debug.WriteLine($"Call took {stopwatch.ElapsedMilliseconds} ms");
54-
55+
OnResponseTimeReported?.Invoke(this, stopwatch.Elapsed);
5556
token.ThrowIfCancellationRequested();
5657

5758
// Exit the loop when no more data is returned
@@ -80,6 +81,11 @@ public async Task<LogCollection> GetData(CancellationToken token = default, IPro
8081

8182
page++; // Move to the next page
8283
}
84+
catch (OperationCanceledException)
85+
{
86+
Debug.WriteLine("Operation was canceled.");
87+
return log; // Return the log collected so far if the operation is canceled
88+
}
8389
catch (Exception ex) when (!(ex is OperationCanceledException))
8490
{
8591
Debug.WriteLine($"Error occurred during API call: {ex.Message}");

LogViewer/Providers/Api/WebApiLogItemConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ public class WebApiLogItemConverter
8484

8585

8686
default:
87-
builder.WithSegment(LogKeys.RawData, logItem.Data);
8887
break;
8988
}
9089

90+
91+
builder.WithSegment(LogKeys.RawData, logItem.Data);
92+
9193
return builder.Build();
9294
}
9395
}

0 commit comments

Comments
 (0)