Skip to content

Commit

Permalink
Added extra stats to witnessed report (#3)
Browse files Browse the repository at this point in the history
- made distance report obsolete as it is covered by witnessed report
- some http call changes
  • Loading branch information
rhenium2 authored Jun 28, 2022
1 parent f5fbc7c commit 9362dd0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
33 changes: 29 additions & 4 deletions HeliumCat/Commands/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<double>();
var witnessedHeights = new List<int>();
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 ... ");
Expand Down
6 changes: 3 additions & 3 deletions HeliumCat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

var parserResult =
Parser.Default
.ParseArguments<FrontOptions, RadiusOptions, BoxOptions, DirectionOptions, DistanceOptions,
WitnessedOptions>(args);
.ParseArguments<FrontOptions, RadiusOptions, BoxOptions, DirectionOptions, WitnessedOptions>(args);
if (parserResult.Errors.Any())
{
return;
Expand All @@ -21,7 +20,8 @@ await parserResult.WithParsedAsync<FrontOptions>(async options =>
await parserResult.WithParsedAsync<RadiusOptions>(async options => await Commands.RadiusBeaconStats(options));
await parserResult.WithParsedAsync<BoxOptions>(async options => await Commands.BoxBeaconStats(options));
await parserResult.WithParsedAsync<DirectionOptions>(async options => await Commands.Direction(options));
await parserResult.WithParsedAsync<DistanceOptions>(async options => await Commands.Distance(options));
// [Obsolete]
//await parserResult.WithParsedAsync<DistanceOptions>(async options => await Commands.Distance(options));
await parserResult.WithParsedAsync<WitnessedOptions>(async options => await Commands.Witnessed(options));

Console.WriteLine("Done");
12 changes: 6 additions & 6 deletions HeliumCat/Services/HeliumClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ private static async Task<HttpResponseMessage> PollyGet(Func<Task<HttpResponseMe
.Handle<HttpRequestException>()
.Or<TaskCanceledException>()
.OrResult<HttpResponseMessage>(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;
}

Expand Down
18 changes: 15 additions & 3 deletions HeliumCat/Services/HotspotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static async Task<Hotspot> GetHotspotByName(string name)
return hotspots.First();
}

/// <summary>
/// Retrieves the list of hotspots the given hotspot witnessed over the last 5 days.
/// </summary>
/// <param name="hotspotId"></param>
/// <returns></returns>
public static async Task<List<Hotspot>> GetWitnessed(string hotspotId)
{
var uri = $"/v1/hotspots/{hotspotId}/witnessed";
Expand All @@ -49,6 +54,12 @@ public static async Task<Transaction> GetTransaction(string transactionHash)
return DeserializeTransaction(firstData);
}

/// <summary>
/// Lists the challenge (receipts) that the given hotspot was a challenger, challengee or witness in
/// </summary>
/// <param name="hotspotId"></param>
/// <param name="minTime"></param>
/// <returns></returns>
public static async Task<List<PocReceiptsTransaction>> GetChallenges(string hotspotId, DateTime? minTime)
{
var uri = $"/v1/hotspots/{hotspotId}/challenges";
Expand All @@ -61,16 +72,17 @@ public static async Task<List<PocReceiptsTransaction>> GetChallenges(string hots
return Extensions.DeserializeAll<PocReceiptsTransaction>(allData.ToArray());
}

public static async Task<List<PocReceiptsTransaction>> GetChallenges(DateTime? minTime)
public static async Task<List<PocReceiptsTransaction>> GetNetworkChallenges(DateTime? minTime)
{
var uri = $"/v1/challenges";
if (minTime.HasValue)
{
uri += "?min_time=" + minTime.Value.ToString("o", CultureInfo.InvariantCulture);
}

var allData = await HeliumClient.Get(uri);
return Extensions.DeserializeAll<PocReceiptsTransaction>(allData.ToArray());
var allDataStrings = await HeliumClient.Get(uri);
var transactions = Extensions.DeserializeAll<PocReceiptsTransaction>(allDataStrings.ToArray());
return transactions;
}

public static async Task<List<PocReceiptsTransaction>> GetBeaconTransactions(string hotspotId, DateTime? minTime)
Expand Down

0 comments on commit 9362dd0

Please sign in to comment.