From 9362dd015507819e4a1dd02f611ded101f5bd7dc Mon Sep 17 00:00:00 2001 From: rhenium2 <5300091+rhenium2@users.noreply.github.com> Date: Tue, 28 Jun 2022 23:29:41 +1000 Subject: [PATCH] Added extra stats to witnessed report (#3) - made distance report obsolete as it is covered by witnessed report - some http call changes --- HeliumCat/Commands/Commands.cs | 33 ++++++++++++++++++++++++---- HeliumCat/Program.cs | 6 ++--- HeliumCat/Services/HeliumClient.cs | 12 +++++----- HeliumCat/Services/HotspotService.cs | 18 ++++++++++++--- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/HeliumCat/Commands/Commands.cs b/HeliumCat/Commands/Commands.cs index a98e83e..e06d77a 100644 --- a/HeliumCat/Commands/Commands.cs +++ b/HeliumCat/Commands/Commands.cs @@ -76,6 +76,7 @@ public static async Task Direction(DirectionOptions options) Console.WriteLine($"- {hotspot2.ToString()} {Extensions.GetDirectionString(hotspot1, hotspot2)}"); } + [Obsolete] public static async Task Distance(DistanceOptions options) { Console.WriteLine($"distance stats ({options.ToString()}) in the last 5 days"); @@ -98,27 +99,51 @@ public static async Task Distance(DistanceOptions options) public static async Task Witnessed(WitnessedOptions options) { var minDateTime = DateTime.UtcNow.AddMinutes(-options.PastMinutes); - Console.WriteLine($"witnessed stats ({options.ToString()}) in the last 5 days"); + Console.WriteLine($"witnessed stats ({options.ToString()})"); var myHotspot = await GetHotspotByIdentifier(options.Identifier); Console.Write("Fetching witnessed ... "); var witnessed = await HotspotService.GetWitnessedTransactions(myHotspot.Address, minDateTime); Console.WriteLine($"{witnessed.Count}"); + var witnessedDistances = new List(); + var witnessedHeights = new List(); foreach (var transaction in witnessed) { var hotspot = await HotspotService.GetHotspot(transaction.Path[0].Challengee); - Console.Write( + witnessedDistances.Add(Extensions.CalculateDistance(myHotspot, hotspot)); + witnessedHeights.Add(hotspot.Elevation); + + Console.WriteLine( $"- {hotspot.ToString()} {Extensions.GetDirectionString(myHotspot, hotspot)} - {Extensions.GetRelativeTimeString(transaction.Time)}"); } - Console.WriteLine(""); + if (witnessedDistances.Any()) + { + var minDistance = Extensions.ToDistanceText(witnessedDistances.Min()); + var averageDistance = Extensions.ToDistanceText(witnessedDistances.Average()); + var maxDistance = Extensions.ToDistanceText(witnessedDistances.Max()); + Console.WriteLine(""); + Console.WriteLine("--- distance statistics ---"); + Console.WriteLine($"min: {minDistance} avg: {averageDistance} max: {maxDistance}"); + } + + if (witnessedHeights.Any()) + { + var minHeight = Extensions.ToDistanceText(witnessedHeights.Min()); + var averageHeight = Extensions.ToDistanceText(witnessedHeights.Average()); + var maxHeight = Extensions.ToDistanceText(witnessedHeights.Max()); + + Console.WriteLine(""); + Console.WriteLine("--- elevation statistics ---"); + Console.WriteLine($"min: {minHeight} avg: {averageHeight} max: {maxHeight}"); + } } private static async Task BeaconStats(Hotspot[] hotspots, Hotspot myHotspot, DateTime minTime) { Console.Write("fetching all beacons in the world ... "); - var challenges = await HotspotService.GetChallenges(minTime); + var challenges = await HotspotService.GetNetworkChallenges(minTime); Console.WriteLine($"{challenges.Count}"); Console.Write("beacons from hotspots ... "); diff --git a/HeliumCat/Program.cs b/HeliumCat/Program.cs index 364920c..f32992a 100644 --- a/HeliumCat/Program.cs +++ b/HeliumCat/Program.cs @@ -6,8 +6,7 @@ var parserResult = Parser.Default - .ParseArguments(args); + .ParseArguments(args); if (parserResult.Errors.Any()) { return; @@ -21,7 +20,8 @@ await parserResult.WithParsedAsync(async options => await parserResult.WithParsedAsync(async options => await Commands.RadiusBeaconStats(options)); await parserResult.WithParsedAsync(async options => await Commands.BoxBeaconStats(options)); await parserResult.WithParsedAsync(async options => await Commands.Direction(options)); -await parserResult.WithParsedAsync(async options => await Commands.Distance(options)); +// [Obsolete] +//await parserResult.WithParsedAsync(async options => await Commands.Distance(options)); await parserResult.WithParsedAsync(async options => await Commands.Witnessed(options)); Console.WriteLine("Done"); \ No newline at end of file diff --git a/HeliumCat/Services/HeliumClient.cs b/HeliumCat/Services/HeliumClient.cs index 95db06f..6ea429f 100644 --- a/HeliumCat/Services/HeliumClient.cs +++ b/HeliumCat/Services/HeliumClient.cs @@ -21,16 +21,16 @@ private static async Task PollyGet(Func() .Or() .OrResult(TransientHttpFailures) - .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(1 * retryAttempt)) + .WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(3 * retryAttempt)) .ExecuteAsync(func); } - + private static bool TransientHttpFailures(HttpResponseMessage responseMessage) { - return responseMessage.StatusCode == HttpStatusCode.TooManyRequests || - responseMessage.StatusCode == HttpStatusCode.RequestTimeout || - responseMessage.StatusCode == HttpStatusCode.BadGateway || - responseMessage.StatusCode == HttpStatusCode.GatewayTimeout || + return responseMessage.StatusCode == HttpStatusCode.TooManyRequests || + responseMessage.StatusCode == HttpStatusCode.RequestTimeout || + responseMessage.StatusCode == HttpStatusCode.BadGateway || + responseMessage.StatusCode == HttpStatusCode.GatewayTimeout || responseMessage.StatusCode == HttpStatusCode.ServiceUnavailable; } diff --git a/HeliumCat/Services/HotspotService.cs b/HeliumCat/Services/HotspotService.cs index 884c377..9134714 100644 --- a/HeliumCat/Services/HotspotService.cs +++ b/HeliumCat/Services/HotspotService.cs @@ -27,6 +27,11 @@ public static async Task GetHotspotByName(string name) return hotspots.First(); } + /// + /// Retrieves the list of hotspots the given hotspot witnessed over the last 5 days. + /// + /// + /// public static async Task> GetWitnessed(string hotspotId) { var uri = $"/v1/hotspots/{hotspotId}/witnessed"; @@ -49,6 +54,12 @@ public static async Task GetTransaction(string transactionHash) return DeserializeTransaction(firstData); } + /// + /// Lists the challenge (receipts) that the given hotspot was a challenger, challengee or witness in + /// + /// + /// + /// public static async Task> GetChallenges(string hotspotId, DateTime? minTime) { var uri = $"/v1/hotspots/{hotspotId}/challenges"; @@ -61,7 +72,7 @@ public static async Task> GetChallenges(string hots return Extensions.DeserializeAll(allData.ToArray()); } - public static async Task> GetChallenges(DateTime? minTime) + public static async Task> GetNetworkChallenges(DateTime? minTime) { var uri = $"/v1/challenges"; if (minTime.HasValue) @@ -69,8 +80,9 @@ public static async Task> GetChallenges(DateTime? m uri += "?min_time=" + minTime.Value.ToString("o", CultureInfo.InvariantCulture); } - var allData = await HeliumClient.Get(uri); - return Extensions.DeserializeAll(allData.ToArray()); + var allDataStrings = await HeliumClient.Get(uri); + var transactions = Extensions.DeserializeAll(allDataStrings.ToArray()); + return transactions; } public static async Task> GetBeaconTransactions(string hotspotId, DateTime? minTime)